|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// [#29568, ADR-0146 D2/D3/D11] The read-only refusal's remedy, FOLLOWED: a |
| 4 | +// caller refused `422 WRITABLE_PACKAGE_REQUIRED` on a code package reads the |
| 5 | +// route out of the message and takes it, and it works. |
| 6 | +// |
| 7 | +// The composition is the one `os dev` serves (`createStandaloneStack` over an |
| 8 | +// artifact the boot registers as code), driven through the HTTP dispatcher's |
| 9 | +// own doors: `DELETE /packages/:id` for the refusal, the ensure door the |
| 10 | +// message names, and the metadata save door with the `?package=` the message |
| 11 | +// names, carrying the `overrides` declaration it names. The served read of the |
| 12 | +// item's name then answers the saved body. |
| 13 | +// |
| 14 | +// The control is the retired remedy: the same body saved with no package (the |
| 15 | +// ADR-0131 D6 "save an item under its own name") is refused `403 |
| 16 | +// NOT_OVERRIDABLE` and names the pair — which is why the refusal stopped |
| 17 | +// prescribing it. Per-door pins of the sentence itself live in |
| 18 | +// `domains/packages-readonly-remedy-pair-route.test.ts`. |
| 19 | + |
| 20 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 21 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 22 | +import { tmpdir } from 'node:os'; |
| 23 | +import { join } from 'node:path'; |
| 24 | +import { Runtime } from './runtime.js'; |
| 25 | +import { createStandaloneStack } from './standalone-stack.js'; |
| 26 | +import { HttpDispatcher } from './http-dispatcher.js'; |
| 27 | + |
| 28 | +// [#10126] Pay the first transform of the dist-resolved workspace deps the |
| 29 | +// boot reaches through a dynamic `import()` at MODULE LOAD, never inside a |
| 30 | +// clocked `it` (`scripts/check-test-source-alias.mjs`). |
| 31 | +import '@objectstack/metadata'; |
| 32 | +import '@objectstack/objectql'; |
| 33 | +import '@objectstack/service-datasource'; |
| 34 | + |
| 35 | +const BOOT_TIMEOUT = 90_000; |
| 36 | +const BASE = 'com.test.readonly-remedy'; |
| 37 | +const VERSION = '2.3.0'; |
| 38 | +const DASHBOARD = 'rrm_overview'; |
| 39 | + |
| 40 | +const artifact = { |
| 41 | + // `namespace`: the base's `.custom` pair is formed from it (ADR-0146 D3). |
| 42 | + manifest: { id: BASE, name: 'readonly_remedy', type: 'app', version: VERSION, namespace: 'rrm' }, |
| 43 | + objects: [{ name: 'rrm_note', label: 'Note', fields: { title: { type: 'text', label: 'Title' } } }], |
| 44 | + dashboards: [{ name: DASHBOARD, label: 'Overview (shipped)', widgets: [] }], |
| 45 | +}; |
| 46 | + |
| 47 | +/** A `manage_metadata` holder under `single`, the caller every door below admits. */ |
| 48 | +const admin = (): any => ({ |
| 49 | + request: {}, |
| 50 | + environmentId: undefined, |
| 51 | + executionContext: { |
| 52 | + userId: 'u_remedy_admin', |
| 53 | + isSystem: false, |
| 54 | + systemPermissions: ['manage_metadata', 'studio.access', 'setup.access'], |
| 55 | + }, |
| 56 | +}); |
| 57 | + |
| 58 | +describe('the read-only refusal\'s remedy, followed through the real doors, works (#29568)', () => { |
| 59 | + const dirs: string[] = []; |
| 60 | + const kernels: any[] = []; |
| 61 | + let priorHome: string | undefined; |
| 62 | + |
| 63 | + afterEach(async () => { |
| 64 | + for (const k of kernels.splice(0)) { |
| 65 | + try { await k.shutdown(); } catch { /* noop */ } |
| 66 | + } |
| 67 | + if (priorHome === undefined) delete process.env.OS_HOME; |
| 68 | + else process.env.OS_HOME = priorHome; |
| 69 | + for (const d of dirs.splice(0)) { |
| 70 | + try { rmSync(d, { recursive: true, force: true }); } catch { /* noop */ } |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + async function boot() { |
| 75 | + const dir = mkdtempSync(join(tmpdir(), 'os-readonly-remedy-')); |
| 76 | + dirs.push(dir); |
| 77 | + const artifactPath = join(dir, 'objectstack.json'); |
| 78 | + writeFileSync(artifactPath, JSON.stringify(artifact), 'utf-8'); |
| 79 | + // The ensure door really installs the pair; keep its writes in the temp dir. |
| 80 | + priorHome = process.env.OS_HOME; |
| 81 | + process.env.OS_HOME = dir; |
| 82 | + const stack = await createStandaloneStack({ |
| 83 | + artifactPath, |
| 84 | + projectRoot: dir, |
| 85 | + databaseUrl: ':memory:', |
| 86 | + skipSeedData: true, |
| 87 | + runPlatformMigrations: false, |
| 88 | + }); |
| 89 | + const runtime = new Runtime({ cluster: false }); |
| 90 | + const kernel = runtime.getKernel(); |
| 91 | + kernels.push(kernel); |
| 92 | + for (const p of stack.plugins) await kernel.use(p as any); |
| 93 | + await kernel.bootstrap(); |
| 94 | + return { dispatcher: new HttpDispatcher(kernel as any), protocol: kernel.getService('protocol') as any }; |
| 95 | + } |
| 96 | + |
| 97 | + it('DELETE is refused naming the pair route; ensure, then the declared save into the pair, is served', async () => { |
| 98 | + const { dispatcher, protocol } = await boot(); |
| 99 | + |
| 100 | + // 1. The refusal, and the route it names, read out of the message. |
| 101 | + const refused: any = await dispatcher.handlePackages(`/${BASE}`, 'DELETE', {}, {}, admin()); |
| 102 | + expect(refused.response?.status, JSON.stringify(refused.response?.body)).toBe(422); |
| 103 | + expect(refused.response?.body?.error?.code).toBe('WRITABLE_PACKAGE_REQUIRED'); |
| 104 | + const message = String(refused.response?.body?.error?.message); |
| 105 | + const ensureRoute = /ensure it with POST \/api\/v1\/packages\/(\S+)\/ensure-customization,/.exec(message)?.[1]; |
| 106 | + const pair = /\(\?package=(\S+)\)/.exec(message)?.[1]; |
| 107 | + const declared = /declaring overrides: \{ package: '([^']+)', basedOn \}/.exec(message)?.[1]; |
| 108 | + expect({ ensureRoute, pair, declared }).toEqual({ ensureRoute: BASE, pair: `${BASE}.custom`, declared: BASE }); |
| 109 | + |
| 110 | + const shipped = { name: DASHBOARD, label: 'Overview (shipped)', widgets: [] }; |
| 111 | + const ours = { ...shipped, label: 'Overview (ours)' }; |
| 112 | + const served = async () => (await protocol.getMetaItem({ type: 'dashboard', name: DASHBOARD }))?.item?.label; |
| 113 | + expect(await served()).toBe('Overview (shipped)'); |
| 114 | + |
| 115 | + // Control: the retired remedy — the same item saved under its own name, |
| 116 | + // naming no package — is the dead end ADR-0146 D11 closed. |
| 117 | + const packageless: any = await dispatcher.handleMetadata(`/dashboard/${DASHBOARD}`, admin(), 'PUT', ours, {}); |
| 118 | + expect(packageless.response?.status, JSON.stringify(packageless.response?.body)).toBe(403); |
| 119 | + expect(JSON.stringify(packageless.response?.body)).toContain('NOT_OVERRIDABLE'); |
| 120 | + expect(JSON.stringify(packageless.response?.body)).toContain(`${BASE}.custom`); |
| 121 | + expect(await served()).toBe('Overview (shipped)'); |
| 122 | + |
| 123 | + // 2. The ensure door the message names creates the pair. |
| 124 | + const ensured: any = await dispatcher.handlePackages(`/${ensureRoute}/ensure-customization`, 'POST', undefined, {}, admin()); |
| 125 | + expect(ensured.response?.status, JSON.stringify(ensured.response?.body)).toBe(201); |
| 126 | + expect(ensured.response?.body?.data).toMatchObject({ basePackageId: BASE, packageId: pair, created: true }); |
| 127 | + |
| 128 | + // 3. The save the message names: into the pair, declaring the override. |
| 129 | + const saved: any = await dispatcher.handleMetadata( |
| 130 | + `/dashboard/${DASHBOARD}`, admin(), 'PUT', |
| 131 | + { ...ours, overrides: { package: declared, basedOn: VERSION } }, |
| 132 | + { package: pair }, |
| 133 | + ); |
| 134 | + expect(saved.response?.status, JSON.stringify(saved.response?.body)).toBe(200); |
| 135 | + |
| 136 | + // 4. The item's name now serves the customization; the base is untouched. |
| 137 | + expect(await served()).toBe('Overview (ours)'); |
| 138 | + const base: any = await dispatcher.handlePackages(`/${BASE}`, 'GET', undefined, {}, admin()); |
| 139 | + expect(base.response?.status).toBe(200); |
| 140 | + }, BOOT_TIMEOUT); |
| 141 | +}); |
0 commit comments