Simpl-Resultat/src/shared/purchaseUrl.test.ts
le king fu 2362117cb5 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 <noreply@anthropic.com>
2026-11-02 21:57:24 -05:00

103 lines
4.2 KiB
TypeScript

import { describe, it, expect } from "vitest";
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.
//
// 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.
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(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(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");
});
});