| 1 | import { existsSync, readFileSync } from "node:fs"; |
| 2 | import { createHash } from "node:crypto"; |
| 3 | import { createElement } from "react"; |
| 4 | import { renderToStaticMarkup } from "react-dom/server"; |
| 5 | import { describe, expect, it, vi } from "vitest"; |
| 6 | import { redirect } from "next/navigation"; |
| 7 | import PricingPage from "../app/[locale]/pricing/page"; |
| 8 | import { footerLegalLinks } from "./i18n/links"; |
| 9 | import { getChrome } from "./i18n/dictionaries"; |
| 10 | import { formatLegalDocumentStatus, LEGAL_DOCUMENTS, PrivacyContent, TermsContent, WEBSITE_USAGE_DISCLOSURE } from "./legal-copy"; |
| 11 | import { PrivacyContent as PinnedPrivacyContent, TermsContent as PinnedTermsContent } from "../vendor/legal-documents/legal-content"; |
| 12 | import { USAGE_COUNTING_COPY } from "./content/usage-counting"; |
| 13 | import TermsPage from "../app/[locale]/legal/terms/page"; |
| 14 | import PrivacyPage from "../app/[locale]/legal/privacy/page"; |
| 15 | |
| 16 | const webRoot = new URL("../", import.meta.url); |
| 17 | vi.mock("next/navigation", () => ({ redirect: vi.fn() })); |
| 18 | |
| 19 | describe("public legal routes and retired pricing", () => { |
| 20 | it("ships real pages for the URLs that used to 404", () => { |
| 21 | for (const path of [ |
| 22 | "app/[locale]/legal/terms/page.tsx", |
| 23 | "app/[locale]/legal/privacy/page.tsx", |
| 24 | "app/[locale]/privacy/page.tsx", |
| 25 | "app/[locale]/terms/page.tsx", |
| 26 | ]) { |
| 27 | expect(existsSync(new URL(path, webRoot)), path).toBe(true); |
| 28 | } |
| 29 | }); |
| 30 | |
| 31 | it("aliases /privacy and /terms onto the legal paths instead of inventing a second policy", () => { |
| 32 | expect(footerLegalLinks("en", getChrome("en")).map((l) => l.href)).toEqual([ |
| 33 | "/en/legal/terms", |
| 34 | "/en/legal/privacy", |
| 35 | ]); |
| 36 | for (const id of ["terms", "privacy"] as const) { |
| 37 | expect(LEGAL_DOCUMENTS[id]).toEqual({ version: "2026-10-04", status: "effective", effectiveAt: "2026-10-04" }); |
| 38 | expect(formatLegalDocumentStatus(id, getChrome("en").dateLocale)).toContain("October 4, 2026"); |
| 39 | expect(formatLegalDocumentStatus(id, getChrome("de").dateLocale)).toContain("4. Oktober 2026"); |
| 40 | } |
| 41 | }); |
| 42 | |
| 43 | it("pins the single publication authority and fails on vendored metadata drift", () => { |
| 44 | const pin = JSON.parse(readFileSync(new URL("vendor/legal-documents/PIN.json", webRoot), "utf8")); |
| 45 | expect(pin.sourceRepository).toBe("codewhale-hq/codewhale-platform"); |
| 46 | expect(pin.sourceCommit).toBe("4cd9450aec07ed81d6c56b5ce741424fec533d6b"); |
| 47 | expect(pin.files.map((row: { path: string }) => row.path).sort()).toEqual(["legal-content.tsx", "legal-documents.d.ts", "legal-documents.js"]); |
| 48 | expect(pin.files.find((row: { path: string }) => row.path === "legal-content.tsx")).toEqual({ |
| 49 | source: "apps/web/components/legal/legal-content.tsx", |
| 50 | path: "legal-content.tsx", |
| 51 | sha256: "0ffc3568bb9a8dea5e05c497763a342377fec3526b16fa273103909be9f9a3c1", |
| 52 | }); |
| 53 | for (const row of pin.files) { |
| 54 | const data = readFileSync(new URL(`vendor/legal-documents/${row.path}`, webRoot)); |
| 55 | expect(createHash("sha256").update(data).digest("hex")).toBe(row.sha256); |
| 56 | } |
| 57 | }); |
| 58 | |
| 59 | it("uses the pinned policy components as the sole legal body", () => { |
| 60 | expect(TermsContent).toBe(PinnedTermsContent); |
| 61 | expect(PrivacyContent).toBe(PinnedPrivacyContent); |
| 62 | }); |
| 63 | |
| 64 | it("renders the complete October 4 documents in English and Chinese page chrome", async () => { |
| 65 | for (const locale of ["en", "zh"]) { |
| 66 | const html = renderToStaticMarkup(await TermsPage({ params: Promise.resolve({ locale }) })); |
| 67 | const privacy = renderToStaticMarkup(await PrivacyPage({ params: Promise.resolve({ locale }) })); |
| 68 | for (const document of [html, privacy]) { |
| 69 | expect(document).toContain('data-legal-status="effective"'); |
| 70 | expect(document).toContain('data-legal-version="2026-10-04"'); |
| 71 | expect(document).toContain('data-legal-effective-at="2026-10-04"'); |
| 72 | expect(document).toContain(locale === "en" ? "October 4, 2026" : "2026年10月4日"); |
| 73 | expect(document).not.toMatch(/Not yet in effect|尚未生效|\[SELLER|\[SUPPORT/); |
| 74 | expect(document).toContain("Shannon Labs, Inc."); |
| 75 | expect(document).toContain('href="mailto:help@codewhale.net"'); |
| 76 | expect(document).toContain(`href="/${locale}/legal/terms"`); |
| 77 | expect(document).toContain(`href="/${locale}/legal/privacy"`); |
| 78 | } |
| 79 | expect(html).toContain(renderToStaticMarkup(createElement(PinnedTermsContent))); |
| 80 | expect(privacy).toContain(renderToStaticMarkup(createElement(PinnedPrivacyContent))); |
| 81 | for (const id of ["purchases", "platform-fee", "no-automatic-charges", "expiry", "refunds", "governing-law"]) { |
| 82 | expect(html).toContain(`id="${id}"`); |
| 83 | } |
| 84 | expect(privacy).toContain('id="processors"'); |
| 85 | expect(privacy).toContain('id="payments"'); |
| 86 | } |
| 87 | }); |
| 88 | |
| 89 | it("retains the website usage disclosure and preference control beside the canonical privacy body", async () => { |
| 90 | for (const locale of ["en", "zh"] as const) { |
| 91 | const html = renderToStaticMarkup(await PrivacyPage({ params: Promise.resolve({ locale }) })); |
| 92 | expect(html).toContain(renderToStaticMarkup(createElement("p", null, WEBSITE_USAGE_DISCLOSURE.body))); |
| 93 | expect(html).toContain(WEBSITE_USAGE_DISCLOSURE.title); |
| 94 | expect(html).toContain('id="usage-counting"'); |
| 95 | expect(html).toContain('class="usage-counting"'); |
| 96 | for (const text of [USAGE_COUNTING_COPY.summary[locale], USAGE_COUNTING_COPY.choice[locale], USAGE_COUNTING_COPY.elsewhere[locale]]) { |
| 97 | expect(html).toContain(renderToStaticMarkup(createElement("p", null, text)).slice(3, -4)); |
| 98 | } |
| 99 | } |
| 100 | }); |
| 101 | |
| 102 | it("takes incoming pricing links to installation in the visitor's locale", async () => { |
| 103 | for (const locale of ["en", "zh", "pt-BR"]) { |
| 104 | await PricingPage({ params: Promise.resolve({ locale }) }); |
| 105 | expect(redirect).toHaveBeenLastCalledWith(`/${locale}/install`); |
| 106 | } |
| 107 | }); |
| 108 | }); |
| 109 |