|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#33904, ADR-0150 D4] The merged action list slots an action at D4's key |
| 5 | + * `(package, objectName, name)` where a PACKAGE's contribution of the name |
| 6 | + * binds the object (object-less being a key of its own), and by name |
| 7 | + * elsewhere, as before. |
| 8 | + * |
| 9 | + * Each cell names its FROM (the list slotted every action by name) and its TO. |
| 10 | + * A package-less row on a package's object is that package's customization |
| 11 | + * and shares its slot; a package-less row on no package's object is the |
| 12 | + * environment's own item and is listed beside the packages'. Package-less |
| 13 | + * contributions with no package holding the name keep one slot, which the |
| 14 | + * action route reads as the platform item's key |
| 15 | + * (runtime `action-package-addressing.test.ts`). |
| 16 | + * |
| 17 | + * The read and delete halves, through the protocol, are pinned in |
| 18 | + * `protocol.packageless-save-address.test.ts`. |
| 19 | + */ |
| 20 | + |
| 21 | +import { describe, expect, it } from 'vitest'; |
| 22 | +import { mergePackageAwareOverlay } from './protocol-overlay-merge.js'; |
| 23 | + |
| 24 | +const NAME = 'approve'; |
| 25 | +const action = (label: string, objectName?: string, packageId?: string) => ({ |
| 26 | + name: NAME, label, type: 'url', target: `https://example.invalid/${label}`, |
| 27 | + ...(objectName ? { objectName } : {}), |
| 28 | + ...(packageId ? { _packageId: packageId } : {}), |
| 29 | +}); |
| 30 | +const row = (data: object, packageId?: string, standIn = false) => ({ |
| 31 | + data, packageId, stored: { type: 'action', name: NAME }, ...(standIn ? { standIn: true } : {}), |
| 32 | +}); |
| 33 | +/** `[label, objectName, package]` of each listed item, in list order. */ |
| 34 | +const listed = (out: unknown[]) => (out as any[]).map((i) => [i.label, i.objectName ?? null, i._packageId ?? null]); |
| 35 | + |
| 36 | +describe('[#33904] the list slots an action at its key where a package binds the object', () => { |
| 37 | + it('two packages on two objects, a package-less row on one of them: the row is that package\'s customization, and the other package\'s own item is listed (BASE: the row under both packages, P2\'s item dropped)', () => { |
| 38 | + const out = mergePackageAwareOverlay('action', [action('p1', 'account', 'P1'), action('p2', 'contact', 'P2')], [row(action('env', 'account'))]); |
| 39 | + expect(listed(out)).toEqual([['env', 'account', 'P1'], ['p2', 'contact', 'P2']]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('a list scoped to a code package P on `account`, the environment\'s row on `contact` passed as a stand-in: P\'s slot is P\'s own item, and the stand-in seats none (BASE: P\'s slot served the environment\'s body)', () => { |
| 43 | + const unseated = new WeakSet<object>(); |
| 44 | + const out = mergePackageAwareOverlay('action', [action('p', 'account', 'P')], [row(action('env', 'contact'), undefined, true)], undefined, unseated); |
| 45 | + expect(listed(out)[0]).toEqual(['p', 'account', 'P']); |
| 46 | + // The stand-in reaches a slot of its own: emitted and recorded, so the |
| 47 | + // caller drops it once the last layer has merged. |
| 48 | + expect((out as any[]).slice(1).every((item) => unseated.has(item))).toBe(true); |
| 49 | + }); |
| 50 | + |
| 51 | + it('a package\'s OBJECT-LESS action stored, the environment\'s row on `contact`: both listed (BASE: P\'s item only)', () => { |
| 52 | + const out = mergePackageAwareOverlay('action', [], [row(action('p'), 'P'), row(action('env', 'contact'))]); |
| 53 | + expect(listed(out)).toEqual([['p', null, 'P'], ['env', 'contact', null]]); |
| 54 | + }); |
| 55 | + |
| 56 | + it('a code package\'s OBJECT-LESS action, the environment\'s row on `contact`: both listed (BASE: the environment\'s body under P\'s name)', () => { |
| 57 | + const out = mergePackageAwareOverlay('action', [action('p', undefined, 'P')], [row(action('env', 'contact'))]); |
| 58 | + expect(listed(out)).toEqual([['p', null, 'P'], ['env', 'contact', null]]); |
| 59 | + }); |
| 60 | + |
| 61 | + it('a package on `account`, the environment\'s OBJECT-LESS row: both listed (BASE: P\'s item only)', () => { |
| 62 | + const out = mergePackageAwareOverlay('action', [], [row(action('p', 'account'), 'P'), row(action('env'))]); |
| 63 | + expect(listed(out)).toEqual([['p', 'account', 'P'], ['env', null, null]]); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('CONTROLs (unchanged)', () => { |
| 67 | + it('a package-less row on the object a package binds is its customization', () => { |
| 68 | + const out = mergePackageAwareOverlay('action', [action('p', 'account', 'P')], [row(action('env', 'account'))]); |
| 69 | + expect(listed(out)).toEqual([['env', 'account', 'P']]); |
| 70 | + }); |
| 71 | + |
| 72 | + it('a package-less OBJECT-LESS row beside a package\'s object-less action is its customization', () => { |
| 73 | + const out = mergePackageAwareOverlay('action', [action('p', undefined, 'P')], [row(action('env'))]); |
| 74 | + expect(listed(out)).toEqual([['env', null, 'P']]); |
| 75 | + }); |
| 76 | + |
| 77 | + it('no package holds the name: a platform item on `lead` and the environment\'s object-less row stay one slot', () => { |
| 78 | + const out = mergePackageAwareOverlay('action', [action('platform', 'lead')], [row(action('env'))]); |
| 79 | + expect(listed(out)).toEqual([['env', null, null]]); |
| 80 | + }); |
| 81 | + |
| 82 | + it('another type is slotted by name alone', () => { |
| 83 | + const out = mergePackageAwareOverlay('flow', [{ name: NAME, label: 'p', objectName: 'account', _packageId: 'P' }], [row({ name: NAME, label: 'env', objectName: 'contact' })]); |
| 84 | + expect(listed(out)).toEqual([['env', 'contact', 'P']]); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments