|
| 1 | +import { describe, it, before, after } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { mkdtempSync, writeFileSync, rmSync } from "node:fs"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { tmpdir } from "node:os"; |
| 6 | +import { resolveFilePathsInManifest } from "./manifest.mjs"; |
| 7 | + |
| 8 | +let tempDir; |
| 9 | + |
| 10 | +before(() => { |
| 11 | + tempDir = mkdtempSync(join(tmpdir(), "manifest-test-")); |
| 12 | + // Create test files |
| 13 | + writeFileSync(join(tempDir, "index.html"), "<!DOCTYPE html><html><body>Hello</body></html>"); |
| 14 | + writeFileSync(join(tempDir, "style.css"), "body { margin: 0; }"); |
| 15 | + writeFileSync(join(tempDir, "logo.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])); // PNG header |
| 16 | +}); |
| 17 | + |
| 18 | +after(() => { |
| 19 | + rmSync(tempDir, { recursive: true, force: true }); |
| 20 | +}); |
| 21 | + |
| 22 | +describe("resolveFilePathsInManifest", () => { |
| 23 | + it("resolves path to inline data for text files", () => { |
| 24 | + const manifest = { |
| 25 | + files: [{ file: "index.html", path: "index.html" }], |
| 26 | + }; |
| 27 | + resolveFilePathsInManifest(manifest, tempDir); |
| 28 | + assert.equal(manifest.files[0].data, "<!DOCTYPE html><html><body>Hello</body></html>"); |
| 29 | + assert.equal(manifest.files[0].path, undefined, "path field should be removed"); |
| 30 | + assert.equal(manifest.files[0].encoding, undefined, "text files should not set encoding"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("auto-detects binary files and base64-encodes them", () => { |
| 34 | + const manifest = { |
| 35 | + files: [{ file: "logo.png", path: "logo.png" }], |
| 36 | + }; |
| 37 | + resolveFilePathsInManifest(manifest, tempDir); |
| 38 | + assert.equal(manifest.files[0].encoding, "base64"); |
| 39 | + // Verify it's valid base64 that decodes to our PNG header |
| 40 | + const buf = Buffer.from(manifest.files[0].data, "base64"); |
| 41 | + assert.equal(buf[0], 0x89); |
| 42 | + assert.equal(buf[1], 0x50); // 'P' |
| 43 | + }); |
| 44 | + |
| 45 | + it("leaves entries with existing data untouched", () => { |
| 46 | + const manifest = { |
| 47 | + files: [{ file: "index.html", data: "<h1>inline</h1>" }], |
| 48 | + }; |
| 49 | + resolveFilePathsInManifest(manifest, tempDir); |
| 50 | + assert.equal(manifest.files[0].data, "<h1>inline</h1>"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("mixes path and data entries", () => { |
| 54 | + const manifest = { |
| 55 | + files: [ |
| 56 | + { file: "index.html", data: "<h1>inline</h1>" }, |
| 57 | + { file: "style.css", path: "style.css" }, |
| 58 | + ], |
| 59 | + }; |
| 60 | + resolveFilePathsInManifest(manifest, tempDir); |
| 61 | + assert.equal(manifest.files[0].data, "<h1>inline</h1>"); |
| 62 | + assert.equal(manifest.files[1].data, "body { margin: 0; }"); |
| 63 | + assert.equal(manifest.files[1].path, undefined); |
| 64 | + }); |
| 65 | + |
| 66 | + it("uses path as file name when file is omitted", () => { |
| 67 | + const manifest = { |
| 68 | + files: [{ path: "style.css" }], |
| 69 | + }; |
| 70 | + resolveFilePathsInManifest(manifest, tempDir); |
| 71 | + assert.equal(manifest.files[0].file, "style.css"); |
| 72 | + assert.equal(manifest.files[0].data, "body { margin: 0; }"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("handles manifest with no files array", () => { |
| 76 | + const manifest = { migrations: "CREATE TABLE t (id int)" }; |
| 77 | + resolveFilePathsInManifest(manifest, tempDir); |
| 78 | + assert.equal(manifest.files, undefined, "should not add a files array"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("throws on missing file", () => { |
| 82 | + const manifest = { |
| 83 | + files: [{ file: "missing.html", path: "does-not-exist.html" }], |
| 84 | + }; |
| 85 | + assert.throws( |
| 86 | + () => resolveFilePathsInManifest(manifest, tempDir), |
| 87 | + /ENOENT/, |
| 88 | + ); |
| 89 | + }); |
| 90 | +}); |
0 commit comments