Open History to see the last commit
1import { codeToTokens } from "@pierre/highlights";
2import { pierreDark, pierreLight } from "@pierre/highlights/themes";
3import { describe, expect, it, vi } from "vite-plus/test";
4
5import { highlightCode, tokenizeCode } from "./highlights";
6
7vi.mock("expo", async () => {
8 const { createWasmHighlightsModule } = await import("./highlights.testing");
9 const highlights = createWasmHighlightsModule();
10 return { requireOptionalNativeModule: () => highlights };
11});
12
13describe("highlightCode", () => {
14 it("produces @pierre/highlights' own tokens and colors", () => {
15 const code = [
16 "/** 🎉 docs\r",
17 " */",
18 "export const greet = (name: string) => <b title={`hi ${name}`}>é</b>;",
19 "const multi = `first",
20 "second`; // done",
21 "",
22 ].join("\n");
23
24 for (const [scheme, theme] of [
25 ["light", pierreLight],
26 ["dark", pierreDark],
27 ] as const) {
28 const expected = codeToTokens(code, { lang: "tsx", theme }).tokens.map((line) =>
29 line.map((token) => ({
30 content: token.content,
31 color: token.color ?? null,
32 fontStyle: token.fontStyle ?? null,
33 })),
34 );
35 expect(highlightCode(code, "tsx", scheme)).toEqual(expected);
36 }
37 });
38
39 it("keeps an over-long line as one plain token", () => {
40 const longLine = `const data = "${"x".repeat(1_000)}";`;
41 const [first, second] = highlightCode(`let a = 1;\n${longLine}`, "typescript", "dark");
42
43 expect(first?.length).toBeGreaterThan(1);
44 expect(second).toEqual([{ content: longLine, color: null, fontStyle: null }]);
45 });
46});
47
48it("normalizes only requested lines from full-source records, including CRLF and a long comment opener", () => {
49 const code = `const first = 1;\r\n${" ".repeat(1_001)}/*\r\ninside\r\n*/\r\nconst last = 2;`;
50 const source = tokenizeCode(code, "typescript");
51 expect(source.byteLength).toBeGreaterThan(0);
52 for (let line = 0; line < 5; line++) {
53 expect(source.highlightLine(line, "dark")).toEqual(
54 highlightCode(code, "typescript", "dark")[line],
55 );
56 }
57 expect(source.highlightLine(2, "dark")?.[0]?.color).toBe("#737373");
58 expect(source.highlightLine(100, "dark")).toBeUndefined();
59});