Skip to content

Commit ad4a418

Browse files
committed
test(objectql): pin what the presentation and AI by-name readers answer under package addressing
Adds the nine-type list the ADR-0150 E8 readers consult (package, name) for, and the 409 RESOURCE_CONFLICT refusal they raise, with the expected answers of every reader: the two-holder refusal, D3 (a), the outside-closure step, and the one-holder controls. The reader wiring waits for the resolver core to become reachable from the lean ./core entry; until then the narrowing cases are red by design and the controls green. Claude-Session: https://claude.ai/code/session_01ADHkmTH1oAwgDMigQ9pj37 Co-authored-by: Claude <noreply@anthropic.com>
1 parent b126c13 commit ad4a418

2 files changed

Lines changed: 335 additions & 0 deletions

File tree

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
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+
});
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* # The by-name readers that consult `(package, name)` (ADR-0150 D3/D4)
5+
*
6+
* Child R (#31575) built ONE resolver for a bare name and wired no reader to
7+
* it. Each child E of epic #31527 wires the readers of its types; this module
8+
* holds what those readers share beside the resolver itself: which types are
9+
* wired, and the refusal a reader raises.
10+
*
11+
* ## The types wired so far
12+
*
13+
* {@link PACKAGE_SCOPED_READ_TYPES}: the presentation and AI types of child E8
14+
* (#31680) — app, page, dashboard, report, book, analytics cube, tool, skill
15+
* and agent. A later child adds its own types to the list in its own change.
16+
*
17+
* ## What a reader answers
18+
*
19+
* - **resolved** → the item, from the step that reached it;
20+
* - **not-found** → `undefined`, as before: zero holders is never a refusal;
21+
* - **refused** → {@link PackageScopedNameRefusedError}, thrown: the
22+
* resolver's refusal, its sentence unchanged, naming every candidate.
23+
* ⛔ Never `undefined`: a refusal read as a miss would tell the caller the
24+
* name means nothing, when it means more than one thing (red line 2).
25+
*
26+
* Lean on purpose: it imports nothing, so the registry, the engine and
27+
* `MetadataFacade` — all inside the `./core` entry's closure (ADR-0076 D2) —
28+
* may read it.
29+
*/
30+
31+
/**
32+
* The types whose by-name readers consult `(package, name)` today — child E8's
33+
* nine (#31680), as the registry spells them. Each later child of epic #31527
34+
* appends its own.
35+
*/
36+
export const PACKAGE_SCOPED_READ_TYPES = Object.freeze([
37+
'app',
38+
'page',
39+
'dashboard',
40+
'report',
41+
'book',
42+
'analytics_cube',
43+
'tool',
44+
'skill',
45+
'agent',
46+
] as const);
47+
48+
/** A type whose by-name readers consult `(package, name)` ({@link PACKAGE_SCOPED_READ_TYPES}). */
49+
export type PackageScopedReadType = (typeof PACKAGE_SCOPED_READ_TYPES)[number];
50+
51+
/** Do `type`'s by-name readers consult `(package, name)`? */
52+
export function isPackageScopedReadType(type: unknown): type is PackageScopedReadType {
53+
return typeof type === 'string' && (PACKAGE_SCOPED_READ_TYPES as readonly string[]).includes(type);
54+
}
55+
56+
/**
57+
* The resolver's refusal as a reader carries it: the reason, the item, the
58+
* caller's package, every candidate, and the resolver's one sentence
59+
* (`PackageScopedRefusal`, described here by shape so this module stays lean).
60+
*/
61+
export interface PackageScopedReadRefusal {
62+
readonly status: 'refused';
63+
readonly reason: string;
64+
readonly type: string;
65+
readonly name: string;
66+
readonly callerPackageId: string | null;
67+
readonly candidates: readonly unknown[];
68+
readonly message: string;
69+
}
70+
71+
/**
72+
* A by-name read the package-scoped resolver refused (ADR-0150 D3): the name
73+
* means more than one item here, or only items the caller may not reach. The
74+
* message is the resolver's own sentence, which names every candidate.
75+
*
76+
* The ADR-0112 envelope a wire door answers: `409 RESOURCE_CONFLICT`, the
77+
* standard catalog's member for a name in conflict, which ADR-0150 D5 gives a
78+
* by-name door's ambiguity. ⛔ Never the `500` fallback, and never `404`.
79+
*/
80+
export class PackageScopedNameRefusedError extends Error {
81+
readonly code = 'RESOURCE_CONFLICT';
82+
readonly status = 409;
83+
/** The same number under ADR-0112 D5's spelling — what a consumer holding the THROWN error reads. */
84+
readonly httpStatus = 409;
85+
/** The resolver's refusal: the reason, the caller's package and every candidate. */
86+
readonly refusal: PackageScopedReadRefusal;
87+
88+
constructor(refusal: PackageScopedReadRefusal) {
89+
super(refusal.message);
90+
this.name = 'PackageScopedNameRefusedError';
91+
this.refusal = refusal;
92+
}
93+
}

0 commit comments

Comments
 (0)