Open History to see the last commit
1// Ordinary harness calibration: malformed commands cannot escape the registry
2// fixture or be recorded under a misleading first-occurring option value.
3import assert from 'node:assert/strict'
4import { spawnSync } from 'node:child_process'
5import { existsSync, rmSync, unlinkSync } from 'node:fs'
6import { join } from 'node:path'
7import { test } from 'node:test'
8import { fileURLToPath } from 'node:url'
9import {
10 preparePublishReceiver,
11 withReleaseWorkspace,
12} from './release-fixture.js'
13import { withFiniteComparison } from './finite-comparison.js'
14
15for (const [label, tool, args] of [
16 [
17 'duplicate tag',
18 'pnpm',
19 [
20 'publish',
21 '--json',
22 '--access',
23 'public',
24 '--tag',
25 'rc',
26 '--no-git-checks',
27 '--tag',
28 'latest',
29 ],
30 ],
31 [
32 'foreign publish registry',
33 'pnpm',
34 [
35 'publish',
36 '--json',
37 '--access',
38 'public',
39 '--tag',
40 'rc',
41 '--no-git-checks',
42 '--registry=https://example.invalid',
43 ],
44 ],
45 [
46 'foreign info registry',
47 'npm',
48 ['info', '@tanstack/db', '--registry=https://example.invalid', '--json'],
49 ],
50 ['unknown command', 'pnpm', ['install']],
51])
52 test(`release command fixture rejects ${label}`, async () => {
53 await withReleaseWorkspace(async ({ root }) => {
54 const receiver = preparePublishReceiver(root, {
55 candidate: '1.0.0-rc.1.1',
56 registry: 'stable-history',
57 failure: 'none',
58 })
59 const before = receiver.readRegistry()
60 const result = receiver.invokeCommand(tool, args)
61 assert.equal(result.error, undefined)
62 assert.equal(result.status, 91)
63 assert.match(result.stderr, /unrecognized/)
64 assert.deepEqual(receiver.readRegistry(), before)
65 assert.deepEqual(receiver.readRequests(), [])
66 assert.equal(existsSync(join(root, 'registry.json.lock')), false)
67 })
68 })
69
70for (const stage of [
71 'setup',
72 'success',
73 'mismatch',
74 'cleanup',
75 'witness',
76 'undefined',
77 'undefined-witness',
78])
79 test(`finite comparison preserves ${stage} outcome`, async () => {
80 const primary = stage.startsWith('undefined') ? undefined : new Error(stage)
81 const writerError = new Error('writer')
82 const records = []
83 const result = withFiniteComparison(
84 async (comparison) => {
85 if (stage === 'setup') throw primary
86 comparison()
87 if (stage === 'cleanup') {
88 try {
89 return 'compared'
90 } finally {
91 throw primary
92 }
93 }
94 if (stage !== 'success') throw primary
95 },
96 (record) => {
97 records.push(record)
98 if (stage === 'witness' || stage === 'undefined-witness')
99 throw writerError
100 },
101 )
102 if (stage === 'success') await result
103 else
104 await assert.rejects(result, (error) => {
105 if (stage === 'witness' || stage === 'undefined-witness') {
106 assert.equal(error.cause, primary)
107 assert.deepEqual(error.errors, [primary, writerError])
108 } else assert.equal(error, primary)
109 return true
110 })
111 assert.deepEqual(
112 records,
113 stage === 'setup' ? [] : [{ failed: stage !== 'success' }],
114 )
115 })
116
117for (const stage of ['success', 'mismatch', 'undefined'])
118 test(`workspace cleanup preserves ${stage} outcome`, async () => {
119 const primary = stage === 'undefined' ? undefined : new Error(stage)
120 const cleanup = new Error('cleanup')
121 let root
122 await assert.rejects(
123 withReleaseWorkspace(
124 async (fixture) => {
125 root = fixture.root
126 if (stage !== 'success') throw primary
127 },
128 false,
129 (path, options) => {
130 rmSync(path, options)
131 throw cleanup
132 },
133 ),
134 (error) => {
135 if (stage === 'success') assert.equal(error, cleanup)
136 else {
137 assert.equal(error.cause, primary)
138 assert.deepEqual(error.errors, [primary, cleanup])
139 }
140 return true
141 },
142 )
143 assert.equal(existsSync(root), false)
144 })
145
146for (const tag of ['rc', 'latest'])
147 test(`release command fixture forwards explicit ${tag} tag`, async () => {
148 await withReleaseWorkspace(async ({ root }) => {
149 const receiver = preparePublishReceiver(root, {
150 candidate: '1.0.0-rc.1.1',
151 registry: 'stable-history',
152 failure: 'none',
153 })
154 // Changesets forbids an explicit tag while pre mode is visible. A future
155 // wrapper can legitimately hide pre mode and supply the phase's tag.
156 unlinkSync(join(root, '.changeset', 'pre.json'))
157 const result = receiver.invokeCommand(
158 'pnpm',
159 ['exec', 'changeset', 'publish', '--tag', tag],
160 'root',
161 )
162 assert.equal(result.error, undefined)
163 assert.equal(result.status, 0, result.stderr)
164 const publishes = receiver
165 .readRequests()
166 .filter((entry) => entry.type === 'publish')
167 assert.equal(publishes.length, 4)
168 assert(publishes.every((entry) => entry.success && entry.tag === tag))
169 })
170 })
171
172for (const [property, path] of [
173 ['scripts.release-version', '1:2'],
174 ['scripts.release-version', '72'],
175 ['scripts.release-seed-failure', '14'],
176])
177 test(`finite release replay rejects invalid selector ${property}/${path}`, () => {
178 const root = fileURLToPath(new URL('../', import.meta.url))
179 const result = spawnSync(
180 process.execPath,
181 [
182 join(root, 'node_modules', 'vitest', 'vitest.mjs'),
183 'run',
184 '--config',
185 'scripts/oracle-scripts.vitest.config.mjs',
186 'scripts/release-history-oracle.property.test.js',
187 ],
188 {
189 cwd: root,
190 encoding: 'utf8',
191 timeout: 10_000,
192 env: {
193 ...process.env,
194 TANSTACK_DB_ORACLE_PROPERTY: property,
195 TANSTACK_DB_ORACLE_SEED: '1',
196 TANSTACK_DB_ORACLE_PATH: path,
197 },
198 },
199 )
200 assert.equal(result.error, undefined)
201 assert.equal(result.status, 1)
202 assert.match(
203 result.stdout + result.stderr,
204 /Finite release replay PATH must be one existing case index/,
205 )
206 })