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

publish-history-oracle.property.test.js

181 lines6.1 KB

1/**
2 * Candidates must reach rc and stable versions must reach latest, independent
3 * of registry history. CONTRIBUTING.md supplies this policy; stable minors are
4 * the maintainer's deliberate policy. The shared model states the independent
5 * version/tag law. A registry ledger is controlled input, not a tag classifier.
6 *
7 * The receiver invokes production publishDuringRc with its default command.
8 * A strict pnpm shim forwards that command to the installed real Changesets
9 * CLI. Strict npm/pnpm shims then supply registry metadata and record every info
10 * and publish attempt, including failures. No process falls through to npm or
11 * a real registry. Successful partial publishes persist across one retry;
12 * manifest/pre-state restoration is compared after each attempt.
13 *
14 * This finite grammar crosses numeric candidate boundaries, stable/only-RC/
15 * empty/already-current registry histories and failure in either publish phase.
16 * An already-current workspace cannot have an attempted publish failure, so
17 * those impossible cells are excluded. Npm HTTP, tarballs, installed consumers,
18 * credential authentication and GitHub Actions themselves are not claimed.
19 */
20import assert from 'node:assert/strict'
21import { test } from 'vitest'
22import { declareOracle } from '../packages/db/tests/oracle-declaration.js'
23import { createOracleReach } from '../packages/db/tests/oracle-reach.js'
24import {
25 checkPublishObservation,
26 expectedPublishes,
27 packageNames,
28 publishHistories,
29} from './release-history-oracle.js'
30import {
31 preparePublishReceiver,
32 withReleaseWorkspace,
33} from './release-fixture.js'
34import { publishDuringRc } from './publish-packages.mjs'
35​
36declareOracle({
37 check: [checkPublishObservation],
38 model: [expectedPublishes],
39 grammar: [publishHistories],
40})
41​
42test.each(publishHistories)(
43 'release command registry history %j',
44 async (history) => {
45 const reach = createOracleReach(
46 'release.command-registry',
47 [
48 'registry-read',
49 'restored',
50 'compared',
51 ...(history.failure === 'none' ? [] : ['failure-observed', 'retried']),
52 ...(history.registry === 'current'
53 ? ['already-current']
54 : ['new-version']),
55 ],
56 { replaying: false },
57 )
58 reach.beginHistory()
59 await withReleaseWorkspace(async ({ root }) => {
60 const receiver = preparePublishReceiver(root, history)
61 const expected = expectedPublishes(
62 receiver.manifests,
63 receiver.initialRegistry,
64 )
65 const before = receiver.bytes()
66 let commandError
67 try {
68 await receiver.run(publishDuringRc)
69 } catch (error) {
70 commandError = error
71 }
72 const firstRequests = receiver.readRequests()
73 if (history.failure === 'none') {
74 if (commandError)
75 throw new Error(
76 'Command failed outside controlled registry failure',
77 { cause: commandError },
78 )
79 } else {
80 const failure = firstRequests.find(
81 (entry) => entry.type === 'publish' && !entry.success,
82 )
83 // A setup failure is not evidence for retry or restoration. The exact
84 // controlled failed request must exist before the expected error counts.
85 if (!failure || !commandError)
86 throw new Error(
87 'Controlled package-publish failure was not observed',
88 { cause: commandError },
89 )
90 assert.equal(
91 failure.name,
92 history.failure === 'stable' ? packageNames.query : packageNames.db,
93 'failure reaches its requested publish phase',
94 )
95 reach.reach('failure-observed')
96 }
97 assert.deepEqual(
98 receiver.bytes(),
99 before,
100 'publish attempt restores original workspace bytes',
101 )
102 if (history.failure !== 'none') {
103 await receiver.run(publishDuringRc)
104 assert.deepEqual(
105 receiver.bytes(),
106 before,
107 'publish retry restores original workspace bytes',
108 )
109 reach.reach('retried')
110 }
111 reach.reach('restored')
112 const requests = receiver.readRequests()
113 const infoNames = new Set(
114 requests
115 .filter((entry) => entry.type === 'info')
116 .map((entry) => entry.name),
117 )
118 assert.deepEqual(
119 [...infoNames].sort(),
120 Object.values(packageNames).sort(),
121 'real Changesets inspected both candidate and stable package phases',
122 )
123 reach.reach('registry-read')
124 const failures = requests.filter(
125 (entry) => entry.type === 'publish' && !entry.success,
126 )
127 assert.equal(
128 failures.length,
129 history.failure === 'none' ? 0 : 1,
130 'the retry has exactly one controlled failed request',
131 )
132 checkPublishObservation(
133 requests,
134 expected,
135 'registry after publish and optional retry',
136 )
137 reach.reach('compared')
138 reach.reach(
139 history.registry === 'current' ? 'already-current' : 'new-version',
140 )
141 reach.assertReached()
142 })
143 },
144 20_000,
146​
147// Wrong observations reuse the same judgment as the command receiver. Sorting
148// permits concurrent package order, but must retain omission and multiplicity.
149test.each(['duplicate', 'omission', 'tag', 'version', 'registry', 'access'])(
150 'release publish judgment rejects wrong %s observation',
151 (fault) => {
152 const expected = [
153 { name: packageNames.db, version: '1.0.0-rc.1.10', tag: 'rc' },
154 ]
155 const correct = [
156 {
157 type: 'publish',
158 ...expected[0],
159 success: true,
160 registry: 'http://127.0.0.1:4873',
161 access: 'public',
162 },
163 ]
164 checkPublishObservation(correct, expected, 'legal calibration')
165 const wrong = structuredClone(correct)
166 if (fault === 'duplicate') wrong.push({ ...wrong[0] })
167 else if (fault === 'omission') wrong.length = 0
168 else
169 wrong[0][fault] =
170 fault === 'tag'
171 ? 'latest'
172 : fault === 'version'
173 ? '1.0.0-rc.1.9'
174 : fault === 'registry'
175 ? 'https://example.invalid'
176 : 'restricted'
177 assert.throws(() =>
178 checkPublishObservation(wrong, expected, 'wrong calibration'),
179 )
180 },