// Prospect previews (docs/architecture/prospect-previews.md): the rules that // keep a private draft private and the widget on every page of it. // // Pure — no database, no server. The route composes exactly these pieces. import path from "path"; import { isPreviewAddress, previewHostFor, previewHostUrl } from "../../src/lib/previews/address"; import { PREVIEW_BUILD_RULES, previewBuildPrompt } from "../../src/config/preview-brief"; import { PREVIEWS, previewEntryPath } from "../../src/config/previews"; import { projectChannelLock } from "../../src/lib/execution-access"; import { configuredPreviewCode, createPreviewAccessCookie, previewCodeMatches, verifyPreviewAccessCookie, } from "../../src/lib/previews/access"; import { previewGatePage } from "../../src/lib/previews/gate-page"; import { isPreviewDir, isPreviewSlug, previewDirFor, previewsRoot, slugifyPreviewName, } from "../../src/lib/previews/root"; import { injectPreviewWidget, isHtmlFile, previewContentType, resolvePreviewFile, } from "../../src/lib/previews/serve"; // Every function under test reads the environment when called, not when // imported, so setting it here (after the hoisted imports) is in time. process.env.LOKI_PREVIEWS_ROOT = "/srv/previews"; process.env.AUTH_SECRET = "test-secret-for-previews-0123456789"; process.env.LOKI_PREVIEW_CODE = "aaaaaa11"; let pass = 0; let fail = 0; const ok = (cond: boolean, name: string) => { if (cond) pass++; else fail++; console.log(`${cond ? "✓" : "✗"} ${name}`); }; // ---- where previews live ---- ok(previewsRoot() === "/srv/previews", "the root comes from LOKI_PREVIEWS_ROOT"); ok(slugifyPreviewName("Francesco Tarallo") === "francesco-tarallo", "a name becomes a folder"); ok(slugifyPreviewName("Café Zürich & Söhne") === "cafe-zurich-sohne", "accents fold to ASCII"); ok(slugifyPreviewName("Straße 7") === "strasse-7", "ß becomes ss"); ok(slugifyPreviewName("!!!") === "preview", "a name with no letters still gets a folder"); ok( slugifyPreviewName("x".repeat(200)).length <= PREVIEWS.maxSlugLength, "a long name is cut to the slug limit", ); ok(isPreviewSlug("francesco-tarallo"), "a slug is accepted"); for (const bad of ["", "-a", "a-", "A", "a/b", "..", ".git", "a b"]) { ok(!isPreviewSlug(bad), `"${bad}" is not a slug`); } ok( previewDirFor("francesco-tarallo") === "/srv/previews/francesco-tarallo", "a slug maps to its folder", ); ok(previewDirFor("../etc") === null, "a bad slug maps to nothing"); ok(isPreviewDir("/srv/previews/francesco-tarallo"), "a preview folder is recognised"); ok(!isPreviewDir("/srv/previews"), "the root itself is not a preview"); ok(!isPreviewDir("/srv/previews/a/b"), "a subfolder of a preview is not a preview"); ok(!isPreviewDir("/srv/other/x"), "a folder elsewhere is not a preview"); // ---- previews build on the box, never on a laptop ---- ok( projectChannelLock({ dirPath: "/srv/previews/francesco-tarallo", gitUrl: null }) === "cloud", "a preview folder with no repository is locked to the cloud builder", ); ok( projectChannelLock({ dirPath: "/home/me/code/thing", gitUrl: null }) === "local", "an ordinary folder with no repository is still locked to the laptop", ); // ---- which file a URL may name ---- const DIR = "/srv/previews/francesco-tarallo"; ok(resolvePreviewFile(DIR, []) === path.join(DIR, "index.html"), "no path opens index.html"); ok(resolvePreviewFile(DIR, ["img", "a.jpg"]) === path.join(DIR, "img/a.jpg"), "a nested file"); for (const bad of [ [".git", "config"], ["img", ".env"], [".."], ["..", "other", "index.html"], ["a\\b"], ["a\0b"], [""], ["img", ""], ]) { ok(resolvePreviewFile(DIR, bad) === null, `refused: ${JSON.stringify(bad)}`); } ok(previewContentType("x/a.MP3") === "audio/mpeg", "types by extension, any case"); ok(previewContentType("x/a.php") === null, "an unknown type is not served"); ok(isHtmlFile("index.html") && !isHtmlFile("a.css"), "HTML is recognised"); ok( previewEntryPath("francesco-tarallo") === "/p/francesco-tarallo/index.html", "the entry address", ); // ---- the widget goes on every page, once ---- const page = "