mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 16:51:25 +00:00
perf(test): reduce import and fixture overhead in hot tests
This commit is contained in:
@@ -1,38 +1,54 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { markdownTheme } from "./theme.js";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const cliHighlightMocks = vi.hoisted(() => ({
|
||||
highlight: vi.fn((code: string) => code),
|
||||
supportsLanguage: vi.fn((_lang: string) => true),
|
||||
}));
|
||||
|
||||
vi.mock("cli-highlight", () => cliHighlightMocks);
|
||||
|
||||
const { markdownTheme } = await import("./theme.js");
|
||||
|
||||
const stripAnsi = (str: string) =>
|
||||
str.replace(new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, "g"), "");
|
||||
|
||||
describe("markdownTheme", () => {
|
||||
describe("highlightCode", () => {
|
||||
it("returns highlighted lines for common language inputs", () => {
|
||||
const code = `const x = 42;`;
|
||||
const js = markdownTheme.highlightCode!(code, "javascript");
|
||||
|
||||
expect(js).toBeInstanceOf(Array);
|
||||
expect(js).toHaveLength(1);
|
||||
expect(js[0]).toContain("const");
|
||||
expect(js[0]).toContain("42");
|
||||
beforeEach(() => {
|
||||
cliHighlightMocks.highlight.mockReset();
|
||||
cliHighlightMocks.supportsLanguage.mockReset();
|
||||
cliHighlightMocks.highlight.mockImplementation((code: string) => code);
|
||||
cliHighlightMocks.supportsLanguage.mockReturnValue(true);
|
||||
});
|
||||
|
||||
it("handles unknown or missing language and preserves content", () => {
|
||||
const code = `echo "hello"`;
|
||||
const unknown = markdownTheme.highlightCode!(code, "not-a-real-language");
|
||||
const missing = markdownTheme.highlightCode!(code, undefined);
|
||||
expect(unknown).toBeInstanceOf(Array);
|
||||
expect(missing).toBeInstanceOf(Array);
|
||||
expect(unknown).toHaveLength(1);
|
||||
expect(missing).toHaveLength(1);
|
||||
expect(unknown[0]).toContain("echo");
|
||||
expect(missing[0]).toContain("echo");
|
||||
const codeBlock = `const message = "Hello, World!";
|
||||
console.log(message);`;
|
||||
const result = markdownTheme.highlightCode!(codeBlock, "javascript");
|
||||
const empty = markdownTheme.highlightCode!("", "javascript");
|
||||
it("passes supported language through to the highlighter", () => {
|
||||
markdownTheme.highlightCode!("const x = 42;", "javascript");
|
||||
expect(cliHighlightMocks.supportsLanguage).toHaveBeenCalledWith("javascript");
|
||||
expect(cliHighlightMocks.highlight).toHaveBeenCalledWith(
|
||||
"const x = 42;",
|
||||
expect.objectContaining({ language: "javascript" }),
|
||||
);
|
||||
});
|
||||
|
||||
const stripAnsi = (str: string) =>
|
||||
str.replace(new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, "g"), "");
|
||||
expect(stripAnsi(result[0])).toBe(`const message = "Hello, World!";`);
|
||||
expect(stripAnsi(result[1])).toBe("console.log(message);");
|
||||
expect(empty).toEqual([""]);
|
||||
it("falls back to auto-detect for unknown language and preserves lines", () => {
|
||||
cliHighlightMocks.supportsLanguage.mockReturnValue(false);
|
||||
cliHighlightMocks.highlight.mockImplementation((code: string) => `${code}\nline-2`);
|
||||
const result = markdownTheme.highlightCode!(`echo "hello"`, "not-a-real-language");
|
||||
expect(cliHighlightMocks.highlight).toHaveBeenCalledWith(
|
||||
`echo "hello"`,
|
||||
expect.objectContaining({ language: undefined }),
|
||||
);
|
||||
expect(stripAnsi(result[0] ?? "")).toContain("echo");
|
||||
expect(stripAnsi(result[1] ?? "")).toBe("line-2");
|
||||
});
|
||||
|
||||
it("returns plain highlighted lines when highlighting throws", () => {
|
||||
cliHighlightMocks.highlight.mockImplementation(() => {
|
||||
throw new Error("boom");
|
||||
});
|
||||
const result = markdownTheme.highlightCode!("echo hello", "javascript");
|
||||
expect(result).toHaveLength(1);
|
||||
expect(stripAnsi(result[0] ?? "")).toBe("echo hello");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user