|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { |
| 5 | + validateApprovalApprovers, |
| 6 | + APPROVAL_ROLE_NOT_MEMBERSHIP_TIER, |
| 7 | + APPROVAL_APPROVER_TYPE_UNKNOWN, |
| 8 | +} from './validate-approval-approvers.js'; |
| 9 | + |
| 10 | +function stackWithApprovers(approvers: unknown[]): Record<string, unknown> { |
| 11 | + return { |
| 12 | + flows: [{ |
| 13 | + name: 'expense_approval', |
| 14 | + nodes: [ |
| 15 | + { id: 'start', type: 'start', config: {} }, |
| 16 | + { id: 'step1', type: 'approval', config: { approvers } }, |
| 17 | + ], |
| 18 | + edges: [], |
| 19 | + }], |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +describe('validateApprovalApprovers', () => { |
| 24 | + it('is clean on an empty / flow-less stack', () => { |
| 25 | + expect(validateApprovalApprovers({})).toEqual([]); |
| 26 | + expect(validateApprovalApprovers({ flows: [] })).toEqual([]); |
| 27 | + }); |
| 28 | + |
| 29 | + it('accepts membership tiers for type role (owner/admin/member/guest)', () => { |
| 30 | + const findings = validateApprovalApprovers(stackWithApprovers([ |
| 31 | + { type: 'role', value: 'admin' }, |
| 32 | + { type: 'role', value: 'Owner' }, // case-insensitive |
| 33 | + { type: 'role', value: 'member' }, |
| 34 | + { type: 'role', value: 'guest' }, |
| 35 | + ])); |
| 36 | + expect(findings).toEqual([]); |
| 37 | + }); |
| 38 | + |
| 39 | + it("flags a position name authored as type 'role' (the ADR-0090 D3 hotcrm class)", () => { |
| 40 | + const findings = validateApprovalApprovers(stackWithApprovers([ |
| 41 | + { type: 'role', value: 'sales_manager' }, |
| 42 | + ])); |
| 43 | + expect(findings).toHaveLength(1); |
| 44 | + expect(findings[0].rule).toBe(APPROVAL_ROLE_NOT_MEMBERSHIP_TIER); |
| 45 | + expect(findings[0].severity).toBe('warning'); |
| 46 | + expect(findings[0].where).toContain('expense_approval'); |
| 47 | + expect(findings[0].path).toBe('flows[0].nodes[1].config.approvers[0].value'); |
| 48 | + expect(findings[0].hint).toContain("type: 'position'"); |
| 49 | + }); |
| 50 | + |
| 51 | + it('accepts the position approver type and the other spec types silently', () => { |
| 52 | + const findings = validateApprovalApprovers(stackWithApprovers([ |
| 53 | + { type: 'position', value: 'sales_manager' }, |
| 54 | + { type: 'user', value: 'u1' }, |
| 55 | + { type: 'manager' }, |
| 56 | + { type: 'department', value: 'bu_sales' }, |
| 57 | + { type: 'field', value: 'owner_id' }, |
| 58 | + { type: 'queue', value: 'q1' }, |
| 59 | + { type: 'team', value: 't1' }, |
| 60 | + ])); |
| 61 | + expect(findings).toEqual([]); |
| 62 | + }); |
| 63 | + |
| 64 | + it('flags off-spec approver types, with a canonical fix for the business_unit dialect', () => { |
| 65 | + const findings = validateApprovalApprovers(stackWithApprovers([ |
| 66 | + { type: 'business_unit', value: 'bu_sales' }, |
| 67 | + { type: 'group', value: 'g1' }, |
| 68 | + ])); |
| 69 | + expect(findings).toHaveLength(2); |
| 70 | + expect(findings[0].rule).toBe(APPROVAL_APPROVER_TYPE_UNKNOWN); |
| 71 | + expect(findings[0].hint).toContain("type: 'department'"); |
| 72 | + expect(findings[1].rule).toBe(APPROVAL_APPROVER_TYPE_UNKNOWN); |
| 73 | + }); |
| 74 | + |
| 75 | + it('only scans approval nodes and tolerates malformed shapes', () => { |
| 76 | + const findings = validateApprovalApprovers({ |
| 77 | + flows: [{ |
| 78 | + name: 'f', |
| 79 | + nodes: [ |
| 80 | + { id: 'a', type: 'script', config: { approvers: [{ type: 'role', value: 'sales_manager' }] } }, |
| 81 | + { id: 'b', type: 'approval' }, // no config |
| 82 | + { id: 'c', type: 'approval', config: { approvers: 'oops' } }, |
| 83 | + null, |
| 84 | + ], |
| 85 | + }, null, 'garbage'], |
| 86 | + } as never); |
| 87 | + expect(findings).toEqual([]); |
| 88 | + }); |
| 89 | +}); |
0 commit comments