The best way to control Claude Code, Codex and more from any computer or phone

main
ts 179 lines 7.01 KB
Raw
1 import { Editor, Node, type Extensions, type JSONContent } from "@tiptap/core";
2 import StarterKit from "@tiptap/starter-kit";
3 import { TextSelection } from "@tiptap/pm/state";
4 import { expect, it, vi } from "vite-plus/test";
5
6 import { serializeEditorDoc } from "../composer-rich-text-doc";
7 import { formatInlineContextReference } from "../lib/composerContextReferences";
8 import { captureComposerAttachmentInsertion } from "./composerAttachmentInsertion";
9 import { ComposerMarkdownSourceExtension } from "../composer-markdown-source";
10
11 const image = { kind: "image", contextId: "image_pending", label: "picture.png" } as const;
12
13 function createEditor(content: JSONContent, extensions: Extensions = []) {
14 const editor = new Editor({
15 extensions: [
16 StarterKit.configure({ trailingNode: false }),
17 Node.create({
18 name: "composer-context-reference",
19 group: "inline",
20 inline: true,
21 atom: true,
22 addAttributes: () => ({
23 kind: { default: "" },
24 contextId: { default: "" },
25 label: { default: "" },
26 source: { default: "" },
27 }),
28 }),
29 ...extensions,
30 ],
31 content,
32 });
33 editor.view.updateState(editor.state.reconfigure({ plugins: editor.extensionManager.plugins }));
34 return editor;
35 }
36
37 it.each([
38 { name: "continued typing", selected: "", insertedBefore: "" },
39 { name: "replaced selected text", selected: "selected", insertedBefore: "" },
40 { name: "an edit before the paste point", selected: "", insertedBefore: "prefix " },
41 ])("keeps the paste point and current caret after $name", ({ selected, insertedBefore }) => {
42 const editor = createEditor({
43 type: "doc",
44 content: [{ type: "paragraph", content: [{ type: "text", text: `Before ${selected}` }] }],
45 });
46 try {
47 // Headless Tiptap has no mounted view, so its lifecycle getter reports destroyed.
48 using _alive = vi.spyOn(editor, "isDestroyed", "get").mockReturnValue(false);
49 editor.view.dispatch(
50 editor.state.tr.setSelection(TextSelection.create(editor.state.doc, 8, 8 + selected.length)),
51 );
52 const insertion = captureComposerAttachmentInsertion(editor);
53 if (insertedBefore) editor.view.dispatch(editor.state.tr.insertText(insertedBefore, 1));
54 editor.view.dispatch(editor.state.tr.insertText("yaz"));
55 expect(insertion.insert([image])).toBe(true);
56 expect(serializeEditorDoc(editor.state.doc).value).toContain(
57 formatInlineContextReference(image),
58 );
59 insertion.dispose();
60 editor.view.dispatch(editor.state.tr.insertText("maya"));
61
62 expect(serializeEditorDoc(editor.state.doc).value).toBe(
63 `${insertedBefore}Before ${formatInlineContextReference(image)} yazmaya`,
64 );
65 } finally {
66 editor.destroy();
67 }
68 });
69
70 it("maps source-marker rewrites before the paste point when the caret enters styled text", () => {
71 const editor = createEditor(
72 {
73 type: "doc",
74 content: [
75 {
76 type: "paragraph",
77 content: [
78 { type: "text", text: "bold", marks: [{ type: "bold" }] },
79 { type: "text", text: " after" },
80 ],
81 },
82 ],
83 },
84 [ComposerMarkdownSourceExtension],
85 );
86 try {
87 using _alive = vi.spyOn(editor, "isDestroyed", "get").mockReturnValue(false);
88 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
89 const insertion = captureComposerAttachmentInsertion(editor);
90 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.create(editor.state.doc, 3)));
91 expect(editor.state.doc.textContent).toContain("**bold**");
92 expect(insertion.insert([image])).toBe(true);
93 expect(serializeEditorDoc(editor.state.doc).value).toBe(
94 `**bold** after ${formatInlineContextReference(image)} `,
95 );
96 } finally {
97 editor.destroy();
98 }
99 });
100
101 it("gives up the paste point when the prompt is replaced wholesale", () => {
102 const editor = createEditor({
103 type: "doc",
104 content: [{ type: "paragraph", content: [{ type: "text", text: "Hello world" }] }],
105 });
106 try {
107 using _alive = vi.spyOn(editor, "isDestroyed", "get").mockReturnValue(false);
108 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.create(editor.state.doc, 7)));
109 const insertion = captureComposerAttachmentInsertion(editor);
110 editor.commands.setContent(
111 {
112 type: "doc",
113 content: [
114 { type: "paragraph", content: [{ type: "text", text: "Hello world @src/a.ts" }] },
115 ],
116 },
117 { emitUpdate: false },
118 );
119 expect(insertion.insert([image])).toBe(false);
120 expect(serializeEditorDoc(editor.state.doc).value).toBe("Hello world @src/a.ts");
121 } finally {
122 editor.destroy();
123 }
124 });
125
126 it("keeps a pending image reference as text inside a code block", () => {
127 const editor = createEditor({
128 type: "doc",
129 content: [{ type: "codeBlock", content: [{ type: "text", text: "Before " }] }],
130 });
131 try {
132 using _alive = vi.spyOn(editor, "isDestroyed", "get").mockReturnValue(false);
133 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
134 const insertion = captureComposerAttachmentInsertion(editor);
135 editor.view.dispatch(editor.state.tr.insertText("yaz"));
136 expect(insertion.insert([image])).toBe(true);
137 editor.view.dispatch(editor.state.tr.insertText("maya"));
138 expect(editor.state.doc.childCount).toBe(1);
139 expect(editor.state.doc.firstChild?.type.name).toBe("codeBlock");
140 expect(editor.state.doc.firstChild?.textContent).toBe(
141 `Before ${formatInlineContextReference(image)} yazmaya`,
142 );
143 } finally {
144 editor.destroy();
145 }
146 });
147
148 it.each([
149 { finishesFirst: "first", before: "Before ", after: "" },
150 { finishesFirst: "second", before: "Before ", after: "" },
151 { finishesFirst: "first", before: "Compare", after: " screenshots" },
152 ])(
153 "keeps two pastes at $before|$after in paste order when the $finishesFirst image prepares first",
154 ({ finishesFirst, before, after }) => {
155 const editor = createEditor({
156 type: "doc",
157 content: [{ type: "paragraph", content: [{ type: "text", text: before + after }] }],
158 });
159 try {
160 using _alive = vi.spyOn(editor, "isDestroyed", "get").mockReturnValue(false);
161 editor.view.dispatch(
162 editor.state.tr.setSelection(TextSelection.create(editor.state.doc, before.length + 1)),
163 );
164 const firstImage = { ...image, contextId: "image_first", label: "first.png" };
165 const secondImage = { ...image, contextId: "image_second", label: "second.png" };
166 const first = captureComposerAttachmentInsertion(editor);
167 const second = captureComposerAttachmentInsertion(editor);
168 if (finishesFirst === "second") expect(second.insert([secondImage])).toBe(true);
169 expect(first.insert([firstImage])).toBe(true);
170 if (finishesFirst === "first") expect(second.insert([secondImage])).toBe(true);
171
172 expect(serializeEditorDoc(editor.state.doc).value).toBe(
173 `${before.trimEnd()} ${formatInlineContextReference(firstImage)} ${formatInlineContextReference(secondImage)} ${after.trimStart()}`,
174 );
175 } finally {
176 editor.destroy();
177 }
178 },
179 );