1import { describe, expect, it } from "vite-plus/test";
2
3import { boundedSelectableSourceTokens, prepareSourceFileDocument } from "./source-file-document";
4
5describe("prepareSourceFileDocument", () => {
6 it("normalizes and serializes source rows once for repeated consumers", () => {
7 const first = prepareSourceFileDocument("const value = 1;\r\n\tvalue;\r");
8 const second = prepareSourceFileDocument("const value = 1;\r\n\tvalue;\r");
9 const rows = JSON.parse(first.rowsJson) as ReadonlyArray<{ readonly content: string }>;
10
11 expect(first.contents).toBe("const value = 1;\n\tvalue;\n");
12 expect(first.lines).toEqual(["const value = 1;", "\tvalue;", ""]);
13 expect(rows.map((row) => row.content)).toEqual(["const value = 1;", " value;", ""]);
14 expect(second).toBe(first);
15 });
16});
17
18it("bounds selectable highlighting without allocating spans for huge files", () => {
19 const token = { content: "text", color: "#fff", fontStyle: null };
20 const small = [[token]];
21 expect(boundedSelectableSourceTokens(small)).toBe(small);
22 expect(boundedSelectableSourceTokens(null)).toBeNull();
23 expect(boundedSelectableSourceTokens(Array.from({ length: 20_000 }, () => [token]))).toBeNull();
24 expect(boundedSelectableSourceTokens([Array.from({ length: 2_000 }, () => token)])).toBeNull();
25 const longPlainText = "full contents\n".repeat(50_000);
26 expect(prepareSourceFileDocument(longPlainText).contents).toBe(longPlainText);
27});