| 1 | import { createElement } from "react"; |
| 2 | import { renderToStaticMarkup } from "react-dom/server"; |
| 3 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | import HomePage from "../app/[locale]/page"; |
| 5 | import FeaturesPage, { generateMetadata as featuresMetadata } from "../app/[locale]/features/page"; |
| 6 | import { generateMetadata as installMetadata } from "../app/[locale]/install/page"; |
| 7 | import { HeroSurfacePreview } from "./hero-surface-preview"; |
| 8 | import { SITE_URL } from "../lib/page-meta"; |
| 9 | import { stateLabel, surfaceState } from "../lib/surface-states"; |
| 10 | |
| 11 | vi.mock("next/navigation", () => ({ |
| 12 | redirect: vi.fn(), |
| 13 | notFound: vi.fn(), |
| 14 | usePathname: () => "/en", |
| 15 | useRouter: () => ({}), |
| 16 | })); |
| 17 | |
| 18 | afterEach(() => { |
| 19 | vi.unstubAllGlobals(); |
| 20 | vi.restoreAllMocks(); |
| 21 | }); |
| 22 | |
| 23 | describe("HeroSurfacePreview and homepage surface presentation", () => { |
| 24 | it("renders only the active terminal panel on initial render to keep DOM lean", () => { |
| 25 | const html = renderToStaticMarkup( |
| 26 | createElement(HeroSurfacePreview, { |
| 27 | label: "Preview surface", |
| 28 | terminalLabel: "Terminal app", |
| 29 | desktopLabel: "Desktop app", |
| 30 | terminal: createElement("div", { "data-testid": "terminal-panel" }, "terminal-content"), |
| 31 | desktop: createElement("div", { "data-testid": "desktop-panel" }, "desktop-content"), |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | expect(html).toContain('role="tablist"'); |
| 36 | expect(html).toContain('aria-selected="true"'); |
| 37 | expect(html).toContain("Terminal app"); |
| 38 | expect(html).toContain("Desktop app"); |
| 39 | expect(html).toContain("terminal-content"); |
| 40 | expect(html).not.toContain("desktop-content"); |
| 41 | }); |
| 42 | |
| 43 | it.each(["en", "zh"])("renders hero surface availability line and switcher in %s", async (locale) => { |
| 44 | vi.stubGlobal("fetch", vi.fn(async () => { |
| 45 | throw new Error("offline"); |
| 46 | })); |
| 47 | vi.spyOn(console, "error").mockImplementation(() => {}); |
| 48 | |
| 49 | const element = await HomePage({ params: Promise.resolve({ locale }) }); |
| 50 | const html = renderToStaticMarkup(element); |
| 51 | |
| 52 | const desktopLabel = stateLabel(surfaceState("desktop"), locale); |
| 53 | const cloudLabel = stateLabel(surfaceState("cloud-computers"), locale); |
| 54 | expect(html).toContain(desktopLabel); |
| 55 | expect(html).toContain(cloudLabel); |
| 56 | if (locale === "en") { |
| 57 | expect(html).toContain("The model can be anyone's."); |
| 58 | expect(html).toContain("The agent should be yours."); |
| 59 | } |
| 60 | if (surfaceState("desktop") === "released") { |
| 61 | expect(html).toContain("data-hero-surface-preview"); |
| 62 | } |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | describe("Features page capability overview", () => { |
| 67 | it.each(["en", "zh"])("renders the 5-item capability overview above the catalogue in %s", async (locale) => { |
| 68 | const element = await FeaturesPage({ params: Promise.resolve({ locale }) }); |
| 69 | const html = renderToStaticMarkup(element); |
| 70 | |
| 71 | const overviewIndex = html.indexOf('id="features-overview-title"'); |
| 72 | const catalogueIndex = html.indexOf('class="feature-catalogue"'); |
| 73 | expect(overviewIndex).toBeGreaterThan(-1); |
| 74 | expect(catalogueIndex).toBeGreaterThan(overviewIndex); |
| 75 | |
| 76 | for (const id of ["any-model", "receipts-undo", "multi-agent-fleets", "os-sandbox", "no-account"]) { |
| 77 | expect(html).toContain(`id="${id}"`); |
| 78 | } |
| 79 | expect(html).toContain("/undo"); |
| 80 | expect(html).toContain("Fleet"); |
| 81 | }); |
| 82 | }); |
| 83 | |
| 84 | describe("hreflang metadata completeness for /install and /features", () => { |
| 85 | it.each(["en", "zh"])("advertises en, zh, and x-default on /install for %s", async (locale) => { |
| 86 | const meta = await installMetadata({ params: Promise.resolve({ locale }) }); |
| 87 | const languages = meta.alternates?.languages as Record<string, string>; |
| 88 | expect(languages.en).toBe(`${SITE_URL}/en/install`); |
| 89 | expect(languages.zh).toBe(`${SITE_URL}/zh/install`); |
| 90 | expect(languages["x-default"]).toBe(`${SITE_URL}/en/install`); |
| 91 | }); |
| 92 | |
| 93 | it.each(["en", "zh"])("advertises en, zh, and x-default on /features for %s", async (locale) => { |
| 94 | const meta = await featuresMetadata({ params: Promise.resolve({ locale }) }); |
| 95 | const languages = meta.alternates?.languages as Record<string, string>; |
| 96 | expect(languages.en).toBe(`${SITE_URL}/en/features`); |
| 97 | expect(languages.zh).toBe(`${SITE_URL}/zh/features`); |
| 98 | expect(languages["x-default"]).toBe(`${SITE_URL}/en/features`); |
| 99 | }); |
| 100 | }); |
| 101 |