|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [ADR-0131 D8/D9] The one organization-reach rule, pinned on its own as a |
| 5 | + * table over every caller shape, wall and D9 decision. Both readers — |
| 6 | + * `SettingsService` and the resolver's direct localization fallback — hand |
| 7 | + * their facts to this rule, so these rows are what both of them answer. Their |
| 8 | + * own pins (`settings-organization-reach.pin.test.ts`, |
| 9 | + * `resolve-localization-fallback-organization.pin.test.ts`) hold each reader's |
| 10 | + * facts — the posture source and the failure policy; this table holds the rule. |
| 11 | + */ |
| 12 | + |
| 13 | +import { describe, expect, it } from 'vitest'; |
| 14 | + |
| 15 | +import { |
| 16 | + organizationIdOf, |
| 17 | + organizationReachAdmits, |
| 18 | + organizationReachRowOwner, |
| 19 | + resolveOrganizationReach, |
| 20 | + UNOWNED_ORGANIZATION_REACH, |
| 21 | + type OrganizationReach, |
| 22 | +} from './organization-reach.js'; |
| 23 | +import type { SystemWriteOrganizationDecision } from './system-write-organization.js'; |
| 24 | +import * as securityIndex from './index.js'; |
| 25 | + |
| 26 | +const DERIVED: SystemWriteOrganizationDecision = { kind: 'derived', organizationId: 'org_only' }; |
| 27 | +const LEAN: SystemWriteOrganizationDecision = { kind: 'no-organization-object' }; |
| 28 | +const REFUSE_NONE: SystemWriteOrganizationDecision = { kind: 'refuse', reason: 'no-organization', organizationCount: 0 }; |
| 29 | +const REFUSE_SEVERAL: SystemWriteOrganizationDecision = { |
| 30 | + kind: 'refuse', |
| 31 | + reason: 'ambiguous-organization', |
| 32 | + organizationCount: 2, |
| 33 | +}; |
| 34 | +const REFUSE_WALLED: SystemWriteOrganizationDecision = { kind: 'refuse', reason: 'walled-posture' }; |
| 35 | + |
| 36 | +/** The facts for one row, recording which of them the rule asked. */ |
| 37 | +function factsOf(callerOrganizationId: unknown, wall: boolean, decision: SystemWriteOrganizationDecision) { |
| 38 | + const asked: string[] = []; |
| 39 | + return { |
| 40 | + asked, |
| 41 | + facts: { |
| 42 | + callerOrganizationId, |
| 43 | + wallStands: () => { |
| 44 | + asked.push('wall'); |
| 45 | + return wall; |
| 46 | + }, |
| 47 | + deriveOrganization: async () => { |
| 48 | + asked.push('derive'); |
| 49 | + return decision; |
| 50 | + }, |
| 51 | + }, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +describe('[ADR-0131 D8/D9] resolveOrganizationReach — the one reach rule', () => { |
| 56 | + // [caller organization, wall stands?, D9 decision, reach, facts asked] |
| 57 | + const table: Array<[unknown, boolean, SystemWriteOrganizationDecision, OrganizationReach, string[]]> = [ |
| 58 | + // A caller that names an organization is that organization's, under every |
| 59 | + // posture, and asks nothing else. |
| 60 | + ['org_a', false, DERIVED, { kind: 'own', organizationId: 'org_a' }, []], |
| 61 | + ['org_a', true, REFUSE_WALLED, { kind: 'own', organizationId: 'org_a' }, []], |
| 62 | + ['org_a', false, LEAN, { kind: 'own', organizationId: 'org_a' }, []], |
| 63 | + // A caller that names none, behind a wall: unowned, D9 never asked. |
| 64 | + [undefined, true, DERIVED, UNOWNED_ORGANIZATION_REACH, ['wall']], |
| 65 | + ['', true, LEAN, UNOWNED_ORGANIZATION_REACH, ['wall']], |
| 66 | + // A caller that names none, no wall: whatever D9 decides. |
| 67 | + [undefined, false, DERIVED, { kind: 'own', organizationId: 'org_only' }, ['wall', 'derive']], |
| 68 | + [null, false, LEAN, { kind: 'no-organization-object' }, ['wall', 'derive']], |
| 69 | + ['', false, REFUSE_NONE, UNOWNED_ORGANIZATION_REACH, ['wall', 'derive']], |
| 70 | + [undefined, false, REFUSE_SEVERAL, UNOWNED_ORGANIZATION_REACH, ['wall', 'derive']], |
| 71 | + [undefined, false, REFUSE_WALLED, UNOWNED_ORGANIZATION_REACH, ['wall', 'derive']], |
| 72 | + // A non-string caller id names no organization. |
| 73 | + [42, false, DERIVED, { kind: 'own', organizationId: 'org_only' }, ['wall', 'derive']], |
| 74 | + ]; |
| 75 | + |
| 76 | + it.each(table)('caller=%j · wall=%s · decision=%j → %j', async (caller, wall, decision, reach, askedFacts) => { |
| 77 | + const { asked, facts } = factsOf(caller, wall, decision); |
| 78 | + expect(await resolveOrganizationReach(facts)).toEqual(reach); |
| 79 | + expect(asked).toEqual(askedFacts); |
| 80 | + }); |
| 81 | + |
| 82 | + it('a fact that throws propagates — the reader owns its failure policy', async () => { |
| 83 | + await expect(resolveOrganizationReach({ |
| 84 | + callerOrganizationId: undefined, |
| 85 | + wallStands: () => { throw new Error('posture unreadable'); }, |
| 86 | + deriveOrganization: async () => DERIVED, |
| 87 | + })).rejects.toThrow('posture unreadable'); |
| 88 | + await expect(resolveOrganizationReach({ |
| 89 | + callerOrganizationId: undefined, |
| 90 | + wallStands: async () => false, |
| 91 | + deriveOrganization: async () => { throw new Error('organization read failed'); }, |
| 92 | + })).rejects.toThrow('organization read failed'); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe('[ADR-0131 D8] organizationReachRowOwner / organizationReachAdmits — whose rows a reach reads', () => { |
| 97 | + const own: OrganizationReach = { kind: 'own', organizationId: 'org_a' }; |
| 98 | + const lean: OrganizationReach = { kind: 'no-organization-object' }; |
| 99 | + |
| 100 | + it('the row owner: the organization, NULL for the lean arm, nothing for unowned', () => { |
| 101 | + expect(organizationReachRowOwner(own)).toBe('org_a'); |
| 102 | + expect(organizationReachRowOwner(lean)).toBeNull(); |
| 103 | + expect(organizationReachRowOwner(UNOWNED_ORGANIZATION_REACH)).toBeUndefined(); |
| 104 | + }); |
| 105 | + |
| 106 | + // [reach, row's organization, admitted?] |
| 107 | + const rows: Array<[OrganizationReach, unknown, boolean]> = [ |
| 108 | + [own, 'org_a', true], |
| 109 | + [own, 'org_b', false], |
| 110 | + // A row with no organization is never an organization's fallback. |
| 111 | + [own, null, false], |
| 112 | + [own, undefined, false], |
| 113 | + [own, '', false], |
| 114 | + // The lean arm reads the NULL rows only. |
| 115 | + [lean, null, true], |
| 116 | + [lean, undefined, true], |
| 117 | + [lean, '', true], |
| 118 | + [lean, 'org_a', false], |
| 119 | + // Unowned reads nothing — a NULL row included. |
| 120 | + [UNOWNED_ORGANIZATION_REACH, null, false], |
| 121 | + [UNOWNED_ORGANIZATION_REACH, 'org_a', false], |
| 122 | + ]; |
| 123 | + |
| 124 | + it.each(rows)('reach=%j · row organization=%j → %s', (reach, rowOrganizationId, admitted) => { |
| 125 | + expect(organizationReachAdmits(reach, rowOrganizationId)).toBe(admitted); |
| 126 | + }); |
| 127 | + |
| 128 | + it('"no organization" is spelled one way: a non-empty string, else null', () => { |
| 129 | + expect(organizationIdOf('org_a')).toBe('org_a'); |
| 130 | + expect(organizationIdOf('')).toBeNull(); |
| 131 | + expect(organizationIdOf(null)).toBeNull(); |
| 132 | + expect(organizationIdOf(undefined)).toBeNull(); |
| 133 | + expect(organizationIdOf(7)).toBeNull(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('is exported from core security — the bindings every reader imports', () => { |
| 137 | + expect(securityIndex.resolveOrganizationReach).toBe(resolveOrganizationReach); |
| 138 | + expect(securityIndex.organizationReachAdmits).toBe(organizationReachAdmits); |
| 139 | + expect(securityIndex.organizationReachRowOwner).toBe(organizationReachRowOwner); |
| 140 | + expect(securityIndex.organizationIdOf).toBe(organizationIdOf); |
| 141 | + expect(securityIndex.UNOWNED_ORGANIZATION_REACH).toBe(UNOWNED_ORGANIZATION_REACH); |
| 142 | + }); |
| 143 | +}); |
0 commit comments