File size: 4,750 Bytes
f7733a1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | /**
* Studio code diff, data drawer helpers and streaming progress
* (docs/TESTS.md 6.6, 6.7).
*/
import { describe, it, expect } from "vitest";
import type { UIMessage } from "ai";
import { collapseUnchanged, diffStats, lineDiff } from "../src/editor/embeds/line-diff";
import {
detectPastedTable,
fileUsedByChart,
inferColumnTypes,
pastedTableContent,
uniqueDataName,
} from "../src/editor/embeds/data-insights";
import { progressLabel, streamingProgress } from "../src/editor/embeds/stream-progress";
describe("line diff", () => {
it("6.6.1 marks added and removed lines around unchanged ones", () => {
const before = ["a", "b", "c", "d"].join("\n");
const after = ["a", "B", "c", "d", "e"].join("\n");
const lines = lineDiff(before, after)!;
expect(lines).toEqual([
{ type: "same", text: "a" },
{ type: "del", text: "b" },
{ type: "add", text: "B" },
{ type: "same", text: "c" },
{ type: "same", text: "d" },
{ type: "add", text: "e" },
]);
expect(diffStats(lines)).toEqual({ added: 2, removed: 1 });
});
it("6.6.2 collapses long unchanged runs but keeps context around changes", () => {
const base = Array.from({ length: 30 }, (_, i) => `line ${i}`);
const changed = [...base];
changed[15] = "changed";
const shown = collapseUnchanged(lineDiff(base.join("\n"), changed.join("\n"))!, 2);
expect(shown[0]).toEqual({ type: "skip", count: 13 });
expect(shown.filter((l) => l.type === "del" || l.type === "add")).toHaveLength(2);
expect(shown[shown.length - 1]).toEqual({ type: "skip", count: 12 });
});
it("6.6.3 refuses huge inputs instead of freezing", () => {
const huge = Array.from({ length: 3001 }, (_, i) => String(i)).join("\n");
expect(lineDiff(huge, "x")).toBeNull();
});
});
describe("data helpers", () => {
it("6.7.1 infers column types, tolerating a stray text cell", () => {
const rows = [
["2024-01-01", "1,200", "true", "alpha"],
["2024-02-01", "3.5", "false", "beta"],
["2024-03-01", "n/a", "yes", "gamma"],
["2024-04-01", "7", "no", "delta"],
["2024-05-01", "8%", "true", "epsilon"],
];
expect(inferColumnTypes(rows, 5)).toEqual(["date", "number", "boolean", "text", "empty"]);
});
it("6.7.2 flags files the chart uses: by name or by most of their columns", () => {
const file = { name: "accuracy.csv", columns: ["epoch", "baseline", "transformer", "moe"] };
expect(fileUsedByChart("// from accuracy.csv", file)).toBe(true);
expect(fileUsedByChart("[{epoch:1,baseline:.6,transformer:.7,moe:.74}]", file)).toBe(true);
expect(fileUsedByChart("[{epoch:1}]", file)).toBe(false);
expect(fileUsedByChart("", file)).toBe(false);
});
it("6.7.3 detects a table pasted from a spreadsheet or a CSV, not prose", () => {
expect(detectPastedTable("epoch\tacc\n1\t0.6\n2\t0.7")).toMatchObject({ ext: "tsv", rows: 2, columns: ["epoch", "acc"] });
expect(detectPastedTable('name,value\n"a, b",1\nc,2')).toMatchObject({ ext: "csv", rows: 2 });
expect(detectPastedTable("Make the bars blue.\nAnd bigger.\nThanks")).toBeNull();
expect(detectPastedTable("a,b\n1,2")).toBeNull();
});
it("6.7.4 semicolon tables are stored as CSV under a free name", () => {
const text = "x;label\n1;a,b\n2;c";
const table = detectPastedTable(text)!;
expect(pastedTableContent(text, table)).toBe('x,label\n1,"a,b"\n2,c\n');
const taken = new Set(["pasted-data.csv"]);
expect(uniqueDataName("pasted-data", "csv", (n) => taken.has(n))).toBe("pasted-data-2.csv");
});
});
describe("streaming progress", () => {
it("6.7.5 counts the lines of a chart while its tool input streams", () => {
const message = {
id: "a",
role: "assistant",
parts: [
{ type: "text", text: "On it." },
{ type: "tool-createEmbed", toolCallId: "t", state: "input-streaming", input: { html: "<div>\n<style>\n" } },
],
} as unknown as UIMessage;
const p = streamingProgress(message)!;
expect(p).toMatchObject({ toolName: "createEmbed", lines: 3 });
expect(progressLabel(p)).toBe("Writing chart... 3 lines");
});
it("6.7.6 reports nothing once the input is complete or for other tools", () => {
const done = {
id: "a",
role: "assistant",
parts: [{ type: "tool-createEmbed", toolCallId: "t", state: "input-available", input: { html: "<a/>" } }],
} as unknown as UIMessage;
const read = {
id: "b",
role: "assistant",
parts: [{ type: "tool-readDataFile", toolCallId: "t", state: "input-streaming", input: {} }],
} as unknown as UIMessage;
expect(streamingProgress(done)).toBeNull();
expect(streamingProgress(read)).toBeNull();
});
});
|