Download frontend/tests/embed-data-diff.test.ts from HuggingFaceSwarms/scaling-swarms: direct link, hf CLI and curl.
- Browser
- Download file 4.75 kB
-
https://huggingface.co/spaces/HuggingFaceSwarms/scaling-swarms/resolve/main/frontend/tests/embed-data-diff.test.ts
- Command line
-
hf download hf://spaces/HuggingFaceSwarms/scaling-swarms/frontend/tests/embed-data-diff.test.ts
-
curl -L -o embed-data-diff.test.ts https://huggingface.co/spaces/HuggingFaceSwarms/scaling-swarms/resolve/main/frontend/tests/embed-data-diff.test.ts
4.75 kB
| /** | |
| * 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(); | |
| }); | |
| }); | |