|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import test from 'node:test'; |
| 3 | + |
| 4 | +import { downloadNpmTarball } from './update-brew-formula.mjs'; |
| 5 | + |
| 6 | +const TARBALL_URL = 'https://registry.npmjs.org/@pymodel/pythinker-code/-/pythinker-code-2.0.0.tgz'; |
| 7 | +const BODY = Buffer.from('pythinker-tarball'); |
| 8 | + |
| 9 | +function response(status, body = BODY) { |
| 10 | + return new Response(body, { status }); |
| 11 | +} |
| 12 | + |
| 13 | +function pollClock() { |
| 14 | + let now = 0; |
| 15 | + const sleeps = []; |
| 16 | + const sleep = async (ms) => { |
| 17 | + sleeps.push(ms); |
| 18 | + now += ms; |
| 19 | + }; |
| 20 | + return { |
| 21 | + now: () => now, |
| 22 | + sleeps, |
| 23 | + sleep, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +function pollOptions(clock, fetchImpl) { |
| 28 | + return { |
| 29 | + url: TARBALL_URL, |
| 30 | + fetchImpl, |
| 31 | + sleep: (ms) => clock.sleep(ms), |
| 32 | + now: () => clock.now(), |
| 33 | + log: () => {}, |
| 34 | + budgetMs: 45_000, |
| 35 | + intervalMs: 15_000, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +void test('returns the tarball when the first fetch is HTTP 200', async () => { |
| 40 | + const clock = pollClock(); |
| 41 | + let fetches = 0; |
| 42 | + const result = await downloadNpmTarball( |
| 43 | + pollOptions(clock, async () => { |
| 44 | + fetches += 1; |
| 45 | + return response(200); |
| 46 | + }), |
| 47 | + ); |
| 48 | + |
| 49 | + assert.equal(fetches, 1); |
| 50 | + assert.equal(result.attempts, 1); |
| 51 | + assert.deepEqual(result.tarball, BODY); |
| 52 | + assert.deepEqual(clock.sleeps, []); |
| 53 | +}); |
| 54 | + |
| 55 | +void test('retries after HTTP 404 and returns the tarball once npm serves it', async () => { |
| 56 | + const clock = pollClock(); |
| 57 | + const statuses = [404, 404, 200]; |
| 58 | + const result = await downloadNpmTarball( |
| 59 | + pollOptions(clock, async () => response(statuses.shift() ?? 500)), |
| 60 | + ); |
| 61 | + |
| 62 | + assert.equal(result.attempts, 3); |
| 63 | + assert.deepEqual(result.tarball, BODY); |
| 64 | + assert.deepEqual(clock.sleeps, [15_000, 15_000]); |
| 65 | +}); |
| 66 | + |
| 67 | +void test('retries an empty 200 body until a non-empty tarball arrives', async () => { |
| 68 | + const clock = pollClock(); |
| 69 | + const bodies = [Buffer.alloc(0), BODY]; |
| 70 | + const result = await downloadNpmTarball( |
| 71 | + pollOptions(clock, async () => response(200, bodies.shift() ?? BODY)), |
| 72 | + ); |
| 73 | + |
| 74 | + assert.equal(result.attempts, 2); |
| 75 | + assert.deepEqual(result.tarball, BODY); |
| 76 | + assert.deepEqual(clock.sleeps, [15_000]); |
| 77 | +}); |
| 78 | + |
| 79 | +void test('throws after the budget when every fetch is HTTP 404', async () => { |
| 80 | + const clock = pollClock(); |
| 81 | + let fetches = 0; |
| 82 | + await assert.rejects( |
| 83 | + () => |
| 84 | + downloadNpmTarball( |
| 85 | + pollOptions(clock, async () => { |
| 86 | + fetches += 1; |
| 87 | + return response(404); |
| 88 | + }), |
| 89 | + ), |
| 90 | + { message: 'Failed to download npm tarball: HTTP 404 after 4 attempt(s)' }, |
| 91 | + ); |
| 92 | + assert.equal(fetches, 4); |
| 93 | + assert.deepEqual(clock.sleeps, [15_000, 15_000, 15_000]); |
| 94 | +}); |
| 95 | + |
| 96 | +void test('sleeps the leftover budget then fetches again before giving up', async () => { |
| 97 | + const clock = pollClock(); |
| 98 | + const statuses = [404, 404, 404, 200]; |
| 99 | + const result = await downloadNpmTarball({ |
| 100 | + ...pollOptions(clock, async () => response(statuses.shift() ?? 500)), |
| 101 | + budgetMs: 40_000, |
| 102 | + }); |
| 103 | + assert.equal(result.attempts, 4); |
| 104 | + assert.deepEqual(result.tarball, BODY); |
| 105 | + assert.deepEqual(clock.sleeps, [15_000, 15_000, 10_000]); |
| 106 | +}); |
0 commit comments