|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * PIN — the "Build with Claude Code" tutorial's files, written verbatim into a |
| 5 | + * fresh `blank` scaffold, pass `os validate`, and the run prints the counts the |
| 6 | + * page shows. |
| 7 | + * |
| 8 | + * ## The defect this exists for |
| 9 | + * |
| 10 | + * The page's step 3 showed the agent writing a SECOND app, `src/apps/support.app.ts`, |
| 11 | + * beside the `src/apps/main.app.ts` the `blank` template already ships. Followed |
| 12 | + * verbatim, `os validate` exited 1 ("An 'app' package must define at most one |
| 13 | + * app", ADR-0019 D3 as amended) while the page's callout said every example on |
| 14 | + * it passes `os validate` verbatim in a freshly scaffolded project. Nothing read |
| 15 | + * the page's files against a run: the transcript drift audit reads its |
| 16 | + * `transcript=` block only. The page now shows the Support group added to the |
| 17 | + * starter's one app, and this file is what keeps the callout true. |
| 18 | + * |
| 19 | + * ## The page is the only source |
| 20 | + * |
| 21 | + * Nothing the project receives is written down here. The test reads, from the |
| 22 | + * page itself: |
| 23 | + * |
| 24 | + * - the project name, off step 1's `npm create objectstack@latest NAME`; |
| 25 | + * - every code block with a `title="PATH"`, written to PATH in the project: |
| 26 | + * a path the scaffold already has is the agent's edit (the whole file as |
| 27 | + * the agent leaves it), any other path is a new file; |
| 28 | + * - the barrel block (the `// src/DIR/index.ts` headers and their `export` |
| 29 | + * lines), each line appended to that barrel the way an author adds one; |
| 30 | + * - the validate block (the one that prints `✓ Validation passed`), whose |
| 31 | + * count lines the run must print. |
| 32 | + * |
| 33 | + * Asserted beside the run: every new file is exported by a barrel line the page |
| 34 | + * shows, in its own directory's `index.ts` (the page's rule that metadata |
| 35 | + * exists only if its barrel exports it). Exit statuses and count lines are |
| 36 | + * asserted; prose is not pinned. |
| 37 | + * |
| 38 | + * The `Runtime:` line is not compared. The project here is not installed (it |
| 39 | + * lives under this package's `node_modules`, so its imports resolve to |
| 40 | + * workspace copies), and the plugin count that line reports is a reading of an |
| 41 | + * installed project this file cannot make. |
| 42 | + * |
| 43 | + * ## Where it runs |
| 44 | + * |
| 45 | + * Named `.e2e`, so it runs in the nightly tier (`OS_TEST_TIERS=nightly`, |
| 46 | + * `vitest.config.ts`) and not in the per-PR run: one plain-node scaffold and |
| 47 | + * one oclif cold start. Spawned through `bin/run-dev.js` with this process's |
| 48 | + * own `node`, so `packages/cli/dist` need not be built; the on-ramp's `dist/` |
| 49 | + * is present because `@objectstack/cli#test` depends on `^build`. The page and |
| 50 | + * the on-ramp's `bin/` are declared cross-package inputs of this package |
| 51 | + * (scripts/cross-package-test-inputs.mjs, mirrored into turbo.json). |
| 52 | + */ |
| 53 | + |
| 54 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 55 | +import { execFile } from 'node:child_process'; |
| 56 | +import { appendFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 57 | +import { dirname, join, posix, resolve } from 'node:path'; |
| 58 | +import { fileURLToPath } from 'node:url'; |
| 59 | +import { childEnv } from './helpers/serve-process.js'; |
| 60 | + |
| 61 | +const HERE = resolve(fileURLToPath(import.meta.url), '..'); |
| 62 | +const CLI = resolve(HERE, '../bin/run-dev.js'); |
| 63 | + |
| 64 | +// One `resolve(HERE, …)` call per line: `check:cross-package-test-inputs` |
| 65 | +// reconstructs these reads by SOURCE SCAN. |
| 66 | +const ON_RAMP_BIN = resolve(HERE, '../../..', 'packages/create-objectstack/bin/create-objectstack.js'); |
| 67 | +const PAGE = resolve(HERE, '../../..', 'content/docs/getting-started/build-with-claude-code.mdx'); |
| 68 | + |
| 69 | +/** One plain-node scaffold, then one oclif cold start of the source CLI. */ |
| 70 | +const RUN_TIMEOUT_MS = 240_000; |
| 71 | + |
| 72 | +interface Run { |
| 73 | + code: number; |
| 74 | + stdout: string; |
| 75 | + stderr: string; |
| 76 | +} |
| 77 | + |
| 78 | +function run(file: string, args: string[], cwd: string): Promise<Run> { |
| 79 | + return new Promise((resolvePromise) => { |
| 80 | + execFile( |
| 81 | + file, |
| 82 | + args, |
| 83 | + { cwd, maxBuffer: 8 * 1024 * 1024, env: childEnv({ NO_COLOR: '1' }) }, |
| 84 | + (err, stdout, stderr) => { |
| 85 | + resolvePromise({ |
| 86 | + // `err.code` is the real exit status; a signalled child has none and |
| 87 | + // is reported as 1, never as 0. |
| 88 | + code: err |
| 89 | + ? typeof (err as { code?: unknown }).code === 'number' |
| 90 | + ? (err as unknown as { code: number }).code |
| 91 | + : 1 |
| 92 | + : 0, |
| 93 | + stdout: String(stdout), |
| 94 | + stderr: String(stderr), |
| 95 | + }); |
| 96 | + }, |
| 97 | + ); |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +const os = (args: string[], cwd: string) => run(process.execPath, [CLI, ...args], cwd); |
| 102 | +const out = (r: Run) => r.stdout + r.stderr; |
| 103 | +/** A count line with its runs of spaces folded, so column alignment is not pinned. */ |
| 104 | +const fold = (line: string) => line.trim().replace(/\s+/g, ' '); |
| 105 | + |
| 106 | +// --------------------------------------------------------------------------- |
| 107 | +// The page, read once |
| 108 | +// --------------------------------------------------------------------------- |
| 109 | + |
| 110 | +const page = readFileSync(PAGE, 'utf8'); |
| 111 | + |
| 112 | +/** Every fenced block: its info string and its body. */ |
| 113 | +const FENCE = /^```([^\n]*)\n([\s\S]*?)^```[ \t]*$/gm; |
| 114 | +const blocks = [...page.matchAll(FENCE)].map((m) => ({ info: m[1], body: m[2] })); |
| 115 | + |
| 116 | +/** Step 1's project name. */ |
| 117 | +const projectName = /npm create objectstack@latest ([a-z0-9-]+)/.exec(page)?.[1]; |
| 118 | + |
| 119 | +/** The files step 3 shows, by the path in their `title`. */ |
| 120 | +const titled = blocks |
| 121 | + .map((b) => ({ path: /\btitle="([^"]+)"/.exec(b.info)?.[1], body: b.body })) |
| 122 | + .filter((b): b is { path: string; body: string } => b.path !== undefined); |
| 123 | + |
| 124 | +/** The barrel block: `// src/DIR/index.ts` headers, each followed by its `export` lines. */ |
| 125 | +const BARREL_HEADER = /^\/\/ (src\/[\w-]+\/index\.ts)$/; |
| 126 | +const barrelBlock = blocks.find((b) => { |
| 127 | + const lines = b.body.split('\n').filter((l) => l.trim() !== ''); |
| 128 | + return ( |
| 129 | + lines.length > 0 && |
| 130 | + BARREL_HEADER.test(lines[0]) && |
| 131 | + lines.every((l) => BARREL_HEADER.test(l) || l.startsWith('export ')) |
| 132 | + ); |
| 133 | +}); |
| 134 | +const barrelLines = new Map<string, string[]>(); |
| 135 | +if (barrelBlock) { |
| 136 | + let current = ''; |
| 137 | + for (const line of barrelBlock.body.split('\n')) { |
| 138 | + const header = BARREL_HEADER.exec(line); |
| 139 | + if (header) { |
| 140 | + current = header[1]; |
| 141 | + barrelLines.set(current, []); |
| 142 | + } else if (line.startsWith('export ')) { |
| 143 | + barrelLines.get(current)!.push(line); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +/** The validate block's count lines, `Runtime:` aside (see the header). */ |
| 149 | +const validateBlock = blocks.find((b) => b.body.includes('✓ Validation passed')); |
| 150 | +const COUNT_LINE = /^\s*(Data|UI|Logic|Security|Runtime):/; |
| 151 | +const countLines = (validateBlock?.body ?? '') |
| 152 | + .split('\n') |
| 153 | + .filter((l) => COUNT_LINE.test(l) && !/^\s*Runtime:/.test(l)) |
| 154 | + .map(fold); |
| 155 | + |
| 156 | +// --------------------------------------------------------------------------- |
| 157 | +// The run |
| 158 | +// --------------------------------------------------------------------------- |
| 159 | + |
| 160 | +let root: string; |
| 161 | +let project: string; |
| 162 | +let scaffold: Run; |
| 163 | +let validate: Run; |
| 164 | +const edited: string[] = []; |
| 165 | +const added: string[] = []; |
| 166 | + |
| 167 | +beforeAll(async () => { |
| 168 | + if (!projectName) return; |
| 169 | + root = mkdtempSync(join(HERE, '..', 'node_modules', '.getting-started-claude-code-')); |
| 170 | + project = join(root, projectName); |
| 171 | + scaffold = await run(process.execPath, [ON_RAMP_BIN, projectName, '--skip-install', '--skip-skills'], root); |
| 172 | + if (scaffold.code !== 0) return; |
| 173 | + for (const { path, body } of titled) { |
| 174 | + const target = join(project, path); |
| 175 | + (existsSync(target) ? edited : added).push(path); |
| 176 | + mkdirSync(dirname(target), { recursive: true }); |
| 177 | + writeFileSync(target, body); |
| 178 | + } |
| 179 | + for (const [barrel, lines] of barrelLines) { |
| 180 | + appendFileSync(join(project, barrel), lines.map((l) => l + '\n').join('')); |
| 181 | + } |
| 182 | + validate = await os(['validate'], project); |
| 183 | +}, RUN_TIMEOUT_MS); |
| 184 | + |
| 185 | +afterAll(() => { |
| 186 | + if (root) rmSync(root, { recursive: true, force: true }); |
| 187 | +}); |
| 188 | + |
| 189 | +describe('the Build with Claude Code page, read', () => { |
| 190 | + it('names the project, shows its files, its barrel lines and its validate block', () => { |
| 191 | + expect(projectName, 'step 1 names no `npm create objectstack@latest NAME`').toBeDefined(); |
| 192 | + expect(titled.map((t) => t.path)).not.toEqual([]); |
| 193 | + for (const { path } of titled) expect(path, `${path} is a project source file`).toMatch(/^src\/.+\.ts$/); |
| 194 | + expect(barrelBlock, 'the page shows no barrel block (`// src/DIR/index.ts` then `export` lines)').toBeDefined(); |
| 195 | + expect(countLines, 'the validate block prints no count lines').not.toEqual([]); |
| 196 | + }); |
| 197 | +}); |
| 198 | + |
| 199 | +describe('the page, followed verbatim in a fresh `blank` scaffold', () => { |
| 200 | + it('the on-ramp scaffolds the project the page names', () => { |
| 201 | + expect(scaffold.code, out(scaffold)).toBe(0); |
| 202 | + }); |
| 203 | + |
| 204 | + it('every new file is exported by a barrel line the page shows, in its own directory', () => { |
| 205 | + for (const path of added) { |
| 206 | + const barrel = posix.join(posix.dirname(path), 'index.ts'); |
| 207 | + const stem = posix.basename(path).replace(/\.ts$/, ''); |
| 208 | + const lines = barrelLines.get(barrel) ?? []; |
| 209 | + expect( |
| 210 | + lines.some((l) => new RegExp(`from '\\./${stem.replace(/\./g, '\\.')}(\\.js)?';$`).test(l)), |
| 211 | + `${path}: no line of the page's \`${barrel}\` block exports it`, |
| 212 | + ).toBe(true); |
| 213 | + } |
| 214 | + }); |
| 215 | + |
| 216 | + it('`os validate` exits 0', () => { |
| 217 | + expect(validate.code, out(validate)).toBe(0); |
| 218 | + }); |
| 219 | + |
| 220 | + it("prints every count line of the page's validate block", () => { |
| 221 | + const printed = validate.stdout.split('\n').filter((l) => COUNT_LINE.test(l)).map(fold); |
| 222 | + for (const line of countLines) expect(printed, out(validate)).toContain(line); |
| 223 | + }); |
| 224 | +}); |
0 commit comments