|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `os validate` — the build door an author runs before shipping — refuses a |
| 5 | + * stack that declares a datasource named `default`, run for real (#23634). |
| 6 | + * |
| 7 | + * The measured starting point: a config declaring |
| 8 | + * `datasources: [{ name: 'default', driver: 'sqlite', … }]` validated clean and |
| 9 | + * exited 0, while every boot refused it as host-reserved (the app plugin's |
| 10 | + * "declares a datasource named 'default' — that name is reserved for the |
| 11 | + * host's primary datasource"), with `OS_DATABASE_URL`, with `--dev` and on the |
| 12 | + * standalone path. The exit status is what a CI pipeline reads, so this file |
| 13 | + * spawns the source CLI and asserts what the shell sees, with the refusal's |
| 14 | + * issue code and path from `--json`. |
| 15 | + * |
| 16 | + * Schema-layer pins (`defineStack`, the map form, the reserved set, and the |
| 17 | + * host's own `default` row through `DatasourceSchema`) live in |
| 18 | + * `packages/spec/src/stack-datasource-host-reserved-name.test.ts`. |
| 19 | + * |
| 20 | + * Integration tier: it spawns the source CLI (`packages/cli/vitest-tiers.ts`). |
| 21 | + * Not named `.e2e`, so it runs on every PR and in the merge queue; two spawns, |
| 22 | + * one per fixture. The CLI runs through `bin/run-dev.js` (source, via tsx), and |
| 23 | + * `@objectstack/spec` resolves through `exports` to `dist/`. |
| 24 | + */ |
| 25 | + |
| 26 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 27 | +import { execFile } from 'node:child_process'; |
| 28 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 29 | +import { tmpdir } from 'node:os'; |
| 30 | +import { join, resolve } from 'node:path'; |
| 31 | +import { fileURLToPath } from 'node:url'; |
| 32 | +import { childEnv } from './helpers/serve-process.js'; |
| 33 | +import { writeDefineStackConfig } from './helpers/define-stack-fixture.js'; |
| 34 | + |
| 35 | +const HERE = resolve(fileURLToPath(import.meta.url), '..'); |
| 36 | +const CLI = resolve(HERE, '../bin/run-dev.js'); |
| 37 | +const TSX = resolve(HERE, '../../../node_modules/.bin/tsx'); |
| 38 | + |
| 39 | +/** One stack: one object, and one declared sqlite datasource under `name`. */ |
| 40 | +const stackOf = (name: string) => ({ |
| 41 | + manifest: { |
| 42 | + id: 'com.example.fx', |
| 43 | + namespace: 'fx', |
| 44 | + name: 'Reserved datasource door probe', |
| 45 | + version: '1.0.0', |
| 46 | + type: 'app', |
| 47 | + engines: { protocol: '^18' }, |
| 48 | + }, |
| 49 | + objects: [ |
| 50 | + { |
| 51 | + name: 'fx_task', |
| 52 | + label: 'Task', |
| 53 | + pluralLabel: 'Tasks', |
| 54 | + sharingModel: 'private', |
| 55 | + fields: { name: { type: 'text', label: 'Name', required: true } }, |
| 56 | + }, |
| 57 | + ], |
| 58 | + datasources: [{ name, driver: 'sqlite', config: { filename: `${name}.db` }, autoConnect: true }], |
| 59 | +}); |
| 60 | + |
| 61 | +const FIXTURES = { |
| 62 | + /** The measured shape: the host-reserved name. */ |
| 63 | + reserved: stackOf('default'), |
| 64 | + /** CONTROL: the same entry under a name of the stack's own. */ |
| 65 | + own: stackOf('fx_primary'), |
| 66 | +} as const; |
| 67 | + |
| 68 | +interface Run { |
| 69 | + code: number; |
| 70 | + stdout: string; |
| 71 | + stderr: string; |
| 72 | +} |
| 73 | + |
| 74 | +function runCli(args: string[], cwd: string): Promise<Run> { |
| 75 | + return new Promise((resolvePromise) => { |
| 76 | + execFile( |
| 77 | + TSX, |
| 78 | + [CLI, ...args], |
| 79 | + { cwd, maxBuffer: 16 * 1024 * 1024, env: childEnv({ NO_COLOR: '1' }) }, |
| 80 | + (err, stdout, stderr) => { |
| 81 | + resolvePromise({ |
| 82 | + code: err ? (typeof (err as { code?: unknown }).code === 'number' ? (err as unknown as { code: number }).code : 1) : 0, |
| 83 | + stdout: String(stdout), |
| 84 | + stderr: String(stderr), |
| 85 | + }); |
| 86 | + }, |
| 87 | + ); |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +interface ValidateError { |
| 92 | + code?: string; |
| 93 | + path?: unknown[]; |
| 94 | + message?: string; |
| 95 | +} |
| 96 | + |
| 97 | +/** `os validate --json` on failure: the parse's issues ride `errors`. */ |
| 98 | +const errorsOf = (stdout: string): ValidateError[] => (JSON.parse(stdout) as { errors?: ValidateError[] }).errors ?? []; |
| 99 | + |
| 100 | +const dirs = new Map<keyof typeof FIXTURES, string>(); |
| 101 | + |
| 102 | +beforeAll(() => { |
| 103 | + for (const [kind, stack] of Object.entries(FIXTURES) as Array<[keyof typeof FIXTURES, unknown]>) { |
| 104 | + const dir = mkdtempSync(join(tmpdir(), `os-ds-default-door-${kind}-`)); |
| 105 | + // `strict: false`: the door's own parse must judge the content, not `defineStack` at load. |
| 106 | + writeDefineStackConfig(dir, stack, { strict: false }); |
| 107 | + dirs.set(kind, dir); |
| 108 | + } |
| 109 | +}); |
| 110 | + |
| 111 | +afterAll(() => { |
| 112 | + for (const dir of dirs.values()) rmSync(dir, { recursive: true, force: true }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe('os validate refuses a stack datasource named `default`', () => { |
| 116 | + it('exit 1: refused at datasources.0.name with the runtime\'s sentence and its remedy', async () => { |
| 117 | + const run = await runCli(['validate', '--json'], dirs.get('reserved')!); |
| 118 | + expect(run.code, `expected exit 1:\n${run.stdout}\n${run.stderr}`).toBe(1); |
| 119 | + const errors = errorsOf(run.stdout); |
| 120 | + const located = errors.map((e) => ({ code: e.code, path: (e.path ?? []).join('.') })); |
| 121 | + expect(located).toEqual([{ code: 'custom', path: 'datasources.0.name' }]); |
| 122 | + expect(errors[0]!.message).toContain( |
| 123 | + "declares a datasource named 'default' — that name is reserved for the host's primary datasource", |
| 124 | + ); |
| 125 | + expect(errors[0]!.message).toContain('OS_DATABASE_URL'); |
| 126 | + }, 180_000); |
| 127 | + |
| 128 | + it('CONTROL: the same datasource named `fx_primary`, exit 0', async () => { |
| 129 | + const run = await runCli(['validate', '--json'], dirs.get('own')!); |
| 130 | + expect(run.code, `expected exit 0:\n${run.stdout}\n${run.stderr}`).toBe(0); |
| 131 | + expect((JSON.parse(run.stdout) as { valid?: boolean }).valid).toBe(true); |
| 132 | + }, 180_000); |
| 133 | +}); |
0 commit comments