| 1 | import { test } from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | |
| 4 | import { |
| 5 | MAX_PRODUCTION_IMAGE_BYTES, |
| 6 | checkProductionImage, |
| 7 | productionImageKey, |
| 8 | productionImageOf, |
| 9 | sha256, |
| 10 | sniffProductionImage, |
| 11 | today, |
| 12 | wantsScreenshot, |
| 13 | } from "./production-image.ts"; |
| 14 | |
| 15 | const PNG = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0]; |
| 16 | const JPEG = [0xff, 0xd8, 0xff, 0xe0, 0, 0, 0, 0, 0, 0, 0, 0]; |
| 17 | const WEBP = [...Buffer.from("RIFF"), 0, 0, 0, 0, ...Buffer.from("WEBP")]; |
| 18 | const GIF = [...Buffer.from("GIF89a"), 0, 0, 0, 0, 0, 0]; |
| 19 | const base64 = (bytes: number[] | Uint8Array) => Buffer.from(bytes).toString("base64"); |
| 20 | |
| 21 | test("a production image is a PNG, JPEG or WebP, by its bytes", () => { |
| 22 | assert.equal(sniffProductionImage(Uint8Array.from(PNG)), "image/png"); |
| 23 | assert.equal(sniffProductionImage(Uint8Array.from(JPEG)), "image/jpeg"); |
| 24 | assert.equal(sniffProductionImage(Uint8Array.from(WEBP)), "image/webp"); |
| 25 | assert.equal(sniffProductionImage(Uint8Array.from(GIF)), null); |
| 26 | assert.equal(sniffProductionImage(new TextEncoder().encode("<svg xmlns='http://www.w3.org/2000/svg'/>")), null); |
| 27 | }); |
| 28 | |
| 29 | test("an upload is checked: present, at most 5 MB, and an image", () => { |
| 30 | const png = checkProductionImage(base64(PNG)); |
| 31 | assert.ok(png.ok); |
| 32 | assert.equal(png.ok && png.contentType, "image/png"); |
| 33 | assert.deepEqual(checkProductionImage(""), { ok: false, message: "Choose a PNG, JPEG or WebP image." }); |
| 34 | assert.deepEqual(checkProductionImage(null), { ok: false, message: "Choose a PNG, JPEG or WebP image." }); |
| 35 | assert.deepEqual(checkProductionImage("%%%"), { ok: false, message: "Choose a PNG, JPEG or WebP image." }); |
| 36 | assert.deepEqual(checkProductionImage(base64(GIF)), { ok: false, message: "Use a PNG, JPEG or WebP image." }); |
| 37 | const big = new Uint8Array(MAX_PRODUCTION_IMAGE_BYTES + 1); |
| 38 | big.set(PNG); |
| 39 | assert.deepEqual(checkProductionImage(base64(big)), { ok: false, message: "Use an image of at most 5 MB." }); |
| 40 | }); |
| 41 | |
| 42 | test("an image is kept by its hash, apart from avatars", async () => { |
| 43 | const file = await sha256(new TextEncoder().encode("abc")); |
| 44 | assert.equal(file, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); |
| 45 | assert.equal(productionImageKey(file), `production/${file}`); |
| 46 | }); |
| 47 | |
| 48 | test("the stored image, and whether it is preferred", () => { |
| 49 | assert.equal(productionImageOf({}), null); |
| 50 | assert.deepEqual(productionImageOf({ production_image: "ab", production_image_type: "image/webp", production_image_preferred: 1 }), { |
| 51 | file: "ab", |
| 52 | contentType: "image/webp", |
| 53 | preferred: true, |
| 54 | }); |
| 55 | assert.equal(productionImageOf({ production_image: "ab", production_image_preferred: 0 })?.preferred, false); |
| 56 | }); |
| 57 | |
| 58 | test("only production elsewhere, at an address and not shown as an upload, is visited", () => { |
| 59 | const elsewhere = { runs: "elsewhere", productionUrl: "https://g1t.sh", productionImage: null }; |
| 60 | assert.ok(wantsScreenshot(elsewhere)); |
| 61 | assert.ok(wantsScreenshot({ ...elsewhere, productionImage: { preferred: false } })); |
| 62 | assert.ok(!wantsScreenshot({ ...elsewhere, productionImage: { preferred: true } })); |
| 63 | assert.ok(!wantsScreenshot({ ...elsewhere, productionUrl: null })); |
| 64 | assert.ok(!wantsScreenshot({ ...elsewhere, runs: "g1t" })); |
| 65 | assert.ok(!wantsScreenshot({ ...elsewhere, runs: null })); |
| 66 | }); |
| 67 | |
| 68 | test("the day a screenshot is versioned by", () => { |
| 69 | assert.equal(today(Date.parse("2026-10-11T23:30:00Z")), "2026-10-11"); |
| 70 | }); |