Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/_table/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -443,9 +443,12 @@ export function DataTable<V extends string | number | null>({ 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 (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);
useEffect(() => { copyGen.current++; setCopyState({ phase: "idle" }); }, [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;
setCopyState({ phase: "loading" });
Expand Down
11 changes: 11 additions & 0 deletions lib/table-tsv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ 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 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 },
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]);
}
9 changes: 9 additions & 0 deletions scripts/mutants/mutations/table-copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ 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: "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," },
];
17 changes: 13 additions & 4 deletions test/e2e/tables.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
89 changes: 89 additions & 0 deletions test/table-copy-key.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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
// 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<string, unknown> = {}) => ({
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(), from), copyTableKey(answer(), from));
});

test("C2 a page turn keeps the key", () => {
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(), from), copyTableKey(answer({ query: q }), from));
});

test("C4 one more column changes the key", () => {
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", () => {
const lists = [
{ cohort: "c1", study: "s1", total: 10, all: 13 },
{ cohort: "c1", study: "s2", total: 5, all: 6 },
];
assert.notEqual(copyTableKey(answer(), from), copyTableKey(answer({ lists }), from));
});

test("C6 a changed total changes the key", () => {
assert.notEqual(copyTableKey(answer(), from), copyTableKey(answer({ total: 16 }), from));
});

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(), 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 }));
});
Loading