Skip to content
File

Blob: tests/worker/lib/site-public-url.test.ts

typescript57 lines
1import { describe, expect, it } from "vitest";
2import { buildSitePagePath, buildSitePublicUrl } from "@/worker/lib/site-public-url";
3 
4const baseSite = { slug: "acme", published_at: "2025-01-01T00:00:00Z", home_page_id: null };
5 
6describe("buildSitePublicUrl", () => {
7 it("returns null when the site is not published", () => {
8 expect(
9 buildSitePublicUrl({ ...baseSite, published_at: null }, "bland.site", "P1", "Hello", new URL("https://app/")),
10 ).toBeNull();
11 });
12 
13 it("returns null when no base domain is configured", () => {
14 expect(buildSitePublicUrl(baseSite, null, "P1", "Hello", new URL("https://app/"))).toBeNull();
15 });
16 
17 it("uses https + no port for production domains", () => {
18 expect(buildSitePublicUrl(baseSite, "bland.site", "P1", "Hello World", new URL("https://app.example/"))).toBe(
19 "https://acme.bland.site/hello-world-p1",
20 );
21 });
22 
23 it("normalizes the home page to '/' for production", () => {
24 expect(
25 buildSitePublicUrl({ ...baseSite, home_page_id: "P1" }, "bland.site", "P1", "Home", new URL("https://app/")),
26 ).toBe("https://acme.bland.site/");
27 });
28 
29 it("falls back to 'untitled' when the page title slugifies to empty", () => {
30 expect(buildSitePublicUrl(baseSite, "bland.site", "P1", " ", new URL("https://app/"))).toBe(
31 "https://acme.bland.site/untitled-p1",
32 );
33 });
34 
35 it("derives protocol + port from the request when the base domain is *.localhost", () => {
36 expect(
37 buildSitePublicUrl(baseSite, "bland.localhost", "P1", "Hi There", new URL("http://app.localhost:5173/x")),
38 ).toBe("http://acme.bland.localhost:5173/hi-there-p1");
39 });
40 
41 it("treats the bare 'localhost' base domain as local-dev too", () => {
42 expect(buildSitePublicUrl(baseSite, "localhost", "P1", "Hi", new URL("http://app.localhost:5173/"))).toBe(
43 "http://acme.localhost:5173/hi-p1",
44 );
45 });
46 
47 it("omits the port when the request URL itself has none", () => {
48 expect(buildSitePublicUrl(baseSite, "bland.localhost", "P1", "Hi", new URL("http://app.localhost/"))).toBe(
49 "http://acme.bland.localhost/hi-p1",
50 );
51 });
52 
53 it("builds visible page paths with lowercase page IDs", () => {
54 expect(buildSitePagePath("ABC123", "Hello World")).toBe("/hello-world-abc123");
55 });
56});