From 00557aa0ea969bd83de2615ae1e9b3c82f0e8a91 Mon Sep 17 00:00:00 2001 From: Aaron Lisman Date: Thu, 19 Nov 2026 16:32:44 +0000 Subject: [PATCH 1/2] Table Copy: keep a fetched copy while the table's content is unchanged The Copy button's two steps reset whenever the table answer object was replaced, even by an identical answer (a re-render, a page turn), so a first click's fetch could be dropped and "Copy Ready!" never appear. This flaked the tables e2e once on CI (#457). The reset now keys on what the copied text depends on (copyTableKey: the query, the column ids, each study's row counts), so a sort, a column or a row change still drops it. The e2e waits for the table to settle before the first click, retries the first click once if the button fell back to "Copy", and names the button's label and the status text when it fails. Tests: test/table-copy-key.test.ts (C1-C7, written by a separate agent from the spec table); two planted bugs in table-copy.ts, each failing the suites that import lib/table-tsv.ts. npm test passes; e2e tables passes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Q3ezTXaZDnWotHSveqDGjf --- app/_table/DataTable.tsx | 8 +-- lib/table-tsv.ts | 9 ++++ scripts/mutants/mutations/table-copy.ts | 6 +++ test/e2e/tables.cjs | 17 +++++-- test/table-copy-key.test.ts | 65 +++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 test/table-copy-key.test.ts diff --git a/app/_table/DataTable.tsx b/app/_table/DataTable.tsx index 3ee3d2d9..6f9e95b9 100644 --- a/app/_table/DataTable.tsx +++ b/app/_table/DataTable.tsx @@ -13,7 +13,7 @@ import { Waiting } from "../_oncoprint/OncoprintCard.tsx"; import type { ViewPageBody, TableBody } from "../../lib/api-schemas.ts"; import { ChevronLeft, ChevronRight, Columns3, Copy, Download, X } from "lucide-react"; import { copyText } from "./copy-text.ts"; -import { cutShort } from "../../lib/table-tsv.ts"; +import { copyTableKey, cutShort } from "../../lib/table-tsv.ts"; import { Button } from "../_ui/button"; import { Checkbox } from "../_ui/checkbox"; import { Input } from "../_ui/input"; @@ -443,9 +443,11 @@ export function DataTable({ t, query, updating // the portal's Copy, in its two steps: a first click fetches the table.tsv text ("Copy Ready!"), a second copies // it within that click, which the browser's clipboard needs (a copy after a long fetch is refused) const [copyState, setCopyState] = useState<{ phase: "idle" } | { phase: "loading" } | { phase: "ready"; text: string } | { phase: "done" } | { phase: "failed"; why: string }>({ phase: "idle" }); - // a fetch still running when the table changes (a sort, a filter) answers for the old table: its generation tells + // a fetch still running when the table changes (a sort, a filter, other rows) answers for the old table: its + // generation tells. The same table answered again (a re-render, a page turn) keeps what was fetched. const copyGen = useRef(0); - useEffect(() => { copyGen.current++; setCopyState({ phase: "idle" }); }, [t]); + const copyKey = copyTableKey(t); + useEffect(() => { copyGen.current++; setCopyState({ phase: "idle" }); }, [copyKey]); const fetchCopy = async () => { const g = ++copyGen.current; setCopyState({ phase: "loading" }); diff --git a/lib/table-tsv.ts b/lib/table-tsv.ts index 5026864b..87f17451 100644 --- a/lib/table-tsv.ts +++ b/lib/table-tsv.ts @@ -27,3 +27,12 @@ export function cutShort(text: string): string | null { const line = text.split(/\r?\n/).find((l) => l.startsWith(INCOMPLETE_MARK)); return line === undefined ? null : line.slice(INCOMPLETE_MARK.length).replace(/^[:\s]+/, "").trim() || "the table was cut short"; } + +/** + * What the copied text depends on, as one string: the table's query (its sort among it), its columns and every + * study's row counts. Two answers with the same key copy the same text, so a fetched copy outlives a re-render + * or a page turn, and is dropped when the query, the columns or the rows change. + */ +export function copyTableKey(t: { query: unknown; columns: { id: string }[]; lists: { cohort: string; study: string; total: number; all: number }[]; total: number; all: number }): string { + return JSON.stringify([t.query, t.columns.map((c) => c.id), t.lists.map((l) => [l.cohort, l.study, l.total, l.all]), t.total, t.all]); +} diff --git a/scripts/mutants/mutations/table-copy.ts b/scripts/mutants/mutations/table-copy.ts index 81d51549..3717b7ae 100644 --- a/scripts/mutants/mutations/table-copy.ts +++ b/scripts/mutants/mutations/table-copy.ts @@ -16,4 +16,10 @@ export const mutants: Mutant[] = [ { id: "copy-no-fallback-on-refusal", layer: "counts", guard: "unit", file: "app/_table/copy-text.ts", effect: "A secure page whose Clipboard API refuses gives up instead of trying the textarea copy.", find: " await navigator.clipboard.writeText(text);\n return;\n } catch {", replace: " await navigator.clipboard.writeText(text);\n return;\n } finally {" }, + { id: "copy-key-ignores-rows", layer: "counts", guard: "unit", file: "lib/table-tsv.ts", + effect: "A fetched copy survives a change to a study's rows, so Copy Ready! pastes the old table.", + find: "t.lists.map((l) => [l.cohort, l.study, l.total, l.all]), t.total, t.all]", replace: "t.total]" }, + { id: "copy-key-ignores-sort", layer: "counts", guard: "unit", file: "lib/table-tsv.ts", + effect: "A fetched copy survives a sort, so Copy Ready! pastes the rows in the old order.", + find: "return JSON.stringify([t.query, t.columns", replace: "return JSON.stringify([null, t.columns" }, ]; diff --git a/test/e2e/tables.cjs b/test/e2e/tables.cjs index 41b288de..6c9095ed 100644 --- a/test/e2e/tables.cjs +++ b/test/e2e/tables.cjs @@ -92,17 +92,26 @@ async function tables({ browser, ok, BASE }) { // Copy (MUT-TB-2): before the download, in two clicks, the same text as table.tsv on the clipboard, every row whatever the filter await ctx.grantPermissions(["clipboard-read", "clipboard-write"], { origin: new URL(BASE).origin }); // the portal's two steps: the first click fetches every row ("Copy Ready!"), the second copies within its own click - await page.getByRole("button", { name: "Copy", exact: true }).first().click(); - const ready = await page.getByRole("button", { name: "Copy Ready!" }).first().waitFor({ timeout: 60000 }).then(() => true, () => false); - ok("table: a first Copy click fetches the rows and offers \"Copy Ready!\"", ready); + // the table settles first (no "updating…"), so the click lands on the table the copy is for + await page.getByText("updating…").first().waitFor({ state: "hidden", timeout: 60000 }).catch(() => {}); + const copyButton = page.getByRole("button", { name: /^Copy( Ready!|ing…)?$/ }).first(); + const copyLabel = async () => ((await copyButton.innerText().catch(() => "")) || "").trim(); + const statusText = async () => ((await page.getByRole("status").allInnerTexts().catch(() => [])) || []).join(" | "); + let ready = false; + for (let attempt = 0; attempt < 2 && !ready; attempt++) { + if (await copyLabel() === "Copy") await copyButton.click(); + ready = await page.getByRole("button", { name: "Copy Ready!" }).first().waitFor({ timeout: 60000 }).then(() => true, () => false); + } + ok("table: a first Copy click fetches the rows and offers \"Copy Ready!\"", ready, ready ? "" : "button \"" + (await copyLabel()) + "\" · status " + JSON.stringify(await statusText())); if (ready) await page.getByRole("button", { name: "Copy Ready!" }).first().click(); const copied = await page.getByRole("status").filter({ hasText: "Copied!" }).first().waitFor({ timeout: 60000 }).then(() => true, () => false); + const copyStatus = copied ? "" : " · status " + JSON.stringify(await statusText()); const clip = copied ? await page.evaluate(() => navigator.clipboard.readText()) : ""; const clipLines = clip.split(/\r?\n/); if (clipLines[clipLines.length - 1] === "") clipLines.pop(); const same = clip === tsv; ok("table: Copy puts the table.tsv's text on the clipboard and says \"Copied!\"", copied && same, - (copied ? "Copied!" : "no Copied! status") + " · " + (same ? "identical to table.tsv" : (clipLines.length - 1) + " rows vs " + (lines.length - 1) + ", header " + (clipLines[0] === lines[0] ? "same" : JSON.stringify((clipLines[0] || "").slice(0, 60))))); + (copied ? "Copied!" : "no Copied! status" + copyStatus) + " · " + (same ? "identical to table.tsv" : (clipLines.length - 1) + " rows vs " + (lines.length - 1) + ", header " + (clipLines[0] === lines[0] ? "same" : JSON.stringify((clipLines[0] || "").slice(0, 60))))); } catch (e) { ok("table: the mutation table's controls", false, String(e.message).split("\n")[0].slice(0, 200)); } diff --git a/test/table-copy-key.test.ts b/test/table-copy-key.test.ts new file mode 100644 index 00000000..d7762345 --- /dev/null +++ b/test/table-copy-key.test.ts @@ -0,0 +1,65 @@ +// copyTableKey: two table answers that would copy the same text share a key. +// +// C1 two distinct but deep-equal answers get the same key +// C2 a page turn (rows, offset, pageSize differ) keeps the key +// C3 a changed sort changes the key +// C4 one more column changes the key +// C5 one study's `all` changing (totals unchanged) changes the key +// C6 a changed total changes the key +// C7 a list entry's cohort changing (counts the same) changes the key + +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { copyTableKey } from "../lib/table-tsv.ts"; + +const answer = (over: Record = {}) => ({ + query: { table: "mutations", sort: [{ id: "gene", dir: "asc" }], filters: [] }, + columns: [{ id: "gene" }, { id: "protein" }], + lists: [ + { cohort: "c1", study: "s1", total: 10, all: 12 }, + { cohort: "c1", study: "s2", total: 5, all: 7 }, + ], + total: 15, + all: 19, + rows: [{ gene: "TP53" }], + offset: 0, + pageSize: 50, + ...over, +}); + +test("C1 deep-equal answers share a key", () => { + assert.equal(copyTableKey(answer()), copyTableKey(answer())); +}); + +test("C2 a page turn keeps the key", () => { + assert.equal(copyTableKey(answer()), copyTableKey(answer({ rows: [{ gene: "KRAS" }, { gene: "EGFR" }], offset: 50, pageSize: 100 }))); +}); + +test("C3 a changed sort changes the key", () => { + const q = { table: "mutations", sort: [{ id: "gene", dir: "desc" }], filters: [] }; + assert.notEqual(copyTableKey(answer()), copyTableKey(answer({ query: q }))); +}); + +test("C4 one more column changes the key", () => { + assert.notEqual(copyTableKey(answer()), copyTableKey(answer({ columns: [{ id: "gene" }, { id: "protein" }, { id: "vaf" }] }))); +}); + +test("C5 one study's all changing changes the key", () => { + const lists = [ + { cohort: "c1", study: "s1", total: 10, all: 13 }, + { cohort: "c1", study: "s2", total: 5, all: 6 }, + ]; + assert.notEqual(copyTableKey(answer()), copyTableKey(answer({ lists }))); +}); + +test("C6 a changed total changes the key", () => { + assert.notEqual(copyTableKey(answer()), copyTableKey(answer({ total: 16 }))); +}); + +test("C7 a list entry's cohort changing changes the key", () => { + const lists = [ + { cohort: "c2", study: "s1", total: 10, all: 12 }, + { cohort: "c1", study: "s2", total: 5, all: 7 }, + ]; + assert.notEqual(copyTableKey(answer()), copyTableKey(answer({ lists }))); +}); From 162d5fcff90bfbdb1f46e975f5d3d88a9253baaa Mon Sep 17 00:00:00 2001 From: Aaron Lisman Date: Thu, 19 Nov 2026 16:44:45 +0000 Subject: [PATCH 2/2] Table Copy: key the fetched copy on the document it is fetched from Review fix. copyTableKey keyed only on the answer's query, columns and row counts, but the copy is fetched from the whole view document. A change to the document alone (the drivers setting, a gene or cohort swap with equal counts, the OncoKB or Genome Nexus pins) kept a stale copy, so Copy Ready! could paste the old rows. The key now starts from docJson(), the view's index and its table. Tests: C8-C11 in test/table-copy-key.test.ts (a separate agent); C1-C7 pass the same source on both sides. Planted bug copy-key-ignores-doc; the three copy-key planted bugs each fail the suites that import lib/table-tsv.ts. npm test passes; e2e tables passes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Q3ezTXaZDnWotHSveqDGjf --- app/_table/DataTable.tsx | 7 +++-- lib/table-tsv.ts | 12 ++++---- scripts/mutants/mutations/table-copy.ts | 5 +++- test/table-copy-key.test.ts | 38 ++++++++++++++++++++----- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/app/_table/DataTable.tsx b/app/_table/DataTable.tsx index 6f9e95b9..8f211f48 100644 --- a/app/_table/DataTable.tsx +++ b/app/_table/DataTable.tsx @@ -443,10 +443,11 @@ export function DataTable({ t, query, updating // the portal's Copy, in its two steps: a first click fetches the table.tsv text ("Copy Ready!"), a second copies // it within that click, which the browser's clipboard needs (a copy after a long fetch is refused) const [copyState, setCopyState] = useState<{ phase: "idle" } | { phase: "loading" } | { phase: "ready"; text: string } | { phase: "done" } | { phase: "failed"; why: string }>({ phase: "idle" }); - // a fetch still running when the table changes (a sort, a filter, other rows) answers for the old table: its - // generation tells. The same table answered again (a re-render, a page turn) keeps what was fetched. + // a fetch still running when the table changes (its document: a sort, a filter, a gene, a cohort, a driver + // setting; or its rows) answers for the old table: its generation tells. The same table answered again (a + // re-render, a page turn) keeps what was fetched. const copyGen = useRef(0); - const copyKey = copyTableKey(t); + const copyKey = copyTableKey(t, { doc: docJson ? docJson() : "", view: vi, ...(table ? { table } : {}) }); useEffect(() => { copyGen.current++; setCopyState({ phase: "idle" }); }, [copyKey]); const fetchCopy = async () => { const g = ++copyGen.current; diff --git a/lib/table-tsv.ts b/lib/table-tsv.ts index 87f17451..e7da1cd6 100644 --- a/lib/table-tsv.ts +++ b/lib/table-tsv.ts @@ -29,10 +29,12 @@ export function cutShort(text: string): string | null { } /** - * What the copied text depends on, as one string: the table's query (its sort among it), its columns and every - * study's row counts. Two answers with the same key copy the same text, so a fetched copy outlives a re-render - * or a page turn, and is dropped when the query, the columns or the rows change. + * What the copied text depends on, as one string: the document it is fetched from (the view's cohorts, genes, + * driver settings and query among it), the view's place in it and which of its tables, then the answer's query, + * columns and every study's row counts (the data the document read). Two answers with the same key copy the same + * text, so a fetched copy outlives a re-render or a page turn, and is dropped when any of these change. */ -export function copyTableKey(t: { query: unknown; columns: { id: string }[]; lists: { cohort: string; study: string; total: number; all: number }[]; total: number; all: number }): string { - return JSON.stringify([t.query, t.columns.map((c) => c.id), t.lists.map((l) => [l.cohort, l.study, l.total, l.all]), t.total, t.all]); +export function copyTableKey(t: { query: unknown; columns: { id: string }[]; lists: { cohort: string; study: string; total: number; all: number }[]; total: number; all: number }, + from: { doc: string; view: number; table?: string }): string { + return JSON.stringify([from.doc, from.view, from.table ?? null, t.query, t.columns.map((c) => c.id), t.lists.map((l) => [l.cohort, l.study, l.total, l.all]), t.total, t.all]); } diff --git a/scripts/mutants/mutations/table-copy.ts b/scripts/mutants/mutations/table-copy.ts index 3717b7ae..d84976c8 100644 --- a/scripts/mutants/mutations/table-copy.ts +++ b/scripts/mutants/mutations/table-copy.ts @@ -21,5 +21,8 @@ export const mutants: Mutant[] = [ find: "t.lists.map((l) => [l.cohort, l.study, l.total, l.all]), t.total, t.all]", replace: "t.total]" }, { id: "copy-key-ignores-sort", layer: "counts", guard: "unit", file: "lib/table-tsv.ts", effect: "A fetched copy survives a sort, so Copy Ready! pastes the rows in the old order.", - find: "return JSON.stringify([t.query, t.columns", replace: "return JSON.stringify([null, t.columns" }, + find: "from.table ?? null, t.query, t.columns", replace: "from.table ?? null, null, t.columns" }, + { id: "copy-key-ignores-doc", layer: "counts", guard: "unit", file: "lib/table-tsv.ts", + effect: "A fetched copy survives a change to the document alone (a driver setting, a gene or cohort with equal counts, a pin), so Copy Ready! pastes the old rows.", + find: "JSON.stringify([from.doc, from.view,", replace: "JSON.stringify([null, from.view," }, ]; diff --git a/test/table-copy-key.test.ts b/test/table-copy-key.test.ts index d7762345..4a852d54 100644 --- a/test/table-copy-key.test.ts +++ b/test/table-copy-key.test.ts @@ -7,11 +7,17 @@ // C5 one study's `all` changing (totals unchanged) changes the key // C6 a changed total changes the key // C7 a list entry's cohort changing (counts the same) changes the key +// C8 a changed document (counts equal) changes the key +// C9 a different view index changes the key +// C10 a different table of the view changes the key +// C11 table absent on both sides, deep-equal but distinct from objects, keeps the key import { test } from "node:test"; import assert from "node:assert/strict"; import { copyTableKey } from "../lib/table-tsv.ts"; +const from = { doc: '{"views":[]}', view: 0 }; + const answer = (over: Record = {}) => ({ query: { table: "mutations", sort: [{ id: "gene", dir: "asc" }], filters: [] }, columns: [{ id: "gene" }, { id: "protein" }], @@ -28,20 +34,20 @@ const answer = (over: Record = {}) => ({ }); test("C1 deep-equal answers share a key", () => { - assert.equal(copyTableKey(answer()), copyTableKey(answer())); + assert.equal(copyTableKey(answer(), from), copyTableKey(answer(), from)); }); test("C2 a page turn keeps the key", () => { - assert.equal(copyTableKey(answer()), copyTableKey(answer({ rows: [{ gene: "KRAS" }, { gene: "EGFR" }], offset: 50, pageSize: 100 }))); + assert.equal(copyTableKey(answer(), from), copyTableKey(answer({ rows: [{ gene: "KRAS" }, { gene: "EGFR" }], offset: 50, pageSize: 100 }), from)); }); test("C3 a changed sort changes the key", () => { const q = { table: "mutations", sort: [{ id: "gene", dir: "desc" }], filters: [] }; - assert.notEqual(copyTableKey(answer()), copyTableKey(answer({ query: q }))); + assert.notEqual(copyTableKey(answer(), from), copyTableKey(answer({ query: q }), from)); }); test("C4 one more column changes the key", () => { - assert.notEqual(copyTableKey(answer()), copyTableKey(answer({ columns: [{ id: "gene" }, { id: "protein" }, { id: "vaf" }] }))); + assert.notEqual(copyTableKey(answer(), from), copyTableKey(answer({ columns: [{ id: "gene" }, { id: "protein" }, { id: "vaf" }] }), from)); }); test("C5 one study's all changing changes the key", () => { @@ -49,11 +55,11 @@ test("C5 one study's all changing changes the key", () => { { cohort: "c1", study: "s1", total: 10, all: 13 }, { cohort: "c1", study: "s2", total: 5, all: 6 }, ]; - assert.notEqual(copyTableKey(answer()), copyTableKey(answer({ lists }))); + assert.notEqual(copyTableKey(answer(), from), copyTableKey(answer({ lists }), from)); }); test("C6 a changed total changes the key", () => { - assert.notEqual(copyTableKey(answer()), copyTableKey(answer({ total: 16 }))); + assert.notEqual(copyTableKey(answer(), from), copyTableKey(answer({ total: 16 }), from)); }); test("C7 a list entry's cohort changing changes the key", () => { @@ -61,5 +67,23 @@ test("C7 a list entry's cohort changing changes the key", () => { { cohort: "c2", study: "s1", total: 10, all: 12 }, { cohort: "c1", study: "s2", total: 5, all: 7 }, ]; - assert.notEqual(copyTableKey(answer()), copyTableKey(answer({ lists }))); + assert.notEqual(copyTableKey(answer(), from), copyTableKey(answer({ lists }), from)); +}); + +test("C8 a changed document changes the key", () => { + const doc1 = '{"views":[{"driver":{"oncokb":true}}]}'; + const doc2 = '{"views":[{"driver":{"oncokb":false}}]}'; + assert.notEqual(copyTableKey(answer(), { doc: doc1, view: 0 }), copyTableKey(answer(), { doc: doc2, view: 0 })); +}); + +test("C9 a different view index changes the key", () => { + assert.notEqual(copyTableKey(answer(), { ...from, view: 0 }), copyTableKey(answer(), { ...from, view: 1 })); +}); + +test("C10 a different table of the view changes the key", () => { + assert.notEqual(copyTableKey(answer(), { ...from, table: "mutations" }), copyTableKey(answer(), { ...from, table: "sv" })); +}); + +test("C11 table absent on both sides keeps the key", () => { + assert.equal(copyTableKey(answer(), { doc: '{"views":[]}', view: 0 }), copyTableKey(answer(), { doc: '{"views":[]}', view: 0 })); });