|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The action's `(package, objectName, name)` key (ADR-0150 D4) — #31673, child |
| 5 | + * E2 of epic #31527: the declaration half, R's resolver over the `action` |
| 6 | + * items that own one object ({@link resolveActionByPackage}), and the handler |
| 7 | + * half's coordinate ({@link actionHandlerCoordinate}). |
| 8 | + * |
| 9 | + * The registry is the real `SchemaRegistry`, as R's own suite builds it: |
| 10 | + * packages through `installPackage` (their manifests are the declared closure), |
| 11 | + * items through `registerItem` under each package's composite slot. |
| 12 | + * |
| 13 | + * The negative pins (`refuses`, `never`) were each ablated once — the refused |
| 14 | + * behaviour put back, the pin read red, restored — and the readings are in the |
| 15 | + * PR body, not kept as test files. |
| 16 | + */ |
| 17 | + |
| 18 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 19 | +import { SchemaRegistry } from './registry.js'; |
| 20 | +import { resolveActionByPackage, actionHolderCoordinate } from './action-package-addressing.js'; |
| 21 | +import { actionHandlerCoordinate, ENVIRONMENT_ACTION_COORDINATE, HOST_ACTION_COORDINATE } from './action-handler-coordinate.js'; |
| 22 | +import { resolvePackageScopedName, type PackageScopedResolution } from './package-scoped-resolver.js'; |
| 23 | + |
| 24 | +const A = 'com.acme.alpha'; |
| 25 | +const B = 'com.acme.beta'; |
| 26 | +const P = 'com.acme.caller'; |
| 27 | + |
| 28 | +const manifest = (id: string, deps: string[] = []) => ({ |
| 29 | + id, name: id, version: '1.0.0', type: 'app', namespace: id.split('.').pop()!, |
| 30 | + ...(deps.length > 0 ? { dependencies: Object.fromEntries(deps.map((d) => [d, '^1.0.0'])) } : {}), |
| 31 | +}) as never; |
| 32 | + |
| 33 | +let registry: SchemaRegistry; |
| 34 | +const install = (id: string, deps: string[] = []) => registry.installPackage(manifest(id, deps), 'code'); |
| 35 | + |
| 36 | +/** A package's action `name`, bound to `objectName` (or object-less), labelled by its package. */ |
| 37 | +const ship = (name: string, packageId: string, objectName?: string) => |
| 38 | + registry.registerItem('action', { |
| 39 | + name, label: `${packageId.split('.').pop()} ${objectName ?? 'global'}`, type: 'script', |
| 40 | + body: { language: 'js', source: 'return 1;', capabilities: [] }, |
| 41 | + ...(objectName ? { objectName } : {}), |
| 42 | + }, 'name' as never, packageId); |
| 43 | + |
| 44 | +/** An environment-authored action (no package), as `sys_metadata` hydration registers it. */ |
| 45 | +const authored = (name: string, objectName?: string) => |
| 46 | + registry.registerItem('action', { |
| 47 | + name, label: `env ${objectName ?? 'global'}`, type: 'script', |
| 48 | + body: { language: 'js', source: 'return 1;', capabilities: [] }, |
| 49 | + ...(objectName ? { objectName } : {}), _provenance: 'org', |
| 50 | + }, 'name' as never); |
| 51 | + |
| 52 | +const label = (r: PackageScopedResolution): string | undefined => |
| 53 | + r.status === 'resolved' ? (r.item as { label?: string }).label : undefined; |
| 54 | + |
| 55 | +const resolve = (objectName: string | undefined, actionName: string, callerPackageId?: string | null) => |
| 56 | + resolveActionByPackage(registry, { objectName, actionName, callerPackageId }); |
| 57 | + |
| 58 | +beforeEach(() => { |
| 59 | + registry = new SchemaRegistry(); |
| 60 | + registry.logLevel = 'silent'; |
| 61 | +}); |
| 62 | + |
| 63 | +describe('ADR-0150 D4 — an object-bound action is keyed (package, objectName, name)', () => { |
| 64 | + it('two packages\' same-named actions on two objects never meet: each object resolves its own holder', () => { |
| 65 | + install(A); |
| 66 | + install(B); |
| 67 | + ship('approve', A, 'account'); |
| 68 | + ship('approve', B, 'lead'); |
| 69 | + // Control: R alone, on the registry's (type, name) key, sees two holders. |
| 70 | + expect(resolvePackageScopedName(registry, { type: 'action', name: 'approve' })).toMatchObject({ status: 'refused' }); |
| 71 | + expect(label(resolve('account', 'approve'))).toBe('alpha account'); |
| 72 | + expect(label(resolve('lead', 'approve'))).toBe('beta lead'); |
| 73 | + // A's caller asks for `lead`: A's `account` action is not a candidate; B's is outside A's closure. |
| 74 | + expect(resolve('lead', 'approve', A)).toMatchObject({ |
| 75 | + status: 'refused', reason: 'outside-closure', candidates: [{ kind: 'package', packageId: B }], |
| 76 | + }); |
| 77 | + expect(label(resolve('account', 'approve', A))).toBe('alpha account'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('a caller of package P resolves its own action on the object before a closure holder\'s', () => { |
| 81 | + install(A); |
| 82 | + install(P, [A]); |
| 83 | + ship('approve', A, 'account'); |
| 84 | + ship('approve', P, 'account'); |
| 85 | + const r = resolve('account', 'approve', P); |
| 86 | + expect(r).toMatchObject({ status: 'resolved', via: 'own', holder: { kind: 'package', packageId: P } }); |
| 87 | + expect(label(r)).toBe('caller account'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('refuses a caller whose closure holds the name on the object twice, naming both holders', () => { |
| 91 | + install(A); |
| 92 | + install(B); |
| 93 | + install(P, [A, B]); |
| 94 | + ship('approve', A, 'account'); |
| 95 | + ship('approve', B, 'account'); |
| 96 | + const r = resolve('account', 'approve', P); |
| 97 | + expect(r).toMatchObject({ status: 'refused', reason: 'ambiguous-in-closure' }); |
| 98 | + if (r.status !== 'refused') throw new Error('unreachable'); |
| 99 | + expect(r.candidates).toEqual([{ kind: 'package', packageId: A }, { kind: 'package', packageId: B }]); |
| 100 | + for (const id of [A, B]) expect(r.message).toContain(id); |
| 101 | + }); |
| 102 | + |
| 103 | + it('D3 (a) — refuses a package caller whose only holder on the object is an environment-authored action, naming the environment', () => { |
| 104 | + install(P); |
| 105 | + authored('approve', 'account'); |
| 106 | + const r = resolve('account', 'approve', P); |
| 107 | + expect(r).toMatchObject({ status: 'refused', reason: 'outside-closure', candidates: [{ kind: 'environment' }] }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('D3 (a) control — the same environment-authored action resolves for a caller with no package', () => { |
| 111 | + install(P); |
| 112 | + authored('approve', 'account'); |
| 113 | + expect(resolve('account', 'approve')).toMatchObject({ status: 'resolved', via: 'environment', holder: { kind: 'environment' } }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('refuses a package caller whose only holder is a package outside its closure, naming it — never served', () => { |
| 117 | + install(A); |
| 118 | + install(P); |
| 119 | + ship('approve', A, 'account'); |
| 120 | + expect(resolve('account', 'approve', P)).toMatchObject({ |
| 121 | + status: 'refused', reason: 'outside-closure', candidates: [{ kind: 'package', packageId: A }], |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('falls to the object-less holders only when no holder is bound to the object, in the router\'s order', () => { |
| 126 | + install(A); |
| 127 | + install(B); |
| 128 | + install(P, [A, B]); |
| 129 | + ship('approve', A); // object-less |
| 130 | + expect(label(resolve('account', 'approve', P))).toBe('alpha global'); |
| 131 | + ship('approve', B, 'account'); |
| 132 | + expect(label(resolve('account', 'approve', P))).toBe('beta account'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('a refusal on the object never falls through to an object-less holder', () => { |
| 136 | + install(A); |
| 137 | + install(B); |
| 138 | + install(P, [A, B]); |
| 139 | + ship('approve', A, 'account'); |
| 140 | + ship('approve', B, 'account'); |
| 141 | + registry.registerItem('action', { name: 'approve', label: 'caller global', type: 'script', body: { language: 'js', source: 'return 1;', capabilities: [] } }, 'name' as never, P); |
| 142 | + expect(resolve('account', 'approve', P)).toMatchObject({ status: 'refused', reason: 'ambiguous-in-closure' }); |
| 143 | + }); |
| 144 | + |
| 145 | + it('an object-less route resolves among the object-less holders only', () => { |
| 146 | + install(A); |
| 147 | + install(P, [A]); |
| 148 | + ship('approve', A, 'account'); |
| 149 | + expect(resolve('global', 'approve', P)).toEqual({ status: 'not-found' }); |
| 150 | + ship('approve', P); |
| 151 | + expect(label(resolve('global', 'approve', P))).toBe('caller global'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('one holder of the name answers the same item R\'s own read answers (phase-1 parity, reading (b))', () => { |
| 155 | + install(A); |
| 156 | + ship('approve', A, 'account'); |
| 157 | + const r = resolve('account', 'approve'); |
| 158 | + const direct = resolvePackageScopedName(registry, { type: 'action', name: 'approve' }); |
| 159 | + expect(r).toEqual(direct); |
| 160 | + expect(r.status === 'resolved' ? actionHolderCoordinate(r) : undefined).toBe(A); |
| 161 | + }); |
| 162 | +}); |
| 163 | + |
| 164 | +describe('ADR-0150 D4 — the coordinate a handler is registered under', () => { |
| 165 | + it('reads a package\'s handler owner `app:ID` as that package, a plugin id as itself', () => { |
| 166 | + expect(actionHandlerCoordinate(`app:${A}`)).toBe(A); |
| 167 | + expect(actionHandlerCoordinate('com.objectstack.plugin.auth')).toBe('com.objectstack.plugin.auth'); |
| 168 | + }); |
| 169 | + |
| 170 | + it('reads the authored-action resync as the environment\'s slot, and no owner as host code\'s — two slots', () => { |
| 171 | + expect(actionHandlerCoordinate('metadata-service')).toBe(ENVIRONMENT_ACTION_COORDINATE); |
| 172 | + for (const owner of [undefined, '']) expect(actionHandlerCoordinate(owner)).toBe(HOST_ACTION_COORDINATE); |
| 173 | + expect(HOST_ACTION_COORDINATE).not.toBe(ENVIRONMENT_ACTION_COORDINATE); |
| 174 | + }); |
| 175 | + |
| 176 | + it('an environment holder resolves to the environment\'s coordinate', () => { |
| 177 | + authored('approve', 'account'); |
| 178 | + const r = resolve('account', 'approve'); |
| 179 | + expect(r.status === 'resolved' ? actionHolderCoordinate(r) : 'x').toBe(ENVIRONMENT_ACTION_COORDINATE); |
| 180 | + }); |
| 181 | +}); |
0 commit comments