@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 114 lines 3.35 KB
Raw
1 import { act, useLayoutEffect } from "react";
2 import { createRoot } from "react-dom/client";
3 import { expect, it, vi } from "vite-plus/test";
4 import { useSubmitAfterNativeImages } from "./useSubmitAfterNativeImages";
5
6 function setup() {
7 const root = createRoot(document.createElement("div"));
8 let queue: ReturnType<typeof useSubmitAfterNativeImages>;
9 let generation = 0;
10 let blocked = false;
11 const onSubmit = vi.fn();
12 const preparations: Array<(success: boolean) => void> = [];
13 const onPasteImages = () => new Promise<boolean>((resolve) => preparations.push(resolve));
14 function Probe() {
15 const current = useSubmitAfterNativeImages({
16 draftKey: "draft",
17 readResetGeneration: () => generation,
18 blocked,
19 onPasteImages,
20 onSubmit: (action) => {
21 if (!blocked) onSubmit(action);
22 },
23 });
24 useLayoutEffect(() => {
25 queue = current;
26 });
27 return null;
28 }
29 const render = () => act(() => root.render(<Probe />));
30 render();
31 return {
32 get queue() {
33 return queue!;
34 },
35 onSubmit,
36 setBlocked(value: boolean) {
37 blocked = value;
38 render();
39 },
40 reset() {
41 generation += 1;
42 render();
43 },
44 async finish(success: boolean) {
45 await act(async () => {
46 preparations.shift()!(success);
47 });
48 },
49 [Symbol.dispose]() {
50 act(() => root.unmount());
51 },
52 };
53 }
54
55 it("does not send when conversion finishes without adding an image", async () => {
56 using state = setup();
57 const paste = state.queue.pasteImages(["image"]);
58 expect(state.queue.deferSend()).toBe(true);
59 await state.finish(false);
60 await paste;
61 expect(state.onSubmit).not.toHaveBeenCalled();
62
63 const first = state.queue.pasteImages(["failed"]);
64 const second = state.queue.pasteImages(["successful"]);
65 state.queue.deferSend();
66 await state.finish(false);
67 await state.finish(true);
68 await Promise.all([first, second]);
69 expect(state.onSubmit).not.toHaveBeenCalled();
70
71 const retry = state.queue.pasteImages(["retry"]);
72 state.queue.deferSend();
73 await state.finish(true);
74 await retry;
75 expect(state.onSubmit).toHaveBeenCalledOnce();
76 });
77
78 it("does not send a replacement draft after the original draft was discarded", async () => {
79 using state = setup();
80 const paste = state.queue.pasteImages(["image"]);
81 state.queue.deferSend();
82 state.reset();
83 await state.finish(true);
84 await paste;
85 expect(state.onSubmit).not.toHaveBeenCalled();
86
87 const next = state.queue.pasteImages(["next"]);
88 state.queue.deferSend();
89 state.setBlocked(true);
90 await state.finish(true);
91 await next;
92 state.reset();
93 state.setBlocked(false);
94 expect(state.onSubmit).not.toHaveBeenCalled();
95 });
96
97 it("waits for other attachment preparation and sends once after all images join the draft", async () => {
98 using state = setup();
99 const first = state.queue.pasteImages(["first"]);
100 const second = state.queue.pasteImages(["second"]);
101 state.queue.deferSend("queue");
102 state.queue.deferSend("steer");
103 await state.finish(true);
104 expect(state.onSubmit).not.toHaveBeenCalled();
105 state.setBlocked(true);
106 await state.finish(true);
107 await Promise.all([first, second]);
108 expect(state.onSubmit).not.toHaveBeenCalled();
109 state.setBlocked(false);
110 expect(state.onSubmit).toHaveBeenCalledExactlyOnceWith("steer");
111 state.setBlocked(true);
112 state.setBlocked(false);
113 expect(state.onSubmit).toHaveBeenCalledOnce();
114 });