From 9c790a7d1cf05183c182096b8ff27b79d78ecc39 Mon Sep 17 00:00:00 2001 From: Alessio Caiazza Date: Sat, 31 Oct 2026 13:19:00 +0100 Subject: [PATCH] desktop: Drop wall-clock limits from mention tests The hostile input tests compared elapsed time against fixed limits, so they measured the machine as well as the code. They fail on slower or loaded builders, such as the nixpkgs x86_64-linux check phase, even though rendering stays linear. Compare the CPU time of an input against one four times as long instead. Linear rendering gives about 4 and a quadratic regression about 16. CPU time is needed because a wall-clock ratio inflates under bursty load, when a short run fits between bursts and a long one does not. Rendering past the reference limit gets its own test, since the link count cannot catch a limit that stops linking but keeps rescanning. The bare reference test now puts a valid reference before the long run, so giving up is visible in the result without any timing. AI-assisted: Claude Code (claude-opus-5-5) --- desktop/tests/unit/mentions.test.ts | 45 ++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/desktop/tests/unit/mentions.test.ts b/desktop/tests/unit/mentions.test.ts index f457a7642..dc1305a21 100644 --- a/desktop/tests/unit/mentions.test.ts +++ b/desktop/tests/unit/mentions.test.ts @@ -103,9 +103,7 @@ describe("matchBareReference", () => { }); test("gives up on a run too long to be a reference", () => { - const start = performance.now(); - expect(matchBareReference(`rad:z${"'a".repeat(50_000)}`)).toBeUndefined(); - expect(performance.now() - start).toBeLessThan(50); + expect(matchBareReference(`${rid}${"'a".repeat(50_000)}`)).toBeUndefined(); }); test("leaves an invalid reference as prose", () => { @@ -307,18 +305,43 @@ describe("describeLink", () => { }); describe("hostile input", () => { - test("renders a run of references in linear time", () => { - const start = performance.now(); - void markdownWithExtensions.parse("rad:".repeat(25_000)); - expect(performance.now() - start).toBeLessThan(500); - }); + const renderCpuTime = (src: string) => { + const start = process.cpuUsage(); + void markdownWithExtensions.parse(src); + const { user, system } = process.cpuUsage(start); + return user + system; + }; + + const growthOverFourfoldInput = (unit: string, count: number) => { + let small = Infinity; + let large = Infinity; + for (let round = 0; round < 5; round++) { + small = Math.min(small, renderCpuTime(unit.repeat(count))); + large = Math.min(large, renderCpuTime(unit.repeat(4 * count))); + } + return large / small; + }; + + test( + "renders a run of references in linear time", + { timeout: 120_000 }, + () => { + expect(growthOverFourfoldInput("rad:", 1_250)).toBeLessThan(8); + }, + ); + + test( + "renders references past the limit in linear time", + { timeout: 120_000 }, + () => { + expect(growthOverFourfoldInput(`${rid} `, 625)).toBeLessThan(8); + }, + ); test("links at most a fixed number of references per document", () => { - const start = performance.now(); const html = markdownWithExtensions.parse( - `${rid} `.repeat(5_000), + `${rid} `.repeat(1_000), ) as string; - expect(performance.now() - start).toBeLessThan(1_000); expect(html.match(/