Open History to see the last commit
1/**
2 * Release laws come from CONTRIBUTING.md's Changesets and Releases sections:
3 * candidates start at 1.0.0-rc.1, patches extend the current candidate, and a
4 * minor or major starts the next candidate. Stable packages use ordinary semver
5 * and latest; candidates use rc. Versioning consumes each changeset once.
6 * Exit promotes candidates to 1.0.0 and applies only still-pending work.
7 *
8 * This independent model uses numeric tuples and an authored changeset ledger.
9 * It imports no Changesets planner, production version helper, manifest parser,
10 * or registry classifier. The bounded dependency law uses workspace:* edges:
11 * changing their published exact target requires a dependent patch, as the
12 * repository's updateInternalDependencies=patch policy specifies. Compatible
13 * peers do not require that patch. This is not an arbitrary semver-range model.
14 *
15 * The model predicts manifests, consumed IDs and unique changelog tokens after
16 * each version action, and successful version/tag writes after publishing.
17 * npm HTTP, tarballs, installed consumers, general workspace discovery and
18 * clean installation are separate receiving boundaries, not claims here.
19 */
20import assert from 'node:assert/strict'
21
22export const packageNames = {
23 db: '@tanstack/db',
24 react: '@tanstack/react-db',
25 query: '@tanstack/query-db-collection',
26 offline: '@tanstack/offline-transactions',
27}
28
29// Exhaust these semantic boundaries rather than randomizing a larger grammar.
30// Removing boundary loses numeric 9->10; removing dependency loses stable
31// dependent exit; removing strongest loses major-vs-minor precedence. Reversing
32// pending insertion order must not change the maximum requested release type.
33export const releaseHistories = [
34 'first',
35 'candidate-nine',
36 'patch-nine',
37].flatMap((boundary) =>
38 [false, true].flatMap((stableDependency) =>
39 ['minor', 'major'].flatMap((strongest) =>
40 [false, true].flatMap((reverse) =>
41 ['none', 'stable-only', 'candidate-and-stable'].map((exitWork) => ({
42 boundary,
43 stableDependency,
44 strongest,
45 reverse,
46 exitWork,
47 })),
48 ),
49 ),
50 ),
51)
52
53export const publishHistories = [
54 '1.0.0-rc.1.9',
55 '1.0.0-rc.1.10',
56 '1.0.0-rc.9',
57 '1.0.0-rc.10',
58].flatMap((candidate) =>
59 ['stable-history', 'only-rc', 'never', 'current'].flatMap((registry) =>
60 (registry === 'current' ? ['none'] : ['none', 'stable', 'candidate']).map(
61 (failure) => ({
62 candidate,
63 registry,
64 failure,
65 }),
66 ),
67 ),
68)
69
70// HTTP reception exhausts the original fourteen metadata/failure forms, then
71// adds independent neighboring facts. Exact version presence and tag shape are
72// separate: a tag is not proof that another version is absent. Heterogeneous
73// assignments also prohibit treating all candidate manifests as one version.
74// Bounded enumeration has direct replay by the complete unique Vitest title;
75// it makes no seeded/random-schedule or arbitrary-semver coverage claim.
76export const httpPublishHistories = [
77 ...publishHistories
78 .filter(({ candidate }) => candidate === '1.0.0-rc.1.10')
79 .flatMap((history) =>
80 ['only-rc', 'current'].includes(history.registry)
81 ? ['rc-only', 'latest-alias'].map((tagShape) => ({
82 ...history,
83 tagShape,
84 }))
85 : [{ ...history, tagShape: 'ordinary' }],
86 ),
87 ...['latest-only', 'no-tags', 'older-rc', 'older-rc-latest-current'].map(
88 (tagShape) => ({
89 candidate: '1.0.0-rc.1.10',
90 registry: 'current',
91 failure: 'none',
92 tagShape,
93 }),
94 ),
95 {
96 candidate: '1.0.0-rc.1.10',
97 registry: 'current',
98 failure: 'none',
99 tagShape: 'latest-alias',
100 stableTagShape: 'no-tags',
101 },
102 ...['none', 'candidate'].map((failure) => ({
103 candidate: '1.0.0-rc.1.10',
104 registry: 'only-rc',
105 failure,
106 tagShape: 'no-tags',
107 })),
108 ...[false, true].flatMap((reverse) =>
109 [
110 { currentCandidate: undefined, failure: 'none' },
111 { currentCandidate: undefined, failure: 'candidate-after-success' },
112 { currentCandidate: 'db', failure: 'none' },
113 { currentCandidate: 'react', failure: 'none' },
114 ].map(({ currentCandidate, failure }) => ({
115 candidate: reverse ? '1.0.0-rc.2' : '1.0.0-rc.1.10',
116 reactCandidate: reverse ? '1.0.0-rc.1.10' : '1.0.0-rc.2',
117 registry: currentCandidate === undefined ? 'stable-history' : 'only-rc',
118 tagShape: currentCandidate === undefined ? 'ordinary' : 'latest-alias',
119 ...(currentCandidate === undefined ? {} : { currentCandidate }),
120 failure,
121 })),
122 ),
123]
124
125export const seedFailureHistories = [
126 'first-manifest',
127 'second-manifest',
128 'release-config',
129 'before-pre',
130 'after-pre',
131 'changelog',
132 'after-apply',
133].flatMap((failure) =>
134 [false, true].map((stableDependency) => ({ failure, stableDependency })),
135)
136
137const strength = { none: 0, patch: 1, minor: 2, major: 3 }
138const strongest = (a, b) => (strength[a] >= strength[b] ? a : b)
139const copy = (value) => structuredClone(value)
140
141export function createReleaseModel(stableDependency) {
142 return {
143 mode: 'before',
144 versions: {
145 db: { candidate: 0, patch: 0 },
146 react: { candidate: 0, patch: 0 },
147 query: [1, 4, 8],
148 offline: [1, 1, 4],
149 },
150 stableDependency,
151 pending: [],
152 consumed: [],
153 tokens: Object.fromEntries(
154 Object.keys(packageNames).map((key) => [key, []]),
155 ),
156 }
157}
158
159export function queueRelease(model, id, releases) {
160 assert(!model.pending.some((entry) => entry.id === id))
161 assert(!model.consumed.includes(id), 'changeset IDs cannot be reused')
162 for (const [key, type] of Object.entries(releases)) {
163 assert(
164 key in packageNames,
165 'the grammar names only public fixture packages',
166 )
167 assert(type in strength && type !== 'none', 'a release requires a bump')
168 }
169 model.pending.push({ id, releases: { ...releases } })
170}
171
172// Pending IDs and consumed IDs differ even when public versions match: a later
173// version action must apply one and ignore the other. Candidate patch/counter
174// states also differ: the next patch and minor distinguish their numeric value.
175export function applyReleaseVersion(model, { exit = false } = {}) {
176 const next = copy(model)
177 const requested = Object.fromEntries(
178 Object.keys(packageNames).map((key) => [key, 'none']),
179 )
180 for (const entry of next.pending)
181 for (const [key, type] of Object.entries(entry.releases))
182 requested[key] = strongest(requested[key], type)
183 if (exit) {
184 requested.db = strongest(requested.db, 'patch')
185 requested.react = strongest(requested.react, 'patch')
186 }
187 if (requested.db !== 'none') {
188 requested.react = strongest(requested.react, 'patch')
189 if (next.stableDependency)
190 requested.offline = strongest(requested.offline, 'patch')
191 }
192 for (const key of Object.keys(packageNames)) {
193 const type = requested[key]
194 if (type === 'none') continue
195 const old = next.versions[key]
196 if ('candidate' in old) {
197 next.versions[key] = exit
198 ? [1, 0, 0]
199 : type === 'patch'
200 ? { candidate: old.candidate, patch: old.patch + 1 }
201 : { candidate: old.candidate + 1, patch: 0 }
202 } else {
203 const [major, minor, patch] = old
204 next.versions[key] =
205 type === 'major'
206 ? [major + 1, 0, 0]
207 : type === 'minor'
208 ? [major, minor + 1, 0]
209 : [major, minor, patch + 1]
210 }
211 }
212 for (const entry of next.pending) {
213 next.consumed.push(entry.id)
214 for (const key of Object.keys(entry.releases))
215 next.tokens[key].push({ id: entry.id, version: modelVersions(next)[key] })
216 }
217 next.pending = []
218 next.mode = exit ? 'stable' : 'rc'
219 return next
220}
221
222export function modelVersions(model) {
223 return Object.fromEntries(
224 Object.entries(model.versions).map(([key, value]) => [
225 key,
226 Array.isArray(value)
227 ? value.join('.')
228 : `1.0.0-rc.${value.candidate}${value.patch ? `.${value.patch}` : ''}`,
229 ]),
230 )
231}
232
233export function checkReleaseObservation(actual, model, checkpoint) {
234 const expected = modelVersions(model)
235 assert.deepEqual(actual.versions, expected, `${checkpoint}: package versions`)
236 // Order among independent same-version entries is not promised. Preserve
237 // multiplicity, package and version attribution while comparing the bag.
238 const tokens = (ledger) =>
239 Object.fromEntries(
240 Object.entries(ledger).map(([key, entries]) => [
241 key,
242 entries.map(({ id, version }) => `${version}:${id}`).sort(),
243 ]),
244 )
245 assert.deepEqual(
246 tokens(actual.tokens),
247 tokens(model.tokens),
248 `${checkpoint}: changelog ledger`,
249 )
250 assert.deepEqual(
251 actual.privateManifest,
252 actual.originalPrivateManifest,
253 `${checkpoint}: private package is unchanged`,
254 )
255 assert.deepEqual(
256 actual.ignoredDependency,
257 'workspace:*',
258 `${checkpoint}: ignored dev dependency is restored`,
259 )
260 if (model.mode === 'rc') {
261 assert.deepEqual(actual.pre.mode, 'pre', `${checkpoint}: prerelease mode`)
262 assert.deepEqual(
263 [...actual.pre.changesets].sort(),
264 [...model.consumed].sort(),
265 `${checkpoint}: exactly consumed IDs`,
266 )
267 assert.deepEqual(
268 actual.changesets.sort(),
269 [...model.consumed, ...model.pending.map((entry) => entry.id)].sort(),
270 `${checkpoint}: prerelease files remain replayable`,
271 )
272 } else {
273 assert.equal(
274 actual.pre,
275 undefined,
276 `${checkpoint}: exit removes prerelease state`,
277 )
278 assert.deepEqual(
279 actual.changesets,
280 [],
281 `${checkpoint}: exit removes consumed files`,
282 )
283 }
284 assert.equal(
285 actual.dependencies.react,
286 'workspace:*',
287 `${checkpoint}: candidate direct dependency retains pack-time protocol`,
288 )
289 if (model.stableDependency)
290 assert.equal(
291 actual.dependencies.offline,
292 'workspace:*',
293 `${checkpoint}: stable direct dependency retains pack-time protocol`,
294 )
295 else
296 assert.equal(
297 actual.dependencies.offline,
298 '^1.0.0-rc.0',
299 `${checkpoint}: compatible stable peer stays broad`,
300 )
301 assert.equal(
302 actual.dependencies.query,
303 '^1.0.0-rc.0',
304 `${checkpoint}: query peer retains RC capability requirement`,
305 )
306}
307
308// Registry histories are external input, never a release-tag classifier. A
309// successful publish writes the documented tag even if all old versions were
310// candidates. Concurrent independent package attempts may arrive in any order.
311export function expectedPublishes(manifests, registry) {
312 return manifests
313 .filter(
314 (manifest) =>
315 !manifest.private &&
316 !registry[manifest.name].versions.includes(manifest.version),
317 )
318 .map((manifest) => ({
319 name: manifest.name,
320 version: manifest.version,
321 tag:
322 manifest.name === packageNames.db ||
323 manifest.name === packageNames.react
324 ? 'rc'
325 : 'latest',
326 }))
327}
328
329export function checkPublishObservation(
330 actual,
331 expected,
332 checkpoint,
333 expectedRegistry = 'http://127.0.0.1:4873',
334) {
335 const key = ({ name, version, tag }) => `${name}@${version}:${tag}`
336 const successes = actual.filter(
337 (entry) => entry.type === 'publish' && entry.success,
338 )
339 assert.deepEqual(
340 successes.map(key).sort(),
341 expected.map(key).sort(),
342 `${checkpoint}: each unpublished version reaches its documented tag once`,
343 )
344 for (const entry of actual) {
345 assert.equal(
346 entry.registry,
347 expectedRegistry,
348 `${checkpoint}: local registry only`,
349 )
350 if (entry.type === 'publish')
351 assert.equal(entry.access, 'public', `${checkpoint}: public access`)
352 }
353}