@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
tsx 186 lines 6.66 KB
Raw
1 import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
2 import {
3 serializeAssistantCitation,
4 withAssistantCitationComment,
5 } from "@t3tools/shared/assistantCitations";
6 import { createRef, useState } from "react";
7 import { expect, it } from "vite-plus/test";
8
9 import { page, userEvent } from "vite-plus/test/browser";
10
11 import { findAssistantCitationSourceAnchor } from "~/lib/assistantTextSelection";
12 import { act, render } from "../../test/render";
13 import {
14 ComposerPromptEditorTiptap as ComposerPromptEditor,
15 type ComposerPromptEditorHandle,
16 } from "./ComposerPromptEditorTiptap";
17
18 const citation = {
19 version: 1 as const,
20 environmentId: EnvironmentId.make("environment"),
21 threadId: ThreadId.make("thread"),
22 messageId: MessageId.make("source"),
23 text: "hello",
24 start: 0,
25 end: 5,
26 prefix: "",
27 suffix: "",
28 };
29 const promptBefore = `See ${serializeAssistantCitation(citation)} `;
30
31 function Composer({
32 question,
33 editorRef,
34 disabled = false,
35 send = "clears",
36 }: {
37 question: boolean;
38 editorRef: React.RefObject<ComposerPromptEditorHandle | null>;
39 disabled?: boolean;
40 send?: "clears" | "blocked" | "refusedLater";
41 }) {
42 const [prompt, setPrompt] = useState(promptBefore);
43 const [answer, setAnswer] = useState("");
44 return (
45 <>
46 <div data-assistant-citation-viewport="">
47 <p
48 data-assistant-citation-source={citation.messageId}
49 data-assistant-citation-environment={citation.environmentId}
50 data-assistant-citation-thread={citation.threadId}
51 >
52 hello world
53 </p>
54 </div>
55 <output data-prompt={prompt} data-answer={answer} />
56 <ComposerPromptEditor
57 editorRef={editorRef}
58 value={question ? answer : prompt}
59 cursor={0}
60 contextRecords={new Map()}
61 skills={[]}
62 disabled={disabled}
63 placeholder=""
64 onChange={(next) => (question ? setAnswer(next) : setPrompt(next))}
65 onCitationDraftSave={(replacedValue, savedValue) => {
66 if (replacedValue === prompt) setPrompt(savedValue);
67 }}
68 onCitationSubmitAndSend={() => {
69 if (send === "blocked") return false;
70 if (send === "clears") setPrompt("");
71 return true;
72 }}
73 onPaste={() => {}}
74 />
75 </>
76 );
77 }
78
79 it("keeps an unsaved citation comment when a question takes the composer", async () => {
80 const editorRef = createRef<ComposerPromptEditorHandle>();
81 await using screen = await render(<Composer question={false} editorRef={editorRef} />);
82 const drafts = () => screen.container.querySelector("output")?.dataset;
83
84 const sourceAnchor = findAssistantCitationSourceAnchor(document, citation);
85 if (!sourceAnchor) throw new Error("Citation source did not resolve");
86 await act(async () =>
87 editorRef.current?.requestCitationComment({
88 previousValue: "See ",
89 value: promptBefore,
90 citationStart: "See ".length,
91 sourceAnchor,
92 insertedSpaces: { before: false, after: true },
93 }),
94 );
95 const comment = page.getByLabelText("Comment on selected text");
96 await expect.element(comment).toBeVisible();
97 await comment.click();
98 await userEvent.keyboard("check this");
99
100 await screen.rerender(<Composer question editorRef={editorRef} />);
101 expect(drafts()?.answer).toBe("");
102 await screen.rerender(<Composer question={false} editorRef={editorRef} />);
103
104 expect(drafts()?.prompt).toBe(
105 `See ${serializeAssistantCitation(withAssistantCitationComment(citation, "check this"))} `,
106 );
107 await expect.element(page.getByLabelText("Edit citation comment")).toBeInTheDocument();
108 });
109
110 it("clears the sent prompt when an empty citation comment sends it", async () => {
111 const editorRef = createRef<ComposerPromptEditorHandle>();
112 await using screen = await render(<Composer question={false} editorRef={editorRef} />);
113
114 const sourceAnchor = findAssistantCitationSourceAnchor(document, citation);
115 if (!sourceAnchor) throw new Error("Citation source did not resolve");
116 await act(async () =>
117 editorRef.current?.requestCitationComment({
118 previousValue: "See ",
119 value: promptBefore,
120 citationStart: "See ".length,
121 sourceAnchor,
122 insertedSpaces: { before: false, after: true },
123 }),
124 );
125 const comment = page.getByLabelText("Comment on selected text");
126 await expect.element(comment).toHaveFocus();
127 await userEvent.keyboard("{Control>}{Enter}{/Control}");
128
129 await expect.element(comment).not.toBeInTheDocument();
130 await expect.element(page.getByTestId("composer-editor")).toHaveFocus();
131 await expect.element(screen.getByRole("status")).toHaveAttribute("data-prompt", "");
132 });
133
134 it.each(["blocked", "refusedLater"] as const)(
135 "keeps the caret after the citation when save-and-send is %s",
136 async (send) => {
137 const editorRef = createRef<ComposerPromptEditorHandle>();
138 await using screen = await render(
139 <Composer question={false} editorRef={editorRef} send={send} />,
140 );
141
142 const sourceAnchor = findAssistantCitationSourceAnchor(document, citation);
143 if (!sourceAnchor) throw new Error("Citation source did not resolve");
144 await act(async () =>
145 editorRef.current?.requestCitationComment({
146 previousValue: "See ",
147 value: promptBefore,
148 citationStart: "See ".length,
149 sourceAnchor,
150 insertedSpaces: { before: false, after: true },
151 }),
152 );
153 const comment = page.getByLabelText("Comment on selected text");
154 await expect.element(comment).toHaveFocus();
155 await userEvent.keyboard("{Control>}{Enter}{/Control}");
156 await expect.element(comment).not.toBeInTheDocument();
157 await userEvent.keyboard("x");
158
159 await expect
160 .element(screen.getByRole("status"))
161 .toHaveAttribute("data-prompt", `See ${serializeAssistantCitation(citation)}x `);
162 },
163 );
164
165 it("cancels a new citation while dictation makes the composer read-only", async () => {
166 const editorRef = createRef<ComposerPromptEditorHandle>();
167 await using screen = await render(<Composer question={false} editorRef={editorRef} disabled />);
168
169 const sourceAnchor = findAssistantCitationSourceAnchor(document, citation);
170 if (!sourceAnchor) throw new Error("Citation source did not resolve");
171 await act(async () =>
172 editorRef.current?.requestCitationComment({
173 previousValue: "See ",
174 value: promptBefore,
175 citationStart: "See ".length,
176 sourceAnchor,
177 insertedSpaces: { before: false, after: true },
178 }),
179 );
180 await expect.element(page.getByLabelText("Comment on selected text")).toBeVisible();
181
182 await page.getByRole("button", { name: "Cancel" }).click();
183
184 await expect.element(page.getByLabelText("Comment on selected text")).not.toBeInTheDocument();
185 expect(screen.container.querySelector("output")?.dataset.prompt).toBe("See ");
186 });