|
1 | 1 | import { EnvironmentId } from "@t3tools/contracts"; |
| 2 | +import { act, type ComponentProps, type ReactNode } from "react"; |
2 | 3 | import { renderToStaticMarkup } from "react-dom/server"; |
| 4 | +import { create, type ReactTestRenderer } from "react-test-renderer"; |
3 | 5 | import { describe, expect, it, vi } from "vite-plus/test"; |
4 | 6 |
|
| 7 | +import { getSyntaxHighlighterPromise } from "../lib/syntaxHighlighting"; |
| 8 | +import { Button } from "./ui/button"; |
| 9 | +import { setMarkdownTaskChecked } from "./files/filePreviewMode"; |
| 10 | + |
5 | 11 | vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null })); |
6 | 12 | vi.mock("../hooks/useTheme", () => ({ useTheme: () => ({ resolvedTheme: "dark" }) })); |
| 13 | +vi.mock("../hooks/useSettings", async (importOriginal) => { |
| 14 | + const actual = await importOriginal<typeof import("../hooks/useSettings")>(); |
| 15 | + const settings = actual.getClientSettings(); |
| 16 | + return { |
| 17 | + ...actual, |
| 18 | + useClientSettings: (select?: (value: typeof settings) => unknown) => |
| 19 | + select ? select(settings) : settings, |
| 20 | + }; |
| 21 | +}); |
| 22 | +vi.mock("./ui/tooltip", async () => { |
| 23 | + const { cloneElement, isValidElement } = await import("react"); |
| 24 | + return { |
| 25 | + Tooltip: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 26 | + TooltipTrigger({ |
| 27 | + render, |
| 28 | + children, |
| 29 | + }: ComponentProps<typeof import("./ui/tooltip").TooltipTrigger>) { |
| 30 | + if (!isValidElement(render)) return <>{children}</>; |
| 31 | + return children === undefined ? render : cloneElement(render, undefined, children); |
| 32 | + }, |
| 33 | + TooltipPopup: () => null, |
| 34 | + }; |
| 35 | +}); |
7 | 36 | vi.mock("../state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => vi.fn() })); |
8 | 37 | vi.mock("../state/use-atom-command", () => ({ useAtomCommand: () => vi.fn() })); |
9 | 38 | vi.mock("../state/session", async (importOriginal) => ({ |
@@ -35,6 +64,147 @@ import ChatMarkdown, { |
35 | 64 | shouldUseMarkdownFileBrowserPrimaryAction, |
36 | 65 | } from "./ChatMarkdown"; |
37 | 66 |
|
| 67 | +function codeButton(renderer: ReactTestRenderer, label: string) { |
| 68 | + const button = renderer.root |
| 69 | + .findAllByType(Button) |
| 70 | + .find((instance) => instance.props["aria-label"] === label); |
| 71 | + if (!button) throw new Error(`Missing code button: ${label}`); |
| 72 | + return button.props as ComponentProps<typeof Button>; |
| 73 | +} |
| 74 | + |
| 75 | +describe("ChatMarkdown streaming", () => { |
| 76 | + it("preserves code controls and details without highlighting an unchanged fence again", async () => { |
| 77 | + const highlighter = await getSyntaxHighlighterPromise("text"); |
| 78 | + const highlight = vi.spyOn(highlighter, "codeToHtml"); |
| 79 | + const writeText = vi.fn(async (_text: string) => {}); |
| 80 | + vi.stubGlobal("navigator", { clipboard: { writeText } }); |
| 81 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 82 | + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); |
| 83 | + let renderer: ReactTestRenderer | undefined; |
| 84 | + const text = [ |
| 85 | + "```text", |
| 86 | + "First code block", |
| 87 | + "```", |
| 88 | + "", |
| 89 | + "<details><summary>More</summary>", |
| 90 | + "", |
| 91 | + "Details content", |
| 92 | + "", |
| 93 | + "</details>", |
| 94 | + "", |
| 95 | + "Streaming reply", |
| 96 | + ].join("\n"); |
| 97 | + |
| 98 | + try { |
| 99 | + await act(async () => { |
| 100 | + renderer = create(<ChatMarkdown cwd="/tmp/project" text={text} isStreaming />); |
| 101 | + }); |
| 102 | + const mounted = renderer!; |
| 103 | + const codeBlock = mounted.root.findByProps({ "data-language": "text" }); |
| 104 | + const initialWrap = codeBlock.props["data-wrap"] === "true"; |
| 105 | + const wrap = codeButton(mounted, initialWrap ? "Disable line wrap" : "Wrap lines"); |
| 106 | + const copy = codeButton(mounted, "Copy code"); |
| 107 | + await act(async () => { |
| 108 | + wrap.onClick?.({} as Parameters<NonNullable<typeof wrap.onClick>>[0]); |
| 109 | + copy.onClick?.({} as Parameters<NonNullable<typeof copy.onClick>>[0]); |
| 110 | + }); |
| 111 | + |
| 112 | + const detailsButton = mounted.root.find( |
| 113 | + (instance) => |
| 114 | + instance.type === "button" && instance.props["data-markdown-details-summary"] === "", |
| 115 | + ); |
| 116 | + await act(async () => { |
| 117 | + detailsButton.props.onClick({ nativeEvent: new Event("click") }); |
| 118 | + }); |
| 119 | + const details = mounted.root.findByProps({ "data-markdown-details": "" }); |
| 120 | + expect(details.props["data-markdown-details-open"]).toBe("true"); |
| 121 | + expect(writeText).toHaveBeenCalledWith("First code block\n"); |
| 122 | + expect(highlight).toHaveBeenCalledTimes(1); |
| 123 | + |
| 124 | + for (let index = 0; index < 10; index += 1) { |
| 125 | + await act(async () => { |
| 126 | + mounted.update(<ChatMarkdown cwd="/tmp/project" text={`${text} ${index}`} isStreaming />); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + expect(highlight).toHaveBeenCalledTimes(1); |
| 131 | + expect(mounted.root.findByProps({ "data-language": "text" })).toBe(codeBlock); |
| 132 | + expect(codeBlock.props["data-wrap"]).toBe(String(!initialWrap)); |
| 133 | + expect(mounted.root.findByProps({ "data-markdown-details": "" })).toBe(details); |
| 134 | + expect(details.props["data-markdown-details-open"]).toBe("true"); |
| 135 | + await act(async () => { |
| 136 | + mounted.update( |
| 137 | + <ChatMarkdown |
| 138 | + cwd="/tmp/project" |
| 139 | + text={text.replace("First code block", "Updated code block")} |
| 140 | + isStreaming |
| 141 | + />, |
| 142 | + ); |
| 143 | + }); |
| 144 | + const copyUpdated = codeButton(mounted, "Copied"); |
| 145 | + await act(async () => { |
| 146 | + copyUpdated.onClick?.({} as Parameters<NonNullable<typeof copyUpdated.onClick>>[0]); |
| 147 | + }); |
| 148 | + expect(writeText).toHaveBeenLastCalledWith("Updated code block\n"); |
| 149 | + expect(highlight).toHaveBeenCalledTimes(2); |
| 150 | + } finally { |
| 151 | + await act(async () => renderer?.unmount()); |
| 152 | + vi.useRealTimers(); |
| 153 | + vi.unstubAllGlobals(); |
| 154 | + vi.restoreAllMocks(); |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + it("edits the current task text and marker after reusing a renderer", async () => { |
| 159 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 160 | + let renderer: ReactTestRenderer | undefined; |
| 161 | + let editedText: string | undefined; |
| 162 | + const message = (text: string) => ( |
| 163 | + <ChatMarkdown |
| 164 | + cwd="/tmp/project" |
| 165 | + text={text} |
| 166 | + onTaskListChange={({ markerOffset, checked }) => { |
| 167 | + editedText = setMarkdownTaskChecked(text, markerOffset, checked); |
| 168 | + renderer!.update(message(editedText)); |
| 169 | + }} |
| 170 | + /> |
| 171 | + ); |
| 172 | + |
| 173 | + try { |
| 174 | + await act(async () => { |
| 175 | + renderer = create(message("- [ ] First\n- [ ] Second")); |
| 176 | + }); |
| 177 | + const mounted = renderer!; |
| 178 | + const originalInput = mounted.root.findAllByType("input")[1]!; |
| 179 | + await act(async () => { |
| 180 | + mounted.update(message("- [ ] A longer first task\n- [ ] Second")); |
| 181 | + }); |
| 182 | + |
| 183 | + const input = mounted.root.findAllByType("input")[1]!; |
| 184 | + const listItem = mounted.root.findAllByType("li")[1]!; |
| 185 | + const { onChange } = input.props as ComponentProps<"input">; |
| 186 | + if (!onChange) throw new Error("Task checkbox has no edit handler"); |
| 187 | + await act(async () => { |
| 188 | + onChange({ |
| 189 | + currentTarget: { |
| 190 | + checked: true, |
| 191 | + closest: () => ({ |
| 192 | + dataset: { taskMarkerOffset: String(listItem.props["data-task-marker-offset"]) }, |
| 193 | + }), |
| 194 | + }, |
| 195 | + } as unknown as Parameters<typeof onChange>[0]); |
| 196 | + }); |
| 197 | + |
| 198 | + expect(input).toBe(originalInput); |
| 199 | + expect(editedText).toBe("- [ ] A longer first task\n- [x] Second"); |
| 200 | + expect(mounted.root.findAllByType("input")[1]!.props.checked).toBe(true); |
| 201 | + } finally { |
| 202 | + await act(async () => renderer?.unmount()); |
| 203 | + vi.unstubAllGlobals(); |
| 204 | + } |
| 205 | + }); |
| 206 | +}); |
| 207 | + |
38 | 208 | describe("canUseMarkdownFileShellActions", () => { |
39 | 209 | const environmentId = EnvironmentId.make("environment-1"); |
40 | 210 |
|
|
0 commit comments