Files
youtube-generator/tests/build/legal-docs.test.ts
T
poli0981andClaude Opus 5.5 5ced6b4a86 feat(web): Turnstile gate, in-site legal pages and clean URLs on ytgenerator.stream
A Worker (worker/) now sits in front of the static build, configured from the
same wrangler.jsonc. Cloudflare builds and deploys it; nothing in GitHub
Actions does.

Gate
- First visit shows a branded check page (8 languages, Accept-Language) with
  Cloudflare Turnstile. The token is verified server-side against
  TURNSTILE_SECRET: success, hostname, action "enter", age <= 300 s. Only
  Cloudflare's own test-key results skip the hostname/action check, so
  `npm run cf:dev` works with the published test keys.
- Pass -> `__Host-ytg_gate` (HttpOnly, Secure, SameSite=Lax, 24 h): an HMAC
  over expiry + a user-agent hash, keyed from the Turnstile secret, so no
  second secret exists and a cookie copied into a scraper is useless.
- The page reloads in place instead of redirecting, so there is no `next`
  parameter to open-redirect with; /__gate/status lets the page tell a
  cookie-blocking browser what is wrong instead of looping; after one
  failure it waits for a click rather than resubmitting.
- Missing secret = deployment mistake: the site stays up without the gate and
  the Worker logs it, instead of locking every visitor out.
- The agree box on the gate records `ytg_terms`, which the app reads on boot
  so web visitors meet one blocking screen, not two.

Headers and static files
- worker/headers.ts sets CSP (nonce + strict-dynamic on the gate, no script
  at all on legal pages), nosniff, Referrer-Policy, Permissions-Policy, COOP,
  CORP, X-Frame-Options on everything the Worker returns; _headers covers
  the files served directly; a test keeps the two baselines identical.
- `not_found_handling: "404-page"` (a static 404.html) instead of the SPA
  fallback, which would have handed index.html past the gate for any
  excluded path. The Worker serves the shell itself by fetching "/" - not
  "/index.html", which html_handling would 307 back to the home page.
- security.txt (RFC 9116) is generated per build so Expires stays ~6 months
  ahead; the Web Analytics beacon is injected into the web build only.
- theme-init.js applies the saved theme before first paint (no inline script).

Legal in-site
- build-plugins/legal-docs.ts renders TERMS, PRIVACY, DISCLAIMER (+ vi),
  LICENSE, THIRD_PARTY_NOTICES, SECURITY, NOTICE and CODE_OF_CONDUCT at build
  time into static /legal pages (public, before the gate) and a lazy
  `virtual:legal-docs` module for the in-app Legal Center, which works
  offline on desktop and Android and never leaves the webview.
- PRIVACY, TERMS and SECURITY rewritten for Cloudflare hosting, Turnstile,
  Web Analytics and the two cookies; contacts moved to the poli0981.dev
  aliases (privacy@, legal@, dmca@, copyright@, security@, code@).
- CURRENT_TERMS_VERSION 1 -> 2: the Privacy Policy changed materially.

App
- Web uses BrowserRouter (real paths; analytics can count them), Tauri keeps
  HashRouter; old /#/x bookmarks are rewritten to /x before the router reads
  them. Consent is a layout route; /legal sits outside it.
- <html lang> follows the UI language; a stale chunk after a deploy triggers
  one reload instead of an error page.

.gitattributes pins LF everywhere so Windows checkouts stop failing Prettier.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:26:40 +07:00

98 lines
3.3 KiB
TypeScript

import { describe, it, expect } from "vitest";
import {
extractEffectiveDate,
renderAllLegalDocs,
renderMarkdown,
rewriteHref,
} from "../../build-plugins/legal-docs";
import { LEGAL_DOC_IDS } from "@config/legal";
describe("rewriteHref", () => {
it.each([
["LICENSE", "TERMS.md", "/legal/license"],
["./DISCLAIMER.md", "TERMS.md", "/legal/disclaimer"],
["PRIVACY.md#cookies", "TERMS.md", "/legal/privacy#cookies"],
["../../../PRIVACY.md", "docs/i18n/vi/DISCLAIMER.md", "/legal/privacy"],
[
"CONTRIBUTING.md",
"TERMS.md",
"https://github.com/poli0981/youtube-generator/blob/main/CONTRIBUTING.md",
],
[
"./docs/I18N.md#adding",
"README.md",
"https://github.com/poli0981/youtube-generator/blob/main/docs/I18N.md#adding",
],
["https://example.com/x", "TERMS.md", "https://example.com/x"],
["mailto:legal@poli0981.dev", "TERMS.md", "mailto:legal@poli0981.dev"],
["#section", "TERMS.md", "#section"],
])("%s (from %s) → %s", (href, from, expected) => {
expect(rewriteHref(href, from)).toBe(expected);
});
});
describe("renderMarkdown", () => {
it("reads the title and gives headings anchor ids", () => {
const { title, html } = renderMarkdown(
"# Privacy Policy\n\n## 3. Cookies\n\nText.",
"PRIVACY.md",
);
expect(title).toBe("Privacy Policy");
expect(html).toContain('<h2 id="3-cookies">3. Cookies</h2>');
});
it("opens external links in a new tab and keeps in-site links in place", () => {
const { html } = renderMarkdown(
"[site](https://example.com) and [privacy](PRIVACY.md)",
"TERMS.md",
);
expect(html).toContain(
'<a href="https://example.com" target="_blank" rel="noopener noreferrer">site</a>',
);
expect(html).toContain('<a href="/legal/privacy">privacy</a>');
});
});
describe("extractEffectiveDate", () => {
it("finds the effective date line", () => {
expect(extractEffectiveDate("# X\n\n**Effective:** 2026-09-30\n")).toBe("2026-09-30");
expect(extractEffectiveDate("# X\n\nno date")).toBeNull();
});
});
describe("renderAllLegalDocs (the real repository files)", () => {
const docs = renderAllLegalDocs();
it("renders every document with a title and HTML", () => {
for (const id of LEGAL_DOC_IDS) {
expect(docs[id].title, id).not.toBe("");
expect(docs[id].html.en?.length ?? 0, id).toBeGreaterThan(100);
}
});
it("carries the Vietnamese Disclaimer", () => {
expect(docs.disclaimer.html.vi).toBeTruthy();
});
it("dates the Terms and the Privacy Policy", () => {
expect(docs.terms.effective).toMatch(/^\d{4}-\d{2}-\d{2}$/);
expect(docs.privacy.effective).toMatch(/^\d{4}-\d{2}-\d{2}$/);
});
it("no longer sends readers to GitHub for another legal document", () => {
for (const id of LEGAL_DOC_IDS) {
const html = docs[id].html.en ?? "";
expect(html, id).not.toMatch(
/github\.com\/[^"]+\/blob\/main\/(TERMS|PRIVACY|DISCLAIMER|SECURITY|LICENSE)/,
);
}
});
it("uses the current contact addresses, not the retired one", () => {
const all = LEGAL_DOC_IDS.map((id) => docs[id].html.en ?? "").join("\n");
expect(all).not.toContain("lopop05905@proton.me");
expect(docs.privacy.html.en).toContain("privacy@poli0981.dev");
expect(docs.security.html.en).toContain("security@poli0981.dev");
});
});