|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +// Pays the lint root's first load at collection, never inside a clocked case. |
| 4 | +import './__fixtures__/lint-root-preload.testkit.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * ADR-0150 child E8 (#31680): the by-name readers of the presentation and AI |
| 8 | + * types — app, page, dashboard, report, book, analytics cube, tool, skill and |
| 9 | + * agent — consult `(package, name)` through the one resolver |
| 10 | + * (`resolvePackageScopedName`, child R). |
| 11 | + * |
| 12 | + * The readers, each over the real `SchemaRegistry`: |
| 13 | + * |
| 14 | + * - the protocol's by-name read (`getMetaItem` / `getMetaItemForExecution`, |
| 15 | + * what `GET /api/v1/meta/TYPE/NAME` and MCP `agent_prompt` read) when it |
| 16 | + * names no package — a caller with no package; |
| 17 | + * - `SchemaRegistry.getApp(name, currentPackageId?)`; |
| 18 | + * - `MetadataFacade.get` / `getEntry` / `exists`. |
| 19 | + * |
| 20 | + * Narrowings, each declared on the card and the PR: (1) a bare name of an |
| 21 | + * uncovered type two packages hold refuses naming both, where the first |
| 22 | + * registered was served; (2) a package caller's bare name whose only holder is |
| 23 | + * the environment's own item (ADR-0150 D3 (a)); (3) a package caller's bare |
| 24 | + * name whose one package holder is outside its declared closure (D3 step 3, |
| 25 | + * the epic seat's ruling on #31527). Everything else is the CONTROL: one |
| 26 | + * holder answers as before, zero holders stays not-found, and the protocol |
| 27 | + * read that names a package (`?package=`, child G1's exact qualifier) is not |
| 28 | + * touched here. |
| 29 | + */ |
| 30 | + |
| 31 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 32 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 33 | +import { SchemaRegistry } from './registry.js'; |
| 34 | +import { MetadataFacade } from './metadata-facade.js'; |
| 35 | +import { resolveThrownHttpError } from '@objectstack/types'; |
| 36 | +import { |
| 37 | + PACKAGE_SCOPED_READ_TYPES, |
| 38 | + PackageScopedNameRefusedError, |
| 39 | +} from './package-scoped-read.js'; |
| 40 | +import { isPackageScopedType, resolvePackageScopedName } from './package-scoped-resolver.js'; |
| 41 | +import { BARE_NAME_REGISTRY_TYPES } from './bare-name-one-holder.js'; |
| 42 | + |
| 43 | +const A = 'com.acme.alpha'; |
| 44 | +const B = 'com.acme.beta'; |
| 45 | +const P = 'com.acme.caller'; |
| 46 | + |
| 47 | +/** The nine types this card wires, as the registry spells them. */ |
| 48 | +const E8_TYPES = ['app', 'page', 'dashboard', 'report', 'book', 'analytics_cube', 'tool', 'skill', 'agent'] as const; |
| 49 | +/** Of them, the ones phase 1's one-holder refusal does not cover: two package holders can exist today. */ |
| 50 | +const UNCOVERED = E8_TYPES.filter((t) => !(BARE_NAME_REGISTRY_TYPES as readonly string[]).includes(t)); |
| 51 | + |
| 52 | +const manifest = (id: string, deps: string[] = []) => ({ |
| 53 | + id, |
| 54 | + name: id, |
| 55 | + version: '1.0.0', |
| 56 | + type: 'app', |
| 57 | + namespace: id.split('.').pop()!, |
| 58 | + ...(deps.length > 0 ? { dependencies: Object.fromEntries(deps.map((d) => [d, '^1.0.0'])) } : {}), |
| 59 | +}) as never; |
| 60 | + |
| 61 | +let registry: SchemaRegistry; |
| 62 | +let protocol: ObjectStackProtocolImplementation; |
| 63 | +let facade: MetadataFacade; |
| 64 | + |
| 65 | +const install = (id: string, deps: string[] = []) => registry.installPackage(manifest(id, deps), 'code'); |
| 66 | +const body = (type: string, name: string, tag: string): Record<string, unknown> => |
| 67 | + type === 'skill' |
| 68 | + ? { name, label: `${tag} skill`, description: `${tag} skill`, instructions: tag, tools: [] } |
| 69 | + : type === 'agent' |
| 70 | + ? { name, label: `${tag} agent`, role: tag, instructions: tag } |
| 71 | + : { name, label: `${tag} ${type}` }; |
| 72 | +/** A package's item, in its composite slot. */ |
| 73 | +const ship = (type: string, name: string, packageId: string) => |
| 74 | + registry.registerItem(type, body(type, name, packageId.split('.').pop()!), 'name' as never, packageId); |
| 75 | +/** The environment's own item: a package-less row in the bare slot. */ |
| 76 | +const envItem = (type: string, name: string) => |
| 77 | + registry.registerItem(type, { ...body(type, name, 'env'), _provenance: 'org' }, 'name' as never); |
| 78 | +const labelOf = (item: unknown) => (item as { label?: string } | undefined)?.label; |
| 79 | + |
| 80 | +/** The coded refusal every rewired reader raises, with every candidate named. */ |
| 81 | +async function expectRefusal(read: () => unknown, reason: string, named: readonly string[]) { |
| 82 | + let thrown: unknown; |
| 83 | + try { |
| 84 | + await read(); |
| 85 | + } catch (err) { |
| 86 | + thrown = err; |
| 87 | + } |
| 88 | + expect(thrown, 'the read refuses').toBeInstanceOf(PackageScopedNameRefusedError); |
| 89 | + const err = thrown as PackageScopedNameRefusedError; |
| 90 | + expect(err.code).toBe('RESOURCE_CONFLICT'); |
| 91 | + expect(err.status).toBe(409); |
| 92 | + expect(err.refusal.reason).toBe(reason); |
| 93 | + for (const holder of named) expect(err.message).toContain(holder); |
| 94 | +} |
| 95 | + |
| 96 | +beforeEach(() => { |
| 97 | + registry = new SchemaRegistry({ multiTenant: false }); |
| 98 | + const engine = { |
| 99 | + registry, |
| 100 | + find: vi.fn().mockResolvedValue([]), |
| 101 | + findOne: vi.fn().mockResolvedValue(null), |
| 102 | + insert: vi.fn(), |
| 103 | + update: vi.fn(), |
| 104 | + delete: vi.fn(), |
| 105 | + count: vi.fn().mockResolvedValue(0), |
| 106 | + aggregate: vi.fn().mockResolvedValue([]), |
| 107 | + }; |
| 108 | + protocol = new ObjectStackProtocolImplementation(engine as never); |
| 109 | + facade = new MetadataFacade(registry); |
| 110 | + install(A); |
| 111 | + install(B); |
| 112 | +}); |
| 113 | + |
| 114 | +describe('E8 wires exactly the nine types, every one of them package-scoped', () => { |
| 115 | + it('PACKAGE_SCOPED_READ_TYPES is the card\'s nine, each one R addresses by package', () => { |
| 116 | + expect([...PACKAGE_SCOPED_READ_TYPES].sort()).toEqual([...E8_TYPES].sort()); |
| 117 | + for (const type of PACKAGE_SCOPED_READ_TYPES) expect(isPackageScopedType(type), type).toBe(true); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('the refusal crosses a wire door as 409 RESOURCE_CONFLICT (ADR-0150 D5), never 500 or 404', () => { |
| 122 | + it('the dispatcher\'s thrown-error reading answers 409 with the resolver\'s sentence', () => { |
| 123 | + install(P); |
| 124 | + ship('page', 'home', A); |
| 125 | + ship('page', 'home', B); |
| 126 | + const refusal = resolvePackageScopedName(registry, { type: 'page', name: 'home' }); |
| 127 | + expect(refusal.status).toBe('refused'); |
| 128 | + const err = new PackageScopedNameRefusedError(refusal as never); |
| 129 | + const answer = resolveThrownHttpError(err, 500); |
| 130 | + expect(answer.status).toBe(409); |
| 131 | + expect(answer.code).toBe('RESOURCE_CONFLICT'); |
| 132 | + expect(answer.message).toBe((refusal as { message: string }).message); |
| 133 | + expect(answer.message).toContain(A); |
| 134 | + expect(answer.message).toContain(B); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +describe('narrowing (1) — an uncovered type two packages hold: the bare read refuses naming both', () => { |
| 139 | + for (const type of UNCOVERED) { |
| 140 | + it(`${type}: a caller with no package is refused at the protocol read (both faces), the registry and the facade`, async () => { |
| 141 | + ship(type, 'home', A); |
| 142 | + ship(type, 'home', B); |
| 143 | + await expectRefusal(() => protocol.getMetaItem({ type, name: 'home' }), 'ambiguous-in-environment', [A, B]); |
| 144 | + await expectRefusal(() => protocol.getMetaItemForExecution({ type, name: 'home' }), 'ambiguous-in-environment', [A, B]); |
| 145 | + await expectRefusal(() => facade.get(type, 'home'), 'ambiguous-in-environment', [A, B]); |
| 146 | + await expectRefusal(() => facade.getEntry(type, 'home'), 'ambiguous-in-environment', [A, B]); |
| 147 | + if (type === 'app') await expectRefusal(() => registry.getApp('home'), 'ambiguous-in-environment', [A, B]); |
| 148 | + }); |
| 149 | + |
| 150 | + it(`${type}: each package's own item for its own callers`, async () => { |
| 151 | + ship(type, 'home', A); |
| 152 | + ship(type, 'home', B); |
| 153 | + expect(labelOf(await facade.get(type, 'home', A))).toBe(`alpha ${type}`); |
| 154 | + expect(labelOf(await facade.get(type, 'home', B))).toBe(`beta ${type}`); |
| 155 | + if (type === 'app') { |
| 156 | + expect(labelOf(registry.getApp('home', A))).toBe('alpha app'); |
| 157 | + expect(labelOf(registry.getApp('home', B))).toBe('beta app'); |
| 158 | + } |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + it('`exists` answers true: two holders are a fact, not an absence', async () => { |
| 163 | + ship('page', 'home', A); |
| 164 | + ship('page', 'home', B); |
| 165 | + expect(await facade.exists('page', 'home')).toBe(true); |
| 166 | + }); |
| 167 | +}); |
| 168 | + |
| 169 | +describe('CONTROL — one holder answers exactly as before, at every rewired reader', () => { |
| 170 | + for (const type of E8_TYPES) { |
| 171 | + it(`${type}: one package holder is served to a caller with no package, to the holder and to a dependent`, async () => { |
| 172 | + install(P, [A]); |
| 173 | + ship(type, 'solo', A); |
| 174 | + const before = registry.getItem(type, 'solo'); |
| 175 | + expect(labelOf(before)).toBe(`alpha ${type}`); |
| 176 | + expect(labelOf((await protocol.getMetaItem({ type, name: 'solo' }))?.item)).toBe(`alpha ${type}`); |
| 177 | + expect(labelOf((await protocol.getMetaItemForExecution({ type, name: 'solo' }))?.item)).toBe(`alpha ${type}`); |
| 178 | + expect(labelOf(await facade.get(type, 'solo'))).toBe(`alpha ${type}`); |
| 179 | + expect(labelOf(await facade.get(type, 'solo', A))).toBe(`alpha ${type}`); |
| 180 | + expect(labelOf(await facade.get(type, 'solo', P))).toBe(`alpha ${type}`); |
| 181 | + expect(labelOf(facade.getEntry(type, 'solo'))).toBe(`alpha ${type}`); |
| 182 | + expect(await facade.exists(type, 'solo')).toBe(true); |
| 183 | + }); |
| 184 | + |
| 185 | + it(`${type}: the environment's own item is served to a caller with no package`, async () => { |
| 186 | + envItem(type, 'local'); |
| 187 | + expect(labelOf((await protocol.getMetaItem({ type, name: 'local' }))?.item)).toBe(`env ${type}`); |
| 188 | + expect(labelOf(await facade.get(type, 'local'))).toBe(`env ${type}`); |
| 189 | + }); |
| 190 | + |
| 191 | + it(`${type}: zero holders stays not-found, never a refusal`, async () => { |
| 192 | + expect((await protocol.getMetaItem({ type, name: 'nothing' }))?.item).toBeUndefined(); |
| 193 | + expect(await facade.get(type, 'nothing')).toBeUndefined(); |
| 194 | + expect(await facade.get(type, 'nothing', A)).toBeUndefined(); |
| 195 | + expect(await facade.exists(type, 'nothing')).toBe(false); |
| 196 | + }); |
| 197 | + |
| 198 | + it(`${type}: the protocol read that names a package is untouched (child G1's exact qualifier)`, async () => { |
| 199 | + ship(type, 'solo', A); |
| 200 | + expect(labelOf((await protocol.getMetaItem({ type, name: 'solo', packageId: A }))?.item)).toBe(`alpha ${type}`); |
| 201 | + }); |
| 202 | + } |
| 203 | + |
| 204 | + it('an app served through getApp still carries its navigation contributions', () => { |
| 205 | + ship('app', 'crm', A); |
| 206 | + registry.registerAppNavContribution({ app: 'crm', items: [{ id: 'nav_x', type: 'url', url: '/x', label: 'X' }] }, B); |
| 207 | + for (const app of [registry.getApp('crm'), registry.getApp('crm', A)] as Array<{ navigation?: Array<{ id?: string }> }>) { |
| 208 | + expect(app?.navigation?.map((n) => n.id)).toContain('nav_x'); |
| 209 | + } |
| 210 | + }); |
| 211 | +}); |
| 212 | + |
| 213 | +describe('narrowing (2) — D3 (a): a package caller never resolves to the environment\'s own item', () => { |
| 214 | + for (const type of E8_TYPES) { |
| 215 | + it(`${type}: refused, naming the environment`, async () => { |
| 216 | + install(P); |
| 217 | + envItem(type, 'local'); |
| 218 | + await expectRefusal(() => facade.get(type, 'local', P), 'outside-closure', ['the environment']); |
| 219 | + if (type === 'app') await expectRefusal(() => registry.getApp('local', P), 'outside-closure', ['the environment']); |
| 220 | + }); |
| 221 | + } |
| 222 | +}); |
| 223 | + |
| 224 | +describe('narrowing (3) — D3 step 3: one holder outside the package caller\'s declared closure', () => { |
| 225 | + for (const type of E8_TYPES) { |
| 226 | + it(`${type}: refused, naming the holder; inside the closure it is served`, async () => { |
| 227 | + install(P); |
| 228 | + ship(type, 'solo', A); |
| 229 | + await expectRefusal(() => facade.get(type, 'solo', P), 'outside-closure', [A]); |
| 230 | + if (type === 'app') await expectRefusal(() => registry.getApp('solo', P), 'outside-closure', [A]); |
| 231 | + install('com.acme.dependent', [A]); |
| 232 | + expect(labelOf(await facade.get(type, 'solo', 'com.acme.dependent'))).toBe(`alpha ${type}`); |
| 233 | + }); |
| 234 | + } |
| 235 | +}); |
| 236 | + |
| 237 | +describe('the types E8 does not wire keep the legacy read', () => { |
| 238 | + it('a view (bucket 1) and a flow (child E1) are read by getItem, untouched', async () => { |
| 239 | + ship('flow', 'onboard', A); |
| 240 | + expect(labelOf(await facade.get('flow', 'onboard', P))).toBe('alpha flow'); |
| 241 | + }); |
| 242 | +}); |
0 commit comments