Open History to see the last commit
1import "../index.css";
2
3import { page } from "vitest/browser";
4import { afterEach, describe, expect, it, vi } from "vitest";
5import { render } from "vitest-browser-react";
6
7const { openInPreferredEditorMock, readLocalApiMock } = vi.hoisted(() => ({
8 openInPreferredEditorMock: vi.fn(async () => "vscode"),
9 readLocalApiMock: vi.fn(() => ({
10 server: { getConfig: vi.fn(async () => ({ availableEditors: ["vscode"] })) },
11 shell: { openInEditor: vi.fn(async () => undefined) },
12 })),
13}));
14
15vi.mock("../editorPreferences", () => ({
16 openInPreferredEditor: openInPreferredEditorMock,
17}));
18
19vi.mock("../localApi", () => ({
20 ensureLocalApi: vi.fn(() => {
21 throw new Error("ensureLocalApi not implemented in browser test");
22 }),
23 readLocalApi: readLocalApiMock,
24}));
25
26import ChatMarkdown from "./ChatMarkdown";
27
28describe("ChatMarkdown", () => {
29 afterEach(() => {
30 openInPreferredEditorMock.mockClear();
31 readLocalApiMock.mockClear();
32 localStorage.clear();
33 document.body.innerHTML = "";
34 });
35
36 it("rewrites file uri hrefs into direct paths before rendering", async () => {
37 const filePath =
38 "/Users/yashsingh/p/sco/claude-code-extract/src/utils/permissions/PermissionRule.ts";
39 const screen = await render(
40 <ChatMarkdown text={`[PermissionRule.ts](file://${filePath})`} cwd="/repo/project" />,
41 );
42
43 try {
44 const link = page.getByRole("link", { name: "PermissionRule.ts" });
45 await expect.element(link).toBeInTheDocument();
46 await expect.element(link).toHaveAttribute("href", filePath);
47
48 await link.click();
49
50 await vi.waitFor(() => {
51 expect(openInPreferredEditorMock).toHaveBeenCalledWith(expect.anything(), filePath);
52 });
53 } finally {
54 await screen.unmount();
55 }
56 });
57
58 it("keeps line anchors working after rewriting file uri hrefs", async () => {
59 const filePath =
60 "/Users/yashsingh/p/sco/claude-code-extract/src/utils/permissions/PermissionRule.ts";
61 const screen = await render(
62 <ChatMarkdown text={`[PermissionRule.ts:1](file://${filePath}#L1)`} cwd="/repo/project" />,
63 );
64
65 try {
66 const link = page.getByRole("link", { name: "PermissionRule.ts:1" });
67 await expect.element(link).toBeInTheDocument();
68 await expect.element(link).toHaveAttribute("href", `${filePath}#L1`);
69
70 await link.click();
71
72 await vi.waitFor(() => {
73 expect(openInPreferredEditorMock).toHaveBeenCalledWith(expect.anything(), `${filePath}:1`);
74 });
75 } finally {
76 await screen.unmount();
77 }
78 });
79});