|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Part of PyBLE (https://pyble.dev) — see /LICENSE. |
| 3 | + |
| 4 | +import { createHash } from "node:crypto"; |
| 5 | +import { readFile } from "node:fs/promises"; |
| 6 | +import { basename, join } from "node:path"; |
| 7 | + |
| 8 | +import { describe, expect, it } from "vitest"; |
| 9 | + |
| 10 | +import { tutorialAppCaptures } from "@/lib/tutorial-app-captures"; |
| 11 | + |
| 12 | +const expectedCaptureIds = [ |
| 13 | + "setupScanResults", |
| 14 | + "setupConnectedIdentity", |
| 15 | + "firstProgramEditorConsole", |
| 16 | + "filesMultiDeleteReview", |
| 17 | + "githubBranchChooser", |
| 18 | + "githubPrewriteReview", |
| 19 | + "examplesImportComplete", |
| 20 | + "blocksHelloWorkspace", |
| 21 | +] as const; |
| 22 | + |
| 23 | +const pngSignature = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]); |
| 24 | +const forbiddenMetadataChunks = new Set([ |
| 25 | + "eXIf", |
| 26 | + "iTXt", |
| 27 | + "tEXt", |
| 28 | + "tIME", |
| 29 | + "zTXt", |
| 30 | +]); |
| 31 | + |
| 32 | +function pngChunkTypes(bytes: Buffer): string[] { |
| 33 | + const chunkTypes: string[] = []; |
| 34 | + let offset = pngSignature.length; |
| 35 | + |
| 36 | + while (offset + 12 <= bytes.length) { |
| 37 | + const dataLength = bytes.readUInt32BE(offset); |
| 38 | + const chunkEnd = offset + 12 + dataLength; |
| 39 | + expect(chunkEnd).toBeLessThanOrEqual(bytes.length); |
| 40 | + |
| 41 | + const chunkType = bytes.toString("ascii", offset + 4, offset + 8); |
| 42 | + chunkTypes.push(chunkType); |
| 43 | + offset = chunkEnd; |
| 44 | + |
| 45 | + if (chunkType === "IEND") { |
| 46 | + break; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return chunkTypes; |
| 51 | +} |
| 52 | + |
| 53 | +describe("reviewed physical tutorial capture assets", () => { |
| 54 | + it("keeps the reviewed capture registry complete and unique", () => { |
| 55 | + expect(Object.keys(tutorialAppCaptures)).toEqual(expectedCaptureIds); |
| 56 | + expect( |
| 57 | + new Set(Object.values(tutorialAppCaptures).map(({ src }) => src)).size, |
| 58 | + ).toBe(expectedCaptureIds.length); |
| 59 | + }); |
| 60 | + |
| 61 | + it.each(expectedCaptureIds)( |
| 62 | + "%s binds content-versioned, stripped PNG bytes to reviewed provenance", |
| 63 | + async (captureId) => { |
| 64 | + const capture = tutorialAppCaptures[captureId]; |
| 65 | + const { review } = capture.provenance; |
| 66 | + |
| 67 | + if (review.status !== "reviewed") { |
| 68 | + throw new Error(`${captureId} has not completed derivative review`); |
| 69 | + } |
| 70 | + |
| 71 | + expect(review.sha256).toMatch(/^[0-9a-f]{64}$/); |
| 72 | + expect(basename(capture.src)).toContain(review.sha256.slice(0, 12)); |
| 73 | + expect(capture.src).not.toContain("pending"); |
| 74 | + expect(capture.provenance.rawCapture).toMatch( |
| 75 | + /production-release\.raw\.png$/, |
| 76 | + ); |
| 77 | + expect(capture.provenance.rawCapture).not.toMatch( |
| 78 | + /debug|golden|integration/i, |
| 79 | + ); |
| 80 | + expect(capture.provenance.processing).toMatch( |
| 81 | + /crop.*status.*navigation.*strip metadata.*not retouch/is, |
| 82 | + ); |
| 83 | + |
| 84 | + const asset = await readFile( |
| 85 | + join(process.cwd(), "public", capture.src.slice(1)), |
| 86 | + ); |
| 87 | + expect(asset.subarray(0, pngSignature.length)).toEqual(pngSignature); |
| 88 | + expect(asset.readUInt32BE(16)).toBe(capture.width); |
| 89 | + expect(asset.readUInt32BE(20)).toBe(capture.height); |
| 90 | + expect({ width: capture.width, height: capture.height }).toEqual({ |
| 91 | + width: 2000, |
| 92 | + height: 1092, |
| 93 | + }); |
| 94 | + expect(createHash("sha256").update(asset).digest("hex")).toBe( |
| 95 | + review.sha256, |
| 96 | + ); |
| 97 | + |
| 98 | + const chunkTypes = pngChunkTypes(asset); |
| 99 | + expect(chunkTypes.at(0)).toBe("IHDR"); |
| 100 | + expect(chunkTypes.at(-1)).toBe("IEND"); |
| 101 | + expect( |
| 102 | + chunkTypes.filter((chunkType) => |
| 103 | + forbiddenMetadataChunks.has(chunkType), |
| 104 | + ), |
| 105 | + ).toEqual([]); |
| 106 | + }, |
| 107 | + ); |
| 108 | +}); |
0 commit comments