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 { useEffect } from "react"; |
| 2 | import { expect, it, vi } from "vite-plus/test"; |
| 3 | import { createStore, type StoreApi, useStore } from "zustand"; |
| 4 | |
| 5 | import { act, render } from "../../../test/render"; |
| 6 | import { |
| 7 | addPreparingComposerImages, |
| 8 | cancelPreparingComposerImages, |
| 9 | removePreparingComposerImages, |
| 10 | useSubmitAfterPreparingImages, |
| 11 | } from "./preparingComposerImages"; |
| 12 | |
| 13 | type QueueSubmit = ReturnType<typeof useSubmitAfterPreparingImages>; |
| 14 | |
| 15 | type DraftImagesStore = StoreApi<{ readonly ids: ReadonlyArray<string> }>; |
| 16 | |
| 17 | function createDraftImagesStore(): DraftImagesStore { |
| 18 | return createStore(() => ({ ids: [] })); |
| 19 | } |
| 20 | |
| 21 | // Mirrors ChatComposer: the send reads draft images that a later effect syncs from the store. |
| 22 | function Probe(props: { |
| 23 | targetKey: string; |
| 24 | draftImagesStore: DraftImagesStore; |
| 25 | onReady: (queue: QueueSubmit) => void; |
| 26 | onCommit?: (images: ReadonlyArray<string>) => void; |
| 27 | blocked?: boolean; |
| 28 | }) { |
| 29 | const queue = useSubmitAfterPreparingImages(props.targetKey, props.blocked); |
| 30 | const draftImages = useStore(props.draftImagesStore, (state) => state.ids); |
| 31 | const { onCommit } = props; |
| 32 | useEffect(() => { |
| 33 | onCommit?.(draftImages); |
| 34 | }, [onCommit, draftImages]); |
| 35 | props.onReady(queue); |
| 36 | return null; |
| 37 | } |
| 38 | |
| 39 | function preparingImage(id: string) { |
| 40 | return { id, name: `${id}.png`, file: new File(["png"], `${id}.png`), hidden: false }; |
| 41 | } |
| 42 | |
| 43 | it("sends after pasted images finish compressing, with the compressed image in the draft", async () => { |
| 44 | const target = "submit-after-preparing"; |
| 45 | using _store = { [Symbol.dispose]: () => cancelPreparingComposerImages(target) }; |
| 46 | const draftImagesStore = createDraftImagesStore(); |
| 47 | let queueSubmit: QueueSubmit | undefined; |
| 48 | let committedImages: ReadonlyArray<string> = []; |
| 49 | const onCommit = (images: ReadonlyArray<string>) => { |
| 50 | committedImages = images; |
| 51 | }; |
| 52 | const onReady = (queue: QueueSubmit) => { |
| 53 | queueSubmit = queue; |
| 54 | }; |
| 55 | await using screen = await render( |
| 56 | <Probe |
| 57 | targetKey={target} |
| 58 | draftImagesStore={draftImagesStore} |
| 59 | onCommit={onCommit} |
| 60 | onReady={onReady} |
| 61 | />, |
| 62 | // A bare hook probe; nothing here navigates. |
| 63 | { router: false }, |
| 64 | ); |
| 65 | const submit = vi.fn(() => committedImages); |
| 66 | |
| 67 | expect(queueSubmit?.(submit)).toBe(false); |
| 68 | |
| 69 | addPreparingComposerImages(target, [preparingImage("first"), preparingImage("second")]); |
| 70 | expect(queueSubmit?.(submit)).toBe(true); |
| 71 | |
| 72 | await act(() => removePreparingComposerImages(target, new Set(["first"]))); |
| 73 | expect(submit).not.toHaveBeenCalled(); |
| 74 | |
| 75 | // Intake drops the placeholder and adds the compressed image in the same tick. |
| 76 | await act(() => { |
| 77 | removePreparingComposerImages(target, new Set(["second"])); |
| 78 | draftImagesStore.setState({ ids: ["second"] }); |
| 79 | }); |
| 80 | expect(submit).toHaveBeenCalledOnce(); |
| 81 | expect(submit.mock.results[0]?.value).toEqual(["second"]); |
| 82 | |
| 83 | submit.mockClear(); |
| 84 | await act(() => addPreparingComposerImages(target, [preparingImage("uploading")])); |
| 85 | expect(queueSubmit?.(submit)).toBe(true); |
| 86 | await screen.rerender( |
| 87 | <Probe |
| 88 | targetKey={target} |
| 89 | draftImagesStore={draftImagesStore} |
| 90 | onCommit={onCommit} |
| 91 | onReady={onReady} |
| 92 | blocked |
| 93 | />, |
| 94 | ); |
| 95 | await act(() => removePreparingComposerImages(target, new Set(["uploading"]))); |
| 96 | expect(submit).not.toHaveBeenCalled(); |
| 97 | await screen.rerender( |
| 98 | <Probe |
| 99 | targetKey={target} |
| 100 | draftImagesStore={draftImagesStore} |
| 101 | onCommit={onCommit} |
| 102 | onReady={onReady} |
| 103 | />, |
| 104 | ); |
| 105 | expect(submit).toHaveBeenCalledOnce(); |
| 106 | expect(submit.mock.results[0]?.value).toEqual(["second"]); |
| 107 | }); |
| 108 | |
| 109 | it("drops a waiting send when the composer moves to another draft or discards it", async () => { |
| 110 | const target = "submit-after-preparing-old"; |
| 111 | const otherTarget = "submit-after-preparing-new"; |
| 112 | using _store = { |
| 113 | [Symbol.dispose]: () => { |
| 114 | cancelPreparingComposerImages(target); |
| 115 | cancelPreparingComposerImages(otherTarget); |
| 116 | }, |
| 117 | }; |
| 118 | const draftImagesStore = createDraftImagesStore(); |
| 119 | let queueSubmit: QueueSubmit | undefined; |
| 120 | const onReady = (queue: QueueSubmit) => { |
| 121 | queueSubmit = queue; |
| 122 | }; |
| 123 | await using screen = await render( |
| 124 | <Probe targetKey={target} draftImagesStore={draftImagesStore} onReady={onReady} />, |
| 125 | { |
| 126 | // A bare hook probe; nothing here navigates. |
| 127 | router: false, |
| 128 | }, |
| 129 | ); |
| 130 | const submit = vi.fn(); |
| 131 | addPreparingComposerImages(target, [preparingImage("pasted")]); |
| 132 | expect(queueSubmit?.(submit)).toBe(true); |
| 133 | |
| 134 | // The other draft is still compressing too, so the switch alone must drop the send. |
| 135 | addPreparingComposerImages(otherTarget, [preparingImage("other")]); |
| 136 | await screen.rerender( |
| 137 | <Probe targetKey={otherTarget} draftImagesStore={draftImagesStore} onReady={onReady} />, |
| 138 | ); |
| 139 | await act(() => removePreparingComposerImages(target, new Set(["pasted"]))); |
| 140 | await screen.rerender( |
| 141 | <Probe targetKey={target} draftImagesStore={draftImagesStore} onReady={onReady} />, |
| 142 | ); |
| 143 | expect(submit).not.toHaveBeenCalled(); |
| 144 | |
| 145 | // Discarding the draft cancels its compression, which must not release the held send. |
| 146 | await act(() => addPreparingComposerImages(target, [preparingImage("discarded")])); |
| 147 | expect(queueSubmit?.(submit)).toBe(true); |
| 148 | await act(() => cancelPreparingComposerImages(target)); |
| 149 | expect(submit).not.toHaveBeenCalled(); |
| 150 | }); |
| 151 | |
| 152 | it("keeps the draft unsent when image preparation fails", async () => { |
| 153 | const target = "submit-after-failed-preparation"; |
| 154 | using _store = { [Symbol.dispose]: () => cancelPreparingComposerImages(target) }; |
| 155 | const draftImagesStore = createDraftImagesStore(); |
| 156 | let queueSubmit: QueueSubmit | undefined; |
| 157 | await using _screen = await render( |
| 158 | <Probe |
| 159 | targetKey={target} |
| 160 | draftImagesStore={draftImagesStore} |
| 161 | onReady={(queue) => { |
| 162 | queueSubmit = queue; |
| 163 | }} |
| 164 | />, |
| 165 | // A bare hook probe; nothing here navigates. |
| 166 | { router: false }, |
| 167 | ); |
| 168 | const submit = vi.fn(); |
| 169 | await act(() => addPreparingComposerImages(target, [preparingImage("failed")])); |
| 170 | expect(queueSubmit?.(submit)).toBe(true); |
| 171 | await act(() => |
| 172 | removePreparingComposerImages(target, new Set(["failed"]), { cancelSubmission: true }), |
| 173 | ); |
| 174 | expect(submit).not.toHaveBeenCalled(); |
| 175 | }); |