"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var approval_engine_1 = require("../approval_engine");
var consts_1 = require("../consts");
var baseApproval = {
    _id: 'approval-1',
    owner: 'owner-1',
    space: 'space-1',
    name: '测试审批单',
    current_step: consts_1.Step.departmental_review,
    ca_reviewer: ['reviewer-1'],
    kids_reviewer: 'reviewer-1',
    handler_ca: ['reviewer-1'],
    handler_kids: ['reviewer-1'],
    ca_approval_status: false,
    kids_approval_status: false
};
describe('postpone review department resolution', function () {
    it('uses CA when the same user is both CA and Kids and Kids is the latest process', function () {
        var departmentType = (0, approval_engine_1.getPostponeReviewDepartmentType)(baseApproval, [
            { type: consts_1.ProcessApprovalType.Kids },
            { type: consts_1.ProcessApprovalType.CA }
        ]);
        expect(departmentType).toBe(consts_1.ProcessApprovalType.CA);
    });
    it('still blocks users who only have a Kids approval role', function () {
        expect(function () { return (0, approval_engine_1.getPostponeReviewDepartmentType)(tslib_1.__assign(tslib_1.__assign({}, baseApproval), { ca_approval_status: '', handler_ca: [] }), [
            { type: consts_1.ProcessApprovalType.Kids }
        ], 'reviewer-1'); }).toThrow('Kids审核人不支持延后审核操作');
    });
    it('falls back to handler fields when process history only exposes Kids', function () {
        var departmentType = (0, approval_engine_1.getPostponeReviewDepartmentType)(baseApproval, [
            { type: consts_1.ProcessApprovalType.Kids }
        ], 'reviewer-1');
        expect(departmentType).toBe(consts_1.ProcessApprovalType.CA);
    });
});
describe('postponeReview', function () {
    it('marks the next review round without immediate activation during an active rejected review step', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
        var directUpdateApproval, insertPostpone, directFindApprovalProcess, record, ctx;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    directUpdateApproval = jest.fn();
                    insertPostpone = jest.fn();
                    directFindApprovalProcess = jest.fn()
                        .mockResolvedValueOnce([])
                        .mockResolvedValueOnce([{ type: consts_1.ProcessApprovalType.CA }]);
                    record = tslib_1.__assign(tslib_1.__assign({}, baseApproval), { person_approved: ['reviewer-1'], person_pending_approve: [], postpone_marker_users: [], review_postponed_users: [] });
                    ctx = {
                        meta: {
                            user: {
                                userId: 'reviewer-1'
                            }
                        },
                        getObject: jest.fn(function (objectName) {
                            if (objectName === 'pepsico_material_approval') {
                                return {
                                    findOne: jest.fn().mockResolvedValue(record),
                                    directUpdate: directUpdateApproval
                                };
                            }
                            if (objectName === 'pepsico_approval_process') {
                                return {
                                    directFind: directFindApprovalProcess
                                };
                            }
                            if (objectName === 'pepsico_review_postpone') {
                                return {
                                    directFind: jest.fn().mockResolvedValue([]),
                                    insert: insertPostpone
                                };
                            }
                            throw new Error("Unexpected object: ".concat(objectName));
                        })
                    };
                    return [4, (0, approval_engine_1.postponeReview)(ctx, 'approval-1', { postpone_reason: '本轮已驳回，下轮延后' })];
                case 1:
                    _a.sent();
                    expect(insertPostpone).toHaveBeenCalledWith(expect.objectContaining({
                        pepsico_material_approval: 'approval-1',
                        department_type: consts_1.ProcessApprovalType.CA,
                        postpone_by: 'reviewer-1',
                        postpone_reason: '本轮已驳回，下轮延后',
                        is_marker: true,
                        notification_sent: false,
                        postponed_user_ids: 'reviewer-1'
                    }));
                    expect(insertPostpone.mock.calls[0][0]).not.toHaveProperty('visible_after');
                    expect(directUpdateApproval).toHaveBeenCalledWith('approval-1', {
                        postpone_marker_users: ['reviewer-1']
                    });
                    expect(directUpdateApproval).not.toHaveBeenCalledWith('approval-1', expect.objectContaining({
                        review_postponed_users: expect.any(Array)
                    }));
                    return [2];
            }
        });
    }); });
});
//# sourceMappingURL=postponeReview.test.js.map