115 行
3.0 KiB
JavaScript
115 行
3.0 KiB
JavaScript
const workflowRoutes = require('../../../src/routes/workflow');
|
|
|
|
describe('workflow purchase ERP helper', () => {
|
|
const { handlePurchaseApprovalErpSync } = workflowRoutes.__testables;
|
|
|
|
function createRequest() {
|
|
return {
|
|
id: 'req-1',
|
|
ip: '127.0.0.1',
|
|
headers: {},
|
|
employee: {
|
|
id: 'actor-1',
|
|
department_id: 'dept-1',
|
|
permissionCodes: ['procurement.manage'],
|
|
},
|
|
};
|
|
}
|
|
|
|
function createTx() {
|
|
return {
|
|
auditLog: {
|
|
create: vi.fn().mockResolvedValue({ id: 'audit-1' }),
|
|
},
|
|
};
|
|
}
|
|
|
|
it('auto enqueues commitment journal when gate is enabled', async () => {
|
|
const tx = createTx();
|
|
const loadContext = vi.fn().mockResolvedValue({
|
|
purchaseRequest: {
|
|
id: 'pr-1',
|
|
department_id: 'dept-1',
|
|
},
|
|
approvalOccurredAt: new Date('2026-03-08T10:00:00.000Z'),
|
|
});
|
|
const evaluateGate = vi.fn().mockResolvedValue({
|
|
enabled: true,
|
|
reason: 'env_inherit_department_allowed',
|
|
});
|
|
const enqueueCommitment = vi.fn().mockResolvedValue({
|
|
id: 'outbox-1',
|
|
status: 'pending',
|
|
duplicated: false,
|
|
requeued: false,
|
|
eventType: 'erp.journal.export',
|
|
});
|
|
|
|
const result = await handlePurchaseApprovalErpSync({
|
|
tx,
|
|
config: {},
|
|
request: createRequest(),
|
|
actorId: 'actor-1',
|
|
purchaseRequestId: 'pr-1',
|
|
loadContext,
|
|
evaluateGate,
|
|
enqueueCommitment,
|
|
});
|
|
|
|
expect(enqueueCommitment).toHaveBeenCalledWith(
|
|
tx,
|
|
{},
|
|
expect.objectContaining({
|
|
actorId: 'actor-1',
|
|
requeueOnFailure: false,
|
|
}),
|
|
);
|
|
expect(tx.auditLog.create).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
data: expect.objectContaining({
|
|
action: 'ERP_SYNC_ENQUEUED',
|
|
target_type: 'purchase_request',
|
|
target_id: 'pr-1',
|
|
}),
|
|
}),
|
|
);
|
|
expect(result.skipped).toBe(false);
|
|
});
|
|
|
|
it('writes skip audit when approval gate is disabled', async () => {
|
|
const tx = createTx();
|
|
const loadContext = vi.fn().mockResolvedValue({
|
|
purchaseRequest: {
|
|
id: 'pr-1',
|
|
department_id: 'dept-1',
|
|
},
|
|
approvalOccurredAt: new Date('2026-03-08T10:00:00.000Z'),
|
|
});
|
|
const evaluateGate = vi.fn().mockResolvedValue({ enabled: false, reason: 'flag_force_off' });
|
|
const enqueueCommitment = vi.fn();
|
|
|
|
const result = await handlePurchaseApprovalErpSync({
|
|
tx,
|
|
config: {},
|
|
request: createRequest(),
|
|
actorId: 'actor-1',
|
|
purchaseRequestId: 'pr-1',
|
|
loadContext,
|
|
evaluateGate,
|
|
enqueueCommitment,
|
|
});
|
|
|
|
expect(enqueueCommitment).not.toHaveBeenCalled();
|
|
expect(tx.auditLog.create).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
data: expect.objectContaining({
|
|
action: 'ERP_SYNC_SKIPPED_BY_FEATURE_FLAG',
|
|
target_type: 'purchase_request',
|
|
target_id: 'pr-1',
|
|
}),
|
|
}),
|
|
);
|
|
expect(result.skipped).toBe(true);
|
|
});
|
|
});
|