Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

pull-roundtrip.ts

112 lines3.7 KB

1#!/usr/bin/env bun
2/**
3 * E2E roundtrip for Phase 12 (PLAN.md task 12.6): against a live service
4 * (default the local dev server), prove the full build-time pull flow —
5 * pull a real selection, typecheck the generated module with the real tsc,
6 * prove the second pull is a pure 304 no-op, then stale the manifest entry
7 * and prove `update` (a full pull) rewrites exactly that file.
8 *
9 * bun scripts/pull-roundtrip.ts [baseUrl]
10 */
11import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
12import { tmpdir } from 'node:os'
13import { join } from 'node:path'
14import ts from 'typescript'
15​
16import { MANIFEST_FILENAME, pull, readManifest } from '@modelschemas/codegen'
17​
18const baseUrl = process.argv[2] ?? 'http://localhost:3100'
19const SELECTION = 'anthropic/v1/messages#request'
20const KEY = 'anthropic/chat/v1/messages#request'
21​
22function check(condition: boolean, label: string): void {
23 if (!condition) {
24 console.error(`FAIL ${label}`)
25 process.exit(1)
26 }
27 console.log(`ok ${label}`)
29​
30function compile(source: string): Array<string> {
31 const fileName = '/generated.ts'
32 const options: ts.CompilerOptions = {
33 strict: true,
34 noEmit: true,
35 skipLibCheck: true,
36 exactOptionalPropertyTypes: true,
37 target: ts.ScriptTarget.ES2022,
38 }
39 const host = ts.createCompilerHost(options)
40 const getSourceFile = host.getSourceFile.bind(host)
41 host.getSourceFile = (name, languageVersion, ...rest) =>
42 name === fileName
43 ? ts.createSourceFile(fileName, source, ts.ScriptTarget.ES2022, true)
44 : getSourceFile(name, languageVersion, ...rest)
45 const fileExists = host.fileExists.bind(host)
46 host.fileExists = (name) => name === fileName || fileExists(name)
47 return ts
48 .getPreEmitDiagnostics(ts.createProgram([fileName], options, host))
49 .map((d) => ts.flattenDiagnosticMessageText(d.messageText, '\n'))
51​
52const outDir = await mkdtemp(join(tmpdir(), 'modelschemas-roundtrip-'))
53try {
54 const config = { baseUrl, outDir, selections: [SELECTION] }
55​
56 const first = await pull(config)
57 check(
58 first.written.length === 1 && first.failed.length === 0,
59 `pull ${SELECTION} from ${baseUrl}`,
60 )
61​
62 const manifest = await readManifest(outDir)
63 const entry = manifest?.entries[KEY]
64 check(entry !== undefined, 'manifest entry recorded')
65 if (entry === undefined || manifest === null) process.exit(1)
66​
67 const module_ = await readFile(join(outDir, entry.path), 'utf8')
68 const diagnostics = compile(module_)
69 check(
70 diagnostics.length === 0,
71 `generated module compiles clean (strict + exactOptionalPropertyTypes)${
72 diagnostics.length > 0 ? `:\n${diagnostics.join('\n')}` : ''
73 }`,
74 )
75 check(
76 !/^import /m.test(module_) && /^export const /m.test(module_),
77 'generated module is pure (no imports, const + types only)',
78 )
79​
80 const second = await pull(config)
81 check(
82 second.written.length === 0 && second.unchanged.includes(KEY),
83 'second pull is a conditional no-op (304)',
84 )
85​
86 // Stale the lockfile entry → update must rewrite exactly that file.
87 entry.etag = '"stale"'
88 entry.contentHash = '0'.repeat(64)
89 await writeManifestRaw()
90 const third = await pull(config)
91 check(
92 third.written.length === 1 && third.written[0] === KEY,
93 'update rewrites exactly the staled file',
94 )
95 const refreshed = await readManifest(outDir)
96 check(
97 refreshed?.entries[KEY]?.contentHash !== '0'.repeat(64),
98 'manifest re-records the live content hash',
99 )
100​
101 console.log('PASS pull-roundtrip')
102​
103 async function writeManifestRaw(): Promise<void> {
104 await writeFile(
105 join(outDir, MANIFEST_FILENAME),
106 JSON.stringify(manifest, null, 2),
107 'utf8',
108 )
109 }
110} finally {
111 await rm(outDir, { recursive: true, force: true })