|
| 1 | +import { describe, expect, mock, test } from "bun:test"; |
| 2 | + |
| 3 | +// The default downloader inside connectPublicHttps used to forward `maxBytes: undefined` |
| 4 | +// to pinnedHttpGet, whose cap is optional — so a caller that omitted a limit removed the |
| 5 | +// byte ceiling entirely instead of inheriting MAX_DOWNLOAD_BYTES. Both production callers |
| 6 | +// happen to pass an explicit limit today, which is why the existing suites (they all |
| 7 | +// inject `pinnedDownload` and bypass the default path) could not see it. |
| 8 | + |
| 9 | +const lookupMock = mock(async (): Promise<{ address: string; family: number }[]> => [ |
| 10 | + { address: "93.184.216.34", family: 4 }, |
| 11 | +]); |
| 12 | +mock.module("node:dns/promises", () => ({ lookup: lookupMock })); |
| 13 | + |
| 14 | +const MIN_PNG = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); |
| 15 | +const seenMaxBytes: Array<number | undefined> = []; |
| 16 | + |
| 17 | +mock.module("../../src/lib/pinned-http", () => ({ |
| 18 | + pinnedHttpGet: async ( |
| 19 | + _url: string, |
| 20 | + _pinned: unknown, |
| 21 | + _signal?: AbortSignal, |
| 22 | + options?: { maxBytes?: number }, |
| 23 | + ) => { |
| 24 | + seenMaxBytes.push(options?.maxBytes); |
| 25 | + return new Response(MIN_PNG, { status: 200 }); |
| 26 | + }, |
| 27 | + PinnedHttpError: class extends Error {}, |
| 28 | +})); |
| 29 | + |
| 30 | +const { fetchPublicHttpsImage, MAX_DOWNLOAD_BYTES } = await import( |
| 31 | + `../../src/images/artifacts?cap=${Date.now()}` |
| 32 | +); |
| 33 | + |
| 34 | +describe("default image downloader byte cap", () => { |
| 35 | + test("an omitted maxBytes inherits MAX_DOWNLOAD_BYTES instead of removing the cap", async () => { |
| 36 | + seenMaxBytes.length = 0; |
| 37 | + const resp = await fetchPublicHttpsImage("https://public-host/image.png"); |
| 38 | + expect(resp.status).toBe(200); |
| 39 | + expect(seenMaxBytes).toEqual([MAX_DOWNLOAD_BYTES]); |
| 40 | + expect(seenMaxBytes[0]).not.toBeUndefined(); |
| 41 | + }); |
| 42 | + |
| 43 | + test("an explicit tighter limit is preserved", async () => { |
| 44 | + seenMaxBytes.length = 0; |
| 45 | + await fetchPublicHttpsImage("https://public-host/image.png", { maxBytes: 1024 }); |
| 46 | + expect(seenMaxBytes).toEqual([1024]); |
| 47 | + }); |
| 48 | +}); |
0 commit comments