diff --git a/src/__integration__/readme-guard.test.ts b/src/__integration__/readme-guard.test.ts new file mode 100644 index 0000000..79aab43 --- /dev/null +++ b/src/__integration__/readme-guard.test.ts @@ -0,0 +1,166 @@ +/** + * Guard: the README, the repository's home page on Forgejo, sends its readers to Forgejo and to + * the product page, never to the GitHub copy, and does not claim that no data leaves the + * computer (#523). + * + * What it protects. Until #523 the README sent downloads to a GitHub copy frozen at v0.4.5, + * showed a version badge read from that copy, described a release built by GitHub Actions and + * opened on « 100 % locale … Aucune donnée ne quitte votre ordinateur », false since the app + * talks to the licence server. `readmeOffences` flags: + * 1. any match of /github/i: a URL, a badge (`img.shields.io/github/`), « GitHub Actions »; + * 2. « 100 % local » or « Aucune donnée ne quitte », also across a line break or a no-break + * space; + * 3. a missing link to the French product page or to the latest Forgejo release: a Markdown + * link target, `]()`, so the URL in a code span or in plain text does not count. + * + * The rule of line 3. Its sentence reuses the list of the servers the app contacts, as the + * in-app guide gives it in `docs.settings.tips[6]` (`src/i18n/locales/fr.json` and `en.json`): + * on its own, only the licence server, and only when a key is stored; updates, the Maximus + * account, feedback and market prices only on request. When that list changes, line 3 follows. + * A sentence of the guide that does not describe a connection, such as the label that the + * product page links carry (#525), does not go up into the README. This guard does not compare + * the two texts: the review does. + * + * The trap. `check-frontend.yml` ignores `*.md` at the root (`paths-ignore`), so a PR that only + * touches `README.md` does not run this guard: a README that fails it only turns red the next + * PR that runs `check-frontend`, unrelated to it. The review of such a PR runs it by hand, + * `npx vitest run src/__integration__/readme-guard.test.ts` (`REVIEW.md`, « Pièges de revue »). + * + * Each check is a test of its own on the README, passing ones included, after a canary that + * proves the file read is the README. The samples below check that each check flags what it + * targets and lets a conforming text through. + */ + +import { describe, it, expect } from "vitest"; +import { readFileSync } from "fs"; +import { resolve } from "path"; + +const README = readFileSync(resolve(import.meta.dirname, "..", "..", "README.md"), "utf8"); + +const PRODUCT_PAGE = "https://lacompagniemaximus.com/fr/produits/simpl-resultat"; +const LATEST_RELEASE = "https://git.lacompagniemaximus.com/maximus/simpl-resultat/releases/latest"; + +const NO_GITHUB = "no GitHub (URL, badge, « GitHub Actions »)"; +const NO_FULLY_LOCAL = "no « 100 % local »"; +const NO_DATA_LEAVES = "no « Aucune donnée ne quitte »"; +const LINKS_PRODUCT_PAGE = "a link to the French product page"; +const LINKS_LATEST_RELEASE = "a link to the latest Forgejo release"; + +// `g` only to list every match. `\s` also matches a line break and the no-break spaces of +// French typography (U+00A0, U+202F): a sentence wrapped over two lines, or typed with one of +// them, is still caught. +const FORBIDDEN: [check: string, pattern: RegExp][] = [ + [NO_GITHUB, /github/gi], + [NO_FULLY_LOCAL, /100\s*%\s*local/gi], + [NO_DATA_LEAVES, /aucune\s+donnée\s+ne\s+quitte/gi], +]; +const REQUIRED_LINKS: [check: string, url: string][] = [ + [LINKS_PRODUCT_PAGE, PRODUCT_PAGE], + [LINKS_LATEST_RELEASE, LATEST_RELEASE], +]; +const CHECKS = [...FORBIDDEN, ...REQUIRED_LINKS].map(([check]) => check); + +interface Offence { + check: string; + /** `line : ` for a forbidden mention, `missing` for a required link. */ + at: string; +} + +/** The offences of a README text, none when it conforms. */ +function readmeOffences(text: string): Offence[] { + const lines = text.split("\n"); + const mentions = FORBIDDEN.flatMap(([check, pattern]) => + [...text.matchAll(pattern)].map((match) => { + const line = text.slice(0, match.index).split("\n").length; + return { check, at: `line ${line}: ${lines[line - 1].trim()}` }; + }), + ); + const missing = REQUIRED_LINKS.filter(([, url]) => !text.includes(`](${url})`)).map( + ([check]) => ({ check, at: "missing" }), + ); + return [...mentions, ...missing]; +} + +/** The checks that a text fails, each named once. */ +function failedChecks(text: string): string[] { + return [...new Set(readmeOffences(text).map((offence) => offence.check))]; +} + +const INTRO = + "Application de bureau pour importer, catégoriser et analyser vos transactions financières " + + "personnelles ou d'entreprise. Vos données restent sur votre ordinateur, et l'application " + + "fonctionne sans connexion : d'elle-même, elle ne contacte que le serveur de licences, et " + + "seulement si une clé est enregistrée ; les mises à jour, la connexion au compte Maximus, la " + + "rétroaction et les cours du marché ne partent qu'à votre demande."; +const PRODUCT_LINE = `Certaines fonctionnalités demandent la licence Base : voir le tableau des éditions sur la [page produit](${PRODUCT_PAGE}).`; +const RELEASE_LINE = ` - \`.rpm\` pour Fedora / openSUSE, depuis la [dernière release](${LATEST_RELEASE})`; +const CONFORMING = [ + "# Simpl'Résultat", + INTRO, + PRODUCT_LINE, + RELEASE_LINE, + "| [SQLite](https://www.sqlite.org/) | Base de données locale |", +].join("\n\n"); + +describe("README guard (#523)", () => { + it("reads the README at the root of the repository", () => { + expect(README.split("\n")[0]).toBe("# Simpl'Résultat"); + expect(README).toContain("\n## Installation\n"); + }); + + it.each(CHECKS)("the README passes the check: %s", (check) => { + const found = readmeOffences(README).filter((offence) => offence.check === check); + expect(found.map((offence) => offence.at)).toEqual([]); + }); + + it.each([ + "1. Rendez-vous sur la page [**Releases**](https://github.com/Le-King-Fu/simpl-resultat/releases/latest)", + "![Version](https://img.shields.io/github/v/release/Le-King-Fu/simpl-resultat?label=version)", + "Le workflow GitHub Actions compile automatiquement l'application pour Windows et Linux.", + "Copie archivée : https://GITHUB.COM/Le-King-Fu/simpl-resultat", + ])("flags GitHub in %j", (line) => { + expect(failedChecks(`${CONFORMING}\n\n${line}\n`)).toEqual([NO_GITHUB]); + }); + + it.each([ + ["the old line 3", "Application de bureau 100 % locale pour importer, catégoriser et analyser."], + ["no space", "Une application 100% locale."], + ["a no-break space (U+00A0)", "Une application 100\u00a0% locale."], + ["a narrow no-break space (U+202F), in capitals", "Une application 100\u202f% LOCALE."], + ["a line break", "Une application 100 %\nlocale."], + ])("flags « 100 % local »: %s", (_how, line) => { + expect(failedChecks(`${CONFORMING}\n\n${line}\n`)).toEqual([NO_FULLY_LOCAL]); + }); + + it.each([ + ["the old line 3", "Aucune donnée ne quitte votre ordinateur."], + ["a line break", "Aucune donnée\nne quitte votre ordinateur."], + ])("flags « Aucune donnée ne quitte »: %s", (_how, line) => { + expect(failedChecks(`${CONFORMING}\n\n${line}\n`)).toEqual([NO_DATA_LEAVES]); + }); + + it.each([ + [LINKS_PRODUCT_PAGE, "removed", PRODUCT_LINE, ""], + [LINKS_PRODUCT_PAGE, "in a code span", PRODUCT_LINE, `Voir \`${PRODUCT_PAGE}\`.`], + [LINKS_PRODUCT_PAGE, "in English", PRODUCT_LINE, PRODUCT_LINE.replace("/fr/", "/en/")], + [LINKS_LATEST_RELEASE, "removed", RELEASE_LINE, ""], + [LINKS_LATEST_RELEASE, "in plain text", RELEASE_LINE, `Releases : ${LATEST_RELEASE}`], + [ + LINKS_LATEST_RELEASE, + "to the list of releases", + RELEASE_LINE, + RELEASE_LINE.replace("/releases/latest", "/releases"), + ], + ])("flags the lack of %s, link %s", (check, _how, line, replacement) => { + expect(failedChecks(CONFORMING.replace(line, replacement))).toEqual([check]); + }); + + it.each([ + "", + "Le tag déclenche `.forgejo/workflows/release.yml`, qui publie la release avec son `latest.json`.", + "Les rapports couvrent 100 % des transactions importées.", + "Aucune donnée comptable n'est envoyée.", + ])("lets the conforming text through, plus %j", (line) => { + expect(readmeOffences(`${CONFORMING}\n\n${line}\n`)).toEqual([]); + }); +});