"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var consts_1 = require("../consts");
var approval_engine_1 = require("../approval_engine");
var material_1 = require("../material");
function makeMaterialApproval(overrides) {
    if (overrides === void 0) { overrides = {}; }
    return tslib_1.__assign({ _id: 'test-approval-id', classification: 'Brand KV', social_media: '', followers: 0, kol_classification: '', brief_flow_number: '', brief_flow: '', autonumber: '', name: 'Test Approval', product: 'Test Product', material_type: '', campaign_type: '', launch_time: '', available_channels: '', communication_context: '', content_rule: '', custom_tags: '', expiry_date: '', effective_date: '', bgm: '', subtitles: '', music_copyright: '', font_copyright: '', copyright_music: '', copyright_front: '', material_owner: '', child_files: 'no', kids_reviewer: '', kids_approval_status: '', kids_file_name: '', materials_quantity: 0, reviewer: [], assistance: '', approval_status: '', approval_materials_quantity: 0, ns_reviewer: ['ns-user-1'], ns_assistance: '', ns_approval_status: '', ns_materials_quantity: 0, legal_reviewer: ['legal-user-1'], legal_assistance: '', legal_approval_status: '', legal_materials_quantity: 0, sra_reviewer: ['sra-user-1'], sra_assistance: '', sra_approval_status: '', sra_materials_quantity: 0, ca_reviewer: ['ca-user-1'], ca_assistance: '', ca_approval_status: '', ca_materials_quantity: 0, owner: 'owner-user-1', current_step: consts_1.Step.draft, owner_department: '', review_status: '', person_pending_approve: '', person_view: '', person_approved: '', launch: '', postpone_launch_date: '', aunch_time: '', instance_state: '', space: 'test-space', materials_uploaded_to_onedrive_status: '', materials_xml_to_word_status: '', material_owner_id: '', external_submitted: false, is_external: false, internal_collaborator: '', is_skip_approver: false, pepsico_flow_rule: '', shop_tag: '', pre_reviewer: '', pre_ns_reviewer: '', pre_sra_reviewer: '', pre_ca_reviewer: '', pre_legal_reviewer: '', pre_approval_status: '', pre_ns_approval_status: '', pre_sra_approval_status: '', pre_ca_approval_status: '', pre_legal_approval_status: '' }, overrides);
}
describe('hasReviewed', function () {
    test('returns true when status is true (approved)', function () {
        expect((0, approval_engine_1.hasReviewed)(true)).toBe(true);
    });
    test('returns true when status is false (rejected)', function () {
        expect((0, approval_engine_1.hasReviewed)(false)).toBe(true);
    });
    test('returns true when status is "pass" (bypassed)', function () {
        expect((0, approval_engine_1.hasReviewed)(consts_1.ApprovalStatus.pass)).toBe(true);
    });
    test('returns true when status is "approved"', function () {
        expect((0, approval_engine_1.hasReviewed)(consts_1.ApprovalStatus.approved)).toBe(true);
    });
    test('returns false when status is empty string (pending)', function () {
        expect((0, approval_engine_1.hasReviewed)('')).toBe(false);
    });
    test('returns false when status is undefined', function () {
        expect((0, approval_engine_1.hasReviewed)(undefined)).toBe(false);
    });
    test('returns false when status is null', function () {
        expect((0, approval_engine_1.hasReviewed)(null)).toBe(false);
    });
});
describe('hasAnyPreReviewer', function () {
    describe('personnel_review type', function () {
        test('returns true when pre_reviewer is set', function () {
            var approval = makeMaterialApproval({ pre_reviewer: 'pre-user-1' });
            expect((0, approval_engine_1.hasAnyPreReviewer)(approval, consts_1.Step.personnel_review)).toBe(true);
        });
        test('returns false when pre_reviewer is empty', function () {
            var approval = makeMaterialApproval({ pre_reviewer: '' });
            expect((0, approval_engine_1.hasAnyPreReviewer)(approval, consts_1.Step.personnel_review)).toBe(false);
        });
        test('ignores department pre-reviewers for personnel_review type', function () {
            var approval = makeMaterialApproval({
                pre_reviewer: '',
                pre_ns_reviewer: 'pre-ns-user',
                pre_ca_reviewer: 'pre-ca-user',
            });
            expect((0, approval_engine_1.hasAnyPreReviewer)(approval, consts_1.Step.personnel_review)).toBe(false);
        });
    });
    describe('departmental_review type', function () {
        test('returns true when pre_ns_reviewer is set', function () {
            var approval = makeMaterialApproval({ pre_ns_reviewer: 'pre-ns-user' });
            expect((0, approval_engine_1.hasAnyPreReviewer)(approval, consts_1.Step.departmental_review)).toBe(true);
        });
        test('returns true when pre_legal_reviewer is set', function () {
            var approval = makeMaterialApproval({ pre_legal_reviewer: 'pre-legal-user' });
            expect((0, approval_engine_1.hasAnyPreReviewer)(approval, consts_1.Step.departmental_review)).toBe(true);
        });
        test('returns true when pre_sra_reviewer is set', function () {
            var approval = makeMaterialApproval({ pre_sra_reviewer: 'pre-sra-user' });
            expect((0, approval_engine_1.hasAnyPreReviewer)(approval, consts_1.Step.departmental_review)).toBe(true);
        });
        test('returns true when pre_ca_reviewer is set', function () {
            var approval = makeMaterialApproval({ pre_ca_reviewer: 'pre-ca-user' });
            expect((0, approval_engine_1.hasAnyPreReviewer)(approval, consts_1.Step.departmental_review)).toBe(true);
        });
        test('returns true when multiple department pre-reviewers are set', function () {
            var approval = makeMaterialApproval({
                pre_ns_reviewer: 'pre-ns-user',
                pre_ca_reviewer: 'pre-ca-user',
            });
            expect((0, approval_engine_1.hasAnyPreReviewer)(approval, consts_1.Step.departmental_review)).toBe(true);
        });
        test('returns false when no department pre-reviewers are set', function () {
            var approval = makeMaterialApproval({});
            expect((0, approval_engine_1.hasAnyPreReviewer)(approval, consts_1.Step.departmental_review)).toBe(false);
        });
        test('ignores pre_reviewer (personnel) for departmental type', function () {
            var approval = makeMaterialApproval({ pre_reviewer: 'pre-user-1' });
            expect((0, approval_engine_1.hasAnyPreReviewer)(approval, consts_1.Step.departmental_review)).toBe(false);
        });
    });
});
describe('getPreReviewStatuses', function () {
    test('returns empty array when no department pre-reviewers are configured', function () {
        var approval = makeMaterialApproval({});
        expect((0, approval_engine_1.getPreReviewStatuses)(approval)).toEqual([]);
    });
    test('returns status for NS when pre_ns_reviewer is set', function () {
        var approval = makeMaterialApproval({
            pre_ns_reviewer: 'pre-ns-user',
            pre_ns_approval_status: true,
        });
        var result = (0, approval_engine_1.getPreReviewStatuses)(approval);
        expect(result).toHaveLength(1);
        expect(result[0]).toEqual({
            dept: 'NS',
            status: true,
            preField: 'pre_ns_approval_status',
            deptApprovalField: 'ns_approval_status',
        });
    });
    test('returns statuses for all configured departments', function () {
        var approval = makeMaterialApproval({
            pre_ns_reviewer: 'pre-ns-user',
            pre_ns_approval_status: true,
            pre_legal_reviewer: 'pre-legal-user',
            pre_legal_approval_status: false,
            pre_sra_reviewer: 'pre-sra-user',
            pre_sra_approval_status: '',
            pre_ca_reviewer: 'pre-ca-user',
            pre_ca_approval_status: 'pass',
        });
        var result = (0, approval_engine_1.getPreReviewStatuses)(approval);
        expect(result).toHaveLength(4);
        expect(result.map(function (r) { return r.dept; })).toEqual(['NS', 'Legal', 'SRA', 'CA']);
    });
    test('only includes departments with pre-reviewers configured', function () {
        var approval = makeMaterialApproval({
            pre_ns_reviewer: 'pre-ns-user',
            pre_ns_approval_status: true,
            pre_sra_reviewer: 'pre-sra-user',
            pre_sra_approval_status: false,
        });
        var result = (0, approval_engine_1.getPreReviewStatuses)(approval);
        expect(result).toHaveLength(2);
        expect(result.map(function (r) { return r.dept; })).toEqual(['NS', 'SRA']);
    });
    test('correctly maps approval status fields', function () {
        var approval = makeMaterialApproval({
            pre_ns_reviewer: 'x',
            pre_ns_approval_status: '',
        });
        var result = (0, approval_engine_1.getPreReviewStatuses)(approval);
        expect(result[0].preField).toBe('pre_ns_approval_status');
        expect(result[0].deptApprovalField).toBe('ns_approval_status');
    });
});
describe('hasNonPreReviewDepartments', function () {
    test('returns true when no department has pre-reviewers and no statuses are positive', function () {
        var approval = makeMaterialApproval({
            ns_approval_status: '',
            legal_approval_status: '',
            sra_approval_status: '',
            ca_approval_status: '',
        });
        expect((0, approval_engine_1.hasNonPreReviewDepartments)(approval)).toBe(true);
    });
    test('returns false when all departments have pre-reviewers', function () {
        var approval = makeMaterialApproval({
            pre_ns_reviewer: 'x',
            pre_legal_reviewer: 'x',
            pre_sra_reviewer: 'x',
            pre_ca_reviewer: 'x',
        });
        expect((0, approval_engine_1.hasNonPreReviewDepartments)(approval)).toBe(false);
    });
    test('returns true when some departments lack pre-reviewers and are not approved', function () {
        var approval = makeMaterialApproval({
            pre_ns_reviewer: 'x',
            pre_legal_reviewer: '',
            pre_sra_reviewer: 'x',
            pre_ca_reviewer: '',
            legal_approval_status: '',
            ca_approval_status: '',
        });
        expect((0, approval_engine_1.hasNonPreReviewDepartments)(approval)).toBe(true);
    });
    test('returns false when departments without pre-reviewers are already approved', function () {
        var approval = makeMaterialApproval({
            pre_ns_reviewer: 'x',
            pre_legal_reviewer: '',
            pre_sra_reviewer: 'x',
            pre_ca_reviewer: '',
            legal_approval_status: true,
            ca_approval_status: 'pass',
        });
        expect((0, approval_engine_1.hasNonPreReviewDepartments)(approval)).toBe(false);
    });
    test('returns true when one department without pre-reviewer is still pending', function () {
        var approval = makeMaterialApproval({
            pre_ns_reviewer: 'x',
            pre_legal_reviewer: 'x',
            pre_sra_reviewer: 'x',
            pre_ca_reviewer: '',
            ca_approval_status: '',
        });
        expect((0, approval_engine_1.hasNonPreReviewDepartments)(approval)).toBe(true);
    });
});
describe('getApproverFieldByType', function () {
    describe('original types still work', function () {
        test('CA → ca_approval_status', function () {
            expect((0, material_1.getApproverFieldByType)(consts_1.ProcessApprovalType.CA)).toBe('ca_approval_status');
        });
        test('Legal → legal_approval_status', function () {
            expect((0, material_1.getApproverFieldByType)(consts_1.ProcessApprovalType.Legal)).toBe('legal_approval_status');
        });
        test('NS → ns_approval_status', function () {
            expect((0, material_1.getApproverFieldByType)(consts_1.ProcessApprovalType.NS)).toBe('ns_approval_status');
        });
        test('SRA → sra_approval_status', function () {
            expect((0, material_1.getApproverFieldByType)(consts_1.ProcessApprovalType.SRA)).toBe('sra_approval_status');
        });
        test('Kids → kids_approval_status', function () {
            expect((0, material_1.getApproverFieldByType)(consts_1.ProcessApprovalType.Kids)).toBe('kids_approval_status');
        });
        test('guding → approval_status', function () {
            expect((0, material_1.getApproverFieldByType)(consts_1.ProcessApprovalType.guding)).toBe('approval_status');
        });
    });
    describe('pre-review types', function () {
        test('PreNS → pre_ns_approval_status', function () {
            expect((0, material_1.getApproverFieldByType)(consts_1.ProcessApprovalType.PreNS)).toBe('pre_ns_approval_status');
        });
        test('PreLegal → pre_legal_approval_status', function () {
            expect((0, material_1.getApproverFieldByType)(consts_1.ProcessApprovalType.PreLegal)).toBe('pre_legal_approval_status');
        });
        test('PreSRA → pre_sra_approval_status', function () {
            expect((0, material_1.getApproverFieldByType)(consts_1.ProcessApprovalType.PreSRA)).toBe('pre_sra_approval_status');
        });
        test('PreCA → pre_ca_approval_status', function () {
            expect((0, material_1.getApproverFieldByType)(consts_1.ProcessApprovalType.PreCA)).toBe('pre_ca_approval_status');
        });
        test('Preguding → pre_approval_status', function () {
            expect((0, material_1.getApproverFieldByType)(consts_1.ProcessApprovalType.Preguding)).toBe('pre_approval_status');
        });
    });
    describe('edge cases', function () {
        test('AI type returns empty string', function () {
            expect((0, material_1.getApproverFieldByType)(consts_1.ProcessApprovalType.AI)).toBe('');
        });
        test('Others type returns empty string', function () {
            expect((0, material_1.getApproverFieldByType)(consts_1.ProcessApprovalType.Others)).toBe('');
        });
        test('unknown type returns empty string', function () {
            expect((0, material_1.getApproverFieldByType)('unknown')).toBe('');
        });
    });
});
describe('Step enum', function () {
    test('pre_review step is defined', function () {
        expect(consts_1.Step.pre_review).toBe('pre_review');
    });
    test('all expected steps exist', function () {
        expect(consts_1.Step.draft).toBe('draft');
        expect(consts_1.Step.ai_review).toBe('ai_review');
        expect(consts_1.Step.pre_review).toBe('pre_review');
        expect(consts_1.Step.departmental_review).toBe('departmental_review');
        expect(consts_1.Step.personnel_review).toBe('personnel_review');
        expect(consts_1.Step.submitter_revisions).toBe('submitter_revisions');
        expect(consts_1.Step.awaiting_deployment).toBe('awaiting_deployment');
        expect(consts_1.Step.conclusion).toBe('conclusion');
    });
});
describe('ProcessApprovalType enum', function () {
    test('PreNS type is defined', function () {
        expect(consts_1.ProcessApprovalType.PreNS).toBe('PreNS');
    });
    test('PreLegal type is defined', function () {
        expect(consts_1.ProcessApprovalType.PreLegal).toBe('PreLegal');
    });
    test('PreSRA type is defined', function () {
        expect(consts_1.ProcessApprovalType.PreSRA).toBe('PreSRA');
    });
    test('PreCA type is defined', function () {
        expect(consts_1.ProcessApprovalType.PreCA).toBe('PreCA');
    });
    test('Preguding type is defined', function () {
        expect(consts_1.ProcessApprovalType.Preguding).toBe('Preguding');
    });
});
describe('MaterialApproval pre-review fields', function () {
    test('pre-reviewer fields can be set and read', function () {
        var approval = makeMaterialApproval({
            pre_reviewer: 'user-1',
            pre_ns_reviewer: 'user-2',
            pre_sra_reviewer: 'user-3',
            pre_ca_reviewer: 'user-4',
            pre_legal_reviewer: 'user-5',
        });
        expect(approval.pre_reviewer).toBe('user-1');
        expect(approval.pre_ns_reviewer).toBe('user-2');
        expect(approval.pre_sra_reviewer).toBe('user-3');
        expect(approval.pre_ca_reviewer).toBe('user-4');
        expect(approval.pre_legal_reviewer).toBe('user-5');
    });
    test('pre-approval-status fields can be set and read', function () {
        var approval = makeMaterialApproval({
            pre_approval_status: true,
            pre_ns_approval_status: false,
            pre_sra_approval_status: 'pass',
            pre_ca_approval_status: '',
            pre_legal_approval_status: 'approved',
        });
        expect(approval.pre_approval_status).toBe(true);
        expect(approval.pre_ns_approval_status).toBe(false);
        expect(approval.pre_sra_approval_status).toBe('pass');
        expect(approval.pre_ca_approval_status).toBe('');
        expect(approval.pre_legal_approval_status).toBe('approved');
    });
});
//# sourceMappingURL=pre_review_helpers.test.js.map