import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { chmodSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import type { LeftOutReference } from "./leftOutReference.js"; import { stagePublishFiles } from "./publishProject.js"; let dir: string; const locked: string[] = []; beforeEach(() => { dir = mkdtempSync(join(tmpdir(), "hf-publish-unreadable-")); }); afterEach(() => { for (const path of locked.splice(0)) chmodSync(path, 0o755); rmSync(dir, { recursive: true, force: true }); }); function write(path: string, content = "x"): void { mkdirSync(dirname(join(dir, path)), { recursive: true }); writeFileSync(join(dir, path), content, "utf-8"); } function lock(path: string): void { chmodSync(join(dir, path), 0o000); locked.push(join(dir, path)); } function stage(): { keys: string[]; leftOut: LeftOutReference[]; warnings: string[] } { const leftOut: LeftOutReference[] = []; const warnings: string[] = []; const staged = stagePublishFiles(dir, Infinity, (warning, reference) => { warnings.push(warning); leftOut.push(reference); }); return { keys: [...staged.keys()].sort(), leftOut, warnings }; } const page = (body = "") => `
'));
write("logo.png");
write("draft.png");
lock("draft.png");
const { keys, leftOut } = stage();
expect(keys).toEqual(["index.html", "logo.png"]);
expect(leftOut.map(({ file, reference }) => [file, reference])).toEqual([
["draft.png", "draft.png"],
]);
});
it.skipIf(!canDenyReads)("names the page that uses it when the video does", () => {
write("index.html", page('
'));
write("assets/logo.png");
lock("assets/logo.png");
const { keys, leftOut, warnings } = stage();
expect(keys).toEqual(["index.html"]);
expect(leftOut).toEqual([
expect.objectContaining({ file: "assets/logo.png", reference: "assets/logo.png" }),
expect.objectContaining({
file: "index.html",
reference: "assets/logo.png",
reason: "unreadable",
cause: "denied",
}),
]);
expect(warnings[1]).toBe(
"index.html uses assets/logo.png, which could not be read because this computer does not allow reading it; it was left out of the upload.",
);
});
it.skipIf(!canDenyReads)("names the page that uses a file inside a folder it cannot list", () => {
write("index.html", page(''));
write("media/intro.mp4");
lock("media");
const { leftOut } = stage();
expect(leftOut.map(({ file, reference, cause }) => [file, reference, cause])).toEqual([
["media", "media", "denied"],
["index.html", "media/intro.mp4", "denied"],
]);
});
it.skipIf(!canDenyReads)("refuses, naming it, when it is the page the video starts from", () => {
write("index.html", page());
lock("index.html");
expect(() => stage()).toThrow(
"index.html could not be read because this computer does not allow reading it.",
);
});
});