From b24f2a5e7bbe8c502bd1db041b96f90d2438a76f Mon Sep 17 00:00:00 2001 From: saberzero1 Date: Tue, 26 May 2026 12:56:32 +0200 Subject: [PATCH] test: add regression tests for baseurl in serve mode --- quartz/components/renderPage.test.ts | 61 +++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/quartz/components/renderPage.test.ts b/quartz/components/renderPage.test.ts index 27c9abd..bce6eea 100644 --- a/quartz/components/renderPage.test.ts +++ b/quartz/components/renderPage.test.ts @@ -1,10 +1,11 @@ import test, { describe } from "node:test" import assert from "node:assert" -import { renderTranscludes } from "./renderPage" +import { renderTranscludes, pageResources } from "./renderPage" import { Root, Element } from "hast" import { FullSlug } from "../util/path" import { GlobalConfiguration } from "../cfg" import { QuartzComponentProps } from "./types" +import { StaticResources } from "../util/resources" function makeTranscludeBlockquote(targetSlug: string, block?: string): Element { return { @@ -266,3 +267,61 @@ describe("renderTranscludes", () => { assert.ok(text.includes("Circular transclusion"), "self-reference should be blocked") }) }) + +describe("pageResources", () => { + const emptyResources: StaticResources = { + css: [], + js: [], + additionalHead: [], + } + + test("uses baseDir prefix for resource paths in production mode", () => { + const result = pageResources("/quartz" as FullSlug, emptyResources) + assert.ok( + result.css[0].content.startsWith("/quartz/"), + `expected css path to start with /quartz/, got: ${result.css[0].content}`, + ) + const externalJs = result.js.find((j) => j.contentType === "external" && "src" in j) + assert.ok(externalJs && "src" in externalJs) + assert.ok( + externalJs.src.startsWith("/quartz/"), + `expected js src to start with /quartz/, got: ${externalJs.src}`, + ) + }) + + test("omits subpath prefix when baseDir is empty (serve mode)", () => { + const result = pageResources("." as FullSlug, emptyResources) + for (const css of result.css) { + assert.ok( + !css.content.includes("/quartz/"), + `css path should not contain /quartz/, got: ${css.content}`, + ) + } + for (const js of result.js) { + if (js.contentType === "external" && "src" in js) { + assert.ok( + !js.src.includes("/quartz/"), + `js src should not contain /quartz/, got: ${js.src}`, + ) + } + } + }) + + test("contentIndex path reflects baseDir", () => { + const withPrefix = pageResources("/quartz" as FullSlug, emptyResources) + const inlineJs = withPrefix.js.find((j) => j.contentType === "inline" && "script" in j) + assert.ok(inlineJs && "script" in inlineJs) + assert.ok( + inlineJs.script.includes("/quartz/static/contentIndex.json"), + `expected contentIndex fetch to include /quartz/ prefix, got: ${inlineJs.script}`, + ) + + const withoutPrefix = pageResources("." as FullSlug, emptyResources) + const inlineJsServe = withoutPrefix.js.find((j) => j.contentType === "inline" && "script" in j) + assert.ok(inlineJsServe && "script" in inlineJsServe) + assert.ok( + !inlineJsServe.script.includes("/quartz/static/contentIndex.json"), + `expected contentIndex fetch without /quartz/ prefix in serve mode, got: ${inlineJsServe.script}`, + ) + }) +})