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 · L1" });
67 await expect.element(link).toBeInTheDocument();
68 await expect.element(link).toHaveAttribute("href", `${filePath}:1`);
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
80 it("shows column information inline when present", async () => {
81 const filePath =
82 "/Users/yashsingh/p/sco/claude-code-extract/src/utils/permissions/PermissionRule.ts";
83 const screen = await render(
84 <ChatMarkdown text={`[PermissionRule.ts](file://${filePath}#L1C7)`} cwd="/repo/project" />,
85 );
86
87 try {
88 const link = page.getByRole("link", { name: "PermissionRule.ts · L1:C7" });
89 await expect.element(link).toBeInTheDocument();
90 await expect.element(link).toHaveAttribute("href", `${filePath}:1:7`);
91
92 await link.click();
93
94 await vi.waitFor(() => {
95 expect(openInPreferredEditorMock).toHaveBeenCalledWith(
96 expect.anything(),
97 `${filePath}:1:7`,
98 );
99 });
100 } finally {
101 await screen.unmount();
102 }
103 });
104
105 it("disambiguates duplicate file basenames inline", async () => {
106 const firstPath = "/Users/yashsingh/p/t3code/apps/web/src/components/chat/MessagesTimeline.tsx";
107 const secondPath = "/Users/yashsingh/p/t3code/apps/web/src/components/MessagesTimeline.tsx";
108 const screen = await render(
109 <ChatMarkdown
110 text={`See [MessagesTimeline.tsx](file://${firstPath}) and [MessagesTimeline.tsx](file://${secondPath}).`}
111 cwd="/repo/project"
112 />,
113 );
114
115 try {
116 await expect
117 .element(page.getByRole("link", { name: "MessagesTimeline.tsx · components/chat" }))
118 .toBeInTheDocument();
119 await expect
120 .element(page.getByRole("link", { name: "MessagesTimeline.tsx · src/components" }))
121 .toBeInTheDocument();
122 } finally {
123 await screen.unmount();
124 }
125 });
126
127 it("keeps normal web links unchanged", async () => {
128 const screen = await render(
129 <ChatMarkdown text="[OpenAI](https://openai.com/docs)" cwd="/repo/project" />,
130 );
131
132 try {
133 const link = page.getByRole("link", { name: "OpenAI" });
134 await expect.element(link).toBeInTheDocument();
135 await expect.element(link).toHaveAttribute("href", "https://openai.com/docs");
136 await expect.element(link).toHaveAttribute("target", "_blank");
137 } finally {
138 await screen.unmount();
139 }
140 });
141});