|
| 1 | +import { afterEach, describe, expect, it } from "bun:test"; |
| 2 | +import { createHash } from "node:crypto"; |
| 3 | +import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { hashProjectDir, listSiteFiles } from "./projectHash.js"; |
| 7 | + |
| 8 | +const dirs: string[] = []; |
| 9 | +function tempDir(): string { |
| 10 | + const dir = mkdtempSync(join(tmpdir(), "hf-project-hash-")); |
| 11 | + dirs.push(dir); |
| 12 | + return dir; |
| 13 | +} |
| 14 | +function project(): string { |
| 15 | + const dir = tempDir(); |
| 16 | + writeFileSync(join(dir, "index.html"), "<html>hi</html>"); |
| 17 | + return dir; |
| 18 | +} |
| 19 | +afterEach(() => { |
| 20 | + for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); |
| 21 | +}); |
| 22 | + |
| 23 | +describe("listSiteFiles / hashProjectDir", () => { |
| 24 | + it("follows a symlinked project root", () => { |
| 25 | + const real = project(); |
| 26 | + const link = join(tempDir(), "fx"); |
| 27 | + symlinkSync(real, link); |
| 28 | + expect(listSiteFiles(link)).toEqual(["index.html"]); |
| 29 | + expect(hashProjectDir(link)).toBe(hashProjectDir(real)); |
| 30 | + }); |
| 31 | + |
| 32 | + it("lists symlinked files and folders, and hashes their targets' bytes", () => { |
| 33 | + const dir = project(); |
| 34 | + const outside = tempDir(); |
| 35 | + mkdirSync(join(outside, "media")); |
| 36 | + writeFileSync(join(outside, "shared.css"), "a{}"); |
| 37 | + writeFileSync(join(outside, "media", "clip.txt"), "v1"); |
| 38 | + symlinkSync(join(outside, "shared.css"), join(dir, "shared.css")); |
| 39 | + symlinkSync(join(outside, "media"), join(dir, "media")); |
| 40 | + expect(listSiteFiles(dir)).toEqual(["index.html", "media/clip.txt", "shared.css"]); |
| 41 | + |
| 42 | + const before = hashProjectDir(dir); |
| 43 | + writeFileSync(join(outside, "media", "clip.txt"), "v2"); |
| 44 | + expect(hashProjectDir(dir)).not.toBe(before); |
| 45 | + }); |
| 46 | + |
| 47 | + it("skips top-level skip-list folders", () => { |
| 48 | + const dir = project(); |
| 49 | + mkdirSync(join(dir, "node_modules")); |
| 50 | + writeFileSync(join(dir, "node_modules", "junk.js"), "x"); |
| 51 | + expect(listSiteFiles(dir)).toEqual(["index.html"]); |
| 52 | + }); |
| 53 | + |
| 54 | + it("does not loop on a folder that links back to an ancestor", () => { |
| 55 | + const dir = project(); |
| 56 | + symlinkSync(dir, join(dir, "self")); |
| 57 | + expect(listSiteFiles(dir)).toEqual(["index.html"]); |
| 58 | + }); |
| 59 | + |
| 60 | + it("throws on a dangling symlink, naming it", () => { |
| 61 | + const dir = project(); |
| 62 | + symlinkSync(join(dir, "missing.png"), join(dir, "broken.png")); |
| 63 | + expect(() => listSiteFiles(dir)).toThrow(/cannot read broken\.png/); |
| 64 | + }); |
| 65 | + |
| 66 | + it("never reuses an id from the unversioned scheme", () => { |
| 67 | + const dir = project(); |
| 68 | + const unversioned = createHash("sha256") |
| 69 | + .update("index.html\0<html>hi</html>") |
| 70 | + .digest("hex") |
| 71 | + .slice(0, 16); |
| 72 | + expect(hashProjectDir(dir)).not.toBe(unversioned); |
| 73 | + }); |
| 74 | +}); |
0 commit comments