also looking at this
fix(installer): resolve versions against live sources when one is unreachable
#255
6 files
+400
-44
| @@ -0,0 +1,99 @@ | ||
| 1 | import { afterAll, beforeAll, describe, expect, it, mock } from 'bun:test' | |
| 2 | import { EventEmitter } from 'node:events' | |
| 3 | import * as http from 'node:http' | |
| 4 | import * as nodeHttps from 'node:https' | |
| 5 | ||
| 6 | // Captured before the mock is installed, so it can be put back afterwards: | |
| 7 | // a module mock outlives the file that registered it, and every later test | |
| 8 | // file in this process shares the registry. | |
| 9 | const realHttps = { ...nodeHttps } | |
| 10 | ||
| 11 | // Every https request fails with the status a shared-IP CI runner actually | |
| 12 | // gets from unauthenticated api.github.com once the 60 req/hr quota is gone. | |
| 13 | // A 403 is deliberately non-retryable, so this resolves at once. | |
| 14 | function rateLimitedRequest(): EventEmitter & { destroy: () => void } { | |
| 15 | const req = Object.assign(new EventEmitter(), { destroy: () => {} }) | |
| 16 | setImmediate(() => req.emit('error', Object.assign( | |
| 17 | new Error('HTTP 403 fetching https://api.github.com/repos/oven-sh/bun/releases?per_page=100'), | |
| 18 | { statusCode: 403 }, | |
| 19 | ))) | |
| 20 | return req | |
| 21 | } | |
| 22 | ||
| 23 | mock.module('node:https', () => ({ get: rateLimitedRequest, default: { get: rateLimitedRequest } })) | |
| 24 | ||
| 25 | const { resolveInstallerVersionConstraint } = await import('../src/installer') | |
| 26 | ||
| 27 | // A version no bundled catalog can contain, so resolving it proves the | |
| 28 | // registry was consulted rather than the point-in-time snapshot. | |
| 29 | const METADATA = { | |
| 30 | versions: { | |
| 31 | '99.1.0': { platforms: { 'darwin-arm64': { tarball: 'binaries/bun.com/99.1.0/darwin-arm64/bun.com-99.1.0.tar.gz' } } }, | |
| 32 | '99.1.1': { platforms: { 'darwin-arm64': { tarball: 'binaries/bun.com/99.1.1/darwin-arm64/bun.com-99.1.1.tar.gz' } } }, | |
| 33 | '99.2.0': { platforms: { 'linux-x86-64': { tarball: 'binaries/bun.com/99.2.0/linux-x86-64/bun.com-99.2.0.tar.gz' } } }, | |
| 34 | }, | |
| 35 | } | |
| 36 | ||
| 37 | let server: http.Server | |
| 38 | let serveMetadata = true | |
| 39 | const previousRegistry = process.env.PANTRY_REGISTRY_URL | |
| 40 | ||
| 41 | beforeAll(async () => { | |
| 42 | server = http.createServer((_req, res) => { | |
| 43 | if (!serveMetadata) { | |
| 44 | res.writeHead(403).end('rate limited') | |
| 45 | return | |
| 46 | } | |
| 47 | res.writeHead(200, { 'Content-Type': 'application/json' }).end(JSON.stringify(METADATA)) | |
| 48 | }) | |
| 49 | await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve)) | |
| 50 | const { port } = server.address() as { port: number } | |
| 51 | // http, so these calls go through the un-mocked `node:http` while the bun | |
| 52 | // releases lookup stays dead. | |
| 53 | process.env.PANTRY_REGISTRY_URL = `http://127.0.0.1:${port}` | |
| 54 | }) | |
| 55 | ||
| 56 | afterAll(async () => { | |
| 57 | mock.module('node:https', () => realHttps) | |
| 58 | if (previousRegistry === undefined) delete process.env.PANTRY_REGISTRY_URL | |
| 59 | else process.env.PANTRY_REGISTRY_URL = previousRegistry | |
| 60 | await new Promise<void>(resolve => server.close(() => resolve())) | |
| 61 | }) | |
| 62 | ||
| 63 | const darwinArm64 = { os: 'darwin', arch: 'aarch64' } as const | |
| 64 | ||
| 65 | describe('version resolution with a degraded live source', () => { | |
| 66 | it('falls back to the registry when the bun releases lookup is rate-limited', async () => { | |
| 67 | // The reported failure: `^1.4.1` rejected as unsatisfiable while 1.4.2 was | |
| 68 | // published. The pin here can only come from the registry. | |
| 69 | expect(await resolveInstallerVersionConstraint('bun.com', '^99.1.1', darwinArm64, { quiet: true })).toBe('99.1.1') | |
| 70 | expect(await resolveInstallerVersionConstraint('bun.sh', '^99.1.0', darwinArm64, { quiet: true })).toBe('99.1.1') | |
| 71 | }) | |
| 72 | ||
| 73 | it('honours the platform the registry publishes for', async () => { | |
| 74 | // 99.2.0 is linux-only, so a darwin runner must not be handed it. | |
| 75 | expect(await resolveInstallerVersionConstraint('bun.com', '^99.1.0', darwinArm64, { quiet: true })).toBe('99.1.1') | |
| 76 | expect(await resolveInstallerVersionConstraint('bun.com', '^99.2.0', { os: 'linux', arch: 'x86_64' }, { quiet: true })).toBe('99.2.0') | |
| 77 | }) | |
| 78 | ||
| 79 | it('names both failed sources when no source could be consulted', async () => { | |
| 80 | serveMetadata = false | |
| 81 | try { | |
| 82 | await resolveInstallerVersionConstraint('bun.com', '^99.1.1', darwinArm64, { quiet: true }) | |
| 83 | throw new Error('expected an unsatisfiable-constraint failure') | |
| 84 | } | |
| 85 | catch (error) { | |
| 86 | const message = error instanceof Error ? error.message : String(error) | |
| 87 | expect(message).toContain('No version of bun.com satisfies "^99.1.1".') | |
| 88 | expect(message).toContain('2 of 3 version sources could not be consulted') | |
| 89 | expect(message).toContain('api.github.com/repos/oven-sh/bun/releases: unavailable') | |
| 90 | expect(message).toContain('/binaries/bun.com (darwin-arm64): unavailable') | |
| 91 | // The bundled snapshot answered, and the message says so rather than | |
| 92 | // presenting its ceiling as the whole truth. | |
| 93 | expect(message).toContain('bundled catalog (this pantry build):') | |
| 94 | } | |
| 95 | finally { | |
| 96 | serveMetadata = true | |
| 97 | } | |
| 98 | }) | |
| 99 | }) | |