diff --git a/.changeset/32996-action-route-holders-on-route.md b/.changeset/32996-action-route-holders-on-route.md new file mode 100644 index 00000000000..7b473ac16e0 --- /dev/null +++ b/.changeset/32996-action-route-holders-on-route.md @@ -0,0 +1,26 @@ +--- +'@objectstack/runtime': patch +--- + +fix(runtime)!: the action route answers by who holds the name on the route, and the environment's exact address answers its own item + +Clause-②: yes (narrowing) — narrowed: the bare route `/api/v1/actions/lead/NAME`, where a disabled package's action owns `lead` (bound to it, or object-less) and the environment's own action of that name is on `lead` too, answered the environment's action when exactly one enabled package held the name on another object; it now answers no declaration there, as it already did with none or two. With an enabled package on another object and a disabled package's object-less action, the same route answered the environment's action or no declaration by registration order; it now answers no declaration in both orders. Widened: a served package's own route answers its own action beside the environment's or the platform's item on another object, and beside a disabled co-holder (was no declaration, or the environment's object-less item); and `/api/v1/actions/_env/OBJECT/NAME` answers the environment's own action while a package holds the name (was `404`). +Metadata-protocol: none + + + +**BREAKING** for one composition only: an action name held by a disabled package on an object (or with no object), by the environment's own action on that object, and by exactly one enabled package on another object. The route `/api/v1/actions/OBJECT/NAME` used to serve the environment's action there. It now answers that the action has no declaration, the answer it already gave when no enabled package, or two, held the name elsewhere. A disabled package that still binds the route is a holder there, so the route does not hand its call to another holder's declaration. + +The route now decides from the holders of the name on the route itself: the items bound to the routed object, else the ones with no object. The protocol's by-name read cannot see the routed object, so its answer depended on which holders sat on other objects and on registration order. In order: + +1. No enabled package's action is on the route and a disabled package's is: no declaration. +2. The environment's own action is on the route: that action, with no package (ADR-0150 D3: the environment's own declaration answers first on one owner key). +3. Exactly one enabled package's action is on the route and no platform action is there: that package's own action, read at its exact address and served with no package. A disabled co-holder, and the environment's or the platform's action on another object, do not change it. + +Every other composition answers as before: one holder alone, two enabled packages on the route (refused naming both), the platform's action on the routed object, and every call that names its package. A store read that fails answers `503`, not a declaration. + +The environment's exact address `/api/v1/actions/_env/OBJECT/NAME` answers the environment's own action whatever the other holders. The merged action listing leaves the environment's action out when a package holds the same name, so the address answered `404`. A package-less action with no row in the environment's store is the platform's and still answers `404` there. + +Not changed: the registry keeps one package-less action per name, so a platform action and the environment's own action of one name replace each other, and the later registration stays. That rule is the registry's and is tracked separately. + +What to do: nothing, unless a call relied on the environment's action answering a route that a disabled package's action still binds. Enable that package, or remove its action, or address the environment's action at `/api/v1/actions/_env/OBJECT/NAME`. diff --git a/packages/runtime/src/action-execution.ts b/packages/runtime/src/action-execution.ts index 3c02f6364cb..2f53b4ac9e8 100644 --- a/packages/runtime/src/action-execution.ts +++ b/packages/runtime/src/action-execution.ts @@ -6085,6 +6085,15 @@ export async function resolveRouteActionDeclaration(deps: ActionExecutionDeps, } return { action: undefined, obj, degraded: true, reason: err?.message ?? String(err) }; } + // [#32989, #32996] The bare read cannot see the object: where the name + // has a package-less item or a disabled package beside a package + // holder, its answer depends on who else holds the name. The route's + // answer is who holds the name on the route. + const census = await routeHolderCensus({ + ql, objectName, actionName, obj, ownsRoute, + readAt: (packageId) => byName.call(protocol, { type: 'action', name: actionName, packageId }), + }); + if (census !== undefined) return census; const item: any = (envelope as { item?: unknown } | null | undefined)?.item; const declared = item !== null && typeof item === 'object' && item.name === actionName && ownsRoute(item); return { action: declared ? item : undefined, obj }; @@ -6217,70 +6226,229 @@ async function routeOwnerOfRefusedRead( } /** - * [#32751] The declaration of a route no holder of a refused name owns - * ({@link routeOwnerOfRefusedRead}): the environment's own item, bound to the - * routed object first and object-less after (the order the router reaches a - * handler in). Never read at a holder's exact address, and never served as a - * holder's: the answer carries no package, so the dispatch is unqualified, as - * the bare call's always was. - * - * The environment's own item is a package-less registry item whose row the - * environment's store holds bound to no package ({@link environmentStoredActionNames}, - * the test {@link packagelessOriginOf} applies): the row's source, never a - * byte of the definition. A package-less item with no such row is the - * platform's (host code or a plugin) and is not served here. The protocol's - * reads cannot answer this: beside two package holders its bare read refuses - * the name, and its merged listing folds the package-less item into the - * holders' slots as their stand-in. - * - * Only when no package holds the name on this route's key, a DISABLED one - * included (ruling 6824698376, letter A: a disabled package is still a - * holder). The protocol's reads withhold a disabled package's item, so they - * cannot tell; the registry's listing of every holder can - * (`includeDisabledPackages`). A route a disabled package's action binds - * stays no declaration, never handed to the environment's item, and so does - * a route on a host whose registry cannot list every holder or whose engine - * has no store to read (no `find`, or no `sys_metadata` object). Any other - * store read that throws is the plane failing to answer (ADR-0110 D3): - * `degraded`, never "no row". - */ -async function environmentOwnerOfUnheldRoute( - args: { ql: any; objectName: string; actionName: string; obj: any }, -): Promise { - const { ql, objectName, actionName, obj } = args; - const none: RouteActionDeclaration = { action: undefined, obj }; + * [#32751, #32996] Who holds `actionName` on the route of `objectName`, read + * from the registry's listing of every holder (`includeDisabledPackages`) and + * the environment's own store: each item that owns the route — bound to the + * routed object, or object-less, the two rungs the router reaches a handler + * on, bound first — filed under its holder: + * + * - `served` — an enabled package's item; + * - `disabled` — a disabled package's item. The protocol's reads withhold + * it, so they cannot tell that a disabled package still binds the route; + * this listing can; + * - `environment` — a package-less item whose row the environment's store + * holds bound to no package ({@link environmentStoredActionNames}, the test + * {@link packagelessOriginOf} applies): the row's source, never a byte of + * the definition; + * - `platform` — any other package-less item (host code or a plugin). + * + * The registry keeps ONE package-less item per name: a platform item and the + * environment's own replace each other there, the later registration staying + * (the registry's rule, not this reader's). So a rung never holds both, and + * the environment's item bound to the routed object and its object-less one + * are never both listed: "bound before object-less" among the environment's + * own items is not observable today. + * + * `undefined` when the host cannot answer: a registry with no listing of + * every holder, or an engine with no store to read (no `find`). Every + * package-less item is `platform` when the engine registers no metadata store + * at all ({@link listingNotServed}). Any other store read that throws is the + * plane failing to answer (ADR-0110 D3): `degraded`, never "no row". The store + * is read only when a package-less item owns the route. + */ +interface RouteHolders { served: any[]; disabled: any[]; environment: any[]; platform: any[] } +type RouteHolderReading = + | { degraded: true; reason: string } + | { + degraded?: undefined; + /** Every holder's item of the name, on any key. */ + named: any[]; + /** Is this item's package disabled? */ + isDisabled: (item: any) => boolean; + /** The holders that own the route, both rungs. */ + owners: RouteHolders; + /** The rung the router reaches first: bound to the routed object, else object-less. */ + rung: RouteHolders; + }; + +/** [ADR-0150 D4] Is a registry item a package's (`_packageId`), not package-less? */ +function packagedItem(item: any): boolean { + return typeof item?._packageId === 'string' && item._packageId !== ''; +} + +async function readRouteHolders( + args: { ql: any; objectName: string; actionName: string }, + /** + * Asked of the listing BEFORE the store is read: `false` answers + * `undefined` with no store read, so a caller that can only answer some + * compositions never pays the store's scans (or its outage) on the others. + */ + admit: (named: readonly any[], isDisabled: (item: any) => boolean) => boolean = () => true, +): Promise { + const { ql, objectName, actionName } = args; const registry: any = ql?.registry; - if (typeof registry?.listItems !== 'function') return none; + if (typeof registry?.listItems !== 'function') return undefined; let named: any[]; try { named = (registry.listItems('action', undefined, { includeDisabledPackages: true }) ?? []) .filter((item: any) => item?.name === actionName); } catch { - return none; + return undefined; } - const ownsRoute = (item: any): boolean => { - const owner = standaloneActionOwnerKey(item); - return owner === objectName || isObjectLessActionKey(owner); - }; - const packaged = (item: any): boolean => typeof item?._packageId === 'string' && item._packageId !== ''; - if (named.some((item) => packaged(item) && ownsRoute(item))) return none; - const packageless = named.filter((item) => !packaged(item) && ownsRoute(item)); - if (packageless.length === 0) return none; + const isDisabled = (item: any): boolean => packagedItem(item) + && typeof registry.isPackageDisabled === 'function' && registry.isPackageDisabled(item._packageId) === true; + if (!admit(named, isDisabled)) return undefined; const store = environmentStoredActionNames(ql); - if (store === undefined) return none; - let stored: ReadonlySet; + if (store === undefined) return undefined; + const isBound = (item: any): boolean => + !isObjectLessActionKey(objectName) && standaloneActionOwnerKey(item) === objectName; + const isObjectLess = (item: any): boolean => isObjectLessActionKey(standaloneActionOwnerKey(item)); + const ownerItems = named.filter((item) => isBound(item) || isObjectLess(item)); + let stored: ReadonlySet = new Set(); + if (ownerItems.some((item) => !packagedItem(item))) { + try { + stored = (await store()).actions; + } catch (err: any) { + // An engine that registers no metadata store has no environment + // row: nothing here is the environment's. + if (!listingNotServed(err)) return { degraded: true, reason: err?.message ?? String(err) }; + } + } + const file = (items: any[]): RouteHolders => { + const holders: RouteHolders = { served: [], disabled: [], environment: [], platform: [] }; + for (const item of items) { + if (packagedItem(item)) (isDisabled(item) ? holders.disabled : holders.served).push(item); + else if (stored.has(`${standaloneActionOwnerKey(item)}:${actionName}`)) holders.environment.push(item); + else holders.platform.push(item); + } + return holders; + }; + const bound = ownerItems.filter(isBound); + return { + named, + isDisabled, + owners: file(ownerItems), + rung: file(bound.length > 0 ? bound : ownerItems.filter(isObjectLess)), + }; +} + +/** + * [#32751] The declaration of a route no holder of a refused name owns + * ({@link routeOwnerOfRefusedRead}): the environment's own item on the route's + * first rung ({@link readRouteHolders}). Never read at a holder's exact + * address, and never served as a holder's: the answer carries no package, so + * the dispatch is unqualified, as the bare call's always was. A package-less + * item with no row in the environment's store is the platform's and is not + * served here. The protocol's reads cannot answer this: beside two package + * holders its bare read refuses the name, and its merged listing folds the + * package-less item into the holders' slots as their stand-in. + * + * Only when no package's action owns the route, a DISABLED package's + * included. This is main's own answer, kept: with only the environment's item + * and a disabled package's action on the route — bound to the routed object + * or object-less — the route answers no declaration, never the environment's + * item ({@link routeHolderCensus} gives that answer whatever other holders the + * name has). So does a route on a host whose registry cannot list every + * holder or whose engine has no store to read. + */ +async function environmentOwnerOfUnheldRoute( + args: { ql: any; objectName: string; actionName: string; obj: any }, +): Promise { + const { obj } = args; + const none: RouteActionDeclaration = { action: undefined, obj }; + const reading = await readRouteHolders(args); + if (reading === undefined) return none; + if (reading.degraded) return { action: undefined, obj, degraded: true, reason: reading.reason }; + if (reading.owners.served.length > 0 || reading.owners.disabled.length > 0) return none; + return reading.rung.environment.length === 1 ? { action: reading.rung.environment[0], obj } : none; +} + +/** + * [#32989, #32996] The bare route's declaration when the name has a package + * holder beside a package-less item or a disabled package: the compositions + * where the protocol's bare by-name read, which cannot see the routed object, + * answers by who ELSE holds the name. Beside one package it answers the + * environment's item wherever that item is bound, so a served holder's own + * route read the environment's item on another object and answered no + * declaration; beside a disabled package its answer moves with registration + * order; and it stamps the package-less item with a holder's package. The + * route decides from who holds the name ON THE ROUTE ({@link readRouteHolders}), + * in this order: + * + * 1. no served package's action owns the route and a disabled package's + * does: no declaration, whatever other holders the name has (main's own + * answer beside the environment's item, {@link environmentOwnerOfUnheldRoute}); + * 2. the environment's own item on the route's first rung: that item, with + * no package (ADR-0150 D3: the environment's own declaration answers + * first on one owner key); + * 3. exactly one served package on the route's first rung, and no platform + * item there: that package's own declaration, read at its exact address + * (`packageId`, the protocol's execution face, which the disabled-package + * face governs as the bare read's does) and served with no package, so the + * dispatch is unqualified, as the bare call's always was. A disabled + * co-holder, and the environment's or the platform's item on another + * object, do not change it. A refusal of that address leaves the bare + * read's answer; any other throw is an outage (ADR-0110 D3). + * + * `undefined`, so the bare read answers as before, for every other + * composition: no package holder; nothing package-less and nothing disabled; + * a platform item on the first rung; two served packages there; and a host + * {@link readRouteHolders} cannot read. + */ +async function routeHolderCensus( + args: { + ql: any; objectName: string; actionName: string; obj: any; + ownsRoute: (action: any) => boolean; + readAt: (packageId: string) => Promise; + }, +): Promise { + const { obj, actionName, ownsRoute, readAt } = args; + // [review 6879733874 M1] The composition is judged on the listing alone, + // before the store is read: a name with no package holder, or with + // nothing package-less and nothing disabled beside its packages — the + // one-holder case among them — never reads the store, so it never pays + // its scans nor answers `degraded` when the store is down. + const reading = await readRouteHolders(args, (named, isDisabled) => + named.some(packagedItem) && (named.some((item) => !packagedItem(item)) || named.some(isDisabled))); + if (reading === undefined) return undefined; + if (reading.degraded) return { action: undefined, obj, degraded: true, reason: reading.reason }; + const { owners, rung } = reading; + if (owners.served.length === 0 && owners.disabled.length > 0) return { action: undefined, obj }; + if (rung.environment.length === 1) return { action: rung.environment[0], obj }; + if (rung.served.length !== 1 || rung.platform.length > 0) return undefined; + let envelope: unknown; try { - stored = (await store()).actions; + envelope = await readAt(rung.served[0]._packageId); } catch (err: any) { - // An engine that registers no metadata store has no environment row: - // nothing here is the environment's ({@link listingNotServed}). - if (listingNotServed(err)) return none; + if (refusalOf(err) !== undefined) return undefined; return { action: undefined, obj, degraded: true, reason: err?.message ?? String(err) }; } - const own = packageless.filter((item) => stored.has(`${standaloneActionOwnerKey(item)}:${actionName}`)); - const bound = own.filter((item) => !isObjectLessActionKey(standaloneActionOwnerKey(item))); - const rung = bound.length > 0 ? bound : own; - return rung.length === 1 ? { action: rung[0], obj } : none; + const item: any = (envelope as { item?: unknown } | null | undefined)?.item; + return item !== null && typeof item === 'object' && item.name === actionName && ownsRoute(item) + ? { action: item, obj } + : undefined; +} + +/** + * [#32988] The environment's own item at its exact address + * `/actions/_env/:object/:action` when the merged listing does not list it: + * beside a package holder of the name that listing folds the package-less + * item into a stand-in for the holder, so the address found nothing and + * answered `404`. Read from who holds the name on the route + * ({@link readRouteHolders}): the environment's own item on the route's first + * rung, bound to the routed object first and object-less after, whatever the + * other holders. Never a package's item and never the platform's (no row in + * the environment's store), and never served as a holder's: no package. + * `undefined` when the environment holds none there or the host cannot tell, + * so the address's `404` stands. + */ +async function environmentItemAtOwnAddress( + args: { ql: any; objectName: string; actionName: string; obj: any }, +): Promise { + const reading = await readRouteHolders(args); + if (reading === undefined) return undefined; + if (reading.degraded) return { action: undefined, obj: args.obj, degraded: true, reason: reading.reason }; + return reading.rung.environment.length === 1 ? { action: reading.rung.environment[0], obj: args.obj } : undefined; } /** @@ -6359,7 +6527,15 @@ export async function resolveAddressedRouteActionDeclaration(deps: ActionExecuti }; } catch (err: any) { if (err instanceof PackageScopedNameRefusedError) return { action: undefined, obj, refusal: err.refusal }; - if (err?.status === 404) throw err; + if (err?.status === 404) { + // [#32988] The environment's own item answers its own address + // whatever other holders the name has. + const own = 'environment' in qualifier + ? await environmentItemAtOwnAddress({ ql, objectName, actionName, obj }) + : undefined; + if (own !== undefined) return own; + throw err; + } return { action: undefined, obj, degraded: true, reason: err?.message ?? String(err) }; } } diff --git a/packages/runtime/src/action-package-addressing.test.ts b/packages/runtime/src/action-package-addressing.test.ts index bd70bd68efe..d3ffe5b3fb8 100644 --- a/packages/runtime/src/action-package-addressing.test.ts +++ b/packages/runtime/src/action-package-addressing.test.ts @@ -30,7 +30,10 @@ import { ObjectQL, assertEngineFindOnePredicate } from '@objectstack/objectql'; import { PackageScopedNameRefusedError, resolvePackageScopedHolders } from '@objectstack/core'; import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; import { assertEngineDeleteDispatch, assertEngineUpdateDispatch } from '@objectstack/metadata-core'; -import { actionHandlerOwnerRefusal, executeRegisteredAction, resolveRouteActionDeclaration, type ActionExecutionDeps } from './action-execution.js'; +import { + actionHandlerOwnerRefusal, executeRegisteredAction, resolveAddressedRouteActionDeclaration, resolveRouteActionDeclaration, + type ActionExecutionDeps, +} from './action-execution.js'; const A = 'com.acme.alpha'; const B = 'com.acme.beta'; @@ -874,7 +877,7 @@ describe('[#32365] the bare action read weighs the package-less holder and serve } }); - it('[ruling 6824698376, A] a DISABLED package binding `lead` is still a holder: `lead` stays no declaration, never the environment\'s item', async () => { + it('a DISABLED package binding `lead` is still a holder there (main\'s own answer): `lead` stays no declaration, never the environment\'s item', async () => { const D = 'com.acme.delta'; for (const envFirst of [false, true]) { twoHoldersAndEnvironment(envFirst); @@ -886,6 +889,22 @@ describe('[#32365] the bare action read weighs the package-less holder and serve } }); + it('[#32995] a DISABLED package\'s OBJECT-LESS action owns `lead` too: `lead` stays no declaration, never the environment\'s item, in either registration order', async () => { + const D = 'com.acme.delta'; + for (const envFirst of [false, true]) { + twoHoldersAndEnvironment(envFirst); + install(D); + engine.registry.registerItem('action', { + name: 'approve', label: 'delta', type: 'script', target: 'approve', + body: { language: 'js', source: 'return 1;', capabilities: [] }, + } as never, 'name' as never, D); + engine.registry.disablePackage(D); + const onLead = await routeTo('lead'); + expect([onLead.action, onLead.refusal, onLead.degraded], `env registered ${envFirst ? 'first' : 'last'}`) + .toEqual([undefined, undefined, undefined]); + } + }); + it('NEIGHBOUR — an ENABLED third package C bound to `lead`: `lead` answers C\'s own declaration, never the environment\'s item, in either registration order', async () => { const C = 'com.acme.gamma'; for (const envFirst of [false, true]) { @@ -1257,6 +1276,276 @@ describe('[#32365] the bare action read weighs the package-less holder and serve }); }); +/** + * [#32996, #32989, #32988] The `/actions` route answers by who holds the name + * ON THE ROUTE — the items bound to the routed object, else the object-less + * ones — never by how many holders sit elsewhere or which registered last. + * The protocol's bare by-name read cannot see the object: beside one package + * it answered the environment's item wherever that item is bound, beside a + * disabled package its answer moved with registration order. The route reads + * the registry's listing of every holder (disabled packages included) and the + * environment's own store instead. Every cell runs in both registration + * orders. + * + * BASE (main e540baf8) per cell, in its test's title. + */ +describe('[#32996, #32989, #32988] the action route answers by who holds the name on the route', () => { + const C = 'com.acme.gamma'; + const D = 'com.acme.delta'; + /** A fresh engine whose `find` reads the environment's store, as one kernel's engine does. */ + const freshEngine = () => { + engine = new ObjectQL(); + engine.registry.logLevel = 'silent'; + (protocol as any).engine.registry = engine.registry; + storedRows = []; + (engine as any).find = async (object: string, query: any) => { + if (object !== 'sys_metadata') return []; + const where: Record = query?.where ?? {}; + return storedRows.filter((row) => Object.keys(where).every((field) => { + if (field.startsWith('$')) throw new Error(`engine store double: combinator ${field} is not implemented`); + return row[field] === where[field]; + })); + }; + }; + const shipAt = (packageId: string | undefined, label: string, objectName?: string) => + engine.registry.registerItem('action', { + name: 'approve', label, type: 'script', ...(objectName ? { objectName } : {}), target: 'approve', + body: { language: 'js', source: 'return 1;', capabilities: [] }, + } as never, 'name' as never, packageId); + /** The environment's own item: registered, and its row stored bound to no package. */ + const environmentItem = (objectName?: string) => { + shipAt(undefined, 'env', objectName); + storedRows = [{ + id: 'env-approve', type: 'action', name: 'approve', state: 'active', package_id: null, + metadata: JSON.stringify({ name: 'approve', label: 'env', type: 'script', ...(objectName ? { objectName } : {}) }), + }]; + }; + const routeTo = (objectName: string) => + resolveRouteActionDeclaration(deps, { request: {} } as any, { ql: engine, objectName, actionName: 'approve' }); + const envAddress = (objectName: string) => + resolveAddressedRouteActionDeclaration(deps, { request: {} } as any, { + ql: engine, objectName, actionName: 'approve', qualifier: { environment: true }, + }); + /** One registration order of `steps`, and its reverse. */ + const bothOrders = (steps: Array<() => unknown>): Array<[string, () => void]> => [ + ['registered in order', () => steps.forEach((step) => step())], + ['registered in reverse', () => [...steps].reverse().forEach((step) => step())], + ]; + const answerOf = (answer: { action?: any; refusal?: unknown; degraded?: unknown; packageId?: unknown }) => + [answer.refusal, answer.degraded, answer.packageId, answer.action?.label]; + + describe('#32996 — a disabled package on `lead` beside the environment\'s item there', () => { + for (const [shape, dObject] of [['bound to `lead`', 'lead'], ['object-less', undefined]] as const) { + for (const elsewhere of [0, 1, 2]) { + it(`disabled D ${shape}, ${elsewhere} enabled holder(s) elsewhere: \`lead\` answers no declaration, never the environment's item (BASE with one: the environment's item)`, async () => { + for (const [order, register] of bothOrders([ + () => environmentItem('lead'), + () => shipAt(D, 'delta', dObject), + ...(elsewhere >= 1 ? [() => shipAt(A, 'alpha', 'account')] : []), + ...(elsewhere >= 2 ? [() => shipAt(B, 'beta', 'contact')] : []), + ])) { + freshEngine(); + [A, B, D].forEach((id) => install(id)); + register(); + engine.registry.disablePackage(D); + expect(answerOf(await routeTo('lead')), order).toEqual([undefined, undefined, undefined, undefined]); + } + }); + } + } + + it('CONTROL — with no disabled package, the environment\'s item answers `lead` whatever the holders elsewhere', async () => { + for (const elsewhere of [0, 1, 2]) { + for (const [order, register] of bothOrders([ + () => environmentItem('lead'), + ...(elsewhere >= 1 ? [() => shipAt(A, 'alpha', 'account')] : []), + ...(elsewhere >= 2 ? [() => shipAt(B, 'beta', 'contact')] : []), + ])) { + freshEngine(); + [A, B].forEach((id) => install(id)); + register(); + expect(answerOf(await routeTo('lead')), `${elsewhere} elsewhere, ${order}`).toEqual([undefined, undefined, undefined, 'env']); + } + } + }); + + it('CONTROL — an ENABLED package bound to `lead` beside a disabled one: the enabled package\'s own declaration', async () => { + for (const [order, register] of bothOrders([ + () => shipAt(D, 'delta', 'lead'), + () => shipAt(C, 'gamma', 'lead'), + ])) { + freshEngine(); + [C, D].forEach((id) => install(id)); + register(); + engine.registry.disablePackage(D); + expect(answerOf(await routeTo('lead')), order).toEqual([undefined, undefined, undefined, 'gamma']); + } + }); + }); + + describe('#32989 — a served holder\'s own route', () => { + for (const [envShape, envObject] of [['none', null], ['on `lead`', 'lead'], ['object-less', undefined]] as const) { + for (const [aShape, aObject] of [['on `account`', 'account'], ['on `contact`', 'contact'], ['object-less', undefined], ['absent', null]] as const) { + it(`B on \`contact\`, disabled A ${aShape}, the environment's item ${envShape}: \`contact\` answers B's own declaration, with no package (BASE with the environment's item: no declaration, or its object-less item)`, async () => { + for (const [order, register] of bothOrders([ + ...(aObject !== null ? [() => shipAt(A, 'alpha', aObject)] : []), + () => shipAt(B, 'beta', 'contact'), + ...(envObject !== null ? [() => environmentItem(envObject)] : []), + ])) { + freshEngine(); + [A, B].forEach((id) => install(id)); + register(); + if (aObject !== null) engine.registry.disablePackage(A); + const onContact = await routeTo('contact'); + expect(answerOf(onContact), order).toEqual([undefined, undefined, undefined, 'beta']); + expect(onContact.action, order).toMatchObject({ name: 'approve', objectName: 'contact' }); + } + }); + } + } + + it('B on `contact` beside the platform\'s item on `lead` (no row behind it): `contact` answers B\'s own declaration (BASE: no declaration)', async () => { + for (const [order, register] of bothOrders([() => shipAt(B, 'beta', 'contact'), () => shipAt(undefined, 'platform', 'lead')])) { + freshEngine(); + install(B); + register(); + expect(answerOf(await routeTo('contact')), order).toEqual([undefined, undefined, undefined, 'beta']); + expect((await routeTo('lead')).action?.label, `${order}: the platform's own route keeps its answer`).toBe('platform'); + } + }); + + it('the environment\'s item on `contact` beside B on `contact`: the environment\'s item answers first (ADR-0150 D3), with or without a disabled A (BASE with A registered first: no declaration)', async () => { + for (const withA of [false, true]) { + for (const [order, register] of bothOrders([ + ...(withA ? [() => shipAt(A, 'alpha', 'account')] : []), + () => shipAt(B, 'beta', 'contact'), + () => environmentItem('contact'), + ])) { + freshEngine(); + [A, B].forEach((id) => install(id)); + register(); + if (withA) engine.registry.disablePackage(A); + expect(answerOf(await routeTo('contact')), `${withA ? 'with' : 'without'} A, ${order}`).toEqual([undefined, undefined, undefined, 'env']); + } + } + }); + + it('the environment\'s item on `lead` beside B on `contact` and a disabled A: `lead` answers the environment\'s item when A binds another object, no declaration when A is object-less (BASE: moved with registration order)', async () => { + for (const [aObject, want] of [['account', 'env'], [undefined, undefined]] as const) { + for (const [order, register] of bothOrders([ + () => shipAt(A, 'alpha', aObject), + () => shipAt(B, 'beta', 'contact'), + () => environmentItem('lead'), + ])) { + freshEngine(); + [A, B].forEach((id) => install(id)); + register(); + engine.registry.disablePackage(A); + expect(answerOf(await routeTo('lead')), `A ${aObject ?? 'object-less'}, ${order}`).toEqual([undefined, undefined, undefined, want]); + } + } + }); + + it('a store read that fails is the plane failing to answer: `degraded`, never a declaration (ADR-0110 D3)', async () => { + freshEngine(); + install(B); + shipAt(B, 'beta', 'contact'); + environmentItem('contact'); + (engine as any).find = async () => { throw new Error('store unreachable'); }; + const onContact = await routeTo('contact'); + expect([onContact.action, onContact.degraded, onContact.reason]).toEqual([undefined, true, 'store unreachable']); + }); + }); + + /** + * [review 6879733874 M1] The route judges the composition on the + * registry's listing before it reads the environment's store: a name one + * holder holds never reads the store, so it neither pays the store's scans + * on every dispatch nor answers `degraded` while the store is down. + */ + describe('a name one holder holds never reads the environment\'s store', () => { + const oneHolder: Array<[string, () => void, string]> = [ + ['the environment\'s own item', () => environmentItem('contact'), 'env'], + ['the platform\'s item', () => shipAt(undefined, 'platform', 'contact'), 'platform'], + ['one package\'s item', () => { install(B); shipAt(B, 'beta', 'contact'); }, 'beta'], + ]; + for (const [holder, register, label] of oneHolder) { + it(`${holder}: a store whose read throws leaves the route answering it, and the call dispatches`, async () => { + freshEngine(); + register(); + (engine as any).find = async () => { throw new Error('store unreachable'); }; + engine.registerAction('contact', 'approve', () => `ran ${label}`); + expect(answerOf(await routeTo('contact'))).toEqual([undefined, undefined, undefined, label]); + expect(await executeRegisteredAction(deps, engine, 'contact', ['approve'], {}, undefined)) + .toEqual({ dispatched: true, result: `ran ${label}` }); + }); + + it(`${holder}: the route reads the store zero times`, async () => { + freshEngine(); + register(); + const read = (engine as any).find; + let storeReads = 0; + (engine as any).find = async (object: string, query: any) => { + if (object === 'sys_metadata') storeReads += 1; + return read(object, query); + }; + expect(answerOf(await routeTo('contact'))).toEqual([undefined, undefined, undefined, label]); + expect(storeReads).toBe(0); + }); + } + }); + + describe('#32988 — the environment\'s exact address', () => { + for (const holders of [1, 2]) { + for (const [envShape, envObject, address] of [['on `lead`', 'lead', 'lead'], ['object-less', undefined, 'lead'], ['object-less', undefined, 'global']] as const) { + it(`${holders} package holder(s), the environment's item ${envShape}: \`/actions/_env/${address}/approve\` answers the environment's own item, with no package (BASE: 404)`, async () => { + for (const [order, register] of bothOrders([ + () => shipAt(A, 'alpha', 'account'), + ...(holders === 2 ? [() => shipAt(B, 'beta', 'contact')] : []), + () => environmentItem(envObject), + ])) { + freshEngine(); + [A, B].forEach((id) => install(id)); + register(); + const answer = await envAddress(address); + expect(answerOf(answer), order).toEqual([undefined, undefined, undefined, 'env']); + expect(answer.action, order).toMatchObject({ name: 'approve' }); + } + }); + } + } + + it('a disabled package bound to the same object does not change the environment\'s own address', async () => { + for (const [order, register] of bothOrders([() => shipAt(D, 'delta', 'lead'), () => environmentItem('lead')])) { + freshEngine(); + install(D); + register(); + engine.registry.disablePackage(D); + expect(answerOf(await envAddress('lead')), order).toEqual([undefined, undefined, undefined, 'env']); + } + }); + + it('CONTROL — the platform\'s item (no row behind it) is never the environment\'s: the address still answers 404', async () => { + for (const [order, register] of bothOrders([() => shipAt(A, 'alpha', 'account'), () => shipAt(undefined, 'platform', 'lead')])) { + freshEngine(); + install(A); + register(); + const err = await envAddress('lead').then(() => undefined, (e: any) => e); + expect([err?.code, err?.status], order).toEqual(['RESOURCE_NOT_FOUND', 404]); + } + }); + + it('CONTROL — an object the environment\'s item does not reach still answers 404', async () => { + freshEngine(); + install(A); + shipAt(A, 'alpha', 'account'); + environmentItem('lead'); + const err = await envAddress('contact').then(() => undefined, (e: any) => e); + expect([err?.code, err?.status]).toEqual(['RESOURCE_NOT_FOUND', 404]); + }); + }); +}); + /** * [#32681; seat ruling A on #32681, ADR-0150 D4] ALPHA binds `approve` to * `account`, BETA to `contact`, and the environment saves a package-less