"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var urgent_1 = require("../urgent");
var consts_1 = require("../consts");
var baseApproval = {
    _id: 'approval-1',
    owner: 'user-1',
    space: 'space-1',
    name: '测试审批单',
    current_step: consts_1.Step.departmental_review,
    review_postponed_users: [],
    is_urgent: false,
    urgent_times: 0,
    person_pending_approve: []
};
var createCtx = function (_a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.approval, approval = _c === void 0 ? baseApproval : _c, _d = _b.quota, quota = _d === void 0 ? { _id: 'quota-1', h1_used: 0, h2_used: 0 } : _d;
    var objects = {
        pepsico_material_approval: {
            findOne: jest.fn(function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
                return [2, approval];
            }); }); }),
            directUpdate: jest.fn()
        },
        pepsico_urgent_quota: {
            findOne: jest.fn(function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
                return [2, quota];
            }); }); }),
            directUpdate: jest.fn(),
            insert: jest.fn()
        },
        pepsico_urgent_log: {
            insert: jest.fn()
        },
        space_users: {
            findOne: jest.fn(function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
                return [2, ({ is_collaborator: false })];
            }); }); }),
            find: jest.fn(function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
                return [2, []];
            }); }); })
        }
    };
    return {
        objects: objects,
        ctx: {
            getObject: jest.fn(function (name) { return objects[name]; }),
            broker: {
                call: jest.fn()
            }
        }
    };
};
describe('urgent approval', function () {
    beforeEach(function () {
        jest.useFakeTimers().setSystemTime(new Date('2026-05-30T00:00:00.000Z'));
    });
    afterEach(function () {
        jest.useRealTimers();
    });
    it('does not allow urgent approval when reviewers are postponed', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
        var ctx;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    ctx = createCtx({
                        approval: tslib_1.__assign(tslib_1.__assign({}, baseApproval), { review_postponed_users: ['reviewer-1'] })
                    }).ctx;
                    return [4, expect((0, urgent_1.canUrgent)(ctx, 'approval-1', 'user-1')).resolves.toEqual({
                            canUrgent: false,
                            reason: '存在延后审核人，无法加急'
                        })];
                case 1:
                    _a.sent();
                    return [2];
            }
        });
    }); });
    it('does not allow urgent approval when quota is not configured', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
        var ctx;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    ctx = createCtx({ quota: null }).ctx;
                    return [4, expect((0, urgent_1.canUrgent)(ctx, 'approval-1', 'user-1')).resolves.toEqual({
                            canUrgent: false,
                            reason: '未配置加急次数',
                            quota: {
                                available: 0,
                                used: 0,
                                total: 0,
                                year: 2026,
                                half: 1,
                                configured: false
                            }
                        })];
                case 1:
                    _a.sent();
                    return [2];
            }
        });
    }); });
    it('does not auto-create quota records when executing urgent approval without configuration', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
        var _a, ctx, objects;
        return tslib_1.__generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    _a = createCtx({ quota: null }), ctx = _a.ctx, objects = _a.objects;
                    return [4, expect((0, urgent_1.doUrgent)(ctx, 'approval-1', 'user-1')).rejects.toThrow('未配置加急次数')];
                case 1:
                    _b.sent();
                    expect(objects.pepsico_urgent_quota.insert).not.toHaveBeenCalled();
                    expect(objects.pepsico_material_approval.directUpdate).not.toHaveBeenCalled();
                    expect(objects.pepsico_urgent_log.insert).not.toHaveBeenCalled();
                    return [2];
            }
        });
    }); });
});
//# sourceMappingURL=urgent.test.js.map