refactor(webui): unify code and diff highlighting (#6412)
* refactor(webui): unify code and diff highlighting Replace Prism with shared Highlights tokens and Pierre read-only diffs, retaining rare grammars through lazy Shiki and preserving source, copy, preview and diff controls. Restore single-session Composer ownership inside the conversation column while retaining the shared multi-pane footer. Keep the measured large-diff scroll regression explicit during review. * fix(webui): contain highlighting errors and retain diff selections Refs NAN-343 * test(webui): align pane fixtures after main rebase * chore(webui): update highlighting dependencies Keep grammar fallback coverage on a language that still requires Shiki, and align reading-size assertions with main.
This commit is contained in:
1 parent
f5c359476b
commit
3a37269f3f
44 files changed
+2261
-1072
No files matched your search
@@ -1,31 +1,49 @@
|
||||
import { act, render, screen, waitFor } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { HighlightedCodeProps } from "@/components/HighlightedCode";
|
||||
|
||||
import { CodeBlock } from "@/components/CodeBlock";
|
||||
import { ThemeProvider } from "@/hooks/useTheme";
|
||||
|
||||
vi.mock("react-syntax-highlighter/dist/esm/prism-async-light", () => ({
|
||||
default: ({
|
||||
children,
|
||||
language,
|
||||
customStyle,
|
||||
}: {
|
||||
children: string;
|
||||
language?: string;
|
||||
customStyle?: React.CSSProperties;
|
||||
}) => (
|
||||
<pre
|
||||
data-testid="highlighted-code"
|
||||
data-language={language}
|
||||
style={customStyle}
|
||||
>
|
||||
<code>{children}</code>
|
||||
</pre>
|
||||
),
|
||||
const failures = vi.hoisted(() => new Set<string>());
|
||||
|
||||
// Test CodeBlock's controls and enhancement boundary here; token/DOM rendering has dedicated real-engine and browser coverage.
|
||||
vi.mock("@/components/HighlightedCode", () => ({
|
||||
HighlightedCode: ({ code, language, viewportHighlight }: HighlightedCodeProps) => {
|
||||
if (failures.has(code)) throw new Error("Grammar download failed");
|
||||
return <pre data-testid="highlighted-code" data-language={language || "text"}
|
||||
style={{ overflow: viewportHighlight ? "visible" : "auto" }}><code data-code-text="">{code}</code></pre>;
|
||||
},
|
||||
}));
|
||||
|
||||
describe("CodeBlock", () => {
|
||||
it("contains a failed grammar, preserves source and copy, and enhances changed source", async () => {
|
||||
const source = "; 中文 😀\r\n[server]\r\nhost=localhost\r\n";
|
||||
failures.add(source);
|
||||
const user = userEvent.setup();
|
||||
const writeText = vi.fn().mockResolvedValue(undefined);
|
||||
Object.defineProperty(navigator, "clipboard", { configurable: true, value: { writeText } });
|
||||
const errors = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
const warnings = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
try {
|
||||
const { rerender } = render(<><textarea defaultValue="keep draft" /><CodeBlock code={source} language="ini" /></>);
|
||||
await waitFor(() => expect(warnings).toHaveBeenCalledWith("Code highlighting failed.", expect.any(Error)));
|
||||
expect(screen.getByTestId("plain-code-fallback").textContent).toBe(source);
|
||||
expect(screen.getByRole("textbox")).toHaveValue("keep draft");
|
||||
await user.click(screen.getByRole("button", { name: "Copy code" }));
|
||||
expect(writeText).toHaveBeenCalledWith(source);
|
||||
rerender(<><textarea defaultValue="keep draft" /><CodeBlock code={source + "; updated"} language="ini" /></>);
|
||||
expect((await screen.findByTestId("highlighted-code")).textContent).toBe(source + "; updated");
|
||||
expect(screen.getByRole("textbox")).toHaveValue("keep draft");
|
||||
} finally {
|
||||
failures.clear();
|
||||
errors.mockRestore();
|
||||
warnings.mockRestore();
|
||||
Reflect.deleteProperty(navigator, "clipboard");
|
||||
}
|
||||
});
|
||||
|
||||
it("renders and copies a large code block in full without pagination", async () => {
|
||||
const user = userEvent.setup();
|
||||
const writeText = vi.fn().mockResolvedValue(undefined);
|
||||
@@ -37,7 +55,8 @@ describe("CodeBlock", () => {
|
||||
|
||||
try {
|
||||
render(<CodeBlock code={source} language="typescript" showLineNumbers />);
|
||||
expect((await screen.findByTestId("highlighted-code")).textContent).toBe(source);
|
||||
const code = await screen.findByTestId("highlighted-code");
|
||||
expect([...code.querySelectorAll("[data-code-text]")].map(node => node.textContent).join("")).toBe(source);
|
||||
expect(screen.getAllByRole("button")).toHaveLength(1);
|
||||
await user.click(screen.getByRole("button", { name: "Copy code" }));
|
||||
expect(writeText).toHaveBeenCalledWith(source);
|
||||
@@ -179,7 +198,7 @@ describe("CodeBlock", () => {
|
||||
expect(screen.getByText("const value = 1;")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("normalizes file language aliases before loading Prism", async () => {
|
||||
it("normalizes file language aliases before highlighting", async () => {
|
||||
render(
|
||||
<ThemeProvider theme="light">
|
||||
<CodeBlock language="html" code="<main />" />
|
||||
|
||||
Reference in new issue
Block a user