|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#20393] `os build` refuses a `views:` container whose own `name` disagrees |
| 5 | + * with the object key it binds to — the stack `os serve` refuses at boot — |
| 6 | + * says so in the boot registrar's own words, and writes NO artifact. |
| 7 | + * |
| 8 | + * ## The defect, measured before the fix |
| 9 | + * |
| 10 | + * On an `os init -t app` project with a view `{ name: 'order_line', object: |
| 11 | + * 'my_app_order_line', list: {…} }`, `os build` exited 0 and wrote |
| 12 | + * `dist/objectstack.json` carrying that container; `os serve`, booting that |
| 13 | + * artifact with no config, exited 1 with "Invalid `views:` container from |
| 14 | + * manifest 'com.example.my-app': the container's own `name` is 'order_line', |
| 15 | + * which disagrees with the object key it binds to, 'my_app_order_line' …". |
| 16 | + * `os validate` had refused the same stack since #20331; the build — the door |
| 17 | + * that SHIPS — did not, so it shipped the failure. |
| 18 | + * |
| 19 | + * ## One judge, one walk |
| 20 | + * |
| 21 | + * `compile.ts` makes the call `validate.ts` makes: `findViewContainerNameRefusals` |
| 22 | + * over the parsed stack, which hands each entry to `@objectstack/objectql`'s |
| 23 | + * `viewContainerNameRefusal` — the function the boot loop throws the answer of. |
| 24 | + * So the pins below assert EQUALITY with the message the boot registrar |
| 25 | + * actually throws for the same payload, driven through `ObjectQL.registerApp`, |
| 26 | + * rather than a literal copy of the words. |
| 27 | + * |
| 28 | + * The CLI runs through `bin/run-dev.js` (source, via tsx); its dependencies — |
| 29 | + * `@objectstack/objectql` among them — resolve through `exports` to `dist/`. |
| 30 | + */ |
| 31 | + |
| 32 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 33 | +import { execFile } from 'node:child_process'; |
| 34 | +import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync, mkdirSync } from 'node:fs'; |
| 35 | +import { tmpdir } from 'node:os'; |
| 36 | +import { join, resolve } from 'node:path'; |
| 37 | +import { fileURLToPath } from 'node:url'; |
| 38 | +import { ObjectQL } from '@objectstack/objectql'; |
| 39 | +import { childEnv } from './helpers/serve-process.js'; |
| 40 | + |
| 41 | +const HERE = resolve(fileURLToPath(import.meta.url), '..'); |
| 42 | +const CLI = resolve(HERE, '../bin/run-dev.js'); |
| 43 | +const TSX = resolve(HERE, '../../../node_modules/.bin/tsx'); |
| 44 | +/** A cold `tsx` spawn of the CLI source entry runs well past vitest's 5 s default. */ |
| 45 | +const SPAWN_TIMEOUT_MS = 120_000; |
| 46 | +/** `os build`'s default `--output`, relative to the working directory. */ |
| 47 | +const ARTIFACT = join('dist', 'objectstack.json'); |
| 48 | + |
| 49 | +interface Run { |
| 50 | + code: number; |
| 51 | + stdout: string; |
| 52 | + stderr: string; |
| 53 | +} |
| 54 | + |
| 55 | +function runCli(args: string[], cwd: string): Promise<Run> { |
| 56 | + return new Promise((resolvePromise) => { |
| 57 | + execFile( |
| 58 | + TSX, |
| 59 | + [CLI, ...args], |
| 60 | + { cwd, maxBuffer: 16 * 1024 * 1024, env: childEnv({ NO_COLOR: '1' }) }, |
| 61 | + (err, stdout, stderr) => { |
| 62 | + resolvePromise({ |
| 63 | + code: err ? (typeof (err as { code?: unknown }).code === 'number' ? (err as unknown as { code: number }).code : 1) : 0, |
| 64 | + stdout: String(stdout), |
| 65 | + stderr: String(stderr), |
| 66 | + }); |
| 67 | + }, |
| 68 | + ); |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +function payloadOf(run: Run, label: string): Record<string, any> { |
| 73 | + try { |
| 74 | + return JSON.parse(run.stdout) as Record<string, any>; |
| 75 | + } catch { |
| 76 | + throw new Error(`${label}: stdout was not one JSON document (exit ${run.code})\n${run.stdout}\n${run.stderr}`); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +const NS = 'bvcn'; |
| 81 | +const ID = `com.example.${NS}`; |
| 82 | +const OBJECT = `${NS}_order_line`; |
| 83 | + |
| 84 | +const orderLineObject = (name: string) => ({ |
| 85 | + name, |
| 86 | + label: 'Order Line', |
| 87 | + sharingModel: 'private', |
| 88 | + fields: { name: { type: 'text', label: 'Name' } }, |
| 89 | +}); |
| 90 | + |
| 91 | +const container = (object: string, viewName: string | undefined) => ({ |
| 92 | + ...(viewName === undefined ? {} : { name: viewName }), |
| 93 | + label: 'Order Line', |
| 94 | + object, |
| 95 | + list: { type: 'grid', columns: [{ field: 'name' }] }, |
| 96 | +}); |
| 97 | + |
| 98 | +/** A one-package stack, as data — written to disk as the config AND handed to boot. */ |
| 99 | +function stack(viewName: string | undefined): Record<string, unknown> { |
| 100 | + return { |
| 101 | + manifest: { id: ID, name: NS, version: '1.0.0', type: 'app', namespace: NS }, |
| 102 | + objects: [orderLineObject(OBJECT)], |
| 103 | + views: [container(OBJECT, viewName)], |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * A `packages[]` stack (ADR-0130 D4): the load path registers each body under |
| 109 | + * its own id and never the top level. The divergent container sits in the |
| 110 | + * SECOND body; the first carries a matching one, which must not be reported. |
| 111 | + */ |
| 112 | +const CORE_ID = 'com.example.bvcn-core'; |
| 113 | +const ORDERS_ID = 'com.example.bvcn-orders'; |
| 114 | +const ordersBody = { |
| 115 | + id: ORDERS_ID, |
| 116 | + name: 'bvcn_orders', |
| 117 | + version: '1.0.0', |
| 118 | + type: 'app', |
| 119 | + objects: [orderLineObject('bvcn_orders_line')], |
| 120 | + views: [container('bvcn_orders_line', 'order_line')], |
| 121 | +}; |
| 122 | +function packagesStack(): Record<string, unknown> { |
| 123 | + return { |
| 124 | + packages: [ |
| 125 | + { |
| 126 | + manifest: { |
| 127 | + id: CORE_ID, |
| 128 | + name: 'bvcn_core', |
| 129 | + version: '1.0.0', |
| 130 | + type: 'app', |
| 131 | + objects: [orderLineObject('bvcn_core_line')], |
| 132 | + views: [container('bvcn_core_line', 'bvcn_core_line')], |
| 133 | + }, |
| 134 | + }, |
| 135 | + { manifest: ordersBody }, |
| 136 | + ], |
| 137 | + }; |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * What the boot registrar throws for a payload, driven the way the load path |
| 142 | + * drives it: one body into `registerApp`. |
| 143 | + */ |
| 144 | +function bootRefusal(payload: Record<string, unknown>): any { |
| 145 | + try { |
| 146 | + new ObjectQL().registerApp(payload); |
| 147 | + } catch (e) { |
| 148 | + return e; |
| 149 | + } |
| 150 | + return undefined; |
| 151 | +} |
| 152 | + |
| 153 | +/** A one-package stack (or its artifact) as `AppPlugin` hands it: `{ ...manifest, ...stack }`. */ |
| 154 | +const asBootPayload = (s: Record<string, unknown>) => ({ ...(s.manifest as Record<string, unknown>), ...s }); |
| 155 | + |
| 156 | +const dirs: Record<string, string> = {}; |
| 157 | +let root = ''; |
| 158 | + |
| 159 | +beforeAll(() => { |
| 160 | + root = mkdtempSync(join(tmpdir(), 'os-build-view-container-name-')); |
| 161 | + const make = (label: string, s: Record<string, unknown>) => { |
| 162 | + const dir = join(root, label); |
| 163 | + mkdirSync(dir, { recursive: true }); |
| 164 | + writeFileSync(join(dir, 'objectstack.config.ts'), `export default ${JSON.stringify(s, null, 2)};\n`); |
| 165 | + dirs[label] = dir; |
| 166 | + }; |
| 167 | + make('divergent-json', stack('order_line')); |
| 168 | + make('divergent-text', stack('order_line')); |
| 169 | + make('divergent-packages', packagesStack()); |
| 170 | + make('matching', stack(OBJECT)); |
| 171 | + make('anonymous', stack(undefined)); |
| 172 | +}); |
| 173 | + |
| 174 | +afterAll(() => { |
| 175 | + if (root) rmSync(root, { recursive: true, force: true }); |
| 176 | +}); |
| 177 | + |
| 178 | +describe('#20393 — os build refuses what the boot registrar refuses, in its words, and ships nothing', () => { |
| 179 | + it('premise: the boot registrar refuses both divergent payloads, and accepts both controls', () => { |
| 180 | + // Without this, the pins below could agree with a boot loop that had |
| 181 | + // stopped refusing anything. |
| 182 | + const refusal = bootRefusal(asBootPayload(stack('order_line'))); |
| 183 | + expect(refusal).toBeInstanceOf(Error); |
| 184 | + expect(refusal.code).toBe('VALIDATION_ERROR'); |
| 185 | + expect(bootRefusal(ordersBody)).toBeInstanceOf(Error); |
| 186 | + expect(bootRefusal(asBootPayload(stack(OBJECT)))).toBeUndefined(); |
| 187 | + expect(bootRefusal(asBootPayload(stack(undefined)))).toBeUndefined(); |
| 188 | + }); |
| 189 | + |
| 190 | + it('THE PIN: --json exits 1, reports the boot registrar\'s refusal verbatim, and writes no artifact', async () => { |
| 191 | + const dir = dirs['divergent-json']; |
| 192 | + const run = await runCli(['build', '--json'], dir); |
| 193 | + const payload = payloadOf(run, 'divergent --json'); |
| 194 | + expect(run.code).toBe(1); |
| 195 | + expect(payload.success).toBe(false); |
| 196 | + expect(Array.isArray(payload.errors)).toBe(true); |
| 197 | + expect(payload.errors).toHaveLength(1); |
| 198 | + |
| 199 | + const boot = bootRefusal(asBootPayload(stack('order_line'))); |
| 200 | + const [row] = payload.errors; |
| 201 | + expect(row.message).toBe(boot.message); |
| 202 | + // The envelope boot throws with, carried onto the row. |
| 203 | + expect(row.code).toBe(boot.code); |
| 204 | + expect(row.httpStatus).toBe(boot.httpStatus); |
| 205 | + expect(row.path).toBe('views[0]'); |
| 206 | + // The point of the card: the door that ships ships nothing. |
| 207 | + expect(existsSync(join(dir, ARTIFACT)), 'os build wrote an artifact the server refuses at boot').toBe(false); |
| 208 | + }, SPAWN_TIMEOUT_MS); |
| 209 | + |
| 210 | + it('the text face exits 1, prints the same words, and writes no artifact', async () => { |
| 211 | + const dir = dirs['divergent-text']; |
| 212 | + const run = await runCli(['build'], dir); |
| 213 | + expect(run.code).toBe(1); |
| 214 | + expect(run.stdout).not.toContain('Build complete'); |
| 215 | + expect(run.stdout).toContain(bootRefusal(asBootPayload(stack('order_line'))).message); |
| 216 | + expect(existsSync(join(dir, ARTIFACT))).toBe(false); |
| 217 | + }, SPAWN_TIMEOUT_MS); |
| 218 | + |
| 219 | + it('a `packages[]` stack: the body boot refuses is refused under its own id, and the matching one is not', async () => { |
| 220 | + const dir = dirs['divergent-packages']; |
| 221 | + const run = await runCli(['build', '--json'], dir); |
| 222 | + const payload = payloadOf(run, 'packages --json'); |
| 223 | + expect(run.code).toBe(1); |
| 224 | + expect(payload.success).toBe(false); |
| 225 | + expect(payload.errors).toHaveLength(1); |
| 226 | + const [row] = payload.errors; |
| 227 | + expect(row.path).toBe('packages[1].manifest.views[0]'); |
| 228 | + expect(row.message).toBe(bootRefusal(ordersBody).message); |
| 229 | + expect(row.message).toContain(`from manifest '${ORDERS_ID}'`); |
| 230 | + expect(existsSync(join(dir, ARTIFACT))).toBe(false); |
| 231 | + }, SPAWN_TIMEOUT_MS); |
| 232 | + |
| 233 | + it('CONTROL: a container whose `name` matches its object builds, and the artifact it writes boots', async () => { |
| 234 | + const dir = dirs.matching; |
| 235 | + const run = await runCli(['build', '--json'], dir); |
| 236 | + const payload = payloadOf(run, 'matching --json'); |
| 237 | + expect(payload.success, JSON.stringify(payload.errors ?? payload.error)).toBe(true); |
| 238 | + expect(run.code).toBe(0); |
| 239 | + const artifact = JSON.parse(readFileSync(join(dir, ARTIFACT), 'utf8')) as Record<string, unknown>; |
| 240 | + expect(bootRefusal(asBootPayload(artifact))).toBeUndefined(); |
| 241 | + }, SPAWN_TIMEOUT_MS); |
| 242 | + |
| 243 | + it('CONTROL: a container with no `name` builds — the shape boot also accepts', async () => { |
| 244 | + const dir = dirs.anonymous; |
| 245 | + const run = await runCli(['build', '--json'], dir); |
| 246 | + const payload = payloadOf(run, 'anonymous --json'); |
| 247 | + expect(payload.success, JSON.stringify(payload.errors ?? payload.error)).toBe(true); |
| 248 | + expect(run.code).toBe(0); |
| 249 | + expect(existsSync(join(dir, ARTIFACT))).toBe(true); |
| 250 | + }, SPAWN_TIMEOUT_MS); |
| 251 | +}); |
0 commit comments