|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #17432 — the route surface `projectResolution: 'required'` actually serves. |
| 5 | + * |
| 6 | + * ## What is pinned, and why a pin rather than a repair |
| 7 | + * |
| 8 | + * Under `required` the dispatcher plugin drops the UNSCOPED mounts of |
| 9 | + * `registerAutomationRoutes` / `registerActionRoutes` / `registerAIRoutes`, and |
| 10 | + * keeps `/packages*` mounted unscoped as well as scoped. That asymmetry is |
| 11 | + * RULED, not an oversight: the mount site records it in full |
| 12 | + * (`dispatcher-plugin.ts`, beside the scoped package mount), because taking a |
| 13 | + * mounted route surface away is a different change with a different blast |
| 14 | + * radius than adding a missing door — #16781's residue under ruling C' on |
| 15 | + * #14503 step 2. |
| 16 | + * |
| 17 | + * A recorded decision that lives only in a comment is one a future "tidy-up" |
| 18 | + * has to NOTICE. This file is that decision expressed as a test: moving |
| 19 | + * `registerPackageRoutes(prefix)` into the `required` branch turns the first |
| 20 | + * case below red, and the reader is sent to the comment rather than to a |
| 21 | + * paragraph nobody read. The isolation half — that the unscoped door reaches no |
| 22 | + * other environment's package data, which is what makes keeping it safe — is |
| 23 | + * measured in `packages-unscoped-environment-binding.test.ts`. |
| 24 | + * |
| 25 | + * ## The composition, and the discriminator |
| 26 | + * |
| 27 | + * `plugin-hono-server` + the dispatcher, scoping on, `required`. No |
| 28 | + * `createHonoApp`, no `@objectstack/rest`, no service plugins — so nothing may |
| 29 | + * supply a second door and a mount is the only way in, exactly as in |
| 30 | + * `dispatcher-plugin.scoped-packages-door.integration.test.ts`, whose |
| 31 | + * discriminator this file reuses: a credential-less request that REACHES the |
| 32 | + * dispatcher is answered from the anonymous-deny floor (`ANONYMOUS_DENY_STATUS` |
| 33 | + * / `ANONYMOUS_DENY_CODE`, imported rather than spelled), a verdict no |
| 34 | + * transport-level sink emits, while a path no mount claims is answered by the |
| 35 | + * transport's own `notFound`. Both directions are measured below rather than |
| 36 | + * assumed, and the dropped sibling mounts are the second direction: they are |
| 37 | + * the reason "404 means unmounted" is a reading here. |
| 38 | + * |
| 39 | + * ⚠️ The `/ai` family is deliberately NOT among the dropped-mount cases. Its |
| 40 | + * dynamic routes arrive from `AIServicePlugin` through the `ai:routes` hook, |
| 41 | + * which this composition never fires, so an `/ai` 404 here would be silent |
| 42 | + * about `projectResolution` — it would be about the absent service plugin. |
| 43 | + * `registerAIRoutes`' own mounts sit on the same branch as automation and |
| 44 | + * actions; those two carry the case. |
| 45 | + */ |
| 46 | + |
| 47 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 48 | +import { ANONYMOUS_DENY_CODE, ANONYMOUS_DENY_STATUS, LiteKernel } from '@objectstack/core'; |
| 49 | +import { HonoServerPlugin } from '@objectstack/plugin-hono-server'; |
| 50 | +import type { IHttpServer } from '@objectstack/spec/contracts'; |
| 51 | + |
| 52 | +import { createDispatcherPlugin } from './dispatcher-plugin.js'; |
| 53 | + |
| 54 | +const PREFIX = '/api/v1'; |
| 55 | +const ENV_ID = 'env_alpha'; |
| 56 | +const PKG_ID = 'com.acme.crm'; |
| 57 | + |
| 58 | +let kernel: LiteKernel | undefined; |
| 59 | +let baseUrl = ''; |
| 60 | + |
| 61 | +beforeAll(async () => { |
| 62 | + kernel = new LiteKernel(); |
| 63 | + kernel.use(new HonoServerPlugin({ port: 0, cors: false })); |
| 64 | + kernel.use(createDispatcherPlugin({ |
| 65 | + prefix: PREFIX, |
| 66 | + scoping: { enableProjectScoping: true, projectResolution: 'required' }, |
| 67 | + enforceProjectMembership: false, |
| 68 | + securityHeaders: false, |
| 69 | + })); |
| 70 | + await kernel.bootstrap(); |
| 71 | + const httpServer = kernel.getService<IHttpServer>('http.server'); |
| 72 | + baseUrl = `http://127.0.0.1:${httpServer.getPort!()}`; |
| 73 | +}, 60_000); |
| 74 | + |
| 75 | +afterAll(async () => { |
| 76 | + if (!kernel) return; |
| 77 | + await Promise.race([ |
| 78 | + kernel.shutdown(), |
| 79 | + new Promise<void>((resolve) => setTimeout(resolve, 10_000)), |
| 80 | + ]); |
| 81 | +}, 60_000); |
| 82 | + |
| 83 | +async function probe(method: string, path: string): Promise<{ status: number; body: any }> { |
| 84 | + const res = await fetch(`${baseUrl}${path}`, { method }); |
| 85 | + let body: any; |
| 86 | + try { body = await res.json(); } catch { body = undefined; } |
| 87 | + return { status: res.status, body }; |
| 88 | +} |
| 89 | + |
| 90 | +/** Did the DISPATCHER answer? Minted inside `dispatch()`, by nothing in the transport. */ |
| 91 | +function dispatcherAnswered(r: { status: number; body: any }): boolean { |
| 92 | + return r.status === ANONYMOUS_DENY_STATUS && r.body?.error?.code === ANONYMOUS_DENY_CODE; |
| 93 | +} |
| 94 | + |
| 95 | +/** The shape "no door answered" takes on this transport. */ |
| 96 | +function transportRefused(r: { status: number; body: any }): boolean { |
| 97 | + const code = r.body?.error?.code; |
| 98 | + return r.status === 404 && (code === undefined || code === 'ROUTE_NOT_FOUND' || code === 'ENDPOINT_NOT_FOUND'); |
| 99 | +} |
| 100 | + |
| 101 | +const UNSCOPED = `${PREFIX}/packages`; |
| 102 | +const SCOPED = `${PREFIX}/environments/${ENV_ID}/packages`; |
| 103 | + |
| 104 | +/** The package routes the card names, destructive verb included. */ |
| 105 | +const PACKAGE_ROUTES: Array<[string, string]> = [ |
| 106 | + ['GET', ''], |
| 107 | + ['GET', `/${PKG_ID}`], |
| 108 | + ['DELETE', `/${PKG_ID}`], |
| 109 | +]; |
| 110 | + |
| 111 | +describe("#17432 — under `required`, /packages* stays mounted unscoped (ruled, not an oversight)", () => { |
| 112 | + for (const [method, sub] of PACKAGE_ROUTES) { |
| 113 | + it(`${method} ${UNSCOPED}${sub} is still served`, async () => { |
| 114 | + const r = await probe(method, `${UNSCOPED}${sub}`); |
| 115 | + expect( |
| 116 | + dispatcherAnswered(r), |
| 117 | + `${method} ${UNSCOPED}${sub} -> ${r.status} ${JSON.stringify(r.body)} — ` |
| 118 | + + 'the unscoped package mount is ruled to stay under `required`; see the mount-site ' |
| 119 | + + 'comment in dispatcher-plugin.ts before changing this', |
| 120 | + ).toBe(true); |
| 121 | + }, 60_000); |
| 122 | + } |
| 123 | + |
| 124 | + for (const [method, sub] of PACKAGE_ROUTES) { |
| 125 | + it(`${method} ${SCOPED}${sub} is served too — the scoped door is additive`, async () => { |
| 126 | + const r = await probe(method, `${SCOPED}${sub}`); |
| 127 | + expect( |
| 128 | + dispatcherAnswered(r), |
| 129 | + `${method} ${SCOPED}${sub} -> ${r.status} ${JSON.stringify(r.body)}`, |
| 130 | + ).toBe(true); |
| 131 | + }, 60_000); |
| 132 | + } |
| 133 | +}); |
| 134 | + |
| 135 | +describe('#17432 — the siblings DO drop their unscoped mounts, which is what makes 404 a reading', () => { |
| 136 | + const DROPPED: Array<[string, string, string]> = [ |
| 137 | + ['GET', `${PREFIX}/automation/flows`, 'automation'], |
| 138 | + ['POST', `${PREFIX}/actions/task/ping`, 'actions'], |
| 139 | + ]; |
| 140 | + |
| 141 | + for (const [method, path, family] of DROPPED) { |
| 142 | + it(`${method} ${path} (${family}, unscoped) is NOT mounted under \`required\``, async () => { |
| 143 | + const r = await probe(method, path); |
| 144 | + expect(dispatcherAnswered(r)).toBe(false); |
| 145 | + expect(transportRefused(r), `${method} ${path} -> ${r.status} ${JSON.stringify(r.body)}`).toBe(true); |
| 146 | + }, 60_000); |
| 147 | + } |
| 148 | + |
| 149 | + it('…while the same families ARE mounted scoped', async () => { |
| 150 | + const r = await probe('GET', `${PREFIX}/environments/${ENV_ID}/automation/flows`); |
| 151 | + expect(dispatcherAnswered(r), `scoped automation -> ${r.status} ${JSON.stringify(r.body)}`).toBe(true); |
| 152 | + }, 60_000); |
| 153 | +}); |
0 commit comments