From 2362117cb5eefc1f0da1c291e3740e4cb1c82414 Mon Sep 17 00:00:00 2001 From: le king fu Date: Mon, 2 Nov 2026 21:57:24 -0500 Subject: [PATCH] test: pin the UTM label of each product page button (#525) purchaseUrl.test.ts: per language, new URL(...) keeps exactly the origin and the path of the contract; the query holds the four keys and no other, in their order; utm_content is the label for each of the four; the exact address is pinned; the language fallbacks stay. A free string, or no label, does not compile (@ts-expect-error, checked by tsc in npm run build). UpsellCtas.render.test.tsx: both buttons open the URL with its query (update-locked for PurchaseButton, update-locked-download for DownloadLatestButton), PurchaseButton opens the label its caller gives, and UpsellGate passes locked-module. PurchaseButton takes only its two labels and DownloadLatestButton none (tsc). New wiring tests under the shared hook harness: ErrorPage opens the buttons by a click on the update check (invoke mocked, check_entitlement false), then renders again; UpdateCard in the notEntitled state; LicenseCard's body, read from the element the card returns, opens license-card. Co-Authored-By: Claude Opus 5.5 --- .../settings/LicenseCard.wiring.test.ts | 97 ++++++++++++++++ .../settings/UpdateCard.wiring.test.ts | 61 ++++++++++ .../shared/ErrorPage.wiring.test.ts | 78 +++++++++++++ .../shared/UpsellCtas.render.test.tsx | 64 +++++++++-- src/shared/purchaseUrl.test.ts | 106 ++++++++++++++---- 5 files changed, 377 insertions(+), 29 deletions(-) create mode 100644 src/components/settings/LicenseCard.wiring.test.ts create mode 100644 src/components/settings/UpdateCard.wiring.test.ts create mode 100644 src/components/shared/ErrorPage.wiring.test.ts diff --git a/src/components/settings/LicenseCard.wiring.test.ts b/src/components/settings/LicenseCard.wiring.test.ts new file mode 100644 index 0000000..75cbe5a --- /dev/null +++ b/src/components/settings/LicenseCard.wiring.test.ts @@ -0,0 +1,97 @@ +// LicenseCard — the purchase button of the free edition and its label (#525). +// +// LicenseCard only renders LicenseCardBody under the key of the stored license +// (#347); the body is not exported, so the test reads it from the element the +// card returns (its `type`) and calls it with that element's props. The body +// calls useState, so it runs under the shared harness (src/test/hookHarness.ts, +// #409). The license context and the machine hook are replaced by fakes, as in +// LicenseCard.render.test.tsx. The test clicks the purchase button through the +// props of the rendered tree and reads the address handed to `openUrl`. +import { describe, it, expect, vi, beforeEach } from "vitest"; +import type { ReactElement, ReactNode } from "react"; + +vi.mock("react", async (orig) => (await import("../../test/hookHarness")).fakeReact(await orig())); +vi.mock("react-i18next", async () => (await import("../../test/i18nMock")).i18nMock()); +vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() })); + +const ctx = vi.hoisted(() => ({ + openUrl: vi.fn((_url: string) => Promise.resolve()), + /** The license context of the free edition: no key stored. */ + license: { + status: "ready", + edition: "free", + features: [], + info: null, + error: null, + validating: false, + validationError: null, + revoked: false, + } as Record, +})); +vi.mock("@tauri-apps/plugin-opener", () => ({ openUrl: (url: string) => ctx.openUrl(url) })); + +vi.mock("../../contexts/LicenseContext", async (importOriginal) => ({ + ...(await importOriginal()), + useLicenseContext: () => ({ ...ctx.license, refresh: vi.fn(), submitKey: vi.fn() }), +})); + +vi.mock("../../hooks/useLicenseMachines", async (importOriginal) => { + const actual = await importOriginal(); + const state = actual.initialLicenseMachinesState; + return { + ...actual, + useLicenseMachines: () => ({ + state, + revoked: false, + activate: vi.fn(), + deactivate: vi.fn(), + toggleList: vi.fn(), + removeKey: vi.fn(), + showEmptyState: actual.selectShowEmptyState(state), + canDeactivate: actual.selectCanDeactivate(state), + canActivate: actual.selectCanActivate(state, false), + canRemoveKey: actual.selectCanRemoveKey(state), + }), + }; +}); + +import { hookHarness } from "../../test/hookHarness"; +import { find, texts } from "../../test/elementTree"; +import LicenseCard from "./LicenseCard"; + +const harness = hookHarness(); + +/** + * One render of the card's body: LicenseCardBody, read from the element that + * LicenseCard returns. LicenseCard itself calls no hook of React: it is called + * as a plain function, and only the body is mounted on the harness. + */ +function renderBody(): ReactNode { + const card = LicenseCard() as ReactElement>; + const Body = card.type as (props: Record) => ReactNode; + expect(Body).toBeTypeOf("function"); + expect(Body).not.toBe(LicenseCard); + return harness.render(() => Body(card.props)); +} + +/** The purchase button of the free edition: the one labelled « Buy Simpl'Result » (`license.purchase`). */ +function purchaseButton(tree: ReactNode) { + const buttons = find(tree, (el) => el.type === "button" && texts(el).includes("t(license.purchase)")); + expect(buttons).toHaveLength(1); + return buttons[0]; +} + +beforeEach(() => { + harness.clear(); + ctx.openUrl.mockClear(); +}); + +describe("LicenseCard — the purchase button of the free edition (#525)", () => { + it("opens the product page labelled license-card", () => { + (purchaseButton(renderBody()).props.onClick as () => void)(); + expect(ctx.openUrl).toHaveBeenCalledTimes(1); + const url = new URL(ctx.openUrl.mock.calls[0][0]); + expect(`${url.origin}${url.pathname}`).toBe("https://lacompagniemaximus.com/fr/produits/simpl-resultat"); + expect(url.searchParams.get("utm_content")).toBe("license-card"); + }); +}); diff --git a/src/components/settings/UpdateCard.wiring.test.ts b/src/components/settings/UpdateCard.wiring.test.ts new file mode 100644 index 0000000..2f1aad3 --- /dev/null +++ b/src/components/settings/UpdateCard.wiring.test.ts @@ -0,0 +1,61 @@ +// UpdateCard — the product page buttons of the free edition and their label (#525). +// +// The card calls useState, useCallback and useEffect, so it runs under the shared +// harness (src/test/hookHarness.ts, #409) to be called as a plain function and +// expose the props of the elements it renders. `useUpdater` is replaced, as in +// UpsellCtas.render.test.tsx, by the state of the free edition: `notEntitled`. +// No effect is run (`commit()`): the card's one effect only fetches release +// notes for an available update. PurchaseButton and DownloadLatestButton stay +// elements, never called: what they open with their label is proved by +// UpsellCtas.render.test.tsx. +import { describe, it, expect, vi, beforeEach } from "vitest"; +import type { ReactNode } from "react"; + +vi.mock("react", async (orig) => (await import("../../test/hookHarness")).fakeReact(await orig())); +vi.mock("react-i18next", async () => (await import("../../test/i18nMock")).i18nMock()); +vi.mock("@tauri-apps/plugin-opener", () => ({ openUrl: vi.fn(() => Promise.resolve()) })); + +vi.mock("../../contexts/LicenseContext", () => ({ + useLicenseContext: () => ({ revoked: false }), +})); + +vi.mock("../../hooks/useUpdater", () => ({ + useUpdater: () => ({ + state: { status: "notEntitled", version: null, body: null, progress: 0, contentLength: null, error: null }, + checkForUpdate: vi.fn(), + downloadAndInstall: vi.fn(), + installAndRestart: vi.fn(), + }), +})); + +import { hookHarness } from "../../test/hookHarness"; +import { find, texts } from "../../test/elementTree"; +import UpdateCard from "./UpdateCard"; +import PurchaseButton from "../shared/PurchaseButton"; +import DownloadLatestButton from "../shared/DownloadLatestButton"; +import HaveKeyButton from "../shared/HaveKeyButton"; + +const harness = hookHarness(); + +/** One render of the mounted card. */ +const render = (): ReactNode => harness.render(() => UpdateCard()); + +beforeEach(() => harness.clear()); + +describe("UpdateCard — updates locked in the free edition (#346, #525)", () => { + it("offers Get Base labelled update-locked and Download the latest version", () => { + const tree = render(); + expect(texts(tree)).toContain("t(settings.updates.notEntitled)"); + + const purchase = find(tree, (el) => el.type === PurchaseButton); + expect(purchase).toHaveLength(1); + expect(purchase[0].props.link).toBe("update-locked"); + + const download = find(tree, (el) => el.type === DownloadLatestButton); + expect(download).toHaveLength(1); + // The download button has a label of its own, fixed inside it. + expect(download[0].props).not.toHaveProperty("link"); + + expect(find(tree, (el) => el.type === HaveKeyButton)).toHaveLength(1); + }); +}); diff --git a/src/components/shared/ErrorPage.wiring.test.ts b/src/components/shared/ErrorPage.wiring.test.ts new file mode 100644 index 0000000..66e3dd6 --- /dev/null +++ b/src/components/shared/ErrorPage.wiring.test.ts @@ -0,0 +1,78 @@ +// ErrorPage — the product page buttons of the free edition and their label (#525). +// +// A static render never shows them: they appear once the update check, run by a +// click, learns that automatic updates are locked (#346). React's hooks come from +// the shared harness (src/test/hookHarness.ts, #409): the page runs as a plain +// function, its state survives from one call to the next as between two renders +// of a mounted component, and the test clicks the update check through the props +// of the rendered tree, awaits it, then renders again to read what it opened (the +// harness does not re-render after a setter). No slot is preset, so the test does +// not depend on the order of the page's hooks (docs/architecture.md, « Limites +// connues »). PurchaseButton and DownloadLatestButton stay elements, never called: +// what they open with their label is proved by UpsellCtas.render.test.tsx. +import { describe, it, expect, vi, beforeEach } from "vitest"; +import type { ReactNode } from "react"; + +vi.mock("react", async (orig) => (await import("../../test/hookHarness")).fakeReact(await orig())); +vi.mock("react-i18next", async () => (await import("../../test/i18nMock")).i18nMock()); + +const tauri = vi.hoisted(() => ({ + /** `check_entitlement` answers false: the free edition. */ + invoke: vi.fn(async (_command: string, _args?: unknown) => false as unknown), + check: vi.fn(async () => null), +})); +vi.mock("@tauri-apps/api/core", () => ({ invoke: tauri.invoke })); +vi.mock("@tauri-apps/plugin-updater", () => ({ check: tauri.check })); +vi.mock("@tauri-apps/plugin-opener", () => ({ openUrl: vi.fn(() => Promise.resolve()) })); + +import { hookHarness } from "../../test/hookHarness"; +import { find, texts } from "../../test/elementTree"; +import ErrorPage from "./ErrorPage"; +import PurchaseButton from "./PurchaseButton"; +import DownloadLatestButton from "./DownloadLatestButton"; + +const harness = hookHarness(); + +/** One render of the mounted page. */ +const render = (): ReactNode => harness.render(() => ErrorPage({})); + +/** The update check: the button labelled « Check for updates ». */ +function updateCheck(tree: ReactNode) { + const buttons = find(tree, (el) => el.type === "button" && texts(el).includes("t(error.checkUpdate)")); + expect(buttons).toHaveLength(1); + return buttons[0]; +} + +const purchases = (tree: ReactNode) => find(tree, (el) => el.type === PurchaseButton); +const downloads = (tree: ReactNode) => find(tree, (el) => el.type === DownloadLatestButton); + +beforeEach(() => { + harness.clear(); + tauri.invoke.mockClear(); + tauri.check.mockClear(); +}); + +describe("ErrorPage — updates locked in the free edition (#346, #525)", () => { + it("shows no product page button before the update check", () => { + const tree = render(); + expect(purchases(tree)).toHaveLength(0); + expect(downloads(tree)).toHaveLength(0); + }); + + it("after the check, offers Get Base labelled update-locked and Download the latest version", async () => { + await (updateCheck(render()).props.onClick as () => Promise)(); + expect(tauri.invoke).toHaveBeenCalledWith("check_entitlement", { feature: "auto-update" }); + // The free edition makes no update check. + expect(tauri.check).not.toHaveBeenCalled(); + + const opened = render(); + expect(texts(opened)).toContain("t(error.updateNotEntitled)"); + const purchase = purchases(opened); + expect(purchase).toHaveLength(1); + expect(purchase[0].props.link).toBe("update-locked"); + const download = downloads(opened); + expect(download).toHaveLength(1); + // The download button has a label of its own, fixed inside it. + expect(download[0].props).not.toHaveProperty("link"); + }); +}); diff --git a/src/components/shared/UpsellCtas.render.test.tsx b/src/components/shared/UpsellCtas.render.test.tsx index 9bb3c32..60daa23 100644 --- a/src/components/shared/UpsellCtas.render.test.tsx +++ b/src/components/shared/UpsellCtas.render.test.tsx @@ -35,6 +35,7 @@ vi.mock("../../hooks/useUpdater", () => ({ })); import { find } from "../../test/elementTree"; +import type { ProductPageLink } from "../../shared/purchaseUrl"; import UpsellGate from "./UpsellGate"; import PurchaseButton from "./PurchaseButton"; import DownloadLatestButton from "./DownloadLatestButton"; @@ -113,30 +114,75 @@ describe("UpsellGate wiring", () => { expect(purchase).toHaveLength(1); expect(purchase[0].props).not.toHaveProperty("onNavigate"); }); + + it("labels Get Base with the locked screen of a module (#525)", () => { + const tree = UpsellGate({ feature: "budget", requiredTier: "base" }); + const purchase = find(tree, (el) => el.type === PurchaseButton); + expect(purchase).toHaveLength(1); + expect(purchase[0].props.link).toBe("locked-module"); + }); }); -// Both buttons open the product page. A failure to open the browser must reach -// the in-app logs, not vanish as an unhandled rejection. -function productPageButtonTests(Button: (props: { className?: string }) => ReactNode) { - it("opens the product page in the app language", () => { +/** The address a button opened: the product page, then its UTM label (#525). */ +const productPage = (locale: "fr" | "en", link: ProductPageLink) => + `https://lacompagniemaximus.com/${locale}/produits/simpl-resultat` + + `?utm_source=app&utm_medium=desktop&utm_campaign=simpl-resultat&utm_content=${link}`; + +// Both buttons open the product page, labelled with their screen (#525). A +// failure to open the browser must reach the in-app logs, not vanish as an +// unhandled rejection. +function productPageButtonTests(render: () => ReactNode, link: ProductPageLink) { + it("opens the product page in the app language, with its label", () => { language = "en"; - const button = Button({}) as ReactElement<{ onClick: () => void }>; + const button = render() as ReactElement<{ onClick: () => void }>; button.props.onClick(); - expect(openUrl).toHaveBeenCalledWith("https://lacompagniemaximus.com/en/produits/simpl-resultat"); + expect(openUrl).toHaveBeenCalledTimes(1); + expect(openUrl).toHaveBeenCalledWith(productPage("en", link)); }); it("logs a failure to open the browser", async () => { const failure = new Error("no browser"); openUrl.mockRejectedValueOnce(failure); - const button = Button({}) as ReactElement<{ onClick: () => void }>; + const button = render() as ReactElement<{ onClick: () => void }>; button.props.onClick(); await vi.waitFor(() => expect(logError).toHaveBeenCalledWith(expect.any(String), failure)); }); } -describe("PurchaseButton", () => productPageButtonTests(PurchaseButton)); +describe("PurchaseButton", () => { + productPageButtonTests(() => PurchaseButton({ link: "update-locked" }), "update-locked"); -describe("DownloadLatestButton", () => productPageButtonTests(DownloadLatestButton)); + it("opens the label its caller gives: the locked screen's is locked-module", () => { + const button = PurchaseButton({ link: "locked-module" }) as ReactElement<{ onClick: () => void }>; + button.props.onClick(); + expect(openUrl).toHaveBeenCalledWith(productPage("fr", "locked-module")); + }); + + it("takes only its two labels, and needs one: checked by tsc, not by vitest", () => { + // If `link` were widened (to ProductPageLink or to string) or made optional, + // these directives would be unused and `npm run build` would fail. + const typeChecks = () => [ + // @ts-expect-error the license card calls purchaseUrl itself + PurchaseButton({ link: "license-card" }), + // @ts-expect-error the download label belongs to DownloadLatestButton + PurchaseButton({ link: "update-locked-download" }), + // @ts-expect-error a label is required + PurchaseButton({}), + ]; + expect(typeChecks).toBeTypeOf("function"); + }); +}); + +describe("DownloadLatestButton", () => { + productPageButtonTests(() => DownloadLatestButton({}), "update-locked-download"); + + it("takes no label: it always opens update-locked-download (checked by tsc)", () => { + const typeChecks = () => + // @ts-expect-error no link prop: the update card and the error page share one label + DownloadLatestButton({ link: "update-locked" }); + expect(typeChecks).toBeTypeOf("function"); + }); +}); describe("UpdateCard in the free edition", () => { it("offers Get Base, I already have a key and Download the latest version", () => { diff --git a/src/shared/purchaseUrl.test.ts b/src/shared/purchaseUrl.test.ts index 54e0224..f2dc814 100644 --- a/src/shared/purchaseUrl.test.ts +++ b/src/shared/purchaseUrl.test.ts @@ -1,37 +1,103 @@ import { describe, it, expect } from "vitest"; -import { purchaseUrl } from "./purchaseUrl"; +import { purchaseUrl, type ProductPageLink } from "./purchaseUrl"; // The old constant pointed at /simpl-resultat, a path the site did not serve // when #270 was filed (307 to /fr/simpl-resultat, then 404). The site has // redirected it since la-compagnie-maximus#153; the app now links the // localized product page, /{locale}/produits/simpl-resultat, directly. -describe("purchaseUrl", () => { - it("opens the French product page when the app is in French", () => { - expect(purchaseUrl("fr")).toBe( - "https://lacompagniemaximus.com/fr/produits/simpl-resultat", - ); - }); +// +// The contract with the site is that path, compiled into every released +// binary. Since #525 the URL also carries a query: a fixed UTM label set by +// the button that opens the page, never a value taken from the user. - it("opens the English product page when the app is in English", () => { - expect(purchaseUrl("en")).toBe( - "https://lacompagniemaximus.com/en/produits/simpl-resultat", - ); +const ORIGIN = "https://lacompagniemaximus.com"; +const pageOf = (locale: "fr" | "en") => `/${locale}/produits/simpl-resultat`; + +/** The four labels, one per button (decision of Max, 2026-11-02). */ +const LINKS: readonly ProductPageLink[] = [ + "locked-module", + "license-card", + "update-locked", + "update-locked-download", +]; + +/** The app's languages and the page each one opens. */ +const LANGUAGES = [ + ["fr", "fr"], + ["en", "en"], +] as const; + +/** The part of the URL the site's contract covers: origin and path, without the query. */ +const contractOf = (url: string) => { + const parsed = new URL(url); + return `${parsed.origin}${parsed.pathname}`; +}; + +describe("purchaseUrl — the path of the contract", () => { + it.each(LANGUAGES)("opens exactly the product page for %s, whatever the label", (language, locale) => { + for (const link of LINKS) { + const url = new URL(purchaseUrl(language, link)); + expect(url.origin).toBe(ORIGIN); + expect(url.pathname).toBe(pageOf(locale)); + expect(url.hash).toBe(""); + expect(url.username).toBe(""); + expect(url.password).toBe(""); + } }); it("maps regional English variants to the English page", () => { - expect(purchaseUrl("en-US")).toBe( - "https://lacompagniemaximus.com/en/produits/simpl-resultat", - ); - expect(purchaseUrl("EN")).toBe( - "https://lacompagniemaximus.com/en/produits/simpl-resultat", - ); + expect(contractOf(purchaseUrl("en-US", "locked-module"))).toBe(`${ORIGIN}${pageOf("en")}`); + expect(contractOf(purchaseUrl("EN", "license-card"))).toBe(`${ORIGIN}${pageOf("en")}`); }); it("falls back to French for any other value", () => { for (const language of ["fr-CA", "de", "", "cimode"]) { - expect(purchaseUrl(language)).toBe( - "https://lacompagniemaximus.com/fr/produits/simpl-resultat", - ); + expect(contractOf(purchaseUrl(language, "update-locked"))).toBe(`${ORIGIN}${pageOf("fr")}`); } }); }); + +describe("purchaseUrl — the UTM label (#525)", () => { + it.each(LANGUAGES)("carries the four UTM keys and no other, in a fixed order (%s)", (language) => { + for (const link of LINKS) { + const { searchParams } = new URL(purchaseUrl(language, link)); + expect([...searchParams.keys()]).toEqual(["utm_source", "utm_medium", "utm_campaign", "utm_content"]); + expect(Object.fromEntries(searchParams)).toEqual({ + utm_source: "app", + utm_medium: "desktop", + utm_campaign: "simpl-resultat", + utm_content: link, + }); + } + }); + + it("sets utm_content to the label, for each of the four", () => { + const contents = LINKS.map((link) => new URL(purchaseUrl("fr", link)).searchParams.get("utm_content")); + expect(contents).toEqual(["locked-module", "license-card", "update-locked", "update-locked-download"]); + }); + + it("writes the exact address the browser opens", () => { + expect(purchaseUrl("fr", "locked-module")).toBe( + "https://lacompagniemaximus.com/fr/produits/simpl-resultat" + + "?utm_source=app&utm_medium=desktop&utm_campaign=simpl-resultat&utm_content=locked-module", + ); + expect(purchaseUrl("en", "update-locked-download")).toBe( + "https://lacompagniemaximus.com/en/produits/simpl-resultat" + + "?utm_source=app&utm_medium=desktop&utm_campaign=simpl-resultat&utm_content=update-locked-download", + ); + }); + + it("takes only the four labels: a free string does not compile", () => { + // Checked by `tsc` (`npm run build`), not by vitest: if `link` were widened to + // `string`, these directives would be unused and the build would fail. A value + // of the user (an e-mail, a version, a key) cannot reach the URL. + const typeChecks = () => { + const fromTheUser: string = "user@example.com"; + // @ts-expect-error a label is one of the four constants of ProductPageLink + purchaseUrl("fr", fromTheUser); + // @ts-expect-error no URL without a label + purchaseUrl("fr"); + }; + expect(typeChecks).toBeTypeOf("function"); + }); +});