|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// Pins that `build-schemas.ts --check` — the script behind |
| 4 | +// `check:authorable-surface`, one of the eight generated-artifact gates |
| 5 | +// `check:generated` runs — reports and NEVER writes (#4711). |
| 6 | +// |
| 7 | +// The defect these tests exist for: the manifest ratchet had no `CHECK` |
| 8 | +// discriminator at all. `--check` recomputed the emitted schema set and, on any |
| 9 | +// addition, rewrote the tracked `json-schema.manifest.json` in place and exited |
| 10 | +// 0. Two things follow, and both were observed: |
| 11 | +// |
| 12 | +// 1. A "check" edited the working tree. The developer's own manifest content |
| 13 | +// was overwritten by a command whose entire job is to look — which is how |
| 14 | +// `git stash pop` / worktree / merge-conflict work fails for a reason |
| 15 | +// nobody traces back to a gate. |
| 16 | +// 2. The additions branch could never go red in CI. Seven of the eight |
| 17 | +// generated artifacts mean "stale ⇒ fail, run the generator"; this one |
| 18 | +// meant "stale ⇒ I'll write it for you", inside the same `check:generated` |
| 19 | +// summary. A gate that repairs what it is meant to detect reports success |
| 20 | +// forever. |
| 21 | +// |
| 22 | +// So the assertions here are deliberately about the SIDE EFFECT and the EXIT |
| 23 | +// CODE, not about the diff arithmetic (which was always correct): every check |
| 24 | +// case compares the manifest bytes before and after the run. |
| 25 | +// |
| 26 | +// ── Why a sandbox rather than the real package ──────────────────────────── |
| 27 | +// The script resolves every path from its own `__dirname`, so running it in |
| 28 | +// place would mutate the repo's tracked `json-schema.manifest.json` — and under |
| 29 | +// `turbo run test` a `pnpm --filter @objectstack/spec build` (whose first step |
| 30 | +// is `gen:schema`) can be writing that very file concurrently, which would make |
| 31 | +// these tests both destructive and flaky. Instead each run happens in a temp |
| 32 | +// tree that COPIES `scripts/` (so `__dirname` lands there) and symlinks the |
| 33 | +// read-only inputs — `src/`, `node_modules/`, `package.json`. That keeps the |
| 34 | +// production code path byte-for-byte: no test-only seam is added to the gate, |
| 35 | +// because a seam is itself a place where the gate can differ from what CI runs. |
| 36 | + |
| 37 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 38 | +import { spawnSync } from 'node:child_process'; |
| 39 | +import fs from 'node:fs'; |
| 40 | +import os from 'node:os'; |
| 41 | +import path from 'node:path'; |
| 42 | +import { fileURLToPath } from 'node:url'; |
| 43 | + |
| 44 | +import { RENAMED_DEFS } from './lib/renamed-defs'; |
| 45 | + |
| 46 | +const HERE = path.dirname(fileURLToPath(import.meta.url)); |
| 47 | +const PKG = path.resolve(HERE, '..'); |
| 48 | +const TSX = path.join(PKG, 'node_modules', '.bin', 'tsx'); |
| 49 | +const REAL_MANIFEST = path.join(PKG, 'json-schema.manifest.json'); |
| 50 | + |
| 51 | +/** |
| 52 | + * Every run loads the entire spec surface and emits ~1700 JSON Schemas (~7s |
| 53 | + * alone, more under turbo's parallel test load). A timeout here should mean |
| 54 | + * "the script hung", not "the runner was busy" — cf. the same note in |
| 55 | + * check-react-blocks-declaration-parity.test.ts. |
| 56 | + */ |
| 57 | +const SPAWN_TIMEOUT_MS = 180_000; |
| 58 | + |
| 59 | +/** A schema key the committed manifest carries; dropping it fakes "one addition pending". */ |
| 60 | +const KNOWN_KEY = 'ui/View'; |
| 61 | +/** A key no build can emit — the `missing` (disappearance) ratchet's input. */ |
| 62 | +const PHANTOM_KEY = 'ui/ZzzNeverEmittedByAnyBuild'; |
| 63 | + |
| 64 | +let sandbox: string; |
| 65 | +let script: string; |
| 66 | +let manifestPath: string; |
| 67 | +let pristine: string; |
| 68 | + |
| 69 | +beforeAll(() => { |
| 70 | + pristine = fs.readFileSync(REAL_MANIFEST, 'utf8'); |
| 71 | + sandbox = fs.mkdtempSync(path.join(os.tmpdir(), 'build-schemas-check-')); |
| 72 | + fs.cpSync(path.join(PKG, 'scripts'), path.join(sandbox, 'scripts'), { recursive: true }); |
| 73 | + for (const entry of ['src', 'node_modules', 'package.json']) { |
| 74 | + fs.symlinkSync(path.join(PKG, entry), path.join(sandbox, entry)); |
| 75 | + } |
| 76 | + // The authorable-surface ratchet runs after the manifest one; give it the |
| 77 | + // committed snapshot so a check that gets that far judges the same contract. |
| 78 | + fs.copyFileSync( |
| 79 | + path.join(PKG, 'authorable-surface.json'), |
| 80 | + path.join(sandbox, 'authorable-surface.json'), |
| 81 | + ); |
| 82 | + script = path.join(sandbox, 'scripts', 'build-schemas.ts'); |
| 83 | + manifestPath = path.join(sandbox, 'json-schema.manifest.json'); |
| 84 | +}); |
| 85 | + |
| 86 | +afterAll(() => { |
| 87 | + if (sandbox) fs.rmSync(sandbox, { recursive: true, force: true }); |
| 88 | +}); |
| 89 | + |
| 90 | +function run(args: string[] = []): { status: number; output: string } { |
| 91 | + const r = spawnSync(TSX, [script, ...args], { |
| 92 | + cwd: sandbox, |
| 93 | + encoding: 'utf8', |
| 94 | + timeout: SPAWN_TIMEOUT_MS, |
| 95 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 96 | + }); |
| 97 | + return { status: r.status ?? -1, output: `${r.stdout ?? ''}${r.stderr ?? ''}` }; |
| 98 | +} |
| 99 | + |
| 100 | +/** Seed the sandbox manifest from the committed one; returns the exact bytes written. */ |
| 101 | +function seedManifest(mutate: (schemas: string[]) => string[]): string { |
| 102 | + const doc = JSON.parse(pristine) as { description?: string; schemas: string[] }; |
| 103 | + doc.schemas = mutate(doc.schemas); |
| 104 | + const text = JSON.stringify(doc, null, 2) + '\n'; |
| 105 | + fs.writeFileSync(manifestPath, text); |
| 106 | + return text; |
| 107 | +} |
| 108 | + |
| 109 | +const readManifest = () => fs.readFileSync(manifestPath, 'utf8'); |
| 110 | + |
| 111 | +describe('build-schemas.ts --check — a check reports, it does not write (#4711)', () => { |
| 112 | + it( |
| 113 | + 'fails on a manifest behind on additions, and leaves the file byte-identical', |
| 114 | + { timeout: SPAWN_TIMEOUT_MS }, |
| 115 | + () => { |
| 116 | + expect(JSON.parse(pristine).schemas).toContain(KNOWN_KEY); |
| 117 | + const stale = seedManifest((s) => s.filter((k) => k !== KNOWN_KEY)); |
| 118 | + |
| 119 | + const { status, output } = run(['--check']); |
| 120 | + |
| 121 | + // The exit code is half the fix: before #4711 this branch exited 0. |
| 122 | + expect(status).toBe(1); |
| 123 | + expect(output).toMatch(/json-schema\.manifest\.json is out of date \(1 schema\(s\) not recorded\)/); |
| 124 | + expect(output).toContain(`+ json-schema/${KNOWN_KEY}.json`); |
| 125 | + // The remedy must be the generator, exactly as the other seven artifacts say. |
| 126 | + expect(output).toMatch(/gen:schema/); |
| 127 | + // …and the file is the other half: not rewritten, not touched. |
| 128 | + expect(readManifest()).toBe(stale); |
| 129 | + expect(output).not.toContain('📒'); |
| 130 | + }, |
| 131 | + ); |
| 132 | + |
| 133 | + it( |
| 134 | + 'fails on a manifest still listing a def RENAMED_DEFS moved away, without writing it', |
| 135 | + { timeout: SPAWN_TIMEOUT_MS }, |
| 136 | + () => { |
| 137 | + // The other half of the same condition, and the half that has no other |
| 138 | + // reporter: a renamed-away source key is deliberately NOT "missing" (the |
| 139 | + // disappearance ratchet excludes it, since the def is published under the |
| 140 | + // new name), so before #4711 the only thing that ever noticed it was the |
| 141 | + // silent rewrite. #4684 / #4703 both depend on that key actually leaving |
| 142 | + // the manifest. |
| 143 | + const [renamedSource] = Object.keys(RENAMED_DEFS); |
| 144 | + // Loud on purpose: an empty table makes this branch dead code, which is a |
| 145 | + // decision (delete the branch, or the test) — not something to skip past. |
| 146 | + expect(renamedSource, 'RENAMED_DEFS is empty — this test exercises nothing').toBeTruthy(); |
| 147 | + const withStaleRename = seedManifest((s) => [...s, renamedSource].sort()); |
| 148 | + |
| 149 | + const { status, output } = run(['--check']); |
| 150 | + |
| 151 | + expect(status).toBe(1); |
| 152 | + expect(output).toMatch( |
| 153 | + /json-schema\.manifest\.json is out of date .*1 renamed-away key\(s\) still listed/, |
| 154 | + ); |
| 155 | + expect(output).toContain(`- json-schema/${renamedSource}.json (renamed away)`); |
| 156 | + expect(readManifest()).toBe(withStaleRename); |
| 157 | + }, |
| 158 | + ); |
| 159 | + |
| 160 | + it( |
| 161 | + 'keeps the disappearance ratchet intact: a schema in the manifest that no build emits still exits 1', |
| 162 | + { timeout: SPAWN_TIMEOUT_MS }, |
| 163 | + () => { |
| 164 | + const withPhantom = seedManifest((s) => [...s, PHANTOM_KEY].sort()); |
| 165 | + |
| 166 | + const { status, output } = run(['--check']); |
| 167 | + |
| 168 | + expect(status).toBe(1); |
| 169 | + expect(output).toMatch(/1 previously published schema\(s\) disappeared from this build/); |
| 170 | + expect(output).toContain(`- json-schema/${PHANTOM_KEY}.json`); |
| 171 | + expect(readManifest()).toBe(withPhantom); |
| 172 | + }, |
| 173 | + ); |
| 174 | + |
| 175 | + it( |
| 176 | + 'still writes the manifest outside --check, so gen:schema keeps recording additions', |
| 177 | + { timeout: SPAWN_TIMEOUT_MS }, |
| 178 | + () => { |
| 179 | + const stale = seedManifest((s) => s.filter((k) => k !== KNOWN_KEY)); |
| 180 | + |
| 181 | + const { status, output } = run([]); |
| 182 | + |
| 183 | + expect(status).toBe(0); |
| 184 | + expect(output).toContain('📒 json-schema.manifest.json updated (+1 schema(s))'); |
| 185 | + expect(readManifest()).not.toBe(stale); |
| 186 | + expect(JSON.parse(readManifest()).schemas).toContain(KNOWN_KEY); |
| 187 | + }, |
| 188 | + ); |
| 189 | + |
| 190 | + it( |
| 191 | + 'is silent about the manifest when it is up to date — the new failure is staleness, not --check itself', |
| 192 | + { timeout: SPAWN_TIMEOUT_MS }, |
| 193 | + () => { |
| 194 | + // Negative control. Without it, "always exit 1 in check mode" would pass |
| 195 | + // every assertion above while breaking the gate for everyone. |
| 196 | + // NOTE: this asserts status 0, so it also re-proves that the COMMITTED |
| 197 | + // manifest and authorable-surface snapshots are current — the same thing |
| 198 | + // `check:authorable-surface` asserts in CI. If it fails here, run |
| 199 | + // `pnpm --filter @objectstack/spec gen:schema` and commit the result. |
| 200 | + const current = seedManifest((s) => s); |
| 201 | + |
| 202 | + const { status, output } = run(['--check']); |
| 203 | + |
| 204 | + expect(output).not.toMatch(/json-schema\.manifest\.json is out of date/); |
| 205 | + expect(output).not.toContain('📒'); |
| 206 | + expect(readManifest()).toBe(current); |
| 207 | + expect(status).toBe(0); |
| 208 | + }, |
| 209 | + ); |
| 210 | +}); |
0 commit comments