Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #5186: [Bug]: Fix up inline code styles in chat window #5226

Merged
merged 12 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions frontend/__tests__/components/chat-message.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,12 @@ describe("ChatMessage", () => {
);
expect(screen.getByTestId("custom-component")).toBeInTheDocument();
});

it("should apply correct styles to inline code", () => {
render(<ChatMessage type="assistant" message="Here is some `inline code` text" />);
const codeElement = screen.getByText("inline code");

expect(codeElement.tagName.toLowerCase()).toBe("code");
expect(codeElement.closest("article")).not.toBeNull();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test title "should apply correct styles to inline code" is unrelated to what the test actually does (check if the elements exist)

Suggested change
it("should apply correct styles to inline code", () => {
render(<ChatMessage type="assistant" message="Here is some `inline code` text" />);
const codeElement = screen.getByText("inline code");
expect(codeElement.tagName.toLowerCase()).toBe("code");
expect(codeElement.closest("article")).not.toBeNull();
});
it("should render inline code", () => {
const { rerender } = render(
<ChatMessage type="assistant" message="Here is some regular text" />,
);
expect(screen.queryByRole("code")).not.toBeInTheDocument();
rerender(
<ChatMessage
type="assistant"
message="Here is some `inline code` text"
/>,
);
expect(screen.getByRole("code")).toBeInTheDocument();
});

Here we check for the presence of a code element only if the content contains backticks. We remove the check for article since that is the message wrapper which we test various times in previous tests.

If we want to test styles, I believe we can take advantage of snapshot testing

});
15 changes: 14 additions & 1 deletion frontend/src/components/markdown/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,20 @@ export function code({
const match = /language-(\w+)/.exec(className || ""); // get the language

if (!match) {
return <code className={className}>{children}</code>;
return (
<code
className={className}
style={{
backgroundColor: "#2a3038",
padding: "0.2em 0.4em",
borderRadius: "4px",
color: "#e6edf3",
border: "1px solid #30363d",
}}
>
{children}
</code>
);
}

return (
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ code {
margin: 0;
font-size: 85%;
white-space: break-spaces;
background-color: var(--bg-neutral-muted);
border-radius: 6px;
background-color: #2a3038;
border-radius: 4px;
color: #e6edf3;
border: 1px solid #30363d;
letter-spacing: -0.2px;
}

.markdown-body pre code {
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/services/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ export function handleAssistantMessage(message: Record<string, unknown>) {
} else if (message.status_update) {
handleStatusMessage(message as unknown as StatusMessage);
} else {
console.error("Unknown message type", message);
store.dispatch(
addErrorMessage({
message: "Unknown message type received",
}),
);
}
}