The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { MessageId, ThreadId } from "@t3tools/contracts"; |
| 2 | import { AtomRegistry } from "effect/reactivity"; |
| 3 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 4 | |
| 5 | import type { ChatMessage } from "../../types"; |
| 6 | import { |
| 7 | applyOptimisticUserMessagesAction, |
| 8 | createOptimisticBlobPreviews, |
| 9 | dropOptimisticHandoff, |
| 10 | emptyOptimisticUserMessages, |
| 11 | loadedHandoffMessageIdsAtom, |
| 12 | reconcileOptimisticUserMessages, |
| 13 | } from "./optimisticUserMessages"; |
| 14 | |
| 15 | const threadA = ThreadId.make("thread-a"); |
| 16 | const threadB = ThreadId.make("thread-b"); |
| 17 | |
| 18 | function message(id: string, previewUrl?: string): ChatMessage { |
| 19 | return { |
| 20 | id: MessageId.make(id), |
| 21 | role: "user", |
| 22 | text: id, |
| 23 | runId: null, |
| 24 | createdAt: "2026-01-01T00:00:00.000Z", |
| 25 | updatedAt: "2026-01-01T00:00:00.000Z", |
| 26 | streaming: false, |
| 27 | ...(previewUrl |
| 28 | ? { |
| 29 | attachments: [ |
| 30 | { |
| 31 | type: "image", |
| 32 | id: `${id}-image`, |
| 33 | name: "image.png", |
| 34 | mimeType: "image/png", |
| 35 | sizeBytes: 1, |
| 36 | previewUrl, |
| 37 | }, |
| 38 | ], |
| 39 | } |
| 40 | : {}), |
| 41 | } as ChatMessage; |
| 42 | } |
| 43 | |
| 44 | const ids = (...values: string[]) => new Set(values.map((value) => MessageId.make(value))); |
| 45 | |
| 46 | describe("optimistic user messages", () => { |
| 47 | it("an acknowledged message stays gone after the server drops its row", () => { |
| 48 | let state = applyOptimisticUserMessagesAction(emptyOptimisticUserMessages(threadA), threadA, [ |
| 49 | message("m1"), |
| 50 | message("m2", "blob:two"), |
| 51 | ]); |
| 52 | state = reconcileOptimisticUserMessages(state, threadA, ids("m1", "m2")); |
| 53 | expect(state.messages).toEqual([]); |
| 54 | expect(state.handoff).toEqual({ m2: ["blob:two"] }); |
| 55 | |
| 56 | // A cancelled queued run or a rewind removes the server's copy again. |
| 57 | state = reconcileOptimisticUserMessages(state, threadA, ids()); |
| 58 | expect(state.messages).toEqual([]); |
| 59 | |
| 60 | state = dropOptimisticHandoff(state, threadA, MessageId.make("m2")); |
| 61 | expect(state.handoff).toEqual({}); |
| 62 | }); |
| 63 | |
| 64 | it("releases the previous thread's messages when the thread changes", () => { |
| 65 | let state = applyOptimisticUserMessagesAction(emptyOptimisticUserMessages(threadA), threadA, [ |
| 66 | message("a1"), |
| 67 | ]); |
| 68 | state = reconcileOptimisticUserMessages(state, threadB, ids()); |
| 69 | state = reconcileOptimisticUserMessages(state, threadA, ids()); |
| 70 | expect(state).toEqual(emptyOptimisticUserMessages(threadA)); |
| 71 | }); |
| 72 | |
| 73 | it("drops a late update for a thread the view has left", () => { |
| 74 | const state = reconcileOptimisticUserMessages( |
| 75 | applyOptimisticUserMessagesAction(emptyOptimisticUserMessages(threadA), threadA, [ |
| 76 | message("a1"), |
| 77 | ]), |
| 78 | threadB, |
| 79 | ids(), |
| 80 | ); |
| 81 | expect(applyOptimisticUserMessagesAction(state, threadA, [message("a2")])).toBe(state); |
| 82 | }); |
| 83 | |
| 84 | it("revokes a thread's blob previews once, when their use ends or the thread is left", () => { |
| 85 | using revoke = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {}); |
| 86 | const previews = createOptimisticBlobPreviews(); |
| 87 | previews.track(threadA, message("a1", "blob:a1")); |
| 88 | previews.track(threadA, message("a2", "blob:a2")); |
| 89 | previews.track(threadB, message("b1", "blob:b1")); |
| 90 | |
| 91 | previews.revoke(threadA, ["blob:a1"]); |
| 92 | previews.revokeThread(threadA); |
| 93 | expect(revoke.mock.calls.map(([url]) => url)).toEqual(["blob:a1", "blob:a2"]); |
| 94 | }); |
| 95 | |
| 96 | it("a send that fails after the server acknowledged it leaves the handed-off previews shown", () => { |
| 97 | using revoke = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {}); |
| 98 | const previews = createOptimisticBlobPreviews(); |
| 99 | const sent = message("m1", "blob:m1"); |
| 100 | previews.track(threadA, sent); |
| 101 | let state = applyOptimisticUserMessagesAction(emptyOptimisticUserMessages(threadA), threadA, [ |
| 102 | sent, |
| 103 | ]); |
| 104 | state = reconcileOptimisticUserMessages(state, threadA, ids("m1")); |
| 105 | previews.handOff(state); |
| 106 | |
| 107 | previews.release(threadA, sent); |
| 108 | expect(revoke).not.toHaveBeenCalled(); |
| 109 | |
| 110 | previews.revokeThread(threadA); |
| 111 | expect(revoke.mock.calls.map(([url]) => url)).toEqual(["blob:m1"]); |
| 112 | }); |
| 113 | |
| 114 | it("a send that fails before acknowledgement revokes its previews", () => { |
| 115 | using revoke = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {}); |
| 116 | const previews = createOptimisticBlobPreviews(); |
| 117 | const sent = message("m1", "blob:m1"); |
| 118 | previews.track(threadA, sent); |
| 119 | const state = applyOptimisticUserMessagesAction(emptyOptimisticUserMessages(threadA), threadA, [ |
| 120 | sent, |
| 121 | ]); |
| 122 | previews.handOff(reconcileOptimisticUserMessages(state, threadA, ids())); |
| 123 | |
| 124 | previews.release(threadA, sent); |
| 125 | expect(revoke.mock.calls.map(([url]) => url)).toEqual(["blob:m1"]); |
| 126 | }); |
| 127 | }); |
| 128 | |
| 129 | class FakeImage { |
| 130 | static readonly created: FakeImage[] = []; |
| 131 | src = ""; |
| 132 | private readonly listeners = new Map<string, () => void>(); |
| 133 | constructor() { |
| 134 | FakeImage.created.push(this); |
| 135 | } |
| 136 | addEventListener(type: string, listener: () => void) { |
| 137 | this.listeners.set(type, listener); |
| 138 | } |
| 139 | fire(type: "load" | "error") { |
| 140 | this.listeners.get(type)?.(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | describe("handoff preloads", () => { |
| 145 | it("reports a handoff loaded, and revokes its blobs, once every server preview loads", async () => { |
| 146 | vi.stubGlobal("Image", FakeImage); |
| 147 | try { |
| 148 | using revoke = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => undefined); |
| 149 | FakeImage.created.length = 0; |
| 150 | const registry = AtomRegistry.make(); |
| 151 | const atom = loadedHandoffMessageIdsAtom( |
| 152 | JSON.stringify([ |
| 153 | ["m-loaded", ["https://server/a.png", "https://server/b.png"], ["blob:a", "blob:b"]], |
| 154 | ["m-failed", ["https://server/c.png"], ["blob:c"]], |
| 155 | ]), |
| 156 | ); |
| 157 | const release = registry.mount(atom); |
| 158 | const image = (src: string) => FakeImage.created.find((candidate) => candidate.src === src); |
| 159 | |
| 160 | image("https://server/a.png")?.fire("load"); |
| 161 | image("https://server/c.png")?.fire("error"); |
| 162 | await Promise.resolve(); |
| 163 | expect(registry.get(atom)).toEqual([]); |
| 164 | |
| 165 | image("https://server/b.png")?.fire("load"); |
| 166 | await vi.waitFor(() => expect(registry.get(atom)).toEqual(["m-loaded"])); |
| 167 | expect(revoke.mock.calls.map(([url]) => url)).toEqual(["blob:a", "blob:b"]); |
| 168 | release(); |
| 169 | } finally { |
| 170 | vi.unstubAllGlobals(); |
| 171 | } |
| 172 | }); |
| 173 | }); |