mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 17:13:33 +01:00
feat(viewers): sort, filter, summarize, group and chart tables
The CSV/TSV viewer (explorer, gists, the author's preview) becomes an analysis view that runs on the anonymized text already in the page: click-to-sort headers (numbers, dates, natural text; stable, empty cells last), a search box and per-column range or value filters, a summary row (Average by default; Sum, Median, Min, Max, Count, Std dev) over the filtered rows, group by with an aggregate, column stats with a histogram, copy as CSV or Markdown and download of the current view, and a Chart view (bar, line, scatter) drawn in inline SVG. The view lives in the URL fragment (#table:group=model, #table-2:... for a gist file) so a link can carry it; links without it open as before. Markdown and notebook tables (pandas DataFrames) get sortable headers, an optional summary row and "Open as table" for the full view. Redaction slots keep rendering; sorting uses the XXXX-n text. The 1 MB limit holds: rows render 200 at a time as one escaped HTML string while aggregates, filters and charts use every row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
98bf382fa8
commit
43e92bef53
19 files changed
+2094
-98
No files matched your search
@@ -32,7 +32,7 @@ function harness(date) {
|
||||
context.nextTick = fn => Promise.resolve().then(fn);
|
||||
context.window.document = context.document;
|
||||
// Modules the controllers import: outline helpers, the diff parser and the reviewer pages.
|
||||
for (const name of ["outline.js", "formatters.js", "review-pages.js"]) {
|
||||
for (const name of ["outline.js", "formatters.js", "table-analysis.js", "review-pages.js"]) {
|
||||
const moduleSource = fs.readFileSync(path.join(__dirname, "../public/script", name), "utf8");
|
||||
vm.runInNewContext(moduleSource.replace(/^import .*;$/gm, "").replace(/export function/g, "function").replace(/export const/g, "var"), context);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
const { expect } = require("chai");
|
||||
const { JSDOM } = require("jsdom");
|
||||
const path = require("path");
|
||||
const { pathToFileURL } = require("url");
|
||||
|
||||
const load = (name) => import(pathToFileURL(path.join(__dirname, "../public/script", name)).href);
|
||||
|
||||
describe("Table analysis", function () {
|
||||
let T, chart, formatters;
|
||||
before(async () => {
|
||||
T = await load("table-analysis.js");
|
||||
chart = await load("table-chart.js");
|
||||
formatters = await load("formatters.js");
|
||||
});
|
||||
const table = (text, sep = ",") => T.analyzeTable(formatters.parseDelimited(text, sep));
|
||||
const all = (t) => [...Array(t.size).keys()];
|
||||
const RESULTS = "model,seed,accuracy,plausible\nBaseline,1,71.0,410\nBaseline,2,71.4,414\n+ Aug.,1,73.8,445\n+ Aug.,2,74.2,449\nOurs,1,78.7,501\nOurs,2,79.1,\n";
|
||||
|
||||
describe("numeric parsing", () => {
|
||||
it("reads grouped thousands, percents, exponents, signs and the Unicode minus", () => {
|
||||
expect(["1,234", "12%", "1e-3", "-0.5", "+3", "−2", ".5", " 7 ", "1,234.5"].map(T.parseNumber)).to.deep.equal([1234, 12, 0.001, -0.5, 3, -2, 0.5, 7, 1234.5]);
|
||||
});
|
||||
it("rejects text, misplaced separators and bare signs", () => {
|
||||
expect(["1,2", "12,34", "abc", "-", "", "1.2.3", "0x10", "Infinity", "12 %"].map(T.parseNumber)).to.deep.equal([null, null, null, null, null, null, null, null, null]);
|
||||
});
|
||||
it("counts decimals as written, including exponents", () => {
|
||||
expect(["78.90", "1e-3", "1.38e-3", "12", "2.5e2", "0.3"].map(T.decimalsOf)).to.deep.equal([2, 3, 5, 0, 0, 1]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("type detection", () => {
|
||||
it("detects numbers, dates and text, ignoring empty and NA cells", () => {
|
||||
expect(T.detectType(["1", "2.5", "", "NA", "1,000"])).to.equal("number");
|
||||
expect(T.detectType(["2026-03-01", "2026-03-02T10:00Z", "", "Mar 3, 2026"])).to.equal("date");
|
||||
expect(T.detectType(["Baseline", "12"])).to.equal("text");
|
||||
expect(T.detectType(["03/01/2026", "04/01/2026"])).to.equal("text");
|
||||
expect(T.detectType(["", ""])).to.equal("text");
|
||||
});
|
||||
it("keeps a column's precision, unit and scientific notation", () => {
|
||||
const t = table("a,b,c,d\n1,12%,1.38e-3,x\n2.25,8%,2.1e-4,y\n");
|
||||
expect(t.columns.map((c) => [c.type, c.decimals, c.unit])).to.deep.equal([["number", 2, ""], ["number", 0, "%"], ["number", 5, ""], ["text", 0, ""]]);
|
||||
expect(t.columns[2].scientific).to.equal(2);
|
||||
expect(T.summarize(t.columns[2], all(t), "mean").text).to.equal("7.950e-4");
|
||||
expect(T.summarize(t.columns[1], all(t), "mean").text).to.equal("10.0%");
|
||||
});
|
||||
it("parses unambiguous dates only", () => {
|
||||
expect(T.parseDate("2026-03-01")).to.equal(Date.UTC(2026, 2, 1));
|
||||
expect(T.parseDate("2026/3/1 10:30")).to.equal(Date.UTC(2026, 2, 1, 10, 30));
|
||||
expect(T.parseDate("1 March 2026")).to.equal(Date.UTC(2026, 2, 1));
|
||||
expect(T.parseDate("2026-13-01")).to.equal(null);
|
||||
expect(T.parseDate("03/01/2026")).to.equal(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("aggregates", () => {
|
||||
const keys = [3, null, 1, 4, 1, 5];
|
||||
const rows = [0, 1, 2, 3, 4, 5];
|
||||
it("skips empty cells", () => {
|
||||
expect(T.aggregate(keys, rows, "count")).to.equal(5);
|
||||
expect(T.aggregate(keys, rows, "sum")).to.equal(14);
|
||||
expect(T.aggregate(keys, rows, "mean")).to.equal(2.8);
|
||||
expect(T.aggregate(keys, rows, "min")).to.equal(1);
|
||||
expect(T.aggregate(keys, rows, "max")).to.equal(5);
|
||||
});
|
||||
it("computes the median for odd and even counts", () => {
|
||||
expect(T.aggregate(keys, rows, "median")).to.equal(3);
|
||||
expect(T.aggregate([4, 1, 3, 2], [0, 1, 2, 3], "median")).to.equal(2.5);
|
||||
});
|
||||
it("uses the sample standard deviation and needs two values", () => {
|
||||
expect(T.aggregate([2, 4, 4, 4, 5, 5, 7, 9], [0, 1, 2, 3, 4, 5, 6, 7], "std")).to.be.closeTo(2.138, 1e-3);
|
||||
expect(T.aggregate([5, null], [0, 1], "std")).to.equal(null);
|
||||
expect(T.aggregate([null], [0], "mean")).to.equal(null);
|
||||
expect(T.aggregate([null], [0], "count")).to.equal(0);
|
||||
});
|
||||
it("formats a summary with the column's precision, over the given rows only", () => {
|
||||
const t = table(RESULTS);
|
||||
expect(T.summarize(t.columns[2], all(t), "mean").text).to.equal("74.7");
|
||||
expect(T.summarize(t.columns[2], [0, 1], "mean").text).to.equal("71.2");
|
||||
expect(T.summarize(t.columns[3], all(t), "mean").text).to.equal("443.8");
|
||||
expect(T.summarize(t.columns[3], all(t), "count").text).to.equal("5");
|
||||
expect(T.summarize(t.columns[3], all(t), "sum").text).to.equal("2,219");
|
||||
expect(T.summarize(t.columns[0], all(t)).text).to.equal("3 distinct");
|
||||
});
|
||||
});
|
||||
|
||||
describe("sorting", () => {
|
||||
it("sorts numbers numerically and keeps empty cells last in both directions", () => {
|
||||
const t = table("k,v\na,10\nb,\nc,9\nd,-1\ne,1e1\n");
|
||||
expect(T.sortIndices(t, all(t), 1, "asc")).to.deep.equal([3, 2, 0, 4, 1]);
|
||||
expect(T.sortIndices(t, all(t), 1, "desc")).to.deep.equal([0, 4, 2, 3, 1]);
|
||||
});
|
||||
it("is stable: ties keep their current order", () => {
|
||||
const t = table(RESULTS);
|
||||
expect(T.sortIndices(t, all(t), 0, "asc")).to.deep.equal([2, 3, 0, 1, 4, 5]);
|
||||
const bySeed = T.sortIndices(t, all(t), 1, "desc");
|
||||
expect(bySeed).to.deep.equal([1, 3, 5, 0, 2, 4]);
|
||||
expect(T.sortIndices(t, bySeed, 0, "asc")).to.deep.equal([3, 2, 1, 0, 5, 4]);
|
||||
});
|
||||
it("sorts text naturally and dates chronologically", () => {
|
||||
const t = table("name,date\nrun 10,Mar 3 2026\nrun 2,2026-03-01\nRun 1,2026-02-28\n");
|
||||
expect(T.sortIndices(t, all(t), 0, "asc")).to.deep.equal([2, 1, 0]);
|
||||
expect(t.columns[1].type).to.equal("date");
|
||||
expect(T.sortIndices(t, all(t), 1, "desc")).to.deep.equal([0, 1, 2]);
|
||||
const d = table("date\n2026-03-10\n2026-02-28\n2026-03-01\n");
|
||||
expect(T.sortIndices(d, all(d), 0, "asc")).to.deep.equal([1, 2, 0]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("filtering", () => {
|
||||
it("combines the search box, numeric ranges and value sets", () => {
|
||||
const t = table(RESULTS);
|
||||
expect(T.filterIndices(t, { query: "aug" })).to.deep.equal([2, 3]);
|
||||
expect(T.filterIndices(t, { ranges: { 2: [74, null] } })).to.deep.equal([3, 4, 5]);
|
||||
expect(T.filterIndices(t, { ranges: { 3: [null, 450] } })).to.deep.equal([0, 1, 2, 3]);
|
||||
expect(T.filterIndices(t, { values: { 0: ["Ours", "Baseline"] }, ranges: { 1: [2, 2] } })).to.deep.equal([1, 5]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("grouping", () => {
|
||||
it("reproduces the mean per model over seeds", () => {
|
||||
const t = table(RESULTS);
|
||||
const g = T.groupTable(t, all(t), 0, "mean");
|
||||
expect(g.header).to.deep.equal(["model", "Rows", "seed", "accuracy", "plausible"]);
|
||||
expect(g.body).to.deep.equal([["Baseline", "2", "1.5", "71.2", "412.0"], ["+ Aug.", "2", "1.5", "74.0", "447.0"], ["Ours", "2", "1.5", "78.9", "501.0"]]);
|
||||
expect(g.columns[3].agg).to.equal("mean");
|
||||
expect(g.columns[3].keys[0]).to.be.closeTo(71.2, 1e-9);
|
||||
});
|
||||
it("groups only the filtered rows and can be sorted, counted and exported", () => {
|
||||
const t = table(RESULTS);
|
||||
const g = T.groupTable(t, T.filterIndices(t, { ranges: { 1: [1, 1] } }), 0, "max");
|
||||
expect(g.body.map((r) => r[0])).to.deep.equal(["Baseline", "+ Aug.", "Ours"]);
|
||||
const order = T.sortIndices(g, [0, 1, 2], 3, "desc");
|
||||
expect(order.map((i) => g.body[i][0])).to.deep.equal(["Ours", "+ Aug.", "Baseline"]);
|
||||
expect(T.toMarkdown(g.header, order.map((i) => g.body[i]), g.columns.map((c) => c.type === "number")).split("\n")[1]).to.equal("| --- | ---: | ---: | ---: | ---: |");
|
||||
});
|
||||
it("labels empty groups", () => {
|
||||
const t = table("k,v\na,1\n,2\na,3\n");
|
||||
expect(T.groupTable(t, all(t), 0, "sum").body).to.deep.equal([["a", "2", "4"], ["(empty)", "1", "2"]]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("export", () => {
|
||||
it("quotes CSV cells only when needed and escapes Markdown pipes", () => {
|
||||
expect(T.toDelimited(["a", "b"], [["x,y", 'say "hi"'], ["plain", ""]])).to.equal('a,b\n"x,y","say ""hi"""\nplain,\n');
|
||||
expect(T.toMarkdown(["a|b"], [["c|d"]])).to.equal("| a\\|b |\n| --- |\n| c\\|d |\n");
|
||||
});
|
||||
});
|
||||
|
||||
describe("shareable state", () => {
|
||||
it("round-trips through the fragment and stays empty by default", () => {
|
||||
expect(T.writeHashState("table", T.DEFAULT_STATE)).to.equal("");
|
||||
const state = { ...T.DEFAULT_STATE, view: "chart", group: "model", agg: "median", sort: { column: "accuracy", dir: "desc" }, values: { model: ["Ours", "+ Aug."] }, ranges: { accuracy: [70, null] }, sums: { accuracy: "std" }, chart: "bar", y: ["accuracy", "plausible"] };
|
||||
const hash = T.writeHashState("table-2", state);
|
||||
expect(hash).to.match(/^#table-2:view=chart&sort=accuracy:desc&group=model&agg=median/);
|
||||
const back = T.readHashState(hash, "table-2");
|
||||
expect(back).to.deep.include({ view: "chart", group: "model", agg: "median", chart: "bar" });
|
||||
expect(back.sort).to.deep.equal({ column: "accuracy", dir: "desc" });
|
||||
expect(back.values).to.deep.equal({ model: ["Ours", "+ Aug."] });
|
||||
expect(back.ranges).to.deep.equal({ accuracy: [70, null] });
|
||||
expect(back.y).to.deep.equal(["accuracy", "plausible"]);
|
||||
expect(T.readHashState(hash, "table")).to.equal(null);
|
||||
expect(T.readHashState("#L12", "table")).to.equal(null);
|
||||
expect(T.isTableHash("#L12")).to.equal(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("column stats and charts", () => {
|
||||
it("summarizes a column with a histogram", () => {
|
||||
const t = table(RESULTS);
|
||||
const s = T.columnStats(t.columns[3], all(t), 4);
|
||||
expect(s).to.include({ type: "number", count: 5, empty: 1, distinct: 5, min: 410, max: 501 });
|
||||
expect(s.histogram.map((b) => b.count)).to.deep.equal([2, 2, 0, 1]);
|
||||
expect(T.columnStats(t.columns[0], all(t)).top[0]).to.deep.equal(["+ Aug.", 2]);
|
||||
});
|
||||
it("defaults to a bar of the first measure by the first text column, skipping seeds", () => {
|
||||
const t = table(RESULTS);
|
||||
expect(chart.defaultChart(t, false)).to.deep.equal({ type: "bar", x: "model", y: ["accuracy"] });
|
||||
const g = T.groupTable(t, all(t), 0, "mean");
|
||||
expect(chart.defaultChart(g, true)).to.deep.equal({ type: "bar", x: "model", y: ["accuracy"] });
|
||||
});
|
||||
it("averages repeated categories in a bar chart and says so", () => {
|
||||
const t = table(RESULTS);
|
||||
const c = chart.buildChart(t, all(t), { type: "bar", x: "model", y: ["accuracy"], agg: "mean" }, { width: 600 });
|
||||
expect(c.categories.map((x) => x.label)).to.deep.equal(["Baseline", "+ Aug.", "Ours"]);
|
||||
expect(c.categories[2].tip.rows[0].value).to.equal("78.9");
|
||||
expect(c.notes[0]).to.equal("Average per model over 6 rows.");
|
||||
expect(c.marks.every((m) => m.h <= 24)).to.equal(true);
|
||||
});
|
||||
it("draws lines on a date axis in order and scatter points with their rows", () => {
|
||||
const t = table("date\tv\n2026-03-03\t3\n2026-03-01\t1\n2026-03-02\t2\n", "\t");
|
||||
const line = chart.buildChart(t, all(t), { type: "line", x: "date", y: ["v"] }, { width: 600 });
|
||||
expect(line.points.map((p) => p.title)).to.deep.equal(["date 2026-03-01", "date 2026-03-02", "date 2026-03-03"]);
|
||||
expect(line.series[0].path.startsWith("M")).to.equal(true);
|
||||
const scatter = chart.buildChart(t, all(t), { type: "scatter", x: "v", y: ["v"] }, { width: 600 });
|
||||
expect(scatter.series[0].dots).to.have.length(3);
|
||||
});
|
||||
it("explains an empty chart", () => {
|
||||
const t = table(RESULTS);
|
||||
expect(chart.buildChart(t, [], { type: "bar", x: "model", y: ["accuracy"] }, { width: 600 }).empty).to.match(/No rows/);
|
||||
expect(chart.buildChart(t, all(t), { type: "bar", x: "model", y: [] }, { width: 600 }).empty).to.match(/numeric/);
|
||||
});
|
||||
it("picks round ticks", () => {
|
||||
expect(chart.niceTicks(0, 79.1, 5).ticks).to.deep.equal([0, 20, 40, 60, 80]);
|
||||
expect(chart.tickLabel(25000, 5000)).to.equal("25K");
|
||||
expect(chart.tickLabel(0.775, 0.025)).to.equal("0.775");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Markdown and notebook tables", () => {
|
||||
let dom;
|
||||
before(() => { dom = new JSDOM("<!doctype html><body></body>"); global.window = dom.window; });
|
||||
after(() => { delete global.window; dom.window.close(); });
|
||||
it("reads redaction chips and slots back as XXXX-n and the pandas header rows", async () => {
|
||||
const enhance = await load("table-enhance.js");
|
||||
const doc = dom.window.document;
|
||||
doc.body.innerHTML = '<table class="dataframe"><thead><tr><th></th><th>acc</th></tr><tr><th>model</th><th></th></tr></thead><tbody>'
|
||||
+ '<tr><th>Ours</th><td>78.9</td></tr><tr><th><mark class="redact-chip" data-term="XXXX-2"><i>2</i></mark></th><td>71.2</td></tr></tbody></table>';
|
||||
const data = enhance.tableData(doc.querySelector("table"));
|
||||
expect(data.rows).to.deep.equal([["model", "acc"], ["Ours", "78.9"], ["XXXX-2", "71.2"]]);
|
||||
doc.body.innerHTML = '<table><tr><td colspan="2">x</td></tr><tr><td>1</td><td>2</td></tr></table>';
|
||||
expect(enhance.tableData(doc.querySelector("table"))).to.equal(null);
|
||||
});
|
||||
});
|
||||
});
|
||||
+115
-1
@@ -3123,9 +3123,123 @@ describe("Vue 3 UI", function () {
|
||||
expect(doc.querySelector(".page-toc .toc-heading")).to.equal(null);
|
||||
preview.click(); await delay(60);
|
||||
expect(body(0).querySelector("markdown h1")).to.not.equal(null);
|
||||
doc.querySelectorAll(".gist-file-card")[1].querySelectorAll(".gist-view-toggle .seg-chip")[1].click(); await delay(40);
|
||||
// CSV: Table, Chart and Source.
|
||||
const csvChips = () => doc.querySelectorAll(".gist-file-card")[1].querySelectorAll(".gist-view-toggle .seg-chip");
|
||||
expect([...csvChips()].map((c) => c.textContent)).to.deep.equal(["Table", "Chart", "Source"]);
|
||||
csvChips()[1].click(); await delay(40);
|
||||
expect(body(1).querySelector(".dt-chart")).to.not.equal(null);
|
||||
expect(body(1).querySelector(".csv-table")).to.equal(null);
|
||||
csvChips()[2].click(); await delay(40);
|
||||
expect(body(1).querySelector(".csv-table")).to.equal(null);
|
||||
expect(body(1).querySelector("pre code").textContent).to.equal(files[1].content);
|
||||
expect(ui.errors).to.deep.equal([]);
|
||||
});
|
||||
describe("table analysis", function () {
|
||||
const CSV = "model,seed,accuracy\nBaseline,1,71.0\nBaseline,2,71.4\nOurs,1,78.7\nOurs,2,79.1\nXXXX-2,1,\n";
|
||||
const open = async (hash = "") => {
|
||||
const page = await browser("/r/test/results.csv" + hash, {
|
||||
"/api/repo/test/files/": [{ name: "results.csv", path: "", sha: "1", size: CSV.length }],
|
||||
"/api/repo/test/file/results.csv": CSV,
|
||||
});
|
||||
await delay(120);
|
||||
return page;
|
||||
};
|
||||
const cells = (doc, col) => [...doc.querySelectorAll(".dt-table tbody tr")].map((tr) => tr.cells[col].textContent);
|
||||
const sortButton = (doc, name) => [...doc.querySelectorAll(".dt-sort")].find((b) => b.textContent.trim() === name);
|
||||
|
||||
it("sorts by a header, shows the summary row and keeps redaction slots", async function () {
|
||||
ui = await open();
|
||||
const doc = ui.window.document;
|
||||
expect(cells(doc, 1)).to.deep.equal(["Baseline", "Baseline", "Ours", "Ours", "2"]);
|
||||
expect(doc.querySelector(".dt-table tbody .redact-slot").textContent).to.equal("2");
|
||||
expect(doc.querySelector(".dt-summary .dt-sum.is-text").textContent).to.equal("3 distinct");
|
||||
expect([...doc.querySelectorAll(".dt-sum-fn-label")].map((l) => l.textContent)).to.deep.equal(["Avg", "Avg"]);
|
||||
expect(doc.querySelectorAll(".dt-sum-value")[1].textContent).to.equal("75.1");
|
||||
sortButton(doc, "accuracy").click(); await delay(30);
|
||||
expect(cells(doc, 3)).to.deep.equal(["71.0", "71.4", "78.7", "79.1", ""]);
|
||||
sortButton(doc, "accuracy").click(); await delay(30);
|
||||
expect(cells(doc, 3)).to.deep.equal(["79.1", "78.7", "71.4", "71.0", ""]);
|
||||
expect(doc.querySelector("th[aria-sort=descending]").textContent).to.contain("accuracy");
|
||||
sortButton(doc, "accuracy").click(); await delay(30);
|
||||
expect(cells(doc, 3)).to.deep.equal(["71.0", "71.4", "78.7", "79.1", ""]);
|
||||
expect(doc.querySelector("th[aria-sort=ascending], th[aria-sort=descending]")).to.equal(null);
|
||||
const select = doc.querySelectorAll(".dt-sum-fn select")[1];
|
||||
select.value = "max"; select.dispatchEvent(new ui.window.Event("change")); await delay(30);
|
||||
expect(doc.querySelectorAll(".dt-sum-value")[1].textContent).to.equal("79.1");
|
||||
expect(ui.errors).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it("filters rows, groups them and stores the view in the link", async function () {
|
||||
ui = await open();
|
||||
const doc = ui.window.document;
|
||||
await ui.input(".dt-search-input", "ours");
|
||||
expect(cells(doc, 1)).to.deep.equal(["Ours", "Ours"]);
|
||||
expect(doc.querySelector(".dt-count").textContent).to.equal("2 of 5 rows");
|
||||
expect(doc.querySelectorAll(".dt-sum-value")[1].textContent).to.equal("78.9");
|
||||
await ui.input(".dt-search-input", "");
|
||||
const group = doc.querySelector(".dt-tools .dt-select select");
|
||||
group.value = "model"; group.dispatchEvent(new ui.window.Event("change")); await delay(30);
|
||||
expect(cells(doc, 0)).to.deep.equal(["Baseline", "Ours", "2"]);
|
||||
expect(doc.querySelector(".dt-table tbody tr:last-child .redact-slot")).to.not.equal(null);
|
||||
expect(cells(doc, 3)).to.deep.equal(["71.2", "78.9", "—"]);
|
||||
expect(doc.querySelector(".dt-count").textContent).to.equal("3 groups · 5 rows");
|
||||
await delay(300);
|
||||
expect(ui.window.location.hash).to.equal("#table:group=model");
|
||||
expect(ui.errors).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it("opens a shared link on the grouped chart, with a tooltip on hover", async function () {
|
||||
ui = await open("#table:view=chart&group=model");
|
||||
const doc = ui.window.document;
|
||||
expect(doc.querySelector(".csv-toggle .seg-chip.active").textContent).to.equal("Chart");
|
||||
// jsdom has no layout: give the chart a width.
|
||||
Object.defineProperty(ui.window.HTMLElement.prototype, "clientWidth", { configurable: true, get() { return this.classList.contains("dt-chart") ? 600 : 0; } });
|
||||
doc.querySelector(".csv-toggle .seg-chip").click(); await delay(30);
|
||||
doc.querySelectorAll(".csv-toggle .seg-chip")[1].click(); await delay(60);
|
||||
const svg = doc.querySelector(".dt-svg");
|
||||
expect(svg.getAttribute("aria-label")).to.equal("Bar chart of accuracy by model");
|
||||
expect([...doc.querySelectorAll(".dt-cat")].map((t) => t.textContent)).to.deep.equal(["Baseline", "Ours", "XXXX-2"]);
|
||||
svg.dispatchEvent(new ui.window.KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true })); await delay(30);
|
||||
expect(doc.querySelector(".dt-tip b").textContent).to.equal("71.2");
|
||||
expect(doc.querySelector(".dt-tip-title").textContent).to.equal("Baseline");
|
||||
delete ui.window.HTMLElement.prototype.clientWidth;
|
||||
expect(ui.errors).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it("copies the current view as Markdown", async function () {
|
||||
ui = await open("#table:group=model&sort=accuracy:desc");
|
||||
const copied = [];
|
||||
Object.defineProperty(ui.window.navigator, "clipboard", { value: { writeText: async (text) => copied.push(text) }, configurable: true });
|
||||
const doc = ui.window.document;
|
||||
doc.querySelector(".dt-copy-btn").click(); await delay(20);
|
||||
[...doc.querySelectorAll(".dt-copy-menu button")].find((b) => /Markdown/.test(b.textContent)).click(); await delay(30);
|
||||
expect(copied[0].split("\n").slice(0, 4)).to.deep.equal(["| model | Rows | seed | accuracy |", "| --- | ---: | ---: | ---: |", "| Ours | 2 | 1.5 | 78.9 |", "| Baseline | 2 | 1.5 | 71.2 |"]);
|
||||
expect(ui.errors).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it("sorts a Markdown table, adds a summary row and opens it as a table", async function () {
|
||||
const md = "# Results\n\n| Model | Accuracy |\n| --- | ---: |\n| Baseline | 71.2 |\n| XXXX-3 | 77.5 |\n| Ours | 78.9 |\n";
|
||||
ui = await browser("/r/test/README.md", {
|
||||
"/api/repo/test/files/": [{ name: "README.md", path: "", sha: "1", size: md.length }],
|
||||
"/api/repo/test/file/README.md": md,
|
||||
});
|
||||
await delay(200);
|
||||
const doc = ui.window.document;
|
||||
const table = doc.querySelector(".markdown-body table");
|
||||
expect(table.closest(".md-table")).to.not.equal(null);
|
||||
const firstCol = () => [...table.querySelectorAll("tbody tr")].map((tr) => tr.cells[0].textContent.trim());
|
||||
table.querySelectorAll("th")[1].click(); table.querySelectorAll("th")[1].click(); await delay(20);
|
||||
expect(firstCol()).to.deep.equal(["Ours", "3", "Baseline"]);
|
||||
expect(table.querySelector("tbody .redact-chip, tbody .redact-slot")).to.not.equal(null);
|
||||
doc.querySelector(".md-sigma").click(); await delay(20);
|
||||
expect([...table.querySelectorAll("tfoot td")].map((td) => td.textContent)).to.deep.equal(["Average", "75.9"]);
|
||||
doc.querySelector(".md-open").click(); await delay(60);
|
||||
const dialog = doc.querySelector(".dt-dialog");
|
||||
expect(dialog.querySelector(".dt-dialog-title").textContent).to.equal("Table · Results");
|
||||
expect([...dialog.querySelectorAll(".dt-table tbody tr")].map((tr) => tr.cells[1].textContent)).to.deep.equal(["Baseline", "3", "Ours"]);
|
||||
dialog.querySelector(".dt-dialog-close").click(); await delay(20);
|
||||
expect(doc.querySelector(".dt-dialog")).to.equal(null);
|
||||
expect(ui.errors).to.deep.equal([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user