diff --git a/README.md b/README.md index f3f62fe..c2ff5d7 100644 --- a/README.md +++ b/README.md @@ -103,29 +103,33 @@ without excessive gaps. ## NPM Publishing & Versioning Guide -We use **Semantic Versioning (SemVer)** to manage releases. Instead of manually editing `package.json`, use the following automated workflow to ensure the repository tags and the NPM registry remain perfectly in sync. +We use **Semantic Versioning (SemVer)** to manage releases. Instead of manually editing `package.json`, use the following workflow to keep the repository tags, GitHub Releases, and the npm registry in sync. -### 1. Semantic Versioning Commands +### 1. Bump the version -Decide which part of the version number ($X.Y.Z$) to increment based on your changes: +Start from a clean, up-to-date `main` (`git pull`, `git status`). Decide which part of the version number ($X.Y.Z$) to increment: | Command | Result | Use Case | | :------------------ | :------------------ | :-------------------------------------------------------------------- | -| `pnpm version patch` | `0.0.x` → `0.0.x+1` | Bug fixes, style tweaks, or minor text updates. | -| `pnpm version minor` | `0.x.0` → `0.x+1.0` | New features (e.g., a new PDF theme) that are backward-compatible. | -| `pnpm version major` | `x.0.0` → `x+1.0.0` | Breaking changes (e.g., changing the data tape structure or CLI API). | +| `npm version patch` | `0.0.x` → `0.0.x+1` | Bug fixes, style tweaks, or minor text updates. | +| `npm version minor` | `0.x.0` → `0.x+1.0` | New features (e.g., a new PDF theme) that are backward-compatible. | +| `npm version major` | `x.0.0` → `x+1.0.0` | Breaking changes (e.g., changing the data tape structure or CLI API). | -> **Note:** These commands automatically update `package.json` and `pnpm-lock.yaml`, create a git commit, and generate a git tag. +> **Note:** These commands update `package.json`, create a git commit, and create an annotated git tag (`vX.Y.Z`). Use `npm version`, not `pnpm version` — pnpm rejects the `-m` flag. `%s` in the message is replaced with the new version. Example: ```bash -pnpm version patch -m "Release v%s: Fixed header spacing and updated blah theme" +npm version patch -m "Release v%s: Fixed header spacing and updated blah theme" ``` ### 2. Sync to GitHub (with tags) +```bash git push origin main --follow-tags +``` + +> `--follow-tags` only pushes **annotated** tags. If a tag was created by hand with `git tag vX.Y.Z` (lightweight), push it explicitly: `git push origin vX.Y.Z`. Verify with `git ls-remote --tags origin`. ### 3. Publish to npm @@ -134,14 +138,10 @@ pnpm login pnpm publish ``` -### 5. Finalize GitHub Release - -Go to the Releases tab on GitHub. +### 4. Create the GitHub Release -Click Draft a new release. - -Select the Tag you just pushed (e.g., v0.0.6). - -Click Generate release notes to automatically pull in your commit history. +```bash +gh release create vX.Y.Z --verify-tag --title vX.Y.Z --generate-notes --latest +``` -Click Publish release +Or in the browser: Releases → Draft a new release → select the pushed tag → Generate release notes → Publish release. diff --git a/public/baldur-terminal-cv.pdf b/public/baldur-terminal-cv.pdf index f18a71c..b4b7658 100644 Binary files a/public/baldur-terminal-cv.pdf and b/public/baldur-terminal-cv.pdf differ diff --git a/public/baldur-vintage-cv.pdf b/public/baldur-vintage-cv.pdf index 2f0d5da..d55e475 100644 Binary files a/public/baldur-vintage-cv.pdf and b/public/baldur-vintage-cv.pdf differ diff --git a/public/craig-cv.pdf b/public/craig-cv.pdf deleted file mode 100644 index 6f28f9e..0000000 Binary files a/public/craig-cv.pdf and /dev/null differ diff --git a/public/craig-terminal-cv.pdf b/public/craig-terminal-cv.pdf index ebd160d..dc09573 100644 Binary files a/public/craig-terminal-cv.pdf and b/public/craig-terminal-cv.pdf differ diff --git a/public/craig-vintage-cv.pdf b/public/craig-vintage-cv.pdf index b64b939..9256f8b 100644 Binary files a/public/craig-vintage-cv.pdf and b/public/craig-vintage-cv.pdf differ diff --git a/scripts/copy-pdfs/_tests_/copy-pdfs.test.ts b/scripts/copy-pdfs/_tests_/copy-pdfs.test.ts index 4efa71a..ea196cf 100644 --- a/scripts/copy-pdfs/_tests_/copy-pdfs.test.ts +++ b/scripts/copy-pdfs/_tests_/copy-pdfs.test.ts @@ -25,7 +25,10 @@ describe("copyStaticPdfs", () => { const writes: string[] = []; fs.mkdirSync(publicDir, { recursive: true }); - fs.writeFileSync(path.join(publicDir, "craig-cv.pdf"), "CRAIG PDF"); + fs.writeFileSync( + path.join(publicDir, "craig-terminal-cv.pdf"), + "CRAIG PDF", + ); fs.writeFileSync(path.join(publicDir, "baldur-cv.pdf"), "BALDUR PDF"); fs.writeFileSync(path.join(publicDir, "notes.txt"), "ignore me"); @@ -40,10 +43,14 @@ describe("copyStaticPdfs", () => { }, }); - expect(result.copiedFiles).toEqual(["baldur-cv.pdf", "craig-cv.pdf"]); - expect(fs.readFileSync(path.join(distPdfDir, "craig-cv.pdf"), "utf8")).toBe( - "CRAIG PDF", - ); + expect(result.copiedFiles.sort()).toEqual([ + "baldur-cv.pdf", + "craig-terminal-cv.pdf", + ]); + expect(result.removedFiles).toEqual([]); + expect( + fs.readFileSync(path.join(distPdfDir, "craig-terminal-cv.pdf"), "utf8"), + ).toBe("CRAIG PDF"); expect( fs.readFileSync(path.join(distPdfDir, "baldur-cv.pdf"), "utf8"), ).toBe("BALDUR PDF"); @@ -53,6 +60,37 @@ describe("copyStaticPdfs", () => { ]); }); + // run-cv shows a static download only when its file is packaged, so a CV + // deleted from public/ must not survive in dist/pdf/ from an earlier build. + it("removes PDFs from dist/pdf that are no longer in public", () => { + const publicDir = path.join(tempRoot, "public"); + const distPdfDir = path.join(tempRoot, "dist", "pdf"); + const writes: string[] = []; + + fs.mkdirSync(publicDir, { recursive: true }); + fs.mkdirSync(distPdfDir, { recursive: true }); + fs.writeFileSync(path.join(publicDir, "baldur-cv.pdf"), "BALDUR PDF"); + fs.writeFileSync(path.join(distPdfDir, "nobody-cv.pdf"), "STALE"); + fs.writeFileSync(path.join(distPdfDir, "keep.txt"), "not a pdf"); + + const result = copyStaticPdfs({ + publicDir, + distPdfDir, + stdout: { + write(chunk: string) { + writes.push(chunk); + return true; + }, + }, + }); + + expect(result.removedFiles).toEqual(["nobody-cv.pdf"]); + expect(fs.existsSync(path.join(distPdfDir, "nobody-cv.pdf"))).toBe(false); + expect(fs.existsSync(path.join(distPdfDir, "baldur-cv.pdf"))).toBe(true); + expect(fs.existsSync(path.join(distPdfDir, "keep.txt"))).toBe(true); + expect(writes).toContain("Removed 1 stale PDF file(s): nobody-cv.pdf\n"); + }); + it("throws when the public directory is missing", () => { const publicDir = path.join(tempRoot, "missing-public"); const distPdfDir = path.join(tempRoot, "dist", "pdf"); diff --git a/scripts/copy-pdfs/copy-pdfs.ts b/scripts/copy-pdfs/copy-pdfs.ts index 20d5b1a..d65b115 100644 --- a/scripts/copy-pdfs/copy-pdfs.ts +++ b/scripts/copy-pdfs/copy-pdfs.ts @@ -10,6 +10,7 @@ interface CopyStaticPdfsOptions { interface CopyStaticPdfsResult { copiedFiles: string[]; + removedFiles: string[]; distPdfDir: string; } @@ -21,6 +22,14 @@ export function getCopyPdfPaths() { }; } +function listPdfs(dir: string): string[] { + return fs + .readdirSync(dir, { withFileTypes: true }) + .filter((entry) => entry.isFile()) + .map((entry) => entry.name) + .filter((name) => name.toLowerCase().endsWith(".pdf")); +} + export function copyStaticPdfs({ publicDir, distPdfDir, @@ -35,16 +44,21 @@ export function copyStaticPdfs({ fs.mkdirSync(resolvedDistPdfDir, { recursive: true }); - const files = fs - .readdirSync(resolvedPublicDir, { withFileTypes: true }) - .filter((entry) => entry.isFile()) - .map((entry) => entry.name) - .filter((name) => name.toLowerCase().endsWith(".pdf")); + const files = listPdfs(resolvedPublicDir); if (files.length === 0) { throw new Error(`No PDF files found in ${resolvedPublicDir}`); } + // A PDF deleted from public/ must not linger in dist/pdf/: run-cv offers a + // static download only when its file is packaged. + const removedFiles = listPdfs(resolvedDistPdfDir).filter( + (name) => !files.includes(name), + ); + for (const file of removedFiles) { + fs.rmSync(path.join(resolvedDistPdfDir, file)); + } + for (const file of files) { const source = path.join(resolvedPublicDir, file); const target = path.join(resolvedDistPdfDir, file); @@ -54,9 +68,15 @@ export function copyStaticPdfs({ stdout.write( `Copied ${files.length} static PDF file(s) to ${resolvedDistPdfDir}\n`, ); + if (removedFiles.length > 0) { + stdout.write( + `Removed ${removedFiles.length} stale PDF file(s): ${removedFiles.join(", ")}\n`, + ); + } return { copiedFiles: files, + removedFiles, distPdfDir: resolvedDistPdfDir, }; } diff --git a/scripts/run-cv-web/PORTING.md b/scripts/run-cv-web/PORTING.md index 0507ab4..6f30eec 100644 --- a/scripts/run-cv-web/PORTING.md +++ b/scripts/run-cv-web/PORTING.md @@ -35,7 +35,7 @@ export default defineRunCvWeb({ outDir: "docs/run-cv", basePath: "/run-cv/run-cv/", virtualDist: "/run-cv/dist", - cvUrl: "/run-cv/run-cv/craig-cv.pdf", + cvUrl: "/run-cv/run-cv/baldur-cv.pdf", }); ``` diff --git a/scripts/run-cv-web/browser/gate.ts b/scripts/run-cv-web/browser/gate.ts index 5075933..992ba7e 100644 --- a/scripts/run-cv-web/browser/gate.ts +++ b/scripts/run-cv-web/browser/gate.ts @@ -12,7 +12,7 @@ import config from "virtual:run-cv-web-config"; import { hashHuman } from "../shared/human-hash.js"; import { focusTerminal } from "./setup.js"; -import { seed } from "./shims/fs.js"; +import { registerPdfs, seed } from "./shims/fs.js"; import { setSession } from "./shims/session.js"; interface Payload { @@ -44,7 +44,13 @@ async function unlock(typed: string): Promise { // takes its `if (!name)` branch and prints "Please provide a human name", // which is the wrong screen for someone who did provide one. setSession(found?.human ?? typed, pdfBase); - if (found) seed(found.files); + if (found) boot(found); +} + +/** Seeds the human's markdown and limits the download menu to PDFs they serve. */ +function boot(found: Payload): void { + seed(found.files); + registerPdfs(found.pdfs); } /** Resolves once the program may be imported. */ @@ -61,7 +67,7 @@ export async function runGate(): Promise { if (response.ok) { const found = (await response.json()) as Payload; setSession(found.human, pdfBase); - seed(found.files); + boot(found); start(); return; } diff --git a/scripts/run-cv-web/browser/shims/fs.ts b/scripts/run-cv-web/browser/shims/fs.ts index 3464e09..e5ad2b0 100644 --- a/scripts/run-cv-web/browser/shims/fs.ts +++ b/scripts/run-cv-web/browser/shims/fs.ts @@ -12,10 +12,14 @@ export type MemFs = ReturnType; * ~/Downloads, then opens the copy. The PDFs aren't bundled into the page: * report them as present and skip the copy, and the `open` shim sends the * visitor to the site's own CV instead. + * + * With `known` given, only those basenames count — so run-cv hides a static + * download (e.g. an HR/ATS CV) that this human's payload doesn't serve. */ -export function isPackagedPdf(file: unknown, pdfDir: string): boolean { +export function isPackagedPdf(file: unknown, pdfDir: string, known?: ReadonlySet): boolean { const value = String(file); - return value.startsWith(`${pdfDir}/`) && value.toLowerCase().endsWith(".pdf"); + if (!value.startsWith(`${pdfDir}/`) || !value.toLowerCase().endsWith(".pdf")) return false; + return !known || known.has(value.slice(pdfDir.length + 1).toLowerCase()); } export interface RunCvFsOptions { @@ -31,6 +35,11 @@ export interface RunCvFs { * clears), so this is additive and safe to call more than once. */ seed(files: Record): void; + /** + * Narrows "packaged" to these PDF basenames. Until called, every PDF under + * `pdfDir` counts, which is the single site-wide CV mode with no gate. + */ + registerPdfs(files: readonly string[]): void; } export function createRunCvFs(contents: Record, options: RunCvFsOptions): RunCvFs { @@ -38,30 +47,39 @@ export function createRunCvFs(contents: Record, options: RunCvFs volume.mkdirSync(options.pdfDir, { recursive: true }); volume.mkdirSync(options.downloadsDir, { recursive: true }); + let known: Set | undefined; + const packaged = (file: unknown) => isPackagedPdf(file, options.pdfDir, known); + const fs = createFsFromVolume(volume); const realExistsSync = fs.existsSync; const realCopyFileSync = fs.copyFileSync; fs.existsSync = ((file: unknown) => - isPackagedPdf(file, options.pdfDir) || realExistsSync(file as string)) as typeof fs.existsSync; + packaged(file) || realExistsSync(file as string)) as typeof fs.existsSync; fs.copyFileSync = ((source: unknown, destination: unknown, mode?: number) => - isPackagedPdf(source, options.pdfDir) + packaged(source) ? undefined : realCopyFileSync(source as string, destination as string, mode)) as typeof fs.copyFileSync; - return { fs, seed: (more) => volume.fromJSON(more) }; + return { + fs, + seed: (more) => volume.fromJSON(more), + registerPdfs: (files) => { + known = new Set([...(known ?? []), ...files.map((file) => file.toLowerCase())]); + }, + }; } // Under the gate `files` is empty and the markdown arrives at runtime, once a // visitor types a name that hashes to a payload. An unseeded volume is the // wrong-name path: run-cv finds no `introduction.md` and renders ACCESS DENIED. -const { fs, seed } = createRunCvFs(files, { +const { fs, seed, registerPdfs } = createRunCvFs(files, { pdfDir: `${config.virtualDist}/pdf`, downloadsDir: `${config.homedir}/Downloads`, }); -export { seed }; +export { registerPdfs, seed }; export default fs; // Destructured rather than wrapped in arrows: these are overloaded signatures, // and a `(...args) => fs.x(...args)` wrapper collapses them to the first diff --git a/scripts/run-cv-web/tests/browser/fs.test.ts b/scripts/run-cv-web/tests/browser/fs.test.ts index f31418d..1ac8af8 100644 --- a/scripts/run-cv-web/tests/browser/fs.test.ts +++ b/scripts/run-cv-web/tests/browser/fs.test.ts @@ -6,8 +6,8 @@ const OPTIONS = { pdfDir: PDF_DIR, downloadsDir: "/home/visitor/Downloads" }; describe("isPackagedPdf", () => { it("matches PDFs inside the packaged directory, case-insensitively", () => { - expect(isPackagedPdf(`${PDF_DIR}/craig-cv.pdf`, PDF_DIR)).toBe(true); - expect(isPackagedPdf(`${PDF_DIR}/CRAIG-CV.PDF`, PDF_DIR)).toBe(true); + expect(isPackagedPdf(`${PDF_DIR}/baldur-cv.pdf`, PDF_DIR)).toBe(true); + expect(isPackagedPdf(`${PDF_DIR}/BALDUR-CV.PDF`, PDF_DIR)).toBe(true); }); it("does not match non-PDFs in that directory", () => { @@ -15,8 +15,46 @@ describe("isPackagedPdf", () => { }); it("does not match PDFs elsewhere — a real download must still behave normally", () => { - expect(isPackagedPdf("/home/visitor/Downloads/craig-cv.pdf", PDF_DIR)).toBe(false); - expect(isPackagedPdf(`${PDF_DIR}-other/craig-cv.pdf`, PDF_DIR)).toBe(false); + expect(isPackagedPdf("/home/visitor/Downloads/baldur-cv.pdf", PDF_DIR)).toBe(false); + expect(isPackagedPdf(`${PDF_DIR}-other/baldur-cv.pdf`, PDF_DIR)).toBe(false); + }); + + it("matches only the known basenames when a list is given", () => { + const known = new Set(["craig-terminal-cv.pdf"]); + expect(isPackagedPdf(`${PDF_DIR}/craig-terminal-cv.pdf`, PDF_DIR, known)).toBe(true); + expect(isPackagedPdf(`${PDF_DIR}/CRAIG-TERMINAL-CV.PDF`, PDF_DIR, known)).toBe(true); + expect(isPackagedPdf(`${PDF_DIR}/nobody-cv.pdf`, PDF_DIR, known)).toBe(false); + }); +}); + +// run-cv offers a static download (the HR/ATS CV) only if the file exists, so a +// human whose payload doesn't serve one must see it reported as missing. +describe("registerPdfs", () => { + it("narrows packaged PDFs to the ones the payload serves", () => { + const { fs: memfs, registerPdfs } = createRunCvFs({}, OPTIONS); + registerPdfs(["craig-terminal-cv.pdf", "craig-vintage-cv.pdf"]); + expect(memfs.existsSync(`${PDF_DIR}/craig-terminal-cv.pdf`)).toBe(true); + expect(memfs.existsSync(`${PDF_DIR}/nobody-cv.pdf`)).toBe(false); + }); + + it("keeps a static PDF the payload does serve", () => { + const { fs: memfs, registerPdfs } = createRunCvFs({}, OPTIONS); + registerPdfs(["baldur-cv.pdf", "baldur-terminal-cv.pdf"]); + expect(memfs.existsSync(`${PDF_DIR}/baldur-cv.pdf`)).toBe(true); + }); + + it("treats an empty list as no packaged PDFs, not as any PDF", () => { + const { fs: memfs, registerPdfs } = createRunCvFs({}, OPTIONS); + registerPdfs([]); + expect(memfs.existsSync(`${PDF_DIR}/baldur-cv.pdf`)).toBe(false); + }); + + it("still skips the copy for a registered PDF", () => { + const { fs: memfs, registerPdfs } = createRunCvFs({}, OPTIONS); + registerPdfs(["baldur-cv.pdf"]); + expect(() => + memfs.copyFileSync(`${PDF_DIR}/baldur-cv.pdf`, "/home/visitor/Downloads/baldur-cv.pdf"), + ).not.toThrow(); }); }); @@ -68,16 +106,16 @@ describe("createRunCvFs", () => { // PDFs are not bundled into the page, so the check has to pass anyway. it("reports packaged PDFs as present without them existing", () => { const { fs: memfs } = createRunCvFs({}, OPTIONS); - expect(memfs.existsSync(`${PDF_DIR}/craig-cv.pdf`)).toBe(true); + expect(memfs.existsSync(`${PDF_DIR}/baldur-cv.pdf`)).toBe(true); expect(memfs.existsSync(`${PDF_DIR}/missing.txt`)).toBe(false); }); it("skips the copy of a packaged PDF instead of failing on the missing source", () => { const { fs: memfs } = createRunCvFs({}, OPTIONS); expect(() => - memfs.copyFileSync(`${PDF_DIR}/craig-cv.pdf`, "/home/visitor/Downloads/craig-cv.pdf"), + memfs.copyFileSync(`${PDF_DIR}/baldur-cv.pdf`, "/home/visitor/Downloads/baldur-cv.pdf"), ).not.toThrow(); - expect(memfs.existsSync("/home/visitor/Downloads/craig-cv.pdf")).toBe(false); + expect(memfs.existsSync("/home/visitor/Downloads/baldur-cv.pdf")).toBe(false); }); it("still copies ordinary files", () => { @@ -101,6 +139,6 @@ describe("the default instance", () => { }); it("re-exports the patched functions, not the originals", () => { - expect(fs.existsSync(`${PDF_DIR}/craig-cv.pdf`)).toBe(true); + expect(fs.existsSync(`${PDF_DIR}/craig-terminal-cv.pdf`)).toBe(true); }); }); diff --git a/scripts/run-cv-web/tests/browser/gate.test.ts b/scripts/run-cv-web/tests/browser/gate.test.ts index 178879b..8b4e641 100644 --- a/scripts/run-cv-web/tests/browser/gate.test.ts +++ b/scripts/run-cv-web/tests/browser/gate.test.ts @@ -10,8 +10,10 @@ vi.mock("../../browser/setup.js", () => ({ })); const seed = vi.fn(); +const registerPdfs = vi.fn(); vi.mock("../../browser/shims/fs.js", () => ({ seed: (files: unknown) => seed(files), + registerPdfs: (files: unknown) => registerPdfs(files), })); const setSession = vi.fn(); @@ -25,7 +27,7 @@ const SALT = config.hashSalt; const PAYLOAD = { human: "craig", files: { "/run-cv/dist/humans/craig/introduction.md": "# HI" }, - pdfs: [], + pdfs: ["craig-terminal-cv.pdf", "craig-vintage-cv.pdf"], }; function renderGate(): HTMLInputElement { @@ -85,6 +87,18 @@ describe("runGate", () => { expect(document.body.dataset.state).toBe("running"); }); + // run-cv hides a static download (the HR/ATS CV) whose PDF isn't packaged, + // so the filesystem has to know exactly which PDFs this payload serves. + it("registers the payload's PDFs so missing downloads stay hidden", async () => { + serveOnly("craig"); + const input = renderGate(); + const running = runGate(); + submit(input, "craig"); + await running; + + expect(registerPdfs).toHaveBeenCalledWith(PAYLOAD.pdfs); + }); + // The wrong-name path. run-cv renders its own ACCESS DENIED from an empty // filesystem, which is why nothing is seeded and the boot still happens. it("boots with nothing seeded when the name is unknown", async () => { @@ -95,6 +109,7 @@ describe("runGate", () => { await running; expect(seed).not.toHaveBeenCalled(); + expect(registerPdfs).not.toHaveBeenCalled(); expect(document.body.dataset.state).toBe("running"); }); @@ -158,6 +173,7 @@ describe("runGate", () => { await runGate(); expect(seed).toHaveBeenCalledWith(PAYLOAD.files); + expect(registerPdfs).toHaveBeenCalledWith(PAYLOAD.pdfs); expect(setSession).toHaveBeenCalledWith("craig", `${config.payloadBase}${hash}/pdf/`); }); diff --git a/scripts/run-cv-web/tests/browser/open.test.ts b/scripts/run-cv-web/tests/browser/open.test.ts index 27449db..5103348 100644 --- a/scripts/run-cv-web/tests/browser/open.test.ts +++ b/scripts/run-cv-web/tests/browser/open.test.ts @@ -1,14 +1,17 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import open, { classifyTarget } from "../../browser/shims/open.js"; -const CV = "/craig-curtis-cv.pdf"; +const CV = "/site-cv.pdf"; afterEach(() => vi.restoreAllMocks()); describe("classifyTarget", () => { it("sends any PDF to the site's own CV rather than the packaged one", () => { - expect(classifyTarget("/run-cv/dist/pdf/craig-cv.pdf")).toEqual({ kind: "pdf", url: CV }); - expect(classifyTarget("/home/visitor/Downloads/CRAIG-CV.PDF")).toEqual({ + expect(classifyTarget("/run-cv/dist/pdf/craig-terminal-cv.pdf")).toEqual({ + kind: "pdf", + url: CV, + }); + expect(classifyTarget("/home/visitor/Downloads/CRAIG-TERMINAL-CV.PDF")).toEqual({ kind: "pdf", url: CV, }); @@ -64,7 +67,7 @@ describe("open", () => { it("redirects PDFs to the configured CV", async () => { const windowOpen = vi.spyOn(window, "open").mockReturnValue(null); - await open("/run-cv/dist/pdf/craig-cv.pdf"); + await open("/run-cv/dist/pdf/craig-terminal-cv.pdf"); expect(windowOpen).toHaveBeenCalledWith(CV, "_blank", "noopener,noreferrer"); }); diff --git a/scripts/run-cv-web/tests/fixtures/virtual-config.ts b/scripts/run-cv-web/tests/fixtures/virtual-config.ts index f65a4f1..efa6eed 100644 --- a/scripts/run-cv-web/tests/fixtures/virtual-config.ts +++ b/scripts/run-cv-web/tests/fixtures/virtual-config.ts @@ -3,7 +3,7 @@ import { DEFAULT_PROMPT, DEFAULT_TERMINAL } from "../../src/config.js"; /** Stands in for `virtual:run-cv-web-config` under test. */ const config: BrowserConfig = { - cvUrl: "/craig-curtis-cv.pdf", + cvUrl: "/site-cv.pdf", homedir: "/home/visitor", virtualDist: "/run-cv/dist", humans: ["craig"], diff --git a/scripts/run-cv-web/tests/integration/build.test.ts b/scripts/run-cv-web/tests/integration/build.test.ts index 99f92a5..0373808 100644 --- a/scripts/run-cv-web/tests/integration/build.test.ts +++ b/scripts/run-cv-web/tests/integration/build.test.ts @@ -149,12 +149,17 @@ describe("anonymity", () => { for (const human of config.humans) { const hash = await hashHuman(human, config.hashSalt); const pdfs = await readdir(path.join(out, config.payloadDir, hash, "pdf")); - // The ATS CV plus one per theme in scripts/pdf-gen/styles/themes. - expect(pdfs.sort(), human).toEqual([ - `${human}-cv.pdf`, - `${human}-terminal-cv.pdf`, - `${human}-vintage-cv.pdf`, - ]); + // One per theme in scripts/pdf-gen/styles/themes, plus the HR/ATS CV + // only for a human who has one in the PDF source directory. + const source = await readdir(path.join(REPO, config.pdfDir)); + const ats = `${human}-cv.pdf`; + expect(pdfs.sort(), human).toEqual( + [ + ...(source.includes(ats) ? [ats] : []), + `${human}-terminal-cv.pdf`, + `${human}-vintage-cv.pdf`, + ].sort(), + ); } }); }); diff --git a/scripts/run-cv-web/tests/payloads.test.ts b/scripts/run-cv-web/tests/payloads.test.ts index ab314e0..7214cda 100644 --- a/scripts/run-cv-web/tests/payloads.test.ts +++ b/scripts/run-cv-web/tests/payloads.test.ts @@ -13,13 +13,13 @@ describe("planPayloads", () => { it("pairs each human with the hash their payload is served from", async () => { const plans = await planPayloads( new Map([["craig", files("craig")]]), - new Map([["craig", ["craig-cv.pdf"]]]), + new Map([["craig", ["craig-terminal-cv.pdf"]]]), SALT, ); expect(plans).toHaveLength(1); expect(plans[0]?.hash).toBe(await hashHuman("craig", SALT)); expect(plans[0]?.human).toBe("craig"); - expect(plans[0]?.pdfs).toEqual(["craig-cv.pdf"]); + expect(plans[0]?.pdfs).toEqual(["craig-terminal-cv.pdf"]); }); it("keeps each human's files to themselves", async () => { @@ -58,29 +58,37 @@ describe("planPayloads", () => { }); describe("pdfsFor", () => { + // Baldur has an HR/ATS CV; Craig has only the themed ones. const available = [ - "craig-cv.pdf", "craig-terminal-cv.pdf", "craig-vintage-cv.pdf", "baldur-cv.pdf", + "baldur-terminal-cv.pdf", + "baldur-vintage-cv.pdf", "notes.txt", ]; it("picks every PDF run-cv's download menu can ask a human for", () => { // menu-actions builds `${human}-${theme}-cv.pdf`, plus the named ATS file. - expect(pdfsFor("craig", available)).toEqual([ - "craig-cv.pdf", - "craig-terminal-cv.pdf", - "craig-vintage-cv.pdf", + expect(pdfsFor("baldur", available)).toEqual([ + "baldur-cv.pdf", + "baldur-terminal-cv.pdf", + "baldur-vintage-cv.pdf", ]); }); + // The payload's list is what the browser uses to hide the ATS download, so a + // human without that file must not be given one. + it("leaves out an ATS CV the human doesn't have", () => { + expect(pdfsFor("craig", available)).toEqual(["craig-terminal-cv.pdf", "craig-vintage-cv.pdf"]); + }); + it("never hands one human another's PDFs", () => { - expect(pdfsFor("baldur", available)).toEqual(["baldur-cv.pdf"]); + expect(pdfsFor("craig", available).some((file) => file.startsWith("baldur"))).toBe(false); }); it("matches the prefix case-insensitively, as run-cv lowercases the name", () => { - expect(pdfsFor("CRAIG", ["craig-cv.pdf"])).toEqual(["craig-cv.pdf"]); + expect(pdfsFor("CRAIG", ["craig-terminal-cv.pdf"])).toEqual(["craig-terminal-cv.pdf"]); }); it("does not match a human whose name is a prefix of another", () => { diff --git a/src/App.tsx b/src/App.tsx index 5353e27..eaf52b0 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -19,6 +19,7 @@ import { theme } from "./styles/theme"; import type { HighlightedItem, HumanManifest, Page } from "./types"; import { executeMenuAction } from "./utils/menu-action-executor"; import { + filterAvailableMenuItems, findMenuItemByValue, findMenuItemIndexByValue, getMenuSelectItems, @@ -63,6 +64,20 @@ function getPdfRoot() { const PDF_ROOT = getPdfRoot(); +function pdfExists(filename: string): boolean { + return fs.existsSync(path.join(PDF_ROOT, filename)); +} + +/** Hides static-PDF menu entries whose file isn't packaged for this human. */ +function withAvailablePdfs(page: T): T { + if (!page.menu) return page; + return { ...page, menu: filterAvailableMenuItems(page.menu, pdfExists) }; +} + +async function loadAvailablePage(baseDir: string, file: string): Promise { + return withAvailablePdfs(await getPage(baseDir, file)); +} + export function App({ name }: AppProps) { const { exit } = useApp(); const [human, setHuman] = useState(null); @@ -96,7 +111,7 @@ export function App({ name }: AppProps) { } try { - const data = await getHuman(name); + const data = withAvailablePdfs(await getHuman(name)); setHuman(data); setHistory([data]); } catch { @@ -189,7 +204,7 @@ export function App({ name }: AppProps) { action, currentDir: currentPage.dir, pdfRoot: PDF_ROOT, - loadPage: getPage, + loadPage: loadAvailablePage, onNavigate: (nextPage) => setHistory((prev) => [...prev, nextPage]), onError: (message) => setError(message), }); diff --git a/src/__tests__/menu-action-executor.test.ts b/src/__tests__/menu-action-executor.test.ts index a80b3d2..24b0b09 100644 --- a/src/__tests__/menu-action-executor.test.ts +++ b/src/__tests__/menu-action-executor.test.ts @@ -82,14 +82,14 @@ describe("menu action executor", () => { const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "menu-exec-")); const pdfRoot = path.join(tempRoot, "dist", "pdf"); fs.mkdirSync(pdfRoot, { recursive: true }); - fs.writeFileSync(path.join(pdfRoot, "craig-cv.pdf"), "PDF"); + fs.writeFileSync(path.join(pdfRoot, "baldur-cv.pdf"), "PDF"); const copySpy = vi.spyOn(fs, "copyFileSync").mockImplementation(() => {}); const { errors } = await runExecutor( { type: "download-pdf", - filename: "craig-cv.pdf", + filename: "baldur-cv.pdf", }, { pdfRoot }, ); @@ -100,4 +100,22 @@ describe("menu action executor", () => { copySpy.mockRestore(); fs.rmSync(tempRoot, { recursive: true, force: true }); }); + + it("reports a PDF that is not packaged instead of copying", async () => { + const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "menu-exec-")); + const copySpy = vi.spyOn(fs, "copyFileSync").mockImplementation(() => {}); + + const { errors } = await runExecutor( + { type: "download-pdf", filename: "nobody-cv.pdf" }, + { pdfRoot: tempRoot }, + ); + + expect(errors).toEqual([ + "ARCHIVE ERROR: Resource nobody-cv.pdf not found in package.", + ]); + expect(copySpy).not.toHaveBeenCalled(); + + copySpy.mockRestore(); + fs.rmSync(tempRoot, { recursive: true, force: true }); + }); }); diff --git a/src/__tests__/menu-actions.test.ts b/src/__tests__/menu-actions.test.ts index fad7858..9c6ed54 100644 --- a/src/__tests__/menu-actions.test.ts +++ b/src/__tests__/menu-actions.test.ts @@ -1,5 +1,6 @@ import type { MenuItem } from "../types"; import { + filterAvailableMenuItems, findMenuItemByValue, findMenuItemIndexByValue, getMenuItemValue, @@ -22,13 +23,66 @@ describe("menu actions", () => { it("resolves a static PDF file menu item to download action", () => { const action = resolveMenuAction( - { label: "ATS", file: "craig-cv.pdf" }, - "Craig", + { label: "ATS", file: "baldur-cv.pdf" }, + "Baldur", ); expect(action).toEqual({ type: "download-pdf", - filename: "craig-cv.pdf", + filename: "baldur-cv.pdf", + }); + }); + + describe("filterAvailableMenuItems", () => { + const menu: MenuItem[] = [ + { label: "Terminal", theme: "terminal" }, + { label: "Vintage", theme: "vintage" }, + { label: "ATS", file: "baldur-cv.pdf" }, + ]; + + it("keeps a static PDF entry when its file is packaged", () => { + const available = filterAvailableMenuItems( + menu, + (file) => file === "baldur-cv.pdf", + ); + + expect(available.map((item) => item.label)).toEqual([ + "Terminal", + "Vintage", + "ATS", + ]); + }); + + it("drops a static PDF entry when its file is missing", () => { + const pdfExists = vi.fn(() => false); + const available = filterAvailableMenuItems(menu, pdfExists); + + expect(available.map((item) => item.label)).toEqual([ + "Terminal", + "Vintage", + ]); + expect(pdfExists).toHaveBeenCalledWith("baldur-cv.pdf"); + }); + + it("never checks non-PDF entries, including themed and markdown ones", () => { + const pdfExists = vi.fn(() => false); + const mixed: MenuItem[] = [ + { label: "Career", file: "career/index.md" }, + { label: "Terminal", theme: "terminal" }, + { label: "Worktree", url: "https://github.com/northguild/worktree" }, + ]; + + expect(filterAvailableMenuItems(mixed, pdfExists)).toEqual(mixed); + expect(pdfExists).not.toHaveBeenCalled(); + }); + + it("treats the .pdf extension case-insensitively", () => { + expect( + filterAvailableMenuItems( + [{ label: "ATS", file: "NOBODY-CV.PDF" }], + () => false, + ), + ).toEqual([]); }); }); diff --git a/src/humans/craig/download-cv/index.md b/src/humans/craig/download-cv/index.md index 3857bc4..c754904 100644 --- a/src/humans/craig/download-cv/index.md +++ b/src/humans/craig/download-cv/index.md @@ -5,8 +5,6 @@ menu: theme: "terminal" - label: "Download: Vintage Archive (Sepia)" theme: "vintage" - - label: "Download: HR/ATS-friendly CV" - file: "craig-cv.pdf" --- # DOWNLOAD MISSION LOGS diff --git a/src/utils/menu-actions.ts b/src/utils/menu-actions.ts index 9812e5c..ada42b5 100644 --- a/src/utils/menu-actions.ts +++ b/src/utils/menu-actions.ts @@ -15,6 +15,22 @@ export function getMenuItemValue(item: MenuItem): string | undefined { return item.file ?? item.theme ?? item.url ?? item.link; } +function isStaticPdfItem(item: MenuItem): item is MenuItem & { file: string } { + return !!item.file?.toLowerCase().endsWith(".pdf"); +} + +/** + * Drops static-PDF entries (e.g. an HR/ATS CV) whose file isn't packaged, so a + * human without one never sees a download that can only fail. Themed PDFs are + * generated for every human and are left alone. + */ +export function filterAvailableMenuItems( + menu: MenuItem[], + pdfExists: (filename: string) => boolean, +): MenuItem[] { + return menu.filter((item) => !isStaticPdfItem(item) || pdfExists(item.file)); +} + export function getMenuSelectItems(menu: MenuItem[]): MenuSelectItem[] { return menu .map((item) => { @@ -68,7 +84,7 @@ export function resolveMenuAction( }; } - if (menuItem.file?.toLowerCase().endsWith(".pdf")) { + if (isStaticPdfItem(menuItem)) { return { type: "download-pdf", filename: menuItem.file,