"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const paabHelper_1 = require("../../support/complex/paabHelper");
const setup_1 = __importDefault(require("../../support/setup"));
const utilities_1 = require("../../support/utils/utilities");
describe('multiStep project tests', () => {
    const paabHelper = new paabHelper_1.PaabHelper(1);
    let pa1Session;
    const setup = setup_1.default.getSetup();
    let adminSession;
    let costCenterId;
    let projectName;
    let project1Id;
    beforeAll(async () => {
        ({ adminSession, pa1Session, project1Id } = await paabHelper.createResources(__filename));
    });
    beforeEach(async () => {
        const { data: costCenter } = await adminSession.resources.costCenters.create({
            name: 'project-integration-test-cost-center',
            accountId: setup.getSettings().get('defaultHostingAccountId'),
            description: 'a test object'
        });
        costCenterId = costCenter.id;
        projectName = (0, utilities_1.generateRandomString)(10, utilities_1.validSwbName);
    });
    afterAll(async () => {
        await paabHelper.cleanup();
        await setup.cleanup();
    });
    test('happy path', async () => {
        console.log('Creating Project');
        const { data: createdProject } = await adminSession.resources.projects.create({
            name: projectName,
            description: 'happy path--Project for TOP SECRET dragon research',
            costCenterId
        });
        console.log('Getting Project');
        const { data: getProjectAsITAdmin } = await adminSession.resources.projects
            .project(createdProject.id)
            .get();
        expect(getProjectAsITAdmin).toMatchObject(createdProject);
        const { data: getProjectAsPA } = await pa1Session.resources.projects.project(project1Id).get();
        expect(getProjectAsPA.id).toEqual(project1Id);
        const { data: getProjectAsResearcher } = await adminSession.resources.projects.project(project1Id).get();
        expect(getProjectAsResearcher.id).toEqual(project1Id);
        console.log('Listing Projects');
        const { data: listProject } = await adminSession.resources.projects.get({
            'filter[name][eq]': createdProject.name
        });
        expect(listProject.data).toEqual([createdProject]);
        console.log('Updating Project');
        const newName = (0, utilities_1.generateRandomString)(10, utilities_1.validSwbName);
        const newDescription = 'Happy path--Not a Project studying dragons';
        const { data: updatedProject } = await adminSession.resources.projects
            .project(createdProject.id)
            .update({ name: newName, description: newDescription }, true);
        expect(updatedProject).toMatchObject({ name: newName, description: newDescription });
        const { data: updatedProjectAsPA } = await adminSession.resources.projects
            .project(project1Id)
            .update({ description: newDescription }, true);
        expect(updatedProjectAsPA).toMatchObject({ name: getProjectAsPA.name, description: newDescription });
        console.log('Deleting Project');
        await adminSession.resources.projects.project(createdProject.id).delete();
        await pa1Session.resources.projects.project(project1Id).delete();
        console.log("Listing projects doesn't return deleted project");
        const { data: listProjectAfterDelete } = await adminSession.resources.projects.get({
            'filter[name][eq]': newName
        });
        expect(listProjectAfterDelete.data.length).toEqual(0);
    });
    test('createProject', async () => {
        console.log('Creating Project');
        const projectName = (0, utilities_1.generateRandomString)(10, utilities_1.validSwbName);
        const { data: createdProject1 } = await adminSession.resources.projects.create({
            name: projectName,
            description: 'My uniquely name project',
            costCenterId
        });
        console.log('Delete Project');
        // eslint-disable-next-line no-unused-expressions
        expect(await adminSession.resources.projects.project(createdProject1.id).delete()).resolves;
        console.log('Creating Project with name of deleted project');
        // eslint-disable-next-line no-unused-expressions
        const { data: createdProject2 } = await adminSession.resources.projects.create({
            name: projectName,
            description: 'Another project to replace my uniquely named project',
            costCenterId
        });
        console.log('Deleting New Project');
        // eslint-disable-next-line no-unused-expressions
        expect(await adminSession.resources.projects.project(createdProject2.id).delete()).resolves;
    });
    test('updateProject', async () => {
        console.log('Creating Two Projects');
        const { data: createdProject1 } = await adminSession.resources.projects.create({
            name: projectName,
            description: 'Currently top of my todo list',
            costCenterId
        });
        const otherName = (0, utilities_1.generateRandomString)(10, utilities_1.validSwbName);
        const { data: createdProject2 } = await adminSession.resources.projects.create({
            name: otherName,
            description: 'Backlog project',
            costCenterId
        });
        console.log('Deleting Project');
        // eslint-disable-next-line no-unused-expressions
        expect(await adminSession.resources.projects.project(createdProject1.id).delete()).resolves;
        console.log('Updating remaining project to have the same name as the deleted one');
        const newName = createdProject1.name;
        const newDescription = createdProject1.description;
        const { data: updatedProject } = await adminSession.resources.projects
            .project(createdProject2.id)
            .update({ name: newName, description: newDescription }, true);
        expect(updatedProject).toMatchObject({ name: newName, description: newDescription });
        console.log('Deleting Updated Project');
        // eslint-disable-next-line no-unused-expressions
        expect(await adminSession.resources.projects.project(createdProject2.id).delete()).resolves;
    });
});
//# sourceMappingURL=project.test.js.map