"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Unit tests for the approval policy engine (pure matching + N-of-M quorum). * No DB and no on-chain calls. */ const node_test_1 = require("node:test"); const strict_1 = __importDefault(require("node:assert/strict")); const approvalPolicyEngine_1 = require("../services/approvalPolicyEngine"); function policy(partial) { return { id: 'p1', name: 'test', enabled: true, priority: 100, operation_type: null, wallet_type_id: null, network: null, amount_min_raw: null, amount_max_raw: null, source_wallet_kind: 'any', dest_wallet_kind: 'any', decision: { mode: 'require_approvals', quorum: [{ role: 'super_admin', count: 1 }], forbid_self_approve: true, }, version: 1, updated_by: null, created_at: new Date(), updated_at: new Date(), ...partial, }; } function vote(voterId, roles, decision = 'approve') { return { id: `v-${voterId}`, request_id: 'r1', voter_id: voterId, decision, reason: null, voter_roles: roles, created_at: new Date(), }; } (0, node_test_1.describe)('policyMatches', () => { (0, node_test_1.test)('wildcard policy matches any operation', () => { strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(policy({}), { operationType: 'treasury_hot_to_cold', amountRaw: '1000', }), true); }); (0, node_test_1.test)('disabled policy never matches', () => { strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(policy({ enabled: false }), { operationType: 'ledger_credit' }), false); }); (0, node_test_1.test)('operation_type filter', () => { const p = policy({ operation_type: 'user_withdrawal' }); strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(p, { operationType: 'user_withdrawal' }), true); strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(p, { operationType: 'ledger_credit' }), false); }); (0, node_test_1.test)('network and wallet_type_id filters', () => { const p = policy({ network: 'BSC', wallet_type_id: 2 }); strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(p, { operationType: 'x', network: 'BSC', walletTypeId: 2 }), true); strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(p, { operationType: 'x', network: 'TRON', walletTypeId: 2 }), false); strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(p, { operationType: 'x', network: 'BSC', walletTypeId: 3 }), false); }); (0, node_test_1.test)('amount min/max bounds (inclusive)', () => { const p = policy({ amount_min_raw: '100', amount_max_raw: '1000' }); strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(p, { operationType: 'x', amountRaw: '100' }), true); strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(p, { operationType: 'x', amountRaw: '1000' }), true); strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(p, { operationType: 'x', amountRaw: '99' }), false); strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(p, { operationType: 'x', amountRaw: '1001' }), false); }); (0, node_test_1.test)('policy with amount bounds rejects missing amount', () => { const p = policy({ amount_min_raw: '1' }); strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(p, { operationType: 'x' }), false); strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(p, { operationType: 'x', amountRaw: '' }), false); }); (0, node_test_1.test)('source/dest wallet kinds', () => { const p = policy({ source_wallet_kind: 'hot', dest_wallet_kind: 'cold' }); strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(p, { operationType: 'treasury_hot_to_cold', sourceKind: 'hot', destKind: 'cold', }), true); strict_1.default.equal((0, approvalPolicyEngine_1.policyMatches)(p, { operationType: 'treasury_cold_to_hot', sourceKind: 'cold', destKind: 'hot', }), false); }); }); (0, node_test_1.describe)('N-of-M quorum', () => { (0, node_test_1.test)('single super_admin slot: 0/1 then 1/1', () => { const quorum = [{ role: 'super_admin', count: 1 }]; strict_1.default.equal((0, approvalPolicyEngine_1.isQuorumComplete)(quorum, []), false); const progress0 = (0, approvalPolicyEngine_1.countQuorumProgress)(quorum, []); strict_1.default.equal(progress0.required, 1); strict_1.default.equal(progress0.approved, 0); const votes = [vote('sa1', ['super_admin'])]; strict_1.default.equal((0, approvalPolicyEngine_1.isQuorumComplete)(quorum, votes), true); const progress1 = (0, approvalPolicyEngine_1.countQuorumProgress)(quorum, votes); strict_1.default.equal(progress1.approved, 1); }); (0, node_test_1.test)('dual approval: admin + super_admin', () => { const quorum = [ { role: 'admin', count: 1 }, { role: 'super_admin', count: 1 }, ]; const onlyAdmin = [vote('a1', ['admin'])]; strict_1.default.equal((0, approvalPolicyEngine_1.isQuorumComplete)(quorum, onlyAdmin), false); const both = [vote('a1', ['admin']), vote('sa1', ['super_admin'])]; strict_1.default.equal((0, approvalPolicyEngine_1.isQuorumComplete)(quorum, both), true); }); (0, node_test_1.test)('same voter cannot fill two slots', () => { const quorum = [ { role: 'admin', count: 1 }, { role: 'super_admin', count: 1 }, ]; // One person with both roles — still only one vote row, used once const dualRole = [vote('u1', ['admin', 'super_admin'])]; strict_1.default.equal((0, approvalPolicyEngine_1.isQuorumComplete)(quorum, dualRole), false); }); (0, node_test_1.test)('N=2 of same role', () => { const quorum = [{ role: 'admin', count: 2 }]; strict_1.default.equal((0, approvalPolicyEngine_1.isQuorumComplete)(quorum, [vote('a1', ['admin'])]), false); strict_1.default.equal((0, approvalPolicyEngine_1.isQuorumComplete)(quorum, [vote('a1', ['admin']), vote('a2', ['admin'])]), true); }); (0, node_test_1.test)('permission-annotated votes fill permission slots', () => { const quorum = [{ permission: 'fund_operation.approve', count: 1 }]; const annotated = (0, approvalPolicyEngine_1.annotateVoterRolesForQuorum)(['admin'], ['fund_operation.approve'], quorum); strict_1.default.ok(annotated.includes('perm:fund_operation.approve')); strict_1.default.equal((0, approvalPolicyEngine_1.isQuorumComplete)(quorum, [vote('u1', annotated)]), true); }); (0, node_test_1.test)('voterFillsOpenSlot respects already-filled slots', () => { const quorum = [ { role: 'admin', count: 1 }, { role: 'super_admin', count: 1 }, ]; const votes = [vote('a1', ['admin'])]; strict_1.default.equal((0, approvalPolicyEngine_1.voterFillsOpenSlot)(quorum, votes, ['admin'], []), false); strict_1.default.equal((0, approvalPolicyEngine_1.voterFillsOpenSlot)(quorum, votes, ['super_admin'], []), true); }); (0, node_test_1.test)('reject votes do not count toward quorum', () => { const quorum = [{ role: 'super_admin', count: 1 }]; strict_1.default.equal((0, approvalPolicyEngine_1.isQuorumComplete)(quorum, [vote('sa1', ['super_admin'], 'reject')]), false); }); }); //# sourceMappingURL=approvalPolicyEngine.test.js.map