Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ChatMarkdown.test.tsx

291 lines9.9 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { renderToStaticMarkup } from "react-dom/server";
3import { describe, expect, it, vi } from "vite-plus/test";
4​
5vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null }));
6vi.mock("../hooks/useTheme", () => ({ useTheme: () => ({ resolvedTheme: "dark" }) }));
7vi.mock("../state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => vi.fn() }));
8vi.mock("../state/use-atom-command", () => ({ useAtomCommand: () => vi.fn() }));
9vi.mock("../state/session", async (importOriginal) => ({
10 ...(await importOriginal<typeof import("../state/session")>()),
11 usePreparedConnection: () => ({ _tag: "Loading" }),
12}));
13vi.mock("../state/entities", () => ({
14 readThreadShell: () => null,
15 useProjects: () => [],
16}));
17vi.mock("../remoteOpen", () => ({
18 useRemoteOpenResolution: () => ({ state: { mode: "local-exec" }, isResolved: true }),
19}));
20vi.mock("../editorPreferences", () => ({
21 useOpenInPreferredEditor: () => vi.fn(),
22 usePreferredEditor: () => [null, vi.fn()],
23}));
24vi.mock("~/lib/openPullRequestLink", () => ({
25 findProjectForChangeRequest: () => undefined,
26 matchesLinkedPullRequestUrl: () => false,
27 parseChangeRequestUrl: () => null,
28 useOpenChangeRequestLink: () => vi.fn(),
29}));
30​
31import ChatMarkdown, {
32 canUseMarkdownFileShellActions,
33 hasMarkdownFilePrimaryAction,
34 orderedListGutterStyle,
35 shouldUseMarkdownFileBrowserPrimaryAction,
36} from "./ChatMarkdown";
37​
38describe("canUseMarkdownFileShellActions", () => {
39 const environmentId = EnvironmentId.make("environment-1");
40​
41 it("allows editor and file manager actions for local environments", () => {
42 expect(canUseMarkdownFileShellActions(environmentId, "local-exec", true)).toBe(true);
43 });
44​
45 it("hides shell actions until the environment mode is resolved", () => {
46 expect(canUseMarkdownFileShellActions(environmentId, "local-exec", false)).toBe(false);
47 });
48​
49 it("hides editor and file manager actions for remote environments", () => {
50 expect(canUseMarkdownFileShellActions(environmentId, "remote-links", true)).toBe(false);
51 expect(canUseMarkdownFileShellActions(environmentId, "remote-unavailable", true)).toBe(false);
52 });
53​
54 it("hides shell actions when no environment owns the markdown", () => {
55 expect(canUseMarkdownFileShellActions(null, "local-exec", true)).toBe(false);
56 });
57});
58​
59describe("hasMarkdownFilePrimaryAction", () => {
60 it("keeps the chip interactive when an editor, browser, or panel can open it", () => {
61 expect(
62 hasMarkdownFilePrimaryAction({
63 canOpenInEditor: true,
64 canOpenInBrowser: false,
65 canOpenInPanel: false,
66 }),
67 ).toBe(true);
68 expect(
69 hasMarkdownFilePrimaryAction({
70 canOpenInEditor: false,
71 canOpenInBrowser: true,
72 canOpenInPanel: false,
73 }),
74 ).toBe(true);
75 expect(
76 hasMarkdownFilePrimaryAction({
77 canOpenInEditor: false,
78 canOpenInBrowser: false,
79 canOpenInPanel: true,
80 }),
81 ).toBe(true);
82 });
83​
84 it("removes the link affordance when no primary action can open the file", () => {
85 expect(
86 hasMarkdownFilePrimaryAction({
87 canOpenInEditor: false,
88 canOpenInBrowser: false,
89 canOpenInPanel: false,
90 }),
91 ).toBe(false);
92 });
93});
94​
95describe("ChatMarkdown file option chips", () => {
96 it("keeps the fallback button text selectable", () => {
97 const html = renderToStaticMarkup(
98 <ChatMarkdown cwd="/tmp/project" text="[Source](/tmp/project/src/main.ts)" />,
99 );
100​
101 expect(html).toContain("<button");
102 expect(html).toContain('aria-haspopup="menu"');
103 expect(html).toContain("select-text");
104 });
105});
106​
107describe("shouldUseMarkdownFileBrowserPrimaryAction", () => {
108 it("uses the browser when it is the only available primary action", () => {
109 expect(
110 shouldUseMarkdownFileBrowserPrimaryAction({
111 iconPath: "/tmp/report.html",
112 canOpenInEditor: false,
113 canOpenInBrowser: true,
114 canOpenInPanel: false,
115 }),
116 ).toBe(true);
117 });
118​
119 it("preserves the normal editor and panel defaults for HTML files", () => {
120 expect(
121 shouldUseMarkdownFileBrowserPrimaryAction({
122 iconPath: "/tmp/report.html",
123 canOpenInEditor: true,
124 canOpenInBrowser: true,
125 canOpenInPanel: false,
126 }),
127 ).toBe(false);
128 expect(
129 shouldUseMarkdownFileBrowserPrimaryAction({
130 iconPath: "/tmp/report.html",
131 canOpenInEditor: false,
132 canOpenInBrowser: true,
133 canOpenInPanel: true,
134 }),
135 ).toBe(false);
136 });
137​
138 it("continues to open PDF files in the browser by default", () => {
139 expect(
140 shouldUseMarkdownFileBrowserPrimaryAction({
141 iconPath: "/tmp/report.pdf",
142 canOpenInEditor: true,
143 canOpenInBrowser: true,
144 canOpenInPanel: true,
145 }),
146 ).toBe(true);
147 });
148});
149​
150describe("orderedListGutterStyle", () => {
151 it("leaves the default gutter alone for single-digit lists", () => {
152 expect(orderedListGutterStyle(9, undefined)).toBeUndefined();
153 });
154​
155 it("leaves the default gutter alone for two-digit lists", () => {
156 expect(orderedListGutterStyle(99, undefined)).toBeUndefined();
157 });
158​
159 it("leaves the default gutter alone for a two-digit list that starts above 1", () => {
160 // start=50 + 49 items => last marker is "98", still two digits.
161 expect(orderedListGutterStyle(49, 50)).toBeUndefined();
162 });
163​
164 it("widens the gutter once the last marker reaches three digits", () => {
165 // item 100 is the bug from #6512: a 100-item list starting at 1.
166 expect(orderedListGutterStyle(100, undefined)).toEqual({ "--list-gutter": "4ch" });
167 });
168​
169 it("accounts for a non-default start attribute", () => {
170 // start=95 + 9 items => last marker is "103", three digits.
171 expect(orderedListGutterStyle(9, 95)).toEqual({ "--list-gutter": "4ch" });
172 expect(orderedListGutterStyle(5, "999995")).toEqual({ "--list-gutter": "7ch" });
173 });
174​
175 it("scales further for four-digit markers", () => {
176 expect(orderedListGutterStyle(1000, undefined)).toEqual({ "--list-gutter": "5ch" });
177 });
178​
179 it("uses the widest marker and includes a negative start's minus sign", () => {
180 expect(orderedListGutterStyle(1001, -1000)).toEqual({ "--list-gutter": "6ch" });
181 expect(orderedListGutterStyle(3, -15)).toEqual({ "--list-gutter": "4ch" });
182 expect(orderedListGutterStyle(3, -5)).toBeUndefined();
183 });
184​
185 it("treats a missing/zero item count as a single item", () => {
186 expect(orderedListGutterStyle(0, undefined)).toBeUndefined();
187 expect(orderedListGutterStyle(0, 100)).toEqual({ "--list-gutter": "4ch" });
188 });
189});
190​
191describe("ChatMarkdown Windows file links", () => {
192 const environmentId = EnvironmentId.make("env-windows");
193​
194 it.each([true, false])("preserves drive paths with parseRawHtml=%s", (parseRawHtml) => {
195 const html = renderToStaticMarkup(
196 <ChatMarkdown
197 cwd="C:/Users/shawn/project"
198 environmentId={environmentId}
199 text="[Open](C:/Users/shawn/project/src/main.ts)"
200 lineBreaks={!parseRawHtml}
201 parseRawHtml={parseRawHtml}
202 />,
203 );
204​
205 expect(html).toContain('href="C:/Users/shawn/project/src/main.ts"');
206 expect(html).toContain("chat-markdown-file-link");
207 });
208​
209 it.each([true, false])("normalizes backslashes with parseRawHtml=%s", (parseRawHtml) => {
210 const html = renderToStaticMarkup(
211 <ChatMarkdown
212 cwd="C:/Users/shawn/project"
213 environmentId={environmentId}
214 text={String.raw`[Open](C:\Users\shawn\project\src\main.ts)`}
215 lineBreaks={!parseRawHtml}
216 parseRawHtml={parseRawHtml}
217 />,
218 );
219​
220 expect(html).toContain('href="C:/Users/shawn/project/src/main.ts"');
221 expect(html).toContain("chat-markdown-file-link");
222 });
223​
224 it.each([true, false])(
225 "distinguishes same-named backslash paths with parseRawHtml=%s",
226 (parseRawHtml) => {
227 const html = renderToStaticMarkup(
228 <ChatMarkdown
229 cwd="C:/Users/shawn/project"
230 environmentId={environmentId}
231 text={String.raw`[Source](C:\Users\shawn\project\src\index.ts) and [Test](C:\Users\shawn\project\test\index.ts)`}
232 lineBreaks={!parseRawHtml}
233 parseRawHtml={parseRawHtml}
234 />,
235 );
236​
237 expect(html).toContain("index.ts · project/src");
238 expect(html).toContain("index.ts · project/test");
239 },
240 );
241​
242 it.each([true, false])(
243 "does not disambiguate the same file in links and inline code with parseRawHtml=%s",
244 (parseRawHtml) => {
245 const path = String.raw`C:\Users\shawn\project\src\main.ts`;
246 const html = renderToStaticMarkup(
247 <ChatMarkdown
248 cwd="C:/Users/shawn/project"
249 environmentId={environmentId}
250 text={`[Source](${path}) and \`${path}\``}
251 lineBreaks={!parseRawHtml}
252 parseRawHtml={parseRawHtml}
253 />,
254 );
255​
256 expect(html.match(/chat-markdown-file-link/g)).toHaveLength(2);
257 expect(html).not.toContain("main.ts ·");
258 },
259 );
260​
261 it.each([true, false])("preserves reference links with parseRawHtml=%s", (parseRawHtml) => {
262 const html = renderToStaticMarkup(
263 <ChatMarkdown
264 cwd="C:/Users/shawn/project"
265 environmentId={environmentId}
266 text={"[Open][source]\n\n[source]: C:/Users/shawn/project/src/main.ts"}
267 lineBreaks={!parseRawHtml}
268 parseRawHtml={parseRawHtml}
269 />,
270 );
271​
272 expect(html).toContain('href="C:/Users/shawn/project/src/main.ts"');
273 expect(html).toContain("chat-markdown-file-link");
274 });
275​
276 it.each([true, false])("still rejects unsafe schemes with parseRawHtml=%s", (parseRawHtml) => {
277 const html = renderToStaticMarkup(
278 <ChatMarkdown
279 cwd="C:/Users/shawn/project"
280 environmentId={environmentId}
281 text="[unsafe](javascript:alert(1)) and [unknown](d:alert(1))"
282 lineBreaks={!parseRawHtml}
283 parseRawHtml={parseRawHtml}
284 />,
285 );
286​
287 expect(html).not.toContain("javascript:");
288 expect(html).not.toContain("d:alert");
289 expect(html).not.toContain("chat-markdown-file-link");
290 });
291});