|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { join, resolve } from "node:path"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { execFileSync } from "node:child_process"; |
| 5 | +import { prepareHyperframeLintBody } from "./hyperframeLint.js"; |
| 6 | + |
| 7 | +const hooks = vi.hoisted(() => { |
| 8 | + const state: { |
| 9 | + swap?: () => void; |
| 10 | + target: string; |
| 11 | + descriptors: Set<number>; |
| 12 | + failStat: boolean; |
| 13 | + failRead: boolean; |
| 14 | + } = { |
| 15 | + target: "", |
| 16 | + descriptors: new Set(), |
| 17 | + failStat: false, |
| 18 | + failRead: false, |
| 19 | + }; |
| 20 | + return state; |
| 21 | +}); |
| 22 | +vi.mock("node:fs", async (importOriginal) => { |
| 23 | + const fs = await importOriginal<typeof import("node:fs")>(); |
| 24 | + const swap = () => { |
| 25 | + const run = hooks.swap; |
| 26 | + hooks.swap = undefined; |
| 27 | + run?.(); |
| 28 | + }; |
| 29 | + return { |
| 30 | + ...fs, |
| 31 | + openSync: (...args: Parameters<typeof fs.openSync>) => { |
| 32 | + const fd = fs.openSync(...args); |
| 33 | + hooks.descriptors.add(fd); |
| 34 | + return fd; |
| 35 | + }, |
| 36 | + statSync: (...args: Parameters<typeof fs.statSync>) => { |
| 37 | + const stat = fs.statSync(...args); |
| 38 | + if (args[0] === hooks.target) swap(); |
| 39 | + return stat; |
| 40 | + }, |
| 41 | + fstatSync: (...args: Parameters<typeof fs.fstatSync>) => { |
| 42 | + if (hooks.failStat) throw new Error("stat failure"); |
| 43 | + const stat = fs.fstatSync(...args); |
| 44 | + swap(); |
| 45 | + return stat; |
| 46 | + }, |
| 47 | + readFileSync: (...args: Parameters<typeof fs.readFileSync>) => { |
| 48 | + if (hooks.failRead) throw new Error("read failure"); |
| 49 | + return fs.readFileSync(...args); |
| 50 | + }, |
| 51 | + closeSync: (fd: number) => { |
| 52 | + fs.closeSync(fd); |
| 53 | + hooks.descriptors.delete(fd); |
| 54 | + }, |
| 55 | + }; |
| 56 | +}); |
| 57 | +vi.mock("@hyperframes/lint", () => ({ lintHyperframeHtml: vi.fn() })); |
| 58 | +const fs = await vi.importActual<typeof import("node:fs")>("node:fs"); |
| 59 | +let root: string; |
| 60 | +let projectDir: string; |
| 61 | +beforeEach(() => { |
| 62 | + root = fs.mkdtempSync(join(tmpdir(), "hf-lint-entry-")); |
| 63 | + projectDir = join(root, "project"); |
| 64 | + fs.mkdirSync(projectDir); |
| 65 | + Object.assign(hooks, { target: "", swap: undefined, failStat: false, failRead: false }); |
| 66 | +}); |
| 67 | +afterEach(() => { |
| 68 | + const leaked = [...hooks.descriptors]; |
| 69 | + for (const fd of leaked) fs.closeSync(fd); |
| 70 | + hooks.descriptors.clear(); |
| 71 | + fs.rmSync(root, { recursive: true, force: true }); |
| 72 | + expect(leaked).toEqual([]); |
| 73 | +}); |
| 74 | +function writeEntry(name: string, html = "<html>checked</html>") { |
| 75 | + const path = join(projectDir, name); |
| 76 | + fs.mkdirSync(resolve(path, ".."), { recursive: true }); |
| 77 | + fs.writeFileSync(path, html); |
| 78 | + return path; |
| 79 | +} |
| 80 | +function prepare(entryFile?: string) { |
| 81 | + return prepareHyperframeLintBody({ projectDir, entryFile }); |
| 82 | +} |
| 83 | +function expected(entryFile: string, html = "<html>checked</html>") { |
| 84 | + return { prepared: { entryFile, html, source: "projectDir" } }; |
| 85 | +} |
| 86 | + |
| 87 | +describe("project entry reads", () => { |
| 88 | + it.each(["preferred.html", "index.html", "src/index.html"])( |
| 89 | + "reads the checked file when %s is replaced", |
| 90 | + (entry) => { |
| 91 | + hooks.target = writeEntry(entry); |
| 92 | + hooks.swap = () => { |
| 93 | + fs.renameSync(hooks.target, join(root, "original.html")); |
| 94 | + fs.writeFileSync(hooks.target, "<html>unchecked</html>"); |
| 95 | + }; |
| 96 | + expect(prepare(entry === "preferred.html" ? entry : undefined)).toEqual(expected(entry)); |
| 97 | + expect(hooks.swap).toBeUndefined(); |
| 98 | + }, |
| 99 | + ); |
| 100 | + it("prefers requested entries and accepts empty HTML", () => { |
| 101 | + writeEntry("preferred.html", ""); |
| 102 | + writeEntry("index.html"); |
| 103 | + expect(prepare("preferred.html")).toEqual(expected("preferred.html", "")); |
| 104 | + }); |
| 105 | + it.each(["missing", "directory", "dangling", "non-directory-parent"])( |
| 106 | + "falls back from a %s entry", |
| 107 | + (kind) => { |
| 108 | + writeEntry("index.html"); |
| 109 | + let preferred = "preferred.html"; |
| 110 | + if (kind === "directory") fs.mkdirSync(join(projectDir, preferred)); |
| 111 | + if (kind === "dangling") |
| 112 | + fs.symlinkSync(join(root, "absent"), join(projectDir, preferred), "file"); |
| 113 | + if (kind === "non-directory-parent") { |
| 114 | + writeEntry("parent"); |
| 115 | + preferred = "parent/file.html"; |
| 116 | + } |
| 117 | + expect(prepare(preferred)).toEqual(expected("index.html")); |
| 118 | + }, |
| 119 | + ); |
| 120 | + it.skipIf(process.platform === "win32")("skips a FIFO without waiting for a writer", () => { |
| 121 | + execFileSync("mkfifo", [join(projectDir, "pipe")]); |
| 122 | + writeEntry("index.html"); |
| 123 | + expect(prepare("pipe")).toEqual(expected("index.html")); |
| 124 | + }); |
| 125 | + it("keeps project and entry symlinks working", () => { |
| 126 | + const target = join(root, "linked.html"); |
| 127 | + fs.writeFileSync(target, "linked"); |
| 128 | + fs.symlinkSync(target, join(projectDir, "index.html"), "file"); |
| 129 | + const linkedProject = join(root, "linked-project"); |
| 130 | + fs.symlinkSync(projectDir, linkedProject, process.platform === "win32" ? "junction" : "dir"); |
| 131 | + expect(prepareHyperframeLintBody({ projectDir: linkedProject })).toEqual( |
| 132 | + expected("index.html", "linked"), |
| 133 | + ); |
| 134 | + }); |
| 135 | + it("rejects a sibling directory sharing the project name prefix", () => { |
| 136 | + const sibling = join(root, "project-other"); |
| 137 | + fs.mkdirSync(sibling); |
| 138 | + fs.writeFileSync(join(sibling, "index.html"), "outside"); |
| 139 | + expect(prepare("../project-other/index.html")).toEqual({ |
| 140 | + error: "Entry file must stay inside project directory: ../project-other/index.html", |
| 141 | + }); |
| 142 | + }); |
| 143 | + it("preserves absent-project and absent-entry errors", () => { |
| 144 | + expect(prepare()).toEqual({ |
| 145 | + error: `No HTML entry file found in project directory: ${join(projectDir, "index.html")}`, |
| 146 | + }); |
| 147 | + fs.rmdirSync(projectDir); |
| 148 | + expect(prepare()).toEqual({ error: `Project directory not found: ${projectDir}` }); |
| 149 | + }); |
| 150 | + it.each(["stat", "read"])("propagates %s failures and closes the descriptor", (failure) => { |
| 151 | + writeEntry("index.html"); |
| 152 | + hooks.failStat = failure === "stat"; |
| 153 | + hooks.failRead = failure === "read"; |
| 154 | + expect(() => prepare()).toThrow(`${failure} failure`); |
| 155 | + expect(hooks.descriptors.size).toBe(0); |
| 156 | + }); |
| 157 | + it("keeps inline HTML and files-payload entry selection unchanged", () => { |
| 158 | + expect(prepareHyperframeLintBody({ html: "", entryFile: " custom.html " })).toEqual({ |
| 159 | + prepared: { html: "", entryFile: "custom.html", source: "html" }, |
| 160 | + }); |
| 161 | + expect( |
| 162 | + prepareHyperframeLintBody({ files: { "src/index.html": "source", "index.html": "" } }), |
| 163 | + ).toEqual({ prepared: { html: "source", entryFile: "src/index.html", source: "files" } }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments