| 1 | import { existsSync, readdirSync, readFileSync } from "node:fs"; |
| 2 | import { join } from "node:path"; |
| 3 | import { renderToStaticMarkup } from "react-dom/server"; |
| 4 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | import { ALL_LOCALES } from "./i18n/config"; |
| 6 | import { getChrome } from "./i18n/dictionaries"; |
| 7 | import { NextRequest } from "next/server"; |
| 8 | import { middleware } from "../middleware"; |
| 9 | import { footerDocsLinks, footerLegalLinks, footerProductLinks, footerProjectLinks, navLinks, signInLink } from "./i18n/links"; |
| 10 | import { PERMANENT_ROUTE_REDIRECTS, REDIRECTED_ROUTES } from "./route-redirects"; |
| 11 | import { AVAILABILITY_ROWS, describeRow } from "./content/availability"; |
| 12 | import { desktopDownload, desktopDownloads, linuxDownloads, parseDesktopRelease, type DesktopReleaseFact } from "./desktop-release"; |
| 13 | import { FOUNDER_SWITCHES, FOUNDER_SWITCH_VALUES } from "./founder-switches"; |
| 14 | import { STATE_CHROME_KEY, SURFACE_IDS, SURFACE_STATES, stateLabel, surfaceCapabilities, surfaceState } from "./surface-states"; |
| 15 | import { DesktopDownloadBlock, LinuxDownloadBlock, aptInstallLines } from "../components/desktop-download"; |
| 16 | import HomePage from "../app/[locale]/page"; |
| 17 | import ContributePage from "../app/[locale]/contribute/page"; |
| 18 | import { extractHtmlText } from "./test-html-text"; |
| 19 | |
| 20 | const holder = vi.hoisted(() => ({ release: null as unknown })); |
| 21 | vi.mock("./desktop-release", async (importOriginal) => { |
| 22 | const real = await importOriginal<typeof import("./desktop-release")>(); |
| 23 | return { |
| 24 | ...real, |
| 25 | get DESKTOP_RELEASE() { |
| 26 | return holder.release; |
| 27 | }, |
| 28 | }; |
| 29 | }); |
| 30 | vi.mock("next/navigation", () => ({ redirect: vi.fn(), notFound: vi.fn(), usePathname: () => "/en", useRouter: () => ({}) })); |
| 31 | |
| 32 | const WEB = new URL("..", import.meta.url).pathname; |
| 33 | const LOCALES = ALL_LOCALES.map((entry) => entry.code); |
| 34 | const SHA = "a".repeat(64); |
| 35 | |
| 36 | const fixture: DesktopReleaseFact = { |
| 37 | tag: "v1.2.3", |
| 38 | version: "1.2.3", |
| 39 | publishedAt: "2026-12-01T10:00:00Z", |
| 40 | readAt: "2026-12-01T11:00:00Z", |
| 41 | url: "https://github.com/codewhale-hq/codewhale-app/releases/tag/v1.2.3", |
| 42 | assets: [ |
| 43 | { |
| 44 | name: "Codewhale-1.2.3-arm64.dmg", |
| 45 | platform: "macos", |
| 46 | size: 123_456_789, |
| 47 | sha256: SHA, |
| 48 | signed: true, |
| 49 | notarized: true, |
| 50 | url: "https://github.com/codewhale-hq/codewhale-app/releases/download/v1.2.3/Codewhale-1.2.3-arm64.dmg", |
| 51 | }, |
| 52 | ], |
| 53 | }; |
| 54 | |
| 55 | const APT = { |
| 56 | repository: "https://downloads.codewhale.net/desktop/apt", |
| 57 | keyUrl: "https://downloads.codewhale.net/desktop/apt/codewhale-archive-keyring.asc", |
| 58 | suite: "stable", |
| 59 | component: "main", |
| 60 | architecture: "amd64", |
| 61 | package: "codewhale-desktop", |
| 62 | keyFingerprint: "FE6EED0815F6796F0617D07A8D1B59648C382CE9", |
| 63 | }; |
| 64 | const APPIMAGE_SHA = "c".repeat(64); |
| 65 | const deb = { |
| 66 | name: "codewhale-desktop_1.2.3_amd64.deb", |
| 67 | platform: "linux", |
| 68 | kind: "deb", |
| 69 | arch: "x64", |
| 70 | size: 98_000_000, |
| 71 | sha256: "d".repeat(64), |
| 72 | url: "https://downloads.codewhale.net/apt/pool/main/c/codewhale-desktop/codewhale-desktop_1.2.3_amd64.deb", |
| 73 | apt: APT, |
| 74 | } as const; |
| 75 | const appImage = { |
| 76 | name: "Codewhale-1.2.3-x64.AppImage", |
| 77 | platform: "linux", |
| 78 | kind: "appimage", |
| 79 | arch: "x64", |
| 80 | size: 105_300_000, |
| 81 | sha256: APPIMAGE_SHA, |
| 82 | url: "https://downloads.codewhale.net/desktop/Codewhale-1.2.3-x64.AppImage", |
| 83 | } as const; |
| 84 | const withLinux = { ...fixture, assets: [...fixture.assets, deb, appImage] } as DesktopReleaseFact; |
| 85 | |
| 86 | type Page = (props: { params: Promise<{ locale: string }>; searchParams: Promise<Record<string, string>> }) => Promise<unknown> | unknown; |
| 87 | const render = async (page: Page, locale = "en") => |
| 88 | renderToStaticMarkup((await page({ params: Promise.resolve({ locale }), searchParams: Promise.resolve({}) })) as never); |
| 89 | |
| 90 | beforeEach(() => { |
| 91 | holder.release = null; |
| 92 | vi.stubGlobal("fetch", vi.fn(async () => { throw new Error("network down"); })); |
| 93 | vi.spyOn(console, "error").mockImplementation(() => {}); |
| 94 | }); |
| 95 | |
| 96 | afterEach(() => { |
| 97 | vi.useRealTimers(); |
| 98 | vi.unstubAllGlobals(); |
| 99 | vi.restoreAllMocks(); |
| 100 | }); |
| 101 | |
| 102 | describe("every surface state has copy in every locale", () => { |
| 103 | it.each(LOCALES)("%s", (locale) => { |
| 104 | const chrome = getChrome(locale); |
| 105 | for (const state of SURFACE_STATES) { |
| 106 | const label = stateLabel(state, locale); |
| 107 | expect(label, `${locale} ${state}`).toBeTruthy(); |
| 108 | expect(label).toBe(chrome[STATE_CHROME_KEY[state]]); |
| 109 | } |
| 110 | expect(chrome.numbersAsOf).toContain("{date}"); |
| 111 | expect(chrome.downloadVersion).toContain("{version}"); |
| 112 | expect(chrome.downloadSize).toContain("{size}"); |
| 113 | for (const key of ["downloadMacos", "downloadChecksum", "downloadSigned", "downloadLinux", "downloadAptTitle", "downloadAptTrust", "downloadAppImage", "downloadAppImageTrust"] as const) expect(chrome[key]).toBeTruthy(); |
| 114 | }); |
| 115 | |
| 116 | it("gives every state its own word within a locale", () => { |
| 117 | for (const locale of LOCALES) { |
| 118 | const labels = SURFACE_STATES.map((state) => stateLabel(state, locale)); |
| 119 | expect(new Set(labels).size, locale).toBe(labels.length); |
| 120 | } |
| 121 | }); |
| 122 | |
| 123 | it("maps every state to a chrome key", () => { |
| 124 | expect(Object.keys(STATE_CHROME_KEY).sort()).toEqual([...SURFACE_STATES].sort()); |
| 125 | }); |
| 126 | }); |
| 127 | |
| 128 | describe("today's surface states", () => { |
| 129 | it("are the committed values", () => { |
| 130 | expect(Object.fromEntries(SURFACE_IDS.map((id) => [id, surfaceState(id)]))).toEqual({ |
| 131 | terminal: "released", |
| 132 | "local-browser": "released", |
| 133 | vscode: "available", |
| 134 | desktop: "dev", |
| 135 | "hosted-web": "not_open", |
| 136 | "cloud-computers": "invite_only", |
| 137 | accounts: "open", |
| 138 | phone: "not_public", |
| 139 | "computer-use": "beta", |
| 140 | }); |
| 141 | }); |
| 142 | |
| 143 | it("read in English and Chinese as the permitted wording", () => { |
| 144 | expect(stateLabel("dev", "en")).toBe("Development build"); |
| 145 | expect(stateLabel("not_open", "en")).toBe("Not open yet"); |
| 146 | expect(stateLabel("invite_only", "en")).toBe("Invite-only"); |
| 147 | expect(stateLabel("dev", "zh")).toBe("开发版本"); |
| 148 | expect(stateLabel("not_open", "zh")).toBe("暂未开放"); |
| 149 | }); |
| 150 | |
| 151 | it("render the status of every availability row from its state", () => { |
| 152 | for (const row of AVAILABILITY_ROWS) { |
| 153 | const view = describeRow(row, "en"); |
| 154 | expect(view.status).toBe(stateLabel(surfaceState(row.id), "en")); |
| 155 | expect(view.downloads).toEqual([]); |
| 156 | } |
| 157 | }); |
| 158 | |
| 159 | it("agree with the sign-in page, which says cloud computers are invite-only", async () => { |
| 160 | const { ACCOUNT_ENTRY_COPY } = await import("./content/account-entry"); |
| 161 | expect(surfaceState("cloud-computers")).toBe("invite_only"); |
| 162 | expect(ACCOUNT_ENTRY_COPY.lede.en).toMatch(/invite-only/); |
| 163 | }); |
| 164 | }); |
| 165 | |
| 166 | describe("founder-set switches", () => { |
| 167 | it("default to off, each with the decision that set it", () => { |
| 168 | expect(FOUNDER_SWITCH_VALUES).toEqual({ cloudInvitationsOpen: false, plansPublished: false, cloudComputersBeyondInvite: false, namedVendorAgentsOverAcp: false, accountSignInOffered: false }); |
| 169 | for (const entry of Object.values(FOUNDER_SWITCHES)) expect(entry.decision).toMatch(/§\d/); |
| 170 | }); |
| 171 | |
| 172 | it("are not read from the environment", () => { |
| 173 | const source = readFileSync(join(WEB, "lib/founder-switches.ts"), "utf8"); |
| 174 | expect(source).not.toMatch(/process\.env|import\.meta\.env|getEnv/); |
| 175 | }); |
| 176 | |
| 177 | it("open a request path only for cloud computers and only when the cloud switch is on", () => { |
| 178 | const on = { ...FOUNDER_SWITCH_VALUES, cloudInvitationsOpen: true }; |
| 179 | expect(surfaceCapabilities("cloud-computers").requestAccess).toBe(false); |
| 180 | expect(surfaceCapabilities("cloud-computers", { switches: on }).requestAccess).toBe(true); |
| 181 | for (const id of SURFACE_IDS.filter((candidate) => candidate !== "cloud-computers")) { |
| 182 | expect(surfaceCapabilities(id, { switches: on }).requestAccess, id).toBe(false); |
| 183 | } |
| 184 | }); |
| 185 | |
| 186 | it("never link a surface that is not open or not public", () => { |
| 187 | expect(surfaceCapabilities("hosted-web").link).toBe(false); |
| 188 | expect(surfaceCapabilities("phone").link).toBe(false); |
| 189 | expect(surfaceCapabilities("terminal").link).toBe(true); |
| 190 | }); |
| 191 | }); |
| 192 | |
| 193 | describe("the desktop download", () => { |
| 194 | it("is the published macOS release today, and without release data the desktop is a development build", () => { |
| 195 | const published = parseDesktopRelease(JSON.parse(readFileSync(join(WEB, "data/desktop-published-release.json"), "utf8"))); |
| 196 | expect(published).not.toBeNull(); |
| 197 | expect(desktopDownloads(published).map((download) => download.arch)).toEqual(["arm64", "x64"]); |
| 198 | expect(surfaceState("desktop", { desktopRelease: published })).toBe("released"); |
| 199 | expect(surfaceCapabilities("desktop", { desktopRelease: published }).download).toBe(true); |
| 200 | expect(surfaceState("desktop", { desktopRelease: null })).toBe("dev"); |
| 201 | expect(desktopDownload(null)).toBeNull(); |
| 202 | }); |
| 203 | |
| 204 | it("is offered only for a signed, notarized macOS asset with a checksum", () => { |
| 205 | expect(parseDesktopRelease(fixture)).toEqual(fixture); |
| 206 | const asset = fixture.assets[0]; |
| 207 | const variants: unknown[] = [ |
| 208 | { ...fixture, assets: [{ ...asset, signed: false }] }, |
| 209 | { ...fixture, assets: [{ ...asset, notarized: false }] }, |
| 210 | { ...fixture, assets: [{ ...asset, sha256: null }] }, |
| 211 | { ...fixture, assets: [{ ...asset, sha256: "abc" }] }, |
| 212 | { ...fixture, assets: [{ ...asset, platform: "windows" }] }, |
| 213 | { ...fixture, assets: [{ ...asset, size: 0 }] }, |
| 214 | { ...fixture, assets: [{ ...asset, url: "http://github.com/codewhale-hq/x.dmg" }] }, |
| 215 | { ...fixture, assets: [{ ...asset, url: "https://example.com/x.dmg" }] }, |
| 216 | { ...fixture, tag: "1.2.3" }, |
| 217 | { ...fixture, version: "9.9.9" }, |
| 218 | { ...fixture, publishedAt: "never" }, |
| 219 | { ...fixture, assets: [] }, |
| 220 | ]; |
| 221 | for (const variant of variants) expect(desktopDownload(parseDesktopRelease(variant)), JSON.stringify(variant)).toBeNull(); |
| 222 | }); |
| 223 | |
| 224 | it("makes the desktop state released and shows version, size and checksum", () => { |
| 225 | expect(surfaceState("desktop", { desktopRelease: fixture })).toBe("released"); |
| 226 | const row = AVAILABILITY_ROWS.find((candidate) => candidate.id === "desktop")!; |
| 227 | const view = describeRow(row, "en", fixture); |
| 228 | expect(view.status).toBe("Released"); |
| 229 | expect(view.detail).not.toMatch(/No public download/); |
| 230 | const html = renderToStaticMarkup(DesktopDownloadBlock({ downloads: view.downloads, locale: "en" })); |
| 231 | expect(html).toContain("Download for macOS"); |
| 232 | expect(html).not.toContain("Apple silicon"); |
| 233 | expect(html).toContain(fixture.assets[0].url); |
| 234 | expect(html).toContain("Version 1.2.3"); |
| 235 | expect(html).toContain("123.5 MB"); |
| 236 | expect(html).toContain(SHA); |
| 237 | }); |
| 238 | |
| 239 | it("offers the Apple silicon and Intel installers separately, each with its own size and checksum", () => { |
| 240 | const arm = fixture.assets[0]; |
| 241 | const intelSha = "b".repeat(64); |
| 242 | const intel = { ...arm, name: "Codewhale-1.2.3-x64.dmg", size: 234_567_890, sha256: intelSha, url: arm.url.replace("arm64", "x64") }; |
| 243 | const both = { ...fixture, assets: [intel, arm] }; |
| 244 | expect(desktopDownloads(both).map((download) => download.arch)).toEqual(["arm64", "x64"]); |
| 245 | expect(desktopDownload(both)?.url).toBe(arm.url); |
| 246 | const html = renderToStaticMarkup(DesktopDownloadBlock({ downloads: desktopDownloads(both), locale: "en" })); |
| 247 | expect(html.indexOf("Apple silicon")).toBeGreaterThan(-1); |
| 248 | expect(html.indexOf("Intel")).toBeGreaterThan(html.indexOf("Apple silicon")); |
| 249 | for (const expected of [arm.url, intel.url, SHA, intelSha, "123.5 MB", "234.6 MB"]) expect(html).toContain(expected); |
| 250 | }); |
| 251 | |
| 252 | it.each(["en", "zh"])("renders on /app in %s only when the release object is present", async (locale) => { |
| 253 | const { default: AppPage } = await import("../app/[locale]/app/page"); |
| 254 | for (const page of [AppPage]) { |
| 255 | holder.release = null; |
| 256 | expect(await render(page as Page, locale)).not.toContain(SHA); |
| 257 | holder.release = fixture; |
| 258 | const html = await render(page as Page, locale); |
| 259 | expect(html).toContain(SHA); |
| 260 | expect(html).toContain(getChrome(locale).downloadMacos); |
| 261 | expect(html).toContain("1.2.3"); |
| 262 | } |
| 263 | }); |
| 264 | }); |
| 265 | |
| 266 | describe("the Linux desktop download", () => { |
| 267 | it("parses only the exact deb and AppImage shapes", () => { |
| 268 | expect(parseDesktopRelease(withLinux)).toEqual(withLinux); |
| 269 | const bad: unknown[] = [ |
| 270 | { ...deb, notarized: true }, |
| 271 | { ...deb, signed: true }, |
| 272 | { ...appImage, notarized: true }, |
| 273 | { ...appImage, signed: true }, |
| 274 | { ...appImage, apt: APT }, |
| 275 | { ...appImage, sha256: undefined }, |
| 276 | { ...deb, sha256: "abc" }, |
| 277 | { ...deb, apt: undefined }, |
| 278 | { ...deb, apt: { ...APT, repository: "https://example.com/apt" } }, |
| 279 | { ...deb, apt: { ...APT, repository: "http://downloads.codewhale.net/apt" } }, |
| 280 | { ...deb, apt: { ...APT, keyUrl: "http://downloads.codewhale.net/k.asc" } }, |
| 281 | { ...deb, apt: { ...APT, keyUrl: "https://downloads.codewhale.net/k.asc; rm -rf ~" } }, |
| 282 | { ...deb, apt: { ...APT, package: "codewhale desktop" } }, |
| 283 | { ...deb, apt: { ...APT, suite: "$(id)" } }, |
| 284 | { ...deb, apt: { ...APT, extra: "x" } }, |
| 285 | { ...deb, apt: { ...APT, architecture: undefined } }, |
| 286 | { ...deb, apt: { ...APT, architecture: "i386" } }, |
| 287 | { ...deb, apt: { ...APT, architecture: "amd64; id" } }, |
| 288 | { ...deb, apt: { ...APT, keyFingerprint: undefined } }, |
| 289 | { ...deb, apt: { ...APT, keyFingerprint: APT.keyFingerprint.toLowerCase() } }, |
| 290 | { ...deb, apt: { ...APT, keyFingerprint: APT.keyFingerprint.slice(1) } }, |
| 291 | { ...deb, apt: { ...APT, keyFingerprint: `${APT.keyFingerprint}0` } }, |
| 292 | { ...deb, apt: { ...APT, keyFingerprint: `${APT.keyFingerprint.slice(0, 39)}G` } }, |
| 293 | { ...deb, apt: { ...APT, keyUrl: "https://downloads.codewhale.net/desktop/apt/" } }, |
| 294 | { ...deb, apt: { ...APT, keyUrl: "https://downloads.codewhale.net/desktop/apt/.asc" } }, |
| 295 | { ...deb, arch: "arm64" }, |
| 296 | { ...appImage, arch: undefined }, |
| 297 | { ...appImage, kind: "rpm" }, |
| 298 | { ...appImage, kind: undefined }, |
| 299 | { ...appImage, url: "https://example.com/x.AppImage" }, |
| 300 | ]; |
| 301 | for (const asset of bad) expect(parseDesktopRelease({ ...fixture, assets: [...fixture.assets, asset] }), JSON.stringify(asset)).toBeNull(); |
| 302 | }); |
| 303 | |
| 304 | it("leaves the macOS download and the primary download unchanged", () => { |
| 305 | expect(desktopDownloads(withLinux)).toEqual(desktopDownloads(fixture)); |
| 306 | expect(desktopDownload(withLinux)).toEqual(desktopDownload(fixture)); |
| 307 | expect(linuxDownloads(fixture)).toBeNull(); |
| 308 | expect(linuxDownloads(null)).toBeNull(); |
| 309 | }); |
| 310 | |
| 311 | it("renders the apt lines, the key URL and the AppImage checksum", () => { |
| 312 | const linux = linuxDownloads(withLinux)!; |
| 313 | const lines = aptInstallLines(linux.apt!).split("\n"); |
| 314 | expect(lines).toEqual([ |
| 315 | "sudo install -d -m 0755 /etc/apt/keyrings", |
| 316 | "curl -fsSL https://downloads.codewhale.net/desktop/apt/codewhale-archive-keyring.asc | sudo tee /etc/apt/keyrings/codewhale-archive-keyring.asc >/dev/null", |
| 317 | 'echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/codewhale-archive-keyring.asc] https://downloads.codewhale.net/desktop/apt stable main" | sudo tee /etc/apt/sources.list.d/codewhale.list >/dev/null', |
| 318 | "sudo apt update && sudo apt install codewhale-desktop", |
| 319 | ]); |
| 320 | expect(lines.join("\n")).not.toContain("gpg"); |
| 321 | const html = renderToStaticMarkup(LinuxDownloadBlock({ linux, locale: "en" })); |
| 322 | expect(html).toContain(APT.keyUrl); |
| 323 | expect(html).toContain("FE6E ED08 15F6 796F 0617 D07A 8D1B 5964 8C38 2CE9"); |
| 324 | expect(html).toContain("should have the fingerprint"); |
| 325 | expect(html).toContain(APPIMAGE_SHA); |
| 326 | expect(html).toContain(appImage.url); |
| 327 | expect(html).toContain("105.3 MB"); |
| 328 | expect(html).toContain("does not update itself yet"); |
| 329 | expect(html).not.toMatch(/notarized/i); |
| 330 | }); |
| 331 | |
| 332 | it("says macOS only, or macOS and Linux, from the data on the page and the row", async () => { |
| 333 | const row = AVAILABILITY_ROWS.find((candidate) => candidate.id === "desktop")!; |
| 334 | expect(describeRow(row, "en", fixture).detail).not.toContain("Linux"); |
| 335 | expect(describeRow(row, "en", withLinux).detail).toContain("macOS and Linux"); |
| 336 | expect(describeRow(row, "zh", withLinux).detail).toContain("Linux"); |
| 337 | const { appHeaderCopy } = await import("./content/app-page"); |
| 338 | expect(appHeaderCopy("en", fixture).lede).not.toContain("Linux"); |
| 339 | expect(appHeaderCopy("en", fixture).description).not.toContain("Linux"); |
| 340 | expect(appHeaderCopy("en", withLinux).lede).toContain("macOS and Linux"); |
| 341 | expect(appHeaderCopy("en", withLinux).description).toContain("macOS and Linux"); |
| 342 | }); |
| 343 | |
| 344 | it.each(["en", "zh"])("renders on /app in %s only when Linux assets are present, and macOS-only output is unchanged", async (locale) => { |
| 345 | const { default: AppPage } = await import("../app/[locale]/app/page"); |
| 346 | holder.release = fixture; |
| 347 | const macOnly = await render(AppPage as Page, locale); |
| 348 | expect(macOnly).toContain(SHA); |
| 349 | expect(macOnly).not.toContain("apt install"); |
| 350 | expect(macOnly).not.toContain(APPIMAGE_SHA); |
| 351 | holder.release = withLinux; |
| 352 | const both = await render(AppPage as Page, locale); |
| 353 | expect(both).toContain(SHA); |
| 354 | expect(both).toContain(APPIMAGE_SHA); |
| 355 | expect(both).toContain(APT.keyUrl); |
| 356 | expect(both).toContain(`sudo apt update && sudo apt install ${APT.package}`); |
| 357 | holder.release = { ...fixture, assets: [deb, appImage] } as DesktopReleaseFact; |
| 358 | expect(await render(AppPage as Page, locale)).not.toContain(APPIMAGE_SHA); |
| 359 | }); |
| 360 | }); |
| 361 | |
| 362 | const PAGES: [string, () => Promise<{ default: unknown }>][] = [ |
| 363 | ["", () => import("../app/[locale]/page")], |
| 364 | ["app", () => import("../app/[locale]/app/page")], |
| 365 | ["install", () => import("../app/[locale]/install/page")], |
| 366 | ["signin", () => import("../app/[locale]/signin/page")], |
| 367 | ["faq", () => import("../app/[locale]/faq/page")], |
| 368 | ["features", () => import("../app/[locale]/features/page")], |
| 369 | ["models", () => import("../app/[locale]/models/page")], |
| 370 | ["plugins", () => import("../app/[locale]/plugins/page")], |
| 371 | ["computer-use", () => import("../app/[locale]/computer-use/page")], |
| 372 | ["contribute", () => import("../app/[locale]/contribute/page")], |
| 373 | ["docs", () => import("../app/[locale]/docs/page")], |
| 374 | ["docs/constitution", () => import("../app/[locale]/docs/constitution/page")], |
| 375 | ]; |
| 376 | |
| 377 | describe("with both founder switches off, no page offers a download or a request path", () => { |
| 378 | const pages = PAGES; |
| 379 | const INSTALLER = /\.(?:dmg|pkg|msi|exe)(?:["?#]|$)|downloads\.codewhale\.net/i; |
| 380 | const REQUEST_PATH = /request access|request an invite|request an invitation|join the waitlist|waitlist|apply for access/i; |
| 381 | |
| 382 | it.each(["en", "zh"])("holds in %s", async (locale) => { |
| 383 | const downloadWords = [getChrome("en").downloadMacos, getChrome("zh").downloadMacos]; |
| 384 | for (const [name, load] of pages) { |
| 385 | const html = await render((await load()).default as Page, locale); |
| 386 | const text = extractHtmlText(html, { stripScripts: true, tagReplacement: " " }); |
| 387 | const hrefs = [...html.matchAll(/href="([^"]*)"/g)].map((match) => match[1]); |
| 388 | expect(hrefs.find((href) => INSTALLER.test(href)), `${locale}/${name} installer link`).toBeUndefined(); |
| 389 | expect(text.match(REQUEST_PATH)?.[0], `${locale}/${name} request path`).toBeUndefined(); |
| 390 | for (const word of downloadWords) expect(text, `${locale}/${name} download button`).not.toContain(word); |
| 391 | } |
| 392 | }); |
| 393 | }); |
| 394 | |
| 395 | describe("no page links to a missing or gone route", () => { |
| 396 | const ROUTE_ROOT = join(WEB, "app/[locale]"); |
| 397 | const REDIRECTS = new Set([...REDIRECTED_ROUTES, "signup", "terms", "privacy", "legal"]); |
| 398 | |
| 399 | function routeExists(path: string): boolean { |
| 400 | const segments = path.split("/").filter(Boolean); |
| 401 | if (segments.length === 0) return true; |
| 402 | if (REDIRECTS.has(segments[0])) return true; |
| 403 | let dir = ROUTE_ROOT; |
| 404 | for (const segment of segments) { |
| 405 | if (existsSync(join(dir, segment))) { |
| 406 | dir = join(dir, segment); |
| 407 | continue; |
| 408 | } |
| 409 | const dynamic = existsSync(dir) && readdirSync(dir).find((entry) => /^\[[^.\]]+\]$/.test(entry)); |
| 410 | if (!dynamic) return false; |
| 411 | dir = join(dir, dynamic); |
| 412 | } |
| 413 | return existsSync(join(dir, "page.tsx")); |
| 414 | } |
| 415 | |
| 416 | it.each(["en", "zh"])("answers every header and footer link in %s with a page that exists", (locale) => { |
| 417 | const chrome = getChrome(locale); |
| 418 | const links = [ |
| 419 | ...navLinks(locale, chrome), |
| 420 | signInLink(locale, chrome), |
| 421 | ...footerProductLinks(locale, chrome), |
| 422 | ...footerDocsLinks(locale, chrome), |
| 423 | ...footerProjectLinks(locale, chrome), |
| 424 | ...footerLegalLinks(locale, chrome, "Usage"), |
| 425 | ]; |
| 426 | for (const link of links.filter((l) => l.href.startsWith("/"))) { |
| 427 | const path = link.href.replace(/#.*$/, "").replace(new RegExp(`^/${locale}`), ""); |
| 428 | expect(routeExists(path), `${locale} chrome link ${link.href}`).toBe(true); |
| 429 | expect(REDIRECTED_ROUTES.includes(path.split("/")[1] ?? ""), `${link.href} is a redirect`).toBe(false); |
| 430 | } |
| 431 | for (const link of links.filter((l) => !l.href.startsWith("/"))) expect(link.href, link.label).toMatch(/^https:\/\//); |
| 432 | }); |
| 433 | |
| 434 | it.each(["", "/en", "/zh", "/ar"])("redirects every merged route permanently to a page that exists, under %s", (prefix) => { |
| 435 | for (const [segment, target] of Object.entries(PERMANENT_ROUTE_REDIRECTS)) { |
| 436 | const res = middleware(new NextRequest(new URL(`https://codewhale.net${prefix}/${segment}`), { |
| 437 | headers: new Headers({ host: "codewhale.net", "accept-language": "ar" }), |
| 438 | })); |
| 439 | const locale = prefix.slice(1) || "ar"; |
| 440 | expect(res.status, `${prefix}/${segment}`).toBe(308); |
| 441 | expect(res.headers.get("location"), `${prefix}/${segment}`).toBe(`https://codewhale.net/${locale}${target}`); |
| 442 | expect(routeExists(target.replace(/#.*$/, "")), `${target} exists`).toBe(true); |
| 443 | } |
| 444 | }); |
| 445 | |
| 446 | it("never links the hosted app root while its state is not open", async () => { |
| 447 | expect(surfaceState("hosted-web")).toBe("not_open"); |
| 448 | for (const [path, load] of PAGES) { |
| 449 | const page = await load(); |
| 450 | for (const locale of ["en", "zh"]) { |
| 451 | const html = await render(page.default as Page, locale); |
| 452 | const hrefs = [...html.matchAll(/href="([^"]*)"/g)].map((match) => match[1]); |
| 453 | const root = hrefs.find((href) => /^https:\/\/app\.codewhale\.net\/?(?:[?#].*)?$/.test(href)); |
| 454 | expect(root, `${locale}/${path} links the hosted app root`).toBeUndefined(); |
| 455 | } |
| 456 | } |
| 457 | }); |
| 458 | |
| 459 | it("links only to localized pages that exist", async () => { |
| 460 | for (const [path, load] of PAGES) { |
| 461 | const page = await load(); |
| 462 | const html = await render(page.default as Page, "en"); |
| 463 | const hrefs = [...html.matchAll(/href="(\/en(?:\/[^"?#]*)?)(?:[?#][^"]*)?"/g)].map((match) => match[1]); |
| 464 | for (const href of hrefs) expect(routeExists(href.replace(/^\/en/, "")), `/en/${path} links ${href}`).toBe(true); |
| 465 | } |
| 466 | }); |
| 467 | }); |
| 468 | |
| 469 | describe("numbers from the build-time snapshot carry the date they were read", () => { |
| 470 | it.each(LOCALES)("on the home page in %s", async (locale) => { |
| 471 | const snapshot = JSON.parse(readFileSync(join(WEB, "data/repo-stats-snapshot.json"), "utf8")) as { readAt: string }; |
| 472 | const date = new Intl.DateTimeFormat(getChrome(locale).dateLocale, { dateStyle: "medium", timeZone: "UTC" }).format(new Date(snapshot.readAt)); |
| 473 | const label = getChrome(locale).numbersAsOf.replace("{date}", date); |
| 474 | const html = await render(HomePage as Page, locale); |
| 475 | const counts = [...html.matchAll(/<p class="home-accounts tabular"[^>]*>/g)].map((match) => match[0]); |
| 476 | expect(counts).toHaveLength(1); |
| 477 | expect(counts[0]).toContain(`title="${label}"`); |
| 478 | expect(html).toContain(`<span class="sr-only"> ${label}</span>`); |
| 479 | expect(html.replace(/<span class="sr-only">[^<]*<\/span>/g, "").replace(/title="[^"]*"/g, "")).not.toContain(label); |
| 480 | }); |
| 481 | |
| 482 | it("on /contribute", async () => { |
| 483 | expect(await render(ContributePage as Page)).toMatch(/title="As of [^"]+"/); |
| 484 | }); |
| 485 | }); |
| 486 |