|
1 | | -import { describe, expect, it } from "bun:test"; |
2 | | -import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; |
| 1 | +import { describe, expect, it, spyOn } from "bun:test"; |
| 2 | +import { |
| 3 | + existsSync, |
| 4 | + mkdirSync, |
| 5 | + mkdtempSync, |
| 6 | + readFileSync, |
| 7 | + readdirSync, |
| 8 | + rmSync, |
| 9 | + writeFileSync, |
| 10 | +} from "node:fs"; |
3 | 11 | import { tmpdir } from "node:os"; |
4 | 12 | import { join } from "node:path"; |
5 | 13 | import { compressToWoff2, fontToDataUri } from "./fontCompression.js"; |
@@ -43,6 +51,66 @@ describe("compressToWoff2", () => { |
43 | 51 | }); |
44 | 52 |
|
45 | 53 | describe("fontToDataUri", () => { |
| 54 | + it("preserves a pre-existing temporary file and still populates the cache", async () => { |
| 55 | + const cacheDir = mkdtempSync(join(tmpdir(), "hf-local-font-cache-")); |
| 56 | + const raw = Buffer.from("stable-font-content"); |
| 57 | + const compressed = Buffer.from("compressed-font-content"); |
| 58 | + const compressImpl = async () => compressed; |
| 59 | + const clock = spyOn(Date, "now").mockReturnValue(1234567890); |
| 60 | + try { |
| 61 | + const first = await fontToDataUri(raw, "ttf", { cacheDir, compressImpl }); |
| 62 | + const [cacheName] = readdirSync(cacheDir); |
| 63 | + if (!cacheName) throw new Error("Expected a cached font"); |
| 64 | + rmSync(join(cacheDir, cacheName)); |
| 65 | + const existingName = `${cacheName}.tmp-${process.pid}-${Date.now()}`; |
| 66 | + writeFileSync(join(cacheDir, existingName), "another writer's file"); |
| 67 | + |
| 68 | + expect(await fontToDataUri(raw, "ttf", { cacheDir, compressImpl })).toBe(first); |
| 69 | + expect(readFileSync(join(cacheDir, existingName), "utf8")).toBe("another writer's file"); |
| 70 | + expect(readFileSync(join(cacheDir, cacheName))).toEqual(compressed); |
| 71 | + expect(readdirSync(cacheDir).sort()).toEqual([cacheName, existingName].sort()); |
| 72 | + } finally { |
| 73 | + clock.mockRestore(); |
| 74 | + rmSync(cacheDir, { recursive: true, force: true }); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + it("cleans up its temporary files when publishing the cache fails", async () => { |
| 79 | + const cacheDir = mkdtempSync(join(tmpdir(), "hf-local-font-cache-")); |
| 80 | + const raw = Buffer.from("stable-font-content"); |
| 81 | + const compressImpl = async () => Buffer.from("compressed-font-content"); |
| 82 | + try { |
| 83 | + const first = await fontToDataUri(raw, "ttf", { cacheDir, compressImpl }); |
| 84 | + const [cacheName] = readdirSync(cacheDir); |
| 85 | + if (!cacheName) throw new Error("Expected a cached font"); |
| 86 | + rmSync(join(cacheDir, cacheName)); |
| 87 | + mkdirSync(join(cacheDir, cacheName)); |
| 88 | + |
| 89 | + expect(await fontToDataUri(raw, "ttf", { cacheDir, compressImpl })).toBe(first); |
| 90 | + expect(readdirSync(cacheDir)).toEqual([cacheName]); |
| 91 | + } finally { |
| 92 | + rmSync(cacheDir, { recursive: true, force: true }); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + it("returns compressed data when the cache directory cannot be created", async () => { |
| 97 | + const root = mkdtempSync(join(tmpdir(), "hf-local-font-cache-")); |
| 98 | + const cacheDir = join(root, "not-a-directory"); |
| 99 | + writeFileSync(cacheDir, "keep"); |
| 100 | + try { |
| 101 | + const compressed = Buffer.from("compressed-font-content"); |
| 102 | + const uri = await fontToDataUri(Buffer.from("font"), "ttf", { |
| 103 | + cacheDir, |
| 104 | + compressImpl: async () => compressed, |
| 105 | + }); |
| 106 | + expect(uri).toBe(`data:font/woff2;base64,${compressed.toString("base64")}`); |
| 107 | + expect(readFileSync(cacheDir, "utf8")).toBe("keep"); |
| 108 | + expect(readdirSync(root)).toEqual(["not-a-directory"]); |
| 109 | + } finally { |
| 110 | + rmSync(root, { recursive: true, force: true }); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
46 | 114 | it("reuses cached compression across calls", async () => { |
47 | 115 | const cacheDir = mkdtempSync(join(tmpdir(), "hf-local-font-cache-")); |
48 | 116 | const raw = Buffer.from("stable-font-content"); |
|
0 commit comments