|
1 | | -import { describe, expect, it } from "vite-plus/test"; |
| 1 | +import { EnvironmentId } from "@t3tools/contracts"; |
| 2 | +import { renderToStaticMarkup } from "react-dom/server"; |
| 3 | +import { describe, expect, it, vi } from "vite-plus/test"; |
2 | 4 |
|
3 | | -import { orderedListGutterStyle } from "./ChatMarkdown"; |
| 5 | +vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null })); |
| 6 | +vi.mock("../hooks/useTheme", () => ({ useTheme: () => ({ resolvedTheme: "dark" }) })); |
| 7 | +vi.mock("../state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => vi.fn() })); |
| 8 | +vi.mock("../state/use-atom-command", () => ({ useAtomCommand: () => vi.fn() })); |
| 9 | +vi.mock("../state/session", async (importOriginal) => ({ |
| 10 | + ...(await importOriginal<typeof import("../state/session")>()), |
| 11 | + usePreparedConnection: () => ({ _tag: "Loading" }), |
| 12 | +})); |
| 13 | +vi.mock("../state/entities", () => ({ |
| 14 | + useActiveEnvironmentId: () => EnvironmentId.make("env-windows"), |
| 15 | +})); |
| 16 | +vi.mock("../editorPreferences", () => ({ useOpenInPreferredEditor: () => vi.fn() })); |
| 17 | +vi.mock("~/lib/openPullRequestLink", () => ({ useOpenChangeRequestLink: () => vi.fn() })); |
| 18 | + |
| 19 | +import ChatMarkdown, { orderedListGutterStyle } from "./ChatMarkdown"; |
4 | 20 |
|
5 | 21 | describe("orderedListGutterStyle", () => { |
6 | 22 | it("leaves the default gutter alone for single-digit lists", () => { |
@@ -42,3 +58,97 @@ describe("orderedListGutterStyle", () => { |
42 | 58 | expect(orderedListGutterStyle(0, 100)).toEqual({ "--list-gutter": "4ch" }); |
43 | 59 | }); |
44 | 60 | }); |
| 61 | + |
| 62 | +describe("ChatMarkdown Windows file links", () => { |
| 63 | + it.each([true, false])("preserves drive paths with parseRawHtml=%s", (parseRawHtml) => { |
| 64 | + const html = renderToStaticMarkup( |
| 65 | + <ChatMarkdown |
| 66 | + cwd="C:/Users/shawn/project" |
| 67 | + text="[Open](C:/Users/shawn/project/src/main.ts)" |
| 68 | + lineBreaks={!parseRawHtml} |
| 69 | + parseRawHtml={parseRawHtml} |
| 70 | + />, |
| 71 | + ); |
| 72 | + |
| 73 | + expect(html).toContain('href="C:/Users/shawn/project/src/main.ts"'); |
| 74 | + expect(html).toContain("chat-markdown-file-link"); |
| 75 | + }); |
| 76 | + |
| 77 | + it.each([true, false])("normalizes backslashes with parseRawHtml=%s", (parseRawHtml) => { |
| 78 | + const html = renderToStaticMarkup( |
| 79 | + <ChatMarkdown |
| 80 | + cwd="C:/Users/shawn/project" |
| 81 | + text={String.raw`[Open](C:\Users\shawn\project\src\main.ts)`} |
| 82 | + lineBreaks={!parseRawHtml} |
| 83 | + parseRawHtml={parseRawHtml} |
| 84 | + />, |
| 85 | + ); |
| 86 | + |
| 87 | + expect(html).toContain('href="C:/Users/shawn/project/src/main.ts"'); |
| 88 | + expect(html).toContain("chat-markdown-file-link"); |
| 89 | + }); |
| 90 | + |
| 91 | + it.each([true, false])( |
| 92 | + "distinguishes same-named backslash paths with parseRawHtml=%s", |
| 93 | + (parseRawHtml) => { |
| 94 | + const html = renderToStaticMarkup( |
| 95 | + <ChatMarkdown |
| 96 | + cwd="C:/Users/shawn/project" |
| 97 | + text={String.raw`[Source](C:\Users\shawn\project\src\index.ts) and [Test](C:\Users\shawn\project\test\index.ts)`} |
| 98 | + lineBreaks={!parseRawHtml} |
| 99 | + parseRawHtml={parseRawHtml} |
| 100 | + />, |
| 101 | + ); |
| 102 | + |
| 103 | + expect(html).toContain("index.ts · project/src"); |
| 104 | + expect(html).toContain("index.ts · project/test"); |
| 105 | + }, |
| 106 | + ); |
| 107 | + |
| 108 | + it.each([true, false])( |
| 109 | + "does not disambiguate the same file in links and inline code with parseRawHtml=%s", |
| 110 | + (parseRawHtml) => { |
| 111 | + const path = String.raw`C:\Users\shawn\project\src\main.ts`; |
| 112 | + const html = renderToStaticMarkup( |
| 113 | + <ChatMarkdown |
| 114 | + cwd="C:/Users/shawn/project" |
| 115 | + text={`[Source](${path}) and \`${path}\``} |
| 116 | + lineBreaks={!parseRawHtml} |
| 117 | + parseRawHtml={parseRawHtml} |
| 118 | + />, |
| 119 | + ); |
| 120 | + |
| 121 | + expect(html.match(/chat-markdown-file-link/g)).toHaveLength(2); |
| 122 | + expect(html).not.toContain("main.ts ·"); |
| 123 | + }, |
| 124 | + ); |
| 125 | + |
| 126 | + it.each([true, false])("preserves reference links with parseRawHtml=%s", (parseRawHtml) => { |
| 127 | + const html = renderToStaticMarkup( |
| 128 | + <ChatMarkdown |
| 129 | + cwd="C:/Users/shawn/project" |
| 130 | + text={"[Open][source]\n\n[source]: C:/Users/shawn/project/src/main.ts"} |
| 131 | + lineBreaks={!parseRawHtml} |
| 132 | + parseRawHtml={parseRawHtml} |
| 133 | + />, |
| 134 | + ); |
| 135 | + |
| 136 | + expect(html).toContain('href="C:/Users/shawn/project/src/main.ts"'); |
| 137 | + expect(html).toContain("chat-markdown-file-link"); |
| 138 | + }); |
| 139 | + |
| 140 | + it.each([true, false])("still rejects unsafe schemes with parseRawHtml=%s", (parseRawHtml) => { |
| 141 | + const html = renderToStaticMarkup( |
| 142 | + <ChatMarkdown |
| 143 | + cwd="C:/Users/shawn/project" |
| 144 | + text="[unsafe](javascript:alert(1)) and [unknown](d:alert(1))" |
| 145 | + lineBreaks={!parseRawHtml} |
| 146 | + parseRawHtml={parseRawHtml} |
| 147 | + />, |
| 148 | + ); |
| 149 | + |
| 150 | + expect(html).not.toContain("javascript:"); |
| 151 | + expect(html).not.toContain("d:alert"); |
| 152 | + expect(html).not.toContain("chat-markdown-file-link"); |
| 153 | + }); |
| 154 | +}); |
0 commit comments