fix(ui): keep theme text and selections readable (#6226)

* fix(ui): keep theme text and selections readable

* fix(tui): theme the blank composer cursor cell

* refactor(webui): centralize diff theme colors

* fix(tui): keep conversation selections readable

* fix(ui): preserve nested syntax colors and selection lifetimes
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-15 15:21:28 +08:00
1 parent 73c2441abc
commit c24153db86
32 files changed
+451 -165

No files matched your search

@@ -1611,7 +1611,7 @@ describe("AgentActivityCluster", () => {
<AgentActivityCluster messages={messages} isTurnStreaming={false} hasBodyBelow={false} />,
);
expect(screen.getByText(/^Worked/)).toBe(header);
expect(header).toHaveClass("text-muted-foreground/65");
expect(header).toHaveClass("text-muted-foreground");
expect(header).not.toHaveAttribute("data-sheen-text");
});
+12 -27
View File
@@ -5,27 +5,19 @@ import { describe, expect, it, vi } from "vitest";
import { CodeBlock } from "@/components/CodeBlock";
import { ThemeProvider } from "@/hooks/useTheme";
const mockedStyles = vi.hoisted(() => ({
dark: { pre: { background: "#111" } },
light: { pre: { background: "#fff" } },
}));
vi.mock("react-syntax-highlighter/dist/esm/prism-async-light", () => ({
default: ({
children,
language,
style,
customStyle,
}: {
children: string;
language?: string;
style: Record<string, unknown>;
customStyle?: React.CSSProperties;
}) => (
<pre
data-testid="highlighted-code"
data-language={language}
data-theme={style === mockedStyles.dark ? "dark" : "light"}
style={customStyle}
>
<code>{children}</code>
@@ -33,14 +25,6 @@ vi.mock("react-syntax-highlighter/dist/esm/prism-async-light", () => ({
),
}));
vi.mock("react-syntax-highlighter/dist/esm/styles/prism/one-dark", () => ({
default: mockedStyles.dark,
}));
vi.mock("react-syntax-highlighter/dist/esm/styles/prism/one-light", () => ({
default: mockedStyles.light,
}));
describe("CodeBlock", () => {
it("renders and copies a large code block in full without pagination", async () => {
const user = userEvent.setup();
@@ -220,7 +204,7 @@ describe("CodeBlock", () => {
"ansi",
);
expect(screen.queryByText("ansi")).not.toBeInTheDocument();
expect(screen.getByText("PASS")).toHaveStyle({ color: "#0dbc79" });
expect(screen.getByText("PASS")).toHaveStyle({ color: "#86efac" });
expect(screen.getByText("<script>alert(1)</script>")).toBeInTheDocument();
expect(document.querySelector("script")).toBeNull();
});
@@ -241,6 +225,13 @@ describe("CodeBlock", () => {
});
});
it("clears ANSI foreground and background colors on reset", () => {
render(<CodeBlock language="ansi" code={"\x1b[31;47merror\x1b[0m normal"} />);
expect(screen.getByText("error")).toHaveStyle({ color: "#cd3131", backgroundColor: "#e5e5e5" });
expect(screen.getByText("normal")).not.toHaveAttribute("style");
});
it("copies ANSI output as clean text", async () => {
const user = userEvent.setup();
const writeText = vi.fn().mockResolvedValue(undefined);
@@ -314,7 +305,7 @@ describe("CodeBlock", () => {
try {
const { rerender } = render(
<ThemeProvider theme="dark">
<CodeBlock language="ts" code="const value = 1;" />
<CodeBlock language="ansi" code={"\x1b[32mPASS\x1b[0m"} />
</ThemeProvider>,
);
@@ -323,14 +314,11 @@ describe("CodeBlock", () => {
await Promise.resolve();
});
expect(screen.getByTestId("highlighted-code")).toHaveAttribute(
"data-theme",
"dark",
);
expect(screen.getByText("PASS")).toHaveStyle({ color: "#86efac" });
rerender(
<ThemeProvider theme="light">
<CodeBlock language="ts" code="const value = 1;" />
<CodeBlock language="ansi" code={"\x1b[32mPASS\x1b[0m"} />
</ThemeProvider>,
);
@@ -338,10 +326,7 @@ describe("CodeBlock", () => {
await Promise.resolve();
});
expect(screen.getByTestId("highlighted-code")).toHaveAttribute(
"data-theme",
"light",
);
expect(screen.getByText("PASS")).toHaveStyle({ color: "#166534" });
expect(observer).not.toHaveBeenCalled();
} finally {
vi.stubGlobal("MutationObserver", originalMutationObserver);
@@ -1,9 +1,18 @@
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { render, screen, waitFor } from "@testing-library/react";
import { parse } from "postcss";
import { describe, expect, it } from "vitest";
import { DiffSyntaxHighlight } from "@/components/thread/activity/DiffSyntaxHighlight";
import { ThemeProvider } from "@/hooks/useTheme";
const globalStyles = parse(readFileSync(resolve(process.cwd(), "src/globals.css"), "utf8"));
// happy-dom does not apply cascade layers; retain the real rules and their order.
globalStyles.walkAtRules("layer", (layer) => {
layer.replaceWith(...(layer.nodes ?? []));
});
describe("DiffSyntaxHighlight with Prism", () => {
it("loads the TSX grammar and renders styled syntax tokens", async () => {
render(
@@ -50,10 +59,11 @@ describe("DiffSyntaxHighlight with Prism", () => {
await waitFor(
() => {
const tokens = highlighted.querySelectorAll<HTMLElement>(
'td:last-child span[style*="color"]',
"td:last-child .token",
);
expect(tokens).not.toHaveLength(0);
expect(new Set([...tokens].map((token) => token.style.color)).size).toBeGreaterThan(1);
expect(highlighted.querySelector(".token.keyword")).not.toBeNull();
expect(highlighted.querySelector(".token.string")).not.toBeNull();
},
{ timeout: 10_000 },
);
@@ -62,6 +72,56 @@ describe("DiffSyntaxHighlight with Prism", () => {
expect(highlighted.querySelectorAll("tbody tr")).toHaveLength(4);
});
it.each(["light", "dark"] as const)(
"preserves nested JSX token colors in the %s appearance",
async (appearance) => {
const { container } = render(
<div className={appearance === "dark" ? "dark" : ""}>
<style>{globalStyles.toString()}</style>
<DiffSyntaxHighlight
language="tsx"
lines={[{
kind: "add",
old_lineno: null,
new_lineno: 1,
content: '<span className={enabled ? "yes" : "no"} onClick={() => save(new Event("ok"))}>ready</span>',
}]}
/>
</div>,
);
const highlighted = await screen.findByTestId(
"syntax-highlighted-diff-hunk",
{},
{ timeout: 10_000 },
);
await waitFor(() => {
expect(highlighted.querySelector(".token.function")).not.toBeNull();
}, { timeout: 10_000 });
const colors = getComputedStyle(
appearance === "dark" ? container.firstElementChild! : document.documentElement,
);
const tokens = [...highlighted.querySelectorAll<HTMLElement>(".token")];
for (const [text, category, variable] of [
['"yes"', "string", "--code-string"],
["new", "keyword", "--code-keyword"],
["save", "function", "--code-function"],
["=>", "operator", "--code-function"],
["className", "attr-name", "--code-property"],
["span", "tag", "--code-property"],
]) {
const token = tokens.find(
(node) => node.textContent === text && node.classList.contains(category),
);
const expectedColor = colors.getPropertyValue(variable).trim();
expect(token, `${category}: ${text}`).toBeDefined();
expect(expectedColor).not.toBe("");
expect.soft(getComputedStyle(token!).color, `${category}: ${text}`).toBe(expectedColor);
}
},
);
it("preserves markdown table line boundaries", async () => {
const lines = [
"## 发布安排",
+2 -10
View File
@@ -48,14 +48,6 @@ vi.mock("react-syntax-highlighter/dist/esm/create-element", () => ({
),
}));
vi.mock("react-syntax-highlighter/dist/esm/styles/prism/one-dark", () => ({
default: { dark: { color: "#fff" } },
}));
vi.mock("react-syntax-highlighter/dist/esm/styles/prism/one-light", () => ({
default: { light: { color: "#111" } },
}));
describe("DiffSyntaxHighlight", () => {
it("highlights a complete hunk while preserving diff rows and line numbers", async () => {
render(
@@ -81,10 +73,10 @@ describe("DiffSyntaxHighlight", () => {
expect(highlighted).toHaveAttribute("data-language", "typescript");
expect(screen.getAllByTestId("syntax-token")).toHaveLength(3);
expect(screen.getByText("return oldValue;", { exact: false }).closest("tr")).toHaveClass(
"bg-rose-500/[0.09]",
"bg-diff-deleted",
);
expect(screen.getByText("return newValue;", { exact: false }).closest("tr")).toHaveClass(
"bg-emerald-500/[0.09]",
"bg-diff-added",
);
expect(screen.getAllByText("5")).toHaveLength(2);
expect(screen.getAllByTestId("syntax-token").some((node) => node.textContent?.endsWith("\n"))).toBe(false);
+4 -4
View File
@@ -663,8 +663,8 @@ describe("MessageBubble", () => {
expect(token).toHaveClass("font-[550]");
expect(token.className).not.toContain("rounded");
expect(token.className).not.toContain("px-");
expect(token.getAttribute("style")).toContain("color: #0B5CFF");
expect(token.getAttribute("style")).not.toContain("text-shadow");
expect(token).toHaveClass("text-foreground");
expect(token.style.textShadow).toBe("");
const logo = screen.getByTestId("message-cli-mention-logo-zoom");
expect(logo).toHaveClass("h-[1.1em]", "w-[1.1em]", "rounded-[0.25em]", "top-1/2", "-translate-y-1/2");
expect(logo.parentElement).toHaveClass("mr-1", "w-[1.1em]");
@@ -716,7 +716,7 @@ describe("MessageBubble", () => {
expect(token).toHaveTextContent("@Draw.io");
expect(token.className).not.toContain("rounded");
expect(token.className).not.toContain("px-");
expect(token.getAttribute("style")).toContain("color: #F08705");
expect(token).toHaveClass("text-foreground");
expect(screen.getByTestId("message-cli-mention-logo-drawio")).toBeInTheDocument();
});
@@ -733,7 +733,7 @@ describe("MessageBubble", () => {
const token = screen.getByTestId("message-mcp-mention-browserbase");
expect(token).toHaveTextContent("@Browserbase");
expect(token).toHaveAttribute("title", "MCP server: Browserbase (@browserbase)");
expect(token.getAttribute("style")).toContain("color: #111827");
expect(token).toHaveClass("text-foreground");
const logo = screen.getByTestId("message-mcp-mention-logo-browserbase");
expect(logo).toHaveClass("h-[1.1em]", "w-[1.1em]", "rounded-[0.25em]", "top-1/2", "-translate-y-1/2");
expect(logo.parentElement).toHaveClass("mr-1", "w-[1.1em]");
@@ -57,7 +57,7 @@ describe("ThinkingReasoningShell", () => {
view.rerender(shell(false));
expect(screen.getByRole("button", { name: "Worked, Collapse activity details" })).toBe(button);
expect(screen.getByText("Worked")).toHaveClass("text-muted-foreground/65");
expect(screen.getByText("Worked")).toHaveClass("text-muted-foreground");
expect(screen.getByText("Worked")).not.toHaveAttribute("data-sheen-text");
expect(button.className).toBe(headerClass);
expect(screen.getByTestId("agent-activity-content")).toBe(content);
+4 -5
View File
@@ -2506,8 +2506,8 @@ describe("ThreadComposer", () => {
expect(token.className).not.toContain("zoom-in");
expect(token.className).not.toContain("px-");
expect(token.className).not.toContain("mx-");
expect(token.getAttribute("style")).toContain("color: #5C5543");
expect(token.getAttribute("style")).not.toContain("text-shadow");
expect(token).toHaveClass("text-foreground");
expect(token.style.textShadow).toBe("");
expect(screen.queryByTestId("composer-cli-app-tray")).not.toBeInTheDocument();
const logo = screen.getByTestId("composer-cli-mention-logo-gimp");
expect(logo.className).toContain("top-1/2");
@@ -2543,7 +2543,7 @@ describe("ThreadComposer", () => {
expect(name).toHaveClass("[-webkit-text-stroke:0.4px_currentColor]");
});
it("uses the shared accent when an installed CLI app has no brand metadata", () => {
it("uses the theme foreground when an installed CLI app has no brand metadata", () => {
const mention = "@obsidian-agent-cli";
const app: CliAppInfo = {
name: "obsidian-agent-cli",
@@ -2575,8 +2575,7 @@ describe("ThreadComposer", () => {
});
const token = screen.getByTestId("composer-cli-mention-obsidian-agent-cli");
expect(token.getAttribute("style")).toContain("var(--inline-token-highlight)");
expect(token.getAttribute("style")).not.toContain("var(--primary)");
expect(token).toHaveClass("text-foreground");
});
it.each([
+1 -1
View File
@@ -765,7 +765,7 @@ describe("ThreadMessages", () => {
const timestamp = menu.querySelector<HTMLElement>("[data-message-timestamp]")!;
expect(timestamp).toBeInTheDocument();
expect(timestamp).toHaveClass("min-h-[var(--message-block-control-size)]");
expect(timestamp.parentElement).toHaveClass("border-t", "text-muted-foreground/45");
expect(timestamp.parentElement).toHaveClass("border-t", "text-muted-foreground");
expect(toolbar).not.toContainElement(timestamp);
expect(timestamp).not.toHaveAttribute("tabindex");
expect(timestamp.querySelector("svg")).not.toBeInTheDocument();