Something went wrong. Try again.
My Personal Site (also at https://sammyshear.com) sshear.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230import assert from "node:assert/strict";import { test } from "node:test";import { execFile } from "node:child_process";import { promisify } from "node:util";import { createServer } from "node:http";import { mkdtemp, readFile, rm, writeFile, symlink } from "node:fs/promises";import { tmpdir } from "node:os";import { pathToFileURL } from "node:url";import { publicCVSchema, sectionsFor } from "../src/lib/cv-schema";import { prepareCV } from "./prepare-cv";
const fixture = { schema_version: 1, profile: { name: "Test Person" }, sections: [ { id: "new-section", title: "Future section", type: "entries", entries: [ { id: "cv", title: "CV only", web: "cv" }, { id: "home", title: "Home only", web: "home" }, { id: "both", title: "Both pages", web: "both" }, { id: "none", title: "Hidden from pages", web: "none" } ] }, { id: "skills", title: "Skills", type: "list", entries: [{ id: "cv", title: "Go", web: "home" }] }, { id: "writings", title: "Writings", type: "bibliography", entries: [ { id: "draft", title: "Unpublished draft", entry_type: "unpublished", web: "cv", authors: [{ given: "", family: "Institute", suffix: "" }] } ] }, { id: "empty", title: "Empty", type: "entries", entries: [] } ]};
test("routing preserves order and supports future sections, empty sections and unpublished works", () => { const cv = publicCVSchema.parse(fixture); const routed = sectionsFor(cv, "cv"); assert.deepEqual( routed.map((s) => s.id), ["new-section", "writings"] ); assert.deepEqual( routed[0].entries.map((e) => e.id), ["cv", "both"] ); assert.deepEqual( sectionsFor(cv, "home").map((s) => s.entries.map((e) => e.id)), [["home", "both"], ["cv"]] ); assert.equal(cv.sections[0].entries.length, 4);});
test("runtime validation rejects unsupported versions, wrong fields, unsafe links and duplicate identities", () => { for (const bad of [ { ...fixture, schema_version: 2 }, { ...fixture, profile: { name: 123 } }, { ...fixture, sections: [ { id: "x", title: "X", type: "entries", entries: [ { id: "x", title: "X", web: "none", url: "javascript:alert(1)" } ] } ] }, { ...fixture, sections: [fixture.sections[0], fixture.sections[0]] }, { ...fixture, sections: [ { ...fixture.sections[0], entries: [ fixture.sections[0].entries[0], fixture.sections[0].entries[0] ] } ] } ]) assert.equal(publicCVSchema.safeParse(bad).success, false);});
test("preparation retries transient failures and preserves prior files on invalid downloads", async () => { const dir = await mkdtemp(`${tmpdir()}/website-cv-`); const output = pathToFileURL(`${dir}/`); let mode = "valid"; let jsonRequests = 0; let pdfRequests = 0; const server = createServer((req, res) => { if (req.url === "/cv.json") { jsonRequests++; if (mode === "retry" && jsonRequests === 1) { res.writeHead(503).end(); return; } if (mode === "404") { res.writeHead(404).end(); return; } res.end(mode === "bad-json" ? "{}" : JSON.stringify(fixture)); } else { pdfRequests++; res.end( mode === "bad-pdf" ? "<html>Error</html>" : "%PDF-1.7\nfixture" ); } }); await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve) ); const address = server.address(); assert(address && typeof address !== "string"); const base = `http://127.0.0.1:${address.port}`; try { await prepareCV(base, output); assert.equal(jsonRequests, 1); assert.equal(pdfRequests, 1); const json = await readFile(new URL(".generated/cv.json", output)); const pdf = await readFile(new URL("public/cv/public.pdf", output)); for (mode of ["bad-json", "bad-pdf", "404"]) { jsonRequests = 0; await assert.rejects(prepareCV(base, output)); assert.equal(jsonRequests, 1); assert.deepEqual( await readFile(new URL(".generated/cv.json", output)), json ); assert.deepEqual( await readFile(new URL("public/cv/public.pdf", output)), pdf ); } mode = "retry"; jsonRequests = 0; await prepareCV(base, output); assert.equal(jsonRequests, 2); if (process.env.CV_TEST_BUILD === "1") { // Prerendered server modules resolve external dependencies from the output directory. await symlink( new URL("../node_modules", import.meta.url), `${dir}/node_modules`, "dir" ); const generatedPaths = [ ".generated/cv.json", "public/cv/public.pdf" ]; const previous = await Promise.all( generatedPaths.map(async (path) => { try { return await readFile(path); } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined; throw error; } }) ); try { const result = await promisify(execFile)( "pnpm", ["build", "--outDir", `${dir}/build`], { env: { ...process.env, CV_BASE_URL: base }, timeout: 120_000, maxBuffer: 4 * 1024 * 1024 } ); console.log(result.stdout); const cvHTML = await readFile( `${dir}/build/client/cv/index.html`, "utf8" ); const homeHTML = await readFile( `${dir}/build/client/index.html`, "utf8" ); assert(cvHTML.includes("CV only")); assert(cvHTML.includes("Unpublished draft")); assert(!cvHTML.includes("Home only")); assert(!cvHTML.includes("Hidden from pages")); assert(homeHTML.includes("Home only")); assert(homeHTML.includes("Research Interests")); assert(!homeHTML.includes("CV only")); assert(!homeHTML.includes("Hidden from pages")); assert(cvHTML.includes('href="/cv/public.pdf"')); assert.deepEqual( await readFile(`${dir}/build/client/cv/public.pdf`), pdf ); } finally { for (const [i, path] of generatedPaths.entries()) { if (previous[i]) await writeFile(path, previous[i]); else await rm(path, { force: true }); } } } } finally { await new Promise<void>((resolve, reject) => server.close((err) => (err ? reject(err) : resolve())) ); await rm(dir, { recursive: true, force: true }); }});