Open History to see the last commit
1#!/usr/bin/env node
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, prove the second pull is a pure 304 no-op, then
6 * stale the manifest entry
7 * and prove `update` (a full pull) rewrites exactly that file.
8 *
9 * pnpm run pull-roundtrip [baseUrl]
10 */
11import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
12import { tmpdir } from 'node:os'
13import { join } from 'node:path'
14
15import { MANIFEST_FILENAME, pull, readManifest } from '@modelschemas/codegen'
16
17const baseUrl = process.argv[2] ?? 'http://localhost:3100'
18const SELECTION = 'anthropic/v1/messages#request'
19const KEY = 'anthropic/chat/v1/messages#request'
20
21function check(condition: boolean, label: string): void {
22 if (!condition) {
23 console.error(`FAIL ${label}`)
24 process.exit(1)
25 }
26 console.log(`ok ${label}`)
27}
28
29const outDir = await mkdtemp(join(tmpdir(), 'modelschemas-roundtrip-'))
30try {
31 const config = { baseUrl, outDir, selections: [SELECTION] }
32
33 const first = await pull(config)
34 check(
35 first.written.length === 1 && first.failed.length === 0,
36 `pull ${SELECTION} from ${baseUrl}`,
37 )
38
39 const manifest = await readManifest(outDir)
40 const entry = manifest?.entries[KEY]
41 check(entry !== undefined, 'manifest entry recorded')
42 if (entry === undefined || manifest === null) process.exit(1)
43
44 const module_ = await readFile(join(outDir, entry.path), 'utf8')
45 check(
46 !/^import /m.test(module_) && /^export const /m.test(module_),
47 'generated module is pure (no imports, const + types only)',
48 )
49
50 const second = await pull(config)
51 check(
52 second.written.length === 0 && second.unchanged.includes(KEY),
53 'second pull is a conditional no-op (304)',
54 )
55
56 // Stale the lockfile entry → update must rewrite exactly that file.
57 entry.etag = '"stale"'
58 entry.contentHash = '0'.repeat(64)
59 await writeManifestRaw()
60 const third = await pull(config)
61 check(
62 third.written.length === 1 && third.written[0] === KEY,
63 'update rewrites exactly the staled file',
64 )
65 const refreshed = await readManifest(outDir)
66 check(
67 refreshed?.entries[KEY]?.contentHash !== '0'.repeat(64),
68 'manifest re-records the live content hash',
69 )
70
71 console.log('PASS pull-roundtrip')
72
73 async function writeManifestRaw(): Promise<void> {
74 await writeFile(
75 join(outDir, MANIFEST_FILENAME),
76 JSON.stringify(manifest, null, 2),
77 'utf8',
78 )
79 }
80} finally {
81 await rm(outDir, { recursive: true, force: true })
82}