The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | ASSISTANT_CITATION_MAX_COMMENT_LENGTH, |
| 3 | type AssistantCitation, |
| 4 | EnvironmentId, |
| 5 | MessageId, |
| 6 | ThreadId, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { useState, type ReactNode } from "react"; |
| 9 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 10 | import { userEvent } from "vite-plus/test/browser"; |
| 11 | |
| 12 | import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection"; |
| 13 | import "~/index.css"; |
| 14 | import { act, render } from "../../../test/render"; |
| 15 | |
| 16 | const mocks = vi.hoisted(() => ({ observeSource: vi.fn(), dispose: vi.fn() })); |
| 17 | vi.mock(import("./AssistantCitationSource"), async (importOriginal) => ({ |
| 18 | ...(await importOriginal()), |
| 19 | observeAssistantCitationCommentSource: mocks.observeSource, |
| 20 | })); |
| 21 | vi.mock(import("@tanstack/react-router"), async (importOriginal) => ({ |
| 22 | ...(await importOriginal()), |
| 23 | useNavigate: () => vi.fn(), |
| 24 | Link: (({ children }: { children: ReactNode }) => <a>{children}</a>) as never, |
| 25 | })); |
| 26 | |
| 27 | import { AssistantCitationChip } from "./AssistantCitationChip"; |
| 28 | |
| 29 | const citation = { |
| 30 | version: 1 as const, |
| 31 | environmentId: EnvironmentId.make("environment"), |
| 32 | threadId: ThreadId.make("thread"), |
| 33 | messageId: MessageId.make("source"), |
| 34 | text: "hello", |
| 35 | start: 0, |
| 36 | end: 5, |
| 37 | prefix: "", |
| 38 | suffix: "", |
| 39 | }; |
| 40 | |
| 41 | // The mocked source observer is shared, so every test starts by resetting it. |
| 42 | function setup() { |
| 43 | mocks.observeSource.mockReset().mockReturnValue(mocks.dispose); |
| 44 | mocks.dispose.mockClear(); |
| 45 | const source = document.createElement("p"); |
| 46 | source.textContent = "hello"; |
| 47 | document.body.append(source); |
| 48 | const range = document.createRange(); |
| 49 | range.selectNodeContents(source); |
| 50 | const sourceAnchor = { source, range, viewport: document.body } as AssistantCitationSourceAnchor; |
| 51 | return { sourceAnchor, [Symbol.dispose]: () => source.remove() }; |
| 52 | } |
| 53 | |
| 54 | function mount( |
| 55 | sourceAnchor: AssistantCitationSourceAnchor | undefined, |
| 56 | onSave: () => boolean, |
| 57 | chipCitation: AssistantCitation = citation, |
| 58 | ) { |
| 59 | function Composer() { |
| 60 | const [open, setOpen] = useState(true); |
| 61 | return ( |
| 62 | <AssistantCitationChip |
| 63 | citation={chipCitation} |
| 64 | commentEditor={{ open, sourceAnchor, onOpenChange: setOpen, onSave }} |
| 65 | /> |
| 66 | ); |
| 67 | } |
| 68 | return render(<Composer />); |
| 69 | } |
| 70 | |
| 71 | const textarea = () => |
| 72 | document.querySelector<HTMLTextAreaElement>('textarea[aria-label="Comment on selected text"]'); |
| 73 | |
| 74 | async function typeComment(value: string) { |
| 75 | const input = textarea()!; |
| 76 | await act(async () => { |
| 77 | Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value")!.set!.call( |
| 78 | input, |
| 79 | value, |
| 80 | ); |
| 81 | input.dispatchEvent(new Event("input", { bubbles: true })); |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | async function removeSource() { |
| 86 | const { onUnavailable } = mocks.observeSource.mock.lastCall![0]; |
| 87 | await act(async () => onUnavailable()); |
| 88 | } |
| 89 | |
| 90 | async function clickButton(label: string) { |
| 91 | const button = [...document.querySelectorAll("button")].find( |
| 92 | (element) => element.textContent?.trim() === label, |
| 93 | ); |
| 94 | expect(button, `button ${label}`).toBeDefined(); |
| 95 | await act(async () => button!.click()); |
| 96 | } |
| 97 | |
| 98 | describe("citation comment source disappearance", () => { |
| 99 | it("preserves an over-length draft at the composer until it can be shortened and saved", async () => { |
| 100 | using source = setup(); |
| 101 | const onSave = vi.fn(() => true); |
| 102 | await using _screen = await mount(source.sourceAnchor, onSave); |
| 103 | const draft = "x".repeat(ASSISTANT_CITATION_MAX_COMMENT_LENGTH + 1); |
| 104 | await typeComment(draft); |
| 105 | await removeSource(); |
| 106 | |
| 107 | expect(textarea()?.value).toBe(draft); |
| 108 | expect(textarea()?.getAttribute("aria-invalid")).toBe("true"); |
| 109 | expect(onSave).not.toHaveBeenCalled(); |
| 110 | expect(mocks.dispose).toHaveBeenCalled(); |
| 111 | expect(mocks.observeSource).toHaveBeenCalledTimes(1); |
| 112 | |
| 113 | await typeComment("shortened comment"); |
| 114 | await clickButton("Save"); |
| 115 | expect(onSave).toHaveBeenCalledWith("shortened comment"); |
| 116 | expect(textarea()).toBeNull(); |
| 117 | }); |
| 118 | |
| 119 | it("preserves a rejected save and allows a later retry", async () => { |
| 120 | using source = setup(); |
| 121 | const onSave = vi.fn(() => false); |
| 122 | await using _screen = await mount(source.sourceAnchor, onSave); |
| 123 | await typeComment("keep this draft"); |
| 124 | await removeSource(); |
| 125 | |
| 126 | expect(onSave).toHaveBeenCalledWith("keep this draft"); |
| 127 | expect(textarea()?.value).toBe("keep this draft"); |
| 128 | onSave.mockReturnValue(true); |
| 129 | await clickButton("Save"); |
| 130 | expect(onSave).toHaveBeenLastCalledWith("keep this draft"); |
| 131 | expect(textarea()).toBeNull(); |
| 132 | }); |
| 133 | }); |
| 134 | |
| 135 | describe("citation comment caret", () => { |
| 136 | it("opens a saved comment with the caret after its end, scrolled into view", async () => { |
| 137 | using _source = setup(); |
| 138 | const comment = `${"line\n".repeat(40)}end`; |
| 139 | await using _screen = await mount( |
| 140 | undefined, |
| 141 | vi.fn(() => true), |
| 142 | { ...citation, comment }, |
| 143 | ); |
| 144 | await vi.waitFor(() => expect(document.activeElement).toBe(textarea())); |
| 145 | const input = textarea()!; |
| 146 | |
| 147 | expect(input.scrollHeight).toBeGreaterThan(input.clientHeight); |
| 148 | expect(input.scrollTop).toBe(input.scrollHeight - input.clientHeight); |
| 149 | await userEvent.keyboard("!"); |
| 150 | expect(input.value).toBe(`${comment}!`); |
| 151 | }); |
| 152 | |
| 153 | it("saves with Tab then Enter after typing a comment", async () => { |
| 154 | using _source = setup(); |
| 155 | const onSave = vi.fn(() => true); |
| 156 | await using _screen = await mount(undefined, onSave); |
| 157 | await vi.waitFor(() => expect(document.activeElement).toBe(textarea())); |
| 158 | |
| 159 | await userEvent.keyboard("agreed"); |
| 160 | await userEvent.keyboard("{Tab}"); |
| 161 | await userEvent.keyboard("{Enter}"); |
| 162 | |
| 163 | expect(onSave).toHaveBeenCalledWith("agreed"); |
| 164 | }); |
| 165 | }); |
| 166 | |
| 167 | describe("citation comment popup position", () => { |
| 168 | it("opens at its anchor instead of sliding in from the window corner", async () => { |
| 169 | using source = setup(); |
| 170 | await using _screen = await mount( |
| 171 | source.sourceAnchor, |
| 172 | vi.fn(() => true), |
| 173 | ); |
| 174 | const positioner = () => |
| 175 | document.querySelector<HTMLElement>('[data-slot="popover-positioner"]'); |
| 176 | await vi.waitFor(() => expect(positioner()?.style.opacity).not.toBe("0")); |
| 177 | |
| 178 | const moves = positioner()! |
| 179 | .getAnimations() |
| 180 | .filter( |
| 181 | (animation) => |
| 182 | animation instanceof CSSTransition && |
| 183 | ["transform", "top", "left"].includes(animation.transitionProperty), |
| 184 | ); |
| 185 | expect(moves).toEqual([]); |
| 186 | }); |
| 187 | |
| 188 | it("stays on screen when the cited text ends in a code block's hidden overflow", async () => { |
| 189 | using _source = setup(); |
| 190 | const message = document.createElement("div"); |
| 191 | message.className = "chat-markdown"; |
| 192 | message.style.width = "240px"; |
| 193 | const pre = document.createElement("pre"); |
| 194 | const code = document.createElement("code"); |
| 195 | // Highlighted code is split into token spans, so the range's last client rect |
| 196 | // covers only the final token, wholly inside the hidden overflow. |
| 197 | for (let index = 0; index < 40; index += 1) { |
| 198 | const token = document.createElement("span"); |
| 199 | token.textContent = `token${index} `; |
| 200 | code.append(token); |
| 201 | } |
| 202 | pre.append(code); |
| 203 | message.append(pre); |
| 204 | document.body.prepend(message); |
| 205 | using _message = { [Symbol.dispose]: () => message.remove() }; |
| 206 | const range = document.createRange(); |
| 207 | range.selectNodeContents(code); |
| 208 | const rects = range.getClientRects(); |
| 209 | expect(rects.item(rects.length - 1)!.left).toBeGreaterThan(window.innerWidth); |
| 210 | const sourceAnchor = { |
| 211 | source: message, |
| 212 | range, |
| 213 | viewport: document.body, |
| 214 | } as AssistantCitationSourceAnchor; |
| 215 | |
| 216 | await using _screen = await mount( |
| 217 | sourceAnchor, |
| 218 | vi.fn(() => true), |
| 219 | ); |
| 220 | const popup = () => document.querySelector<HTMLElement>('[data-slot="popover-popup"]'); |
| 221 | await vi.waitFor(() => expect(popup()?.getBoundingClientRect().width).toBeGreaterThan(0)); |
| 222 | // Typing scrolls ancestors to reveal the caret, which shifts the whole app |
| 223 | // sideways when the box is off screen. |
| 224 | await userEvent.keyboard("note"); |
| 225 | |
| 226 | expect(textarea()?.value).toBe("note"); |
| 227 | expect(document.scrollingElement?.scrollLeft).toBe(0); |
| 228 | expect(document.body.scrollLeft).toBe(0); |
| 229 | const rect = popup()!.getBoundingClientRect(); |
| 230 | expect(rect.left).toBeGreaterThanOrEqual(0); |
| 231 | expect(rect.right).toBeLessThanOrEqual(window.innerWidth); |
| 232 | }); |
| 233 | }); |