|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#24472] A top-level action bound by `objectName` to an object that already |
| 5 | + * lists an action of the same name is not attached — the object keeps serving |
| 6 | + * its own declaration (the router's rung 1). When the two declarations DIFFER, |
| 7 | + * that pair is reported, never refused: one warning per pair, naming the |
| 8 | + * object, the action and both packages, at `kernel:ready` and for each artifact |
| 9 | + * that arrives later. A pair that is one declaration carried twice is not a |
| 10 | + * collision. |
| 11 | + * |
| 12 | + * The handler face of the same pair: the engine keys handlers by |
| 13 | + * `OBJECT:ACTION`, so the second package's `registerAction` replaces the |
| 14 | + * first's. It still does — the last registration runs — but it is warned, |
| 15 | + * naming both owners; the same owner re-registering is not. |
| 16 | + */ |
| 17 | + |
| 18 | +import { describe, it, expect, afterEach, vi } from 'vitest'; |
| 19 | +import { ObjectKernel, type PluginContext } from '@objectstack/core'; |
| 20 | +import { defineStack } from '@objectstack/spec'; |
| 21 | +import { ObjectQL } from './engine.js'; |
| 22 | +import { ObjectQLPlugin } from './plugin.js'; |
| 23 | + |
| 24 | +/** The package that OWNS the target object, with an action of its own. */ |
| 25 | +const OWNER = { |
| 26 | + id: 'com.test.owner', name: 'owner', |
| 27 | + objects: [{ |
| 28 | + name: 'bc_org', label: 'Org', |
| 29 | + fields: { name: { name: 'name', type: 'text', label: 'Name' } }, |
| 30 | + actions: [{ name: 'rename', label: 'Rename', type: 'url', target: 'https://example.com/own', locations: ['record_header'] }], |
| 31 | + }], |
| 32 | +}; |
| 33 | + |
| 34 | +/** Another package binding an action of the same NAME to the owner's object — a different declaration. */ |
| 35 | +const TWIN = { |
| 36 | + id: 'com.test.twin', name: 'twin', |
| 37 | + actions: [{ name: 'rename', label: 'Rename (twin)', objectName: 'bc_org', type: 'url', target: 'https://example.com/twin', locations: ['record_header'] }], |
| 38 | +}; |
| 39 | + |
| 40 | +/** Another package carrying the owner's declaration itself, bound by `objectName` — one declaration, two packages. */ |
| 41 | +const CARRIER = { |
| 42 | + id: 'com.test.carrier', name: 'carrier', |
| 43 | + actions: [{ ...OWNER.objects[0].actions[0], objectName: 'bc_org' }], |
| 44 | +}; |
| 45 | + |
| 46 | +/** A package that brings an unrelated object — the late-artifact control. */ |
| 47 | +const BYSTANDER = { |
| 48 | + id: 'com.test.bystander', name: 'bystander', |
| 49 | + objects: [{ name: 'bc_other', label: 'Other', fields: { name: { name: 'name', type: 'text', label: 'Name' } } }], |
| 50 | +}; |
| 51 | + |
| 52 | +async function engineWith(packages: any[]) { |
| 53 | + const engine = new ObjectQL(); |
| 54 | + await engine.init(); |
| 55 | + for (const pkg of packages) engine.registerApp(structuredClone(pkg)); |
| 56 | + return engine; |
| 57 | +} |
| 58 | + |
| 59 | +const servedRename = (engine: ObjectQL): any[] => |
| 60 | + ((engine.registry.getObject('bc_org') as any)?.actions ?? []).filter((a: any) => a?.name === 'rename'); |
| 61 | + |
| 62 | +describe('SchemaRegistry.findBoundActionCollisions', () => { |
| 63 | + for (const [order, packages] of [ |
| 64 | + ['owner first', [OWNER, TWIN]], |
| 65 | + ['twin first', [TWIN, OWNER]], |
| 66 | + ] as const) { |
| 67 | + it(`${order}: a different declaration under the object's own action name is one pair, naming both packages`, async () => { |
| 68 | + const engine = await engineWith([...packages]); |
| 69 | + expect(engine.registry.findBoundActionCollisions()).toEqual([ |
| 70 | + { objectName: 'bc_org', action: 'rename', servedPackageId: 'com.test.owner', shadowedPackageId: 'com.test.twin' }, |
| 71 | + ]); |
| 72 | + // Reported, not acted on: the object still serves its own declaration. |
| 73 | + expect(servedRename(engine).map((a) => a.target)).toEqual(['https://example.com/own']); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + it('one declaration carried by two packages is not a collision', async () => { |
| 78 | + const engine = await engineWith([OWNER, CARRIER]); |
| 79 | + expect(engine.registry.findBoundActionCollisions()).toEqual([]); |
| 80 | + }); |
| 81 | + |
| 82 | + it('a same-stack bound action — merged onto its object and kept top-level — is not a collision', async () => { |
| 83 | + const stack = defineStack({ |
| 84 | + manifest: { id: 'com.test.same', name: 'same', version: '1.0.0', type: 'app' }, |
| 85 | + objects: [{ name: 'bc_same', label: 'Same', fields: { name: { name: 'name', type: 'text', label: 'Name' } } }], |
| 86 | + actions: [{ name: 'same_action', label: 'Same', objectName: 'bc_same', type: 'url', target: 'https://example.com', locations: ['record_header'] }], |
| 87 | + } as any); |
| 88 | + // The JSON round trip is the compiled artifact's shape: two copies, not one reference. |
| 89 | + const engine = await engineWith([JSON.parse(JSON.stringify({ ...stack, ...stack.manifest }))]); |
| 90 | + expect((engine.registry.getObject('bc_same') as any)?.actions?.map((a: any) => a.name)).toEqual(['same_action']); |
| 91 | + expect(engine.registry.findBoundActionCollisions()).toEqual([]); |
| 92 | + }); |
| 93 | + |
| 94 | + it('two packages binding different declarations of one name to a third package\'s object: the first attached is served, the second reported', async () => { |
| 95 | + const owner = { ...OWNER, objects: [{ ...OWNER.objects[0], actions: [] }] }; |
| 96 | + const second = { ...TWIN, id: 'com.test.second', name: 'second', actions: [{ ...TWIN.actions[0], label: 'Rename (second)', target: 'https://example.com/second' }] }; |
| 97 | + const engine = await engineWith([owner, TWIN, second]); |
| 98 | + expect(servedRename(engine).map((a) => a._packageId)).toEqual(['com.test.twin']); |
| 99 | + expect(engine.registry.findBoundActionCollisions()).toEqual([ |
| 100 | + { objectName: 'bc_org', action: 'rename', servedPackageId: 'com.test.twin', shadowedPackageId: 'com.test.second' }, |
| 101 | + ]); |
| 102 | + }); |
| 103 | + |
| 104 | + it('narrowed to a set of packages, it answers only the pairs naming one of them', async () => { |
| 105 | + const engine = await engineWith([OWNER, TWIN, BYSTANDER]); |
| 106 | + expect(engine.registry.findBoundActionCollisions(new Set(['com.test.bystander']))).toEqual([]); |
| 107 | + expect(engine.registry.findBoundActionCollisions(new Set(['com.test.owner']))).toHaveLength(1); |
| 108 | + expect(engine.registry.findBoundActionCollisions(new Set(['com.test.twin']))).toHaveLength(1); |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +// ── The report: at kernel:ready, and for each later artifact ─────────────── |
| 113 | + |
| 114 | +interface ManifestService { |
| 115 | + register(artifact: unknown): Promise<void> | void; |
| 116 | +} |
| 117 | + |
| 118 | +const kernels: ObjectKernel[] = []; |
| 119 | +afterEach(async () => { |
| 120 | + while (kernels.length) { |
| 121 | + const kernel = kernels.pop()!; |
| 122 | + try { if (kernel.getState() === 'running') await kernel.shutdown(); } catch { /* noop */ } |
| 123 | + } |
| 124 | +}); |
| 125 | + |
| 126 | +function registering(name: string, artifacts: unknown[]) { |
| 127 | + return { |
| 128 | + name, |
| 129 | + dependencies: ['com.objectstack.engine.objectql'], |
| 130 | + init: async (ctx: PluginContext) => { |
| 131 | + const manifest = ctx.getService<ManifestService>('manifest'); |
| 132 | + for (const artifact of artifacts) await manifest.register(structuredClone(artifact)); |
| 133 | + }, |
| 134 | + }; |
| 135 | +} |
| 136 | + |
| 137 | +/** A kernel whose logger's `warn` is spied: the plugin's `ctx.logger` is the kernel's own. */ |
| 138 | +function kernelWith(...plugins: any[]) { |
| 139 | + const kernel = new ObjectKernel({ logger: { level: 'silent' }, gracefulShutdown: false }); |
| 140 | + kernels.push(kernel); |
| 141 | + const warn = vi.spyOn((kernel as any).logger, 'warn'); |
| 142 | + const collisionWarnings = () => warn.mock.calls.filter(([, meta]: any[]) => meta && 'shadowedPackageId' in meta); |
| 143 | + return { kernel, collisionWarnings, use: async () => { for (const p of plugins) await kernel.use(p); } }; |
| 144 | +} |
| 145 | + |
| 146 | +describe('the bound-action collision report', () => { |
| 147 | + it('warns once at kernel:ready, naming the object, the action and both packages — and the boot goes on', async () => { |
| 148 | + const { kernel, collisionWarnings, use } = kernelWith(new ObjectQLPlugin(), registering('owner', [OWNER]), registering('twin', [TWIN])); |
| 149 | + await use(); |
| 150 | + await kernel.bootstrap(); |
| 151 | + const warned = collisionWarnings(); |
| 152 | + expect(warned).toHaveLength(1); |
| 153 | + const [message, meta] = warned[0] as [string, any]; |
| 154 | + expect(meta).toEqual({ objectName: 'bc_org', action: 'rename', servedPackageId: 'com.test.owner', shadowedPackageId: 'com.test.twin' }); |
| 155 | + for (const subject of ["'rename'", "'bc_org'", "'com.test.owner'", "'com.test.twin'"]) expect(message).toContain(subject); |
| 156 | + expect(servedRename(kernel.getService<ObjectQL>('objectql')).map((a) => a.target)).toEqual(['https://example.com/own']); |
| 157 | + }); |
| 158 | + |
| 159 | + it('control: one declaration carried by two packages boots with no warning', async () => { |
| 160 | + const { kernel, collisionWarnings, use } = kernelWith(new ObjectQLPlugin(), registering('owner', [OWNER]), registering('carrier', [CARRIER])); |
| 161 | + await use(); |
| 162 | + await kernel.bootstrap(); |
| 163 | + expect(collisionWarnings()).toEqual([]); |
| 164 | + }); |
| 165 | + |
| 166 | + it('an artifact arriving after the boot is judged once it registers, and nothing of it is refused', async () => { |
| 167 | + const { kernel, collisionWarnings, use } = kernelWith(new ObjectQLPlugin(), registering('owner', [OWNER])); |
| 168 | + await use(); |
| 169 | + await kernel.bootstrap(); |
| 170 | + expect(collisionWarnings()).toEqual([]); |
| 171 | + await kernel.getService<ManifestService>('manifest').register(structuredClone(TWIN)); |
| 172 | + expect(collisionWarnings().map(([, meta]) => meta)).toEqual([ |
| 173 | + { objectName: 'bc_org', action: 'rename', servedPackageId: 'com.test.owner', shadowedPackageId: 'com.test.twin' }, |
| 174 | + ]); |
| 175 | + expect(kernel.getService<ObjectQL>('objectql').registry.getItem('action', 'rename')).toBeDefined(); |
| 176 | + }); |
| 177 | + |
| 178 | + it('a pair the boot reported is not reported again for an unrelated late artifact', async () => { |
| 179 | + const { kernel, collisionWarnings, use } = kernelWith(new ObjectQLPlugin(), registering('owner', [OWNER]), registering('twin', [TWIN])); |
| 180 | + await use(); |
| 181 | + await kernel.bootstrap(); |
| 182 | + await kernel.getService<ManifestService>('manifest').register(structuredClone(BYSTANDER)); |
| 183 | + expect(collisionWarnings()).toHaveLength(1); |
| 184 | + }); |
| 185 | +}); |
| 186 | + |
| 187 | +// ── The handler face: registerAction's OBJECT:ACTION key ──────────────────── |
| 188 | + |
| 189 | +describe('ObjectQL.registerAction on a key another owner holds', () => { |
| 190 | + async function engineSpied() { |
| 191 | + const engine = new ObjectQL(); |
| 192 | + const warn = vi.spyOn((engine as any).logger, 'warn'); |
| 193 | + const replacements = () => warn.mock.calls.filter(([, meta]: any[]) => meta && 'replacedPackage' in meta); |
| 194 | + return { engine, replacements }; |
| 195 | + } |
| 196 | + |
| 197 | + it('replaces the handler — the last registration runs — and warns naming both owners', async () => { |
| 198 | + const { engine, replacements } = await engineSpied(); |
| 199 | + engine.registerAction('bc_org', 'rename', () => 'owner', 'app:com.test.owner'); |
| 200 | + engine.registerAction('bc_org', 'rename', () => 'twin', 'app:com.test.twin'); |
| 201 | + expect(await engine.executeAction('bc_org', 'rename', {})).toBe('twin'); |
| 202 | + expect(replacements()).toHaveLength(1); |
| 203 | + const [message, meta] = replacements()[0] as [string, any]; |
| 204 | + expect(meta).toEqual({ objectName: 'bc_org', actionName: 'rename', package: 'app:com.test.twin', replacedPackage: 'app:com.test.owner' }); |
| 205 | + for (const subject of ["'bc_org:rename'", "'app:com.test.owner'", "'app:com.test.twin'"]) expect(message).toContain(subject); |
| 206 | + }); |
| 207 | + |
| 208 | + it('withdrawing the replacing owner leaves the key with no handler — the consequence the warning names', async () => { |
| 209 | + const { engine } = await engineSpied(); |
| 210 | + engine.registerAction('bc_org', 'rename', () => 'owner', 'app:com.test.owner'); |
| 211 | + engine.registerAction('bc_org', 'rename', () => 'twin', 'app:com.test.twin'); |
| 212 | + engine.removeActionsByPackage('app:com.test.twin'); |
| 213 | + await expect(engine.executeAction('bc_org', 'rename', {})).rejects.toThrow(/not found/); |
| 214 | + }); |
| 215 | + |
| 216 | + it('a registration with no package and a package\'s are two owners', async () => { |
| 217 | + const { engine, replacements } = await engineSpied(); |
| 218 | + engine.registerAction('bc_org', 'rename', () => 'owner', 'app:com.test.owner'); |
| 219 | + engine.registerAction('bc_org', 'rename', () => 'imperative'); |
| 220 | + expect(replacements().map(([, meta]) => meta.replacedPackage)).toEqual(['app:com.test.owner']); |
| 221 | + }); |
| 222 | + |
| 223 | + it('the same owner re-registering, and the tear-down-then-register resync, are silent', async () => { |
| 224 | + const { engine, replacements } = await engineSpied(); |
| 225 | + engine.registerAction('bc_org', 'rename', () => 1, 'app:com.test.owner'); |
| 226 | + engine.registerAction('bc_org', 'rename', () => 2, 'app:com.test.owner'); |
| 227 | + engine.registerAction('bc_org', 'unowned', () => 1); |
| 228 | + engine.registerAction('bc_org', 'unowned', () => 2); |
| 229 | + engine.registerAction('bc_org', 'authored', () => 1, 'metadata-service'); |
| 230 | + engine.removeActionsByPackage('metadata-service'); |
| 231 | + engine.registerAction('bc_org', 'authored', () => 2, 'metadata-service'); |
| 232 | + expect(replacements()).toEqual([]); |
| 233 | + expect(await engine.executeAction('bc_org', 'rename', {})).toBe(2); |
| 234 | + }); |
| 235 | +}); |
0 commit comments