|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A RETIRED `requires` token reads as its retirement prescription at the |
| 5 | + * `os validate` / `os build` door — never as "check for a typo". |
| 6 | + * |
| 7 | + * Both commands accept a PLAIN-OBJECT config (`export default { … }`, no |
| 8 | + * `defineStack` call). That config is parsed with |
| 9 | + * `ObjectStackDefinitionSchema.safeParse`, whose `requires` is a bare string |
| 10 | + * array — the `defineStack` vocabulary check never runs — and the only text |
| 11 | + * the author sees for a `requires` token is the capability preflight's |
| 12 | + * `renderCapabilityMessage`. For a word that USED to be a capability (the |
| 13 | + * saved-report stack's `reports`), the typo advice is wrong in a way that |
| 14 | + * sends the author hunting for a spelling instead of deleting the token. |
| 15 | + * |
| 16 | + * Posture is unchanged: an unknown token is an ADVISORY at this door (exit 0, |
| 17 | + * a `{ token, message }` record in `warnings`); only its TEXT is asserted |
| 18 | + * here, and it is asserted EQUAL to the spec-owned prescription — the same |
| 19 | + * string `defineStack` refuses the token with and `os serve` warns with. |
| 20 | + * |
| 21 | + * A misspelled token rides in the same fixture as the control, so "the |
| 22 | + * prescription is shown" cannot pass against a renderer that shows it for |
| 23 | + * every unknown token. |
| 24 | + */ |
| 25 | + |
| 26 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 27 | +import { execFile } from 'node:child_process'; |
| 28 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 29 | +import { tmpdir } from 'node:os'; |
| 30 | +import { join, resolve } from 'node:path'; |
| 31 | +import { fileURLToPath } from 'node:url'; |
| 32 | +import { RETIRED_PLATFORM_CAPABILITY_GUIDANCE } from '@objectstack/spec/kernel'; |
| 33 | +import { childEnv } from './helpers/serve-process.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 | +interface Run { |
| 40 | + code: number; |
| 41 | + stdout: string; |
| 42 | + stderr: string; |
| 43 | +} |
| 44 | + |
| 45 | +function runCli(args: string[], cwd: string): Promise<Run> { |
| 46 | + return new Promise((resolvePromise) => { |
| 47 | + execFile( |
| 48 | + TSX, |
| 49 | + [CLI, ...args], |
| 50 | + { cwd, maxBuffer: 16 * 1024 * 1024, env: childEnv({ NO_COLOR: '1' }) }, |
| 51 | + (err, stdout, stderr) => { |
| 52 | + resolvePromise({ |
| 53 | + code: err ? (typeof (err as { code?: unknown }).code === 'number' ? (err as unknown as { code: number }).code : 1) : 0, |
| 54 | + stdout: String(stdout), |
| 55 | + stderr: String(stderr), |
| 56 | + }); |
| 57 | + }, |
| 58 | + ); |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +function capabilityHints(run: Run, label: string): Map<string, string> { |
| 63 | + let payload: { warnings?: unknown }; |
| 64 | + try { |
| 65 | + payload = JSON.parse(run.stdout) as { warnings?: unknown }; |
| 66 | + } catch { |
| 67 | + throw new Error(`${label}: stdout was not one JSON document (exit ${run.code})\n${run.stdout}\n${run.stderr}`); |
| 68 | + } |
| 69 | + const hints = new Map<string, string>(); |
| 70 | + for (const w of Array.isArray(payload.warnings) ? payload.warnings : []) { |
| 71 | + if (typeof w === 'object' && w !== null && 'token' in w) { |
| 72 | + const r = w as { token: unknown; message: unknown }; |
| 73 | + hints.set(String(r.token), String(r.message)); |
| 74 | + } |
| 75 | + } |
| 76 | + return hints; |
| 77 | +} |
| 78 | + |
| 79 | +const RETIRED_TOKEN = 'reports'; |
| 80 | +const TYPO_TOKEN = 'reportz'; |
| 81 | + |
| 82 | +/** A plain-object config: no `defineStack`, so no parse-time vocabulary check. */ |
| 83 | +const CONFIG = ` |
| 84 | +export default { |
| 85 | + manifest: { id: 'com.example.retiredcap', name: 'retiredcap', version: '1.0.0', type: 'app', namespace: 'retiredcap' }, |
| 86 | + requires: ['${RETIRED_TOKEN}', '${TYPO_TOKEN}'], |
| 87 | + objects: [ |
| 88 | + { |
| 89 | + name: 'rc_thing', |
| 90 | + label: 'Thing', |
| 91 | + sharingModel: 'private', |
| 92 | + fields: { title: { type: 'text', label: 'Title' } }, |
| 93 | + }, |
| 94 | + ], |
| 95 | +}; |
| 96 | +`; |
| 97 | + |
| 98 | +let dir = ''; |
| 99 | + |
| 100 | +beforeAll(() => { |
| 101 | + dir = mkdtempSync(join(tmpdir(), 'os-retired-cap-')); |
| 102 | + writeFileSync(join(dir, 'objectstack.config.ts'), CONFIG); |
| 103 | +}); |
| 104 | + |
| 105 | +afterAll(() => { |
| 106 | + if (dir) rmSync(dir, { recursive: true, force: true }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('a retired `requires` token at the `os validate` / `os build` door (plain-object config)', () => { |
| 110 | + const prescription = RETIRED_PLATFORM_CAPABILITY_GUIDANCE[RETIRED_TOKEN]; |
| 111 | + |
| 112 | + it('the spec carries a prescription for the token under test', () => { |
| 113 | + expect(prescription, 'RETIRED_PLATFORM_CAPABILITY_GUIDANCE lost its `reports` row').toBeTruthy(); |
| 114 | + }); |
| 115 | + |
| 116 | + for (const command of ['validate', 'build'] as const) { |
| 117 | + it(`os ${command} --json: advisory posture, the retired token carries the prescription, the typo keeps the typo hint`, async () => { |
| 118 | + const run = await runCli([command, '--json'], dir); |
| 119 | + expect(run.code, `os ${command} --json failed:\n${run.stdout}${run.stderr}`).toBe(0); |
| 120 | + const hints = capabilityHints(run, `os ${command} --json`); |
| 121 | + expect([...hints.keys()].sort()).toEqual([RETIRED_TOKEN, TYPO_TOKEN].sort()); |
| 122 | + expect(hints.get(RETIRED_TOKEN)).toBe(prescription); |
| 123 | + expect(hints.get(RETIRED_TOKEN)).not.toContain('check for a typo'); |
| 124 | + expect(hints.get(TYPO_TOKEN)).toContain('check for a typo'); |
| 125 | + }, 180_000); |
| 126 | + } |
| 127 | +}); |
0 commit comments