Open History to see the last commit
1/**
2 * Candidate releases reach rc; stable releases reach latest exactly once.
3 * Authority: CONTRIBUTING.md Changesets/Releases and the accepted RC policy.
4 * The existing release ledger supplies the independent version/tag judgment.
5 * This receiver crosses the real npm11/pnpm11/Changesets HTTP boundary, with
6 * the default production publish command and real package-manager archives.
7 * No command shim chooses a tag or fabricates a successful publication.
8 *
9 * The bounded shared grammar retains the original metadata/failure histories
10 * and varies exact-version presence independently of tags. Current candidates
11 * can have only latest, no tags, or rc pointing to an older present version.
12 * The exact wrong-tag aftermath keeps old rc alongside latest=current. A
13 * separate stable-phase neighbor removes stable latest while versions remain.
14 * Missing targets also have a no-tags neighbor. Two different candidate
15 * versions vary which package is already current and reverse their assignment.
16 * A later-candidate fault follows an actual successful candidate upload, in
17 * either package order. Retry must retain it without another upload.
18 * Replay selects the complete unique Vitest test title; this finite enumeration
19 * has no seed/shrink interface and claims no arbitrary schedules or semver.
20 *
21 * The fixture supplies local workspace links directly for packing. It disables
22 * automatic installation, isolates configuration/cache/credentials and keeps
23 * HOME unchanged. This establishes HTTP uploads and packed manifest identity,
24 * not repository installation, consumer APIs, production authentication or
25 * GitHub Actions. POSIX process groups are required. Standard tools and their
26 * required loopback socket access are setup premises; missing tools, malformed
27 * protocol or timeout are fixture/runtime failures, not tag-law RED evidence.
28 */
29import assert from 'node:assert/strict'
30import { test } from 'vitest'
31import { declareOracle } from '../packages/db/tests/oracle-declaration.js'
32import { createOracleReach } from '../packages/db/tests/oracle-reach.js'
33import {
34 checkPublishObservation,
35 expectedPublishes,
36 httpPublishHistories,
37 packageNames,
38} from './release-history-oracle.js'
39import { withReleaseWorkspace } from './release-fixture.js'
40import { withHttpPublishReceiver } from './release-http-fixture.js'
41
42const histories = httpPublishHistories
43
44// Final versions and tags follow the authored initial ledger plus each expected
45// successful publication once. This rule does not classify npm's packuments.
46function checkRegistry(state, initial, expected) {
47 const final = structuredClone(initial)
48 for (const { name, version, tag } of expected) {
49 final[name].versions.push(version)
50 final[name].tags[tag] = version
51 }
52 assert.deepEqual(
53 state.packages,
54 final,
55 'HTTP registry retains each version and its promised tag',
56 )
57}
58
59// Failed attempts are public too: npm forbids publishing an existing exact
60// version. Keep every failure, including duplicates that leave the final ledger
61// unchanged. Only the driver's single authored fault is allowed.
62function checkHttpUploadFailures(uploads, controlledFailure) {
63 const failures = uploads
64 .filter(({ success }) => !success)
65 .map(({ name, controlledFailure, duplicate }) => ({
66 name,
67 controlledFailure,
68 duplicate,
69 }))
70 assert.deepEqual(
71 failures,
72 controlledFailure === undefined
73 ? []
74 : [
75 {
76 name: controlledFailure.name,
77 controlledFailure: true,
78 duplicate: false,
79 },
80 ],
81 'HTTP retry does not republish successful or already-current versions',
82 )
83}
84
85function requireSupportedProtocol(state) {
86 const unexpected = state.requests.filter(
87 ({ type }) => type === 'protocol-error' || type === 'request-error',
88 )
89 if (unexpected.length > 0)
90 throw new Error(
91 `HTTP fixture does not support the actual tool protocol: ${JSON.stringify(unexpected)}`,
92 )
93}
94
95declareOracle({
96 check: [checkPublishObservation, checkRegistry, checkHttpUploadFailures],
97 model: [expectedPublishes],
98 grammar: [histories],
99})
100
101test('finite HTTP grammar keeps every history independently selectable', () => {
102 assert.equal(
103 new Set(histories.map((history) => JSON.stringify(history))).size,
104 histories.length,
105 'complete JSON test titles identify exactly one history',
106 )
107})
108
109test.each(histories)(
110 'publishes through real npm/pnpm HTTP %j',
111 async (history) => {
112 const reach = createOracleReach(
113 'release.http-registry',
114 [
115 'stable-registry-read',
116 'candidate-registry-read',
117 'registry-read',
118 'restored',
119 'compared',
120 ...(history.registry === 'current'
121 ? ['already-current']
122 : ['packed-upload']),
123 ...(history.failure === 'none'
124 ? []
125 : ['controlled-failure', 'partial-publication', 'retry']),
126 ...(history.tagShape === 'ordinary'
127 ? []
128 : [`metadata-${history.tagShape}`]),
129 ...(history.stableTagShape === 'no-tags'
130 ? ['stable-metadata-no-tags']
131 : []),
132 ...(history.reactCandidate === undefined
133 ? []
134 : ['different-candidate-versions']),
135 ...(history.currentCandidate === undefined
136 ? []
137 : ['mixed-current-and-missing']),
138 ...(history.failure === 'candidate-after-success'
139 ? ['candidate-success-before-failure']
140 : []),
141 ...(history.registry === 'current' ? [] : ['missing-exact-candidate']),
142 ],
143 { replaying: false },
144 )
145 reach.beginHistory()
146 let tools
147 let observation
148 let didFail = false
149 let primary
150 try {
151 await withReleaseWorkspace(async ({ root }) =>
152 withHttpPublishReceiver(root, history, async (receiver) => {
153 tools = receiver.tools
154 const expected = expectedPublishes(
155 receiver.manifests,
156 receiver.initialRegistry,
157 )
158 const before = receiver.bytes()
159 let firstError
160 let retryError
161 let controlledFailure
162 try {
163 await receiver.run()
164 } catch (error) {
165 firstError = error
166 }
167 let state = receiver.readState()
168 observation = state
169 requireSupportedProtocol(state)
170 assert.deepEqual(
171 receiver.bytes(),
172 before,
173 'first HTTP attempt restores original workspace bytes',
174 )
175 if (history.failure === 'none') {
176 if (
177 firstError &&
178 !state.requests.some(({ type }) => type === 'publish')
179 )
180 throw new Error(
181 'HTTP release failed before any publication request',
182 { cause: firstError },
183 )
184 } else {
185 const failed = state.requests.filter(
186 ({ type, controlledFailure }) =>
187 type === 'publish' && controlledFailure,
188 )
189 if (failed.length === 0 || firstError === undefined)
190 throw new Error(
191 'HTTP controlled failure boundary was not received',
192 { cause: firstError },
193 )
194 assert.equal(failed.length, 1, 'one controlled failed HTTP request')
195 controlledFailure = failed[0]
196 if (history.failure === 'candidate-after-success') {
197 assert(
198 [packageNames.db, packageNames.react].includes(
199 controlledFailure.name,
200 ),
201 'the later controlled failure belongs to a candidate',
202 )
203 const failedIndex = state.requests.indexOf(controlledFailure)
204 assert(
205 state.requests
206 .slice(0, failedIndex)
207 .some(
208 ({ type, success, name }) =>
209 type === 'publish' &&
210 success &&
211 [packageNames.db, packageNames.react].includes(name),
212 ),
213 'an actual candidate success precedes the later candidate failure',
214 )
215 reach.reach('candidate-success-before-failure')
216 } else
217 assert.equal(
218 controlledFailure.name,
219 history.failure === 'stable'
220 ? packageNames.query
221 : packageNames.db,
222 )
223 reach.reach('controlled-failure')
224 assert(
225 state.requests.some(
226 ({ type, success }) => type === 'publish' && success,
227 ),
228 'successful partial publications precede retry',
229 )
230 reach.reach('partial-publication')
231 try {
232 await receiver.run()
233 } catch (error) {
234 retryError = error
235 }
236 state = receiver.readState()
237 observation = state
238 requireSupportedProtocol(state)
239 assert.deepEqual(
240 receiver.bytes(),
241 before,
242 'HTTP retry restores original workspace bytes',
243 )
244 reach.reach('retry')
245 }
246 reach.reach('restored')
247 const metadata = state.requests.filter(({ type }) => type === 'info')
248 const infos = new Set(metadata.map(({ name }) => name))
249 // The first GET carries source facts before that package can be uploaded.
250 // A stable-phase duplicate can stop the command before any candidate read.
251 // Record each actual receiving path before judging raw failed attempts.
252 for (const [name, initial] of Object.entries(
253 receiver.initialRegistry,
254 )) {
255 const info = metadata.find((entry) => entry.name === name)
256 if (info !== undefined)
257 assert.deepEqual(
258 { versions: info.versions, tags: info.tags },
259 initial,
260 'real npm receives exact initial version and tag facts',
261 )
262 }
263 const stableRead = [packageNames.query, packageNames.offline].every(
264 (name) => infos.has(name),
265 )
266 const candidateRead = [packageNames.db, packageNames.react].every(
267 (name) => infos.has(name),
268 )
269 if (stableRead) {
270 reach.reach('stable-registry-read')
271 if (history.stableTagShape === 'no-tags')
272 reach.reach('stable-metadata-no-tags')
273 }
274 if (candidateRead) {
275 reach.reach('candidate-registry-read')
276 if (history.tagShape !== 'ordinary')
277 reach.reach(`metadata-${history.tagShape}`)
278 if (history.reactCandidate !== undefined) {
279 assert.notEqual(
280 receiver.manifests[0].version,
281 receiver.manifests[1].version,
282 )
283 reach.reach('different-candidate-versions')
284 }
285 const candidateExpected = expected.filter(({ name }) =>
286 [packageNames.db, packageNames.react].includes(name),
287 )
288 if (history.currentCandidate !== undefined) {
289 assert.equal(
290 candidateExpected.length,
291 1,
292 'exactly one candidate target is missing',
293 )
294 reach.reach('mixed-current-and-missing')
295 }
296 if (history.registry !== 'current') {
297 assert(
298 candidateExpected.length > 0,
299 'at least one exact candidate target is missing',
300 )
301 reach.reach('missing-exact-candidate')
302 }
303 }
304 if (stableRead && candidateRead) reach.reach('registry-read')
305 const uploads = state.requests.filter(
306 ({ type }) => type === 'publish',
307 )
308 checkHttpUploadFailures(uploads, controlledFailure)
309 assert.deepEqual(
310 [...infos].sort(),
311 Object.values(packageNames).sort(),
312 'both package phases reached registry metadata',
313 )
314 if (history.failure === 'none')
315 assert.equal(
316 firstError,
317 undefined,
318 'publication without controlled failure fulfills',
319 )
320 else
321 assert.equal(
322 retryError,
323 undefined,
324 'retry after controlled failure fulfills',
325 )
326 for (const upload of uploads) {
327 assert.deepEqual(
328 { name: upload.archive.name, version: upload.archive.version },
329 { name: upload.name, version: upload.version },
330 'real packed manifest matches publication',
331 )
332 assert(upload.archive.files > 0, 'actual packed files arrived')
333 }
334 if (history.registry === 'current') {
335 assert.equal(
336 uploads.length,
337 0,
338 'already-current registry requires no uploads',
339 )
340 reach.reach('already-current')
341 } else {
342 assert(uploads.length > 0, 'real packed publication path ran')
343 reach.reach('packed-upload')
344 }
345 checkPublishObservation(
346 state.requests,
347 expected,
348 'real HTTP publication and optional retry',
349 receiver.origin,
350 )
351 checkRegistry(state, receiver.initialRegistry, expected)
352 reach.reach('compared')
353 reach.assertReached()
354 }),
355 )
356 } catch (error) {
357 didFail = true
358 primary = error
359 throw error
360 } finally {
361 if (process.env.TANSTACK_DB_ORACLE_STATISTICS === '1') {
362 try {
363 console.info(
364 'HTTP release reach',
365 JSON.stringify({
366 history,
367 tools,
368 states: Object.fromEntries(reach.counts()),
369 observation,
370 }),
371 )
372 } catch (report) {
373 throw didFail
374 ? new AggregateError(
375 [primary, report],
376 'HTTP release comparison and reach reporting failures',
377 { cause: primary },
378 )
379 : report
380 }
381 }
382 }
383 },
384 45_000,
385)
386
387test.each(['already-current', 'retained-success'])(
388 'rejects duplicate HTTP attempts for %s',
389 (shape) => {
390 const fault = {
391 name: packageNames.query,
392 success: false,
393 controlledFailure: true,
394 duplicate: false,
395 }
396 const duplicate = {
397 name: packageNames.db,
398 success: false,
399 controlledFailure: false,
400 duplicate: true,
401 }
402 const actual =
403 shape === 'already-current' ? [duplicate] : [fault, duplicate]
404 assert.throws(
405 () =>
406 checkHttpUploadFailures(
407 actual,
408 shape === 'already-current' ? undefined : fault,
409 ),
410 /HTTP retry does not republish/,
411 )
412 },
413)
414
415test.each(['default', 'variable-origin', 'foreign-origin'])(
416 'retains registry-origin judgment %s',
417 (shape) => {
418 const expected = [
419 { name: packageNames.db, version: '1.0.0-rc.1.10', tag: 'rc' },
420 ]
421 const registry =
422 shape === 'default' ? 'http://127.0.0.1:4873' : 'http://127.0.0.1:61234'
423 const observation = [
424 {
425 ...expected[0],
426 type: 'publish',
427 success: true,
428 access: 'public',
429 registry,
430 },
431 ]
432 if (shape === 'default')
433 checkPublishObservation(observation, expected, 'default origin')
434 else {
435 checkPublishObservation(
436 observation,
437 expected,
438 'variable loopback origin',
439 registry,
440 )
441 if (shape === 'foreign-origin')
442 assert.throws(() =>
443 checkPublishObservation(
444 [{ ...observation[0], registry: 'https://example.invalid' }],
445 expected,
446 'foreign observation',
447 registry,
448 ),
449 )
450 }
451 },
452)