ReviewOS

also looking at this

pantry-pm/pantry

fix(installer): resolve versions against live sources when one is unreachable

#255
Merged glennmichael123 wants to merge fix/242-version-source-fallback into main
6 files +400 -44
packages/ts-pantry/test/installer-version-sources.test.tsadded+80-0
Changes to packages/ts-pantry/test/installer-version-sources.test.ts
@@ -0,0 +1,80 @@
1import type { VersionSourceOutcome } from '../src/installer'
2import { describe, expect, it } from 'bun:test'
3import {
4 describeVersionSources,
5 mergeVersionOutcomes,
6 resolveInstallerConstraintFromCandidates,
7 shouldConsultFallbackSource,
8 unsatisfiableConstraintMessage,
9} from '../src/installer'
10
11// The bundled catalog as it shipped in the pantry build the failing workflow
12// pinned: 1.4.0 is its ceiling, so `^1.4.1` has nothing to match in it.
13const staleBundled: VersionSourceOutcome = {
14 source: 'bundled catalog (this pantry build)',
15 versions: ['1.4.0', '1.3.14', '1.3.13'],
16}
17
18const rateLimited: VersionSourceOutcome = {
19 source: 'api.github.com/repos/oven-sh/bun/releases',
20 versions: [],
21 error: 'HTTP 403 fetching https://api.github.com/repos/oven-sh/bun/releases?per_page=100',
22}
23
24const mirrored: VersionSourceOutcome = {
25 source: 'https://registry.pantry.dev/binaries/bun.com (darwin-arm64)',
26 versions: ['1.4.2', '1.4.1', '1.4.0', '1.3.14'],
27}
28
29describe('live version source outcomes', () => {
30 it('treats an errored source as a reason to consult the fallback', () => {
31 // `[]` from a 403 and `[]` from "upstream published nothing" are the same
32 // value; only the recorded error separates them.
33 expect(shouldConsultFallbackSource(rateLimited)).toBe(true)
34 expect(shouldConsultFallbackSource({ source: 'upstream', versions: [] })).toBe(true)
35 expect(shouldConsultFallbackSource({ source: 'upstream', versions: ['1.4.2'] })).toBe(false)
36 })
37
38 it('resolves a pin above the stale ceiling from the fallback source', () => {
39 const degraded = mergeVersionOutcomes([rateLimited, staleBundled])
40 expect(resolveInstallerConstraintFromCandidates('^1.4.1', degraded)).toBeNull()
41
42 // With the registry consulted in the rate-limited source's place, the pin
43 // the workflow actually wrote resolves — and to the newest match, not the
44 // floor it names.
45 const recovered = mergeVersionOutcomes([rateLimited, mirrored, staleBundled])
46 expect(resolveInstallerConstraintFromCandidates('^1.4.1', recovered)).toBe('1.4.2')
47 expect(resolveInstallerConstraintFromCandidates('^1.4.0', recovered)).toBe('1.4.2')
48 })
49
50 it('merges sources newest-first without duplicating shared versions', () => {
51 expect(mergeVersionOutcomes([mirrored, staleBundled])).toEqual([
52 '1.4.2',
53 '1.4.1',
54 '1.4.0',
55 '1.3.14',
56 '1.3.13',
57 ])
58 })
59
60 it('names every source consulted, and says which ones errored', () => {
61 const described = describeVersionSources([rateLimited, mirrored, staleBundled])
62 expect(described).toContain('api.github.com/repos/oven-sh/bun/releases: unavailable (HTTP 403')
63 expect(described).toContain('(darwin-arm64): 4 versions, newest 1.4.2')
64 expect(described).toContain('bundled catalog (this pantry build): 3 versions, newest 1.4.0')
65 })
66
67 it('reports an unsatisfiable pin as incomplete when a source was unreachable', () => {
68 const message = unsatisfiableConstraintMessage('bun.com', '^1.4.1', [rateLimited, staleBundled])
69 expect(message).toContain('No version of bun.com satisfies "^1.4.1".')
70 // The fact that explained the failure, and that the old one-line message
71 // ("checked 143 versions, newest 1.4.0") left out entirely.
72 expect(message).toContain('1 of 2 version sources could not be consulted')
73 expect(message).toContain('HTTP 403')
74
75 // With every source reachable, the pin really is unsatisfiable and the
76 // message must not hedge about it.
77 const genuine = unsatisfiableConstraintMessage('bun.com', '^9.0.0', [mirrored, staleBundled])
78 expect(genuine).not.toContain('could not be consulted')
79 })
80})