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 { RegistryContext } from "@effect/atom-react"; |
| 2 | import { |
| 3 | act, |
| 4 | createRef, |
| 5 | useImperativeHandle, |
| 6 | useLayoutEffect, |
| 7 | useRef, |
| 8 | type ReactNode, |
| 9 | type Ref, |
| 10 | } from "react"; |
| 11 | import { createRoot } from "react-dom/client"; |
| 12 | import { expect, it, vi } from "vite-plus/test"; |
| 13 | |
| 14 | interface NativeEvent { |
| 15 | readonly value: string; |
| 16 | readonly selection: { readonly start: number; readonly end: number }; |
| 17 | readonly eventCount: number; |
| 18 | readonly resetGeneration: number; |
| 19 | } |
| 20 | |
| 21 | interface NativeProps { |
| 22 | readonly ref?: Ref<unknown>; |
| 23 | readonly controlledDocumentJson: string; |
| 24 | readonly onComposerChange: (event: { readonly nativeEvent: NativeEvent }) => void; |
| 25 | } |
| 26 | |
| 27 | // Mirrors the iOS editor's revision and reset bookkeeping (T3ComposerEditorView.setControlledDocumentJson). |
| 28 | const native = vi.hoisted(() => ({ |
| 29 | text: "", |
| 30 | eventCount: 0, |
| 31 | resetGeneration: 0, |
| 32 | pasteEventCount: 0, |
| 33 | mounts: 0, |
| 34 | props: null as NativeProps | null, |
| 35 | deliver: null as ((event: NativeEvent) => void) | null, |
| 36 | })); |
| 37 | |
| 38 | function NativeView({ ref, ...props }: NativeProps) { |
| 39 | const propsRef = useRef(props); |
| 40 | useLayoutEffect(() => { |
| 41 | Object.assign(native, { text: "", eventCount: 0, resetGeneration: 0, pasteEventCount: 0 }); |
| 42 | native.mounts += 1; |
| 43 | }, []); |
| 44 | useImperativeHandle(ref, () => ({ |
| 45 | readState: async () => ({ |
| 46 | value: native.text, |
| 47 | selection: { start: native.text.length, end: native.text.length }, |
| 48 | eventCount: native.eventCount, |
| 49 | resetGeneration: native.resetGeneration, |
| 50 | pasteEventCount: native.pasteEventCount, |
| 51 | }), |
| 52 | })); |
| 53 | useLayoutEffect(() => { |
| 54 | propsRef.current = props; |
| 55 | native.props = props; |
| 56 | native.deliver = (event) => propsRef.current.onComposerChange({ nativeEvent: event }); |
| 57 | }); |
| 58 | const { controlledDocumentJson } = props; |
| 59 | useLayoutEffect(() => { |
| 60 | const document = JSON.parse(controlledDocumentJson) as { |
| 61 | value: string; |
| 62 | mostRecentEventCount: number; |
| 63 | resetGeneration?: number; |
| 64 | }; |
| 65 | const resetGeneration = document.resetGeneration ?? 0; |
| 66 | const isReset = resetGeneration > native.resetGeneration; |
| 67 | if (!isReset && document.mostRecentEventCount < native.eventCount) return; |
| 68 | if (isReset) { |
| 69 | native.resetGeneration = resetGeneration; |
| 70 | native.eventCount = document.mostRecentEventCount; |
| 71 | } |
| 72 | native.text = document.value; |
| 73 | }, [controlledDocumentJson]); |
| 74 | return null; |
| 75 | } |
| 76 | |
| 77 | // A native edit whose event has not reached JS yet. |
| 78 | function pend(value: string): NativeEvent { |
| 79 | native.eventCount += 1; |
| 80 | native.text = value; |
| 81 | return { |
| 82 | value, |
| 83 | selection: { start: value.length, end: value.length }, |
| 84 | eventCount: native.eventCount, |
| 85 | resetGeneration: native.resetGeneration, |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | function emit(value: string, resetGeneration = native.resetGeneration) { |
| 90 | native.eventCount += 1; |
| 91 | native.text = value; |
| 92 | native.props!.onComposerChange({ |
| 93 | nativeEvent: { |
| 94 | value, |
| 95 | selection: { start: value.length, end: value.length }, |
| 96 | eventCount: native.eventCount, |
| 97 | resetGeneration, |
| 98 | }, |
| 99 | }); |
| 100 | } |
| 101 | |
| 102 | vi.mock("expo", () => ({ requireNativeView: () => NativeView })); |
| 103 | // Vite resolves the platform-neutral module; the race lives in the iOS editor. |
| 104 | vi.mock("../native/T3ComposerEditor", () => import("../native/T3ComposerEditor.ios")); |
| 105 | vi.mock("react-native", () => ({ StyleSheet: { flatten: (style: unknown) => style } })); |
| 106 | vi.mock("expo-file-system", () => ({ |
| 107 | Directory: class { |
| 108 | create() {} |
| 109 | }, |
| 110 | File: class { |
| 111 | exists = false; |
| 112 | write() {} |
| 113 | }, |
| 114 | Paths: { document: { uri: "file:///documents" } }, |
| 115 | })); |
| 116 | vi.mock("./ui/Dialog", () => ({ alert: vi.fn() })); |
| 117 | vi.mock("./ui/AppText", () => ({ AppText: ({ children }: { children?: ReactNode }) => children })); |
| 118 | vi.mock("./ComposerContextSheet", () => ({ ComposerContextSheet: () => null })); |
| 119 | vi.mock("./AssistantCitationSheet", () => ({ AssistantCitationSheet: () => null })); |
| 120 | vi.mock("../features/threads/useOpenThreadLink", () => ({ useOpenThreadLink: () => vi.fn() })); |
| 121 | vi.mock("../state/peerLinks", () => ({ useLinkedEnvironments: () => [] })); |
| 122 | vi.mock("../state/model-mentions", () => ({ useModelMentions: () => [] })); |
| 123 | vi.mock("../state/preferences", async () => { |
| 124 | const { Atom } = await import("effect/reactivity"); |
| 125 | return { mobilePreferencesAtom: Atom.make({ _tag: "Initial" }) }; |
| 126 | }); |
| 127 | vi.mock("../lib/composerContextClipboard", () => ({ importComposerContextClipboard: vi.fn() })); |
| 128 | vi.mock("../lib/useUniwindTheme", () => ({ useUniwindTheme: () => ({}) })); |
| 129 | vi.mock("../lib/nativeComposerTheme", () => ({ createNativeComposerTheme: () => ({}) })); |
| 130 | vi.mock("../lib/contextChipIconSource", () => ({ contextChipIconUri: () => "" })); |
| 131 | vi.mock("../features/settings/appearance/useScaledTextRole", () => ({ |
| 132 | useComposerTextRole: () => ({ fontSize: 16, lineHeight: 22 }), |
| 133 | })); |
| 134 | vi.mock("../lib/uuid", () => ({ uuidv4: () => "uuid", randomHex: () => "0000" })); |
| 135 | vi.mock("../state/assets", () => ({ assetEnvironment: {} })); |
| 136 | vi.mock("../state/attachments", () => ({ attachmentEnvironment: {} })); |
| 137 | vi.mock("../state/session", () => ({ environmentSession: {} })); |
| 138 | vi.mock("@t3tools/client-runtime/state/runtime", () => ({ |
| 139 | createEnvironmentRpcCommand: () => Symbol("rpc-command"), |
| 140 | settlePromise: vi.fn(), |
| 141 | squashAtomCommandFailure: vi.fn(), |
| 142 | })); |
| 143 | |
| 144 | import { appAtomRegistry } from "../state/atom-registry"; |
| 145 | import { |
| 146 | clearComposerDraftContent, |
| 147 | getComposerDraftSnapshot, |
| 148 | setComposerDraftText, |
| 149 | useComposerDraft, |
| 150 | } from "../state/use-composer-drafts"; |
| 151 | import { ComposerEditor } from "./ComposerEditor"; |
| 152 | import type { ComposerEditorHandle } from "../native/T3ComposerEditor.types"; |
| 153 | |
| 154 | const DRAFT_KEY = "environment:thread"; |
| 155 | |
| 156 | function ThreadComposer({ |
| 157 | draftKey = DRAFT_KEY, |
| 158 | editorRef, |
| 159 | }: { |
| 160 | draftKey?: string; |
| 161 | editorRef?: Ref<ComposerEditorHandle>; |
| 162 | }) { |
| 163 | const draft = useComposerDraft(draftKey); |
| 164 | return ( |
| 165 | <ComposerEditor |
| 166 | ref={editorRef} |
| 167 | draftKey={draftKey} |
| 168 | value={draft.text} |
| 169 | onChangeText={(text) => setComposerDraftText(draftKey, text)} |
| 170 | /> |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | it("keeps a sent draft cleared when an autocorrection lands in the same batch as the send", async () => { |
| 175 | (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 176 | Object.assign(native, { text: "", eventCount: 0, resetGeneration: 0, props: null }); |
| 177 | const root = createRoot(document.createElement("div")); |
| 178 | await using _root = { [Symbol.asyncDispose]: async () => act(() => root.unmount()) }; |
| 179 | await act(async () => |
| 180 | root.render( |
| 181 | <RegistryContext.Provider value={appAtomRegistry}> |
| 182 | <ThreadComposer /> |
| 183 | </RegistryContext.Provider>, |
| 184 | ), |
| 185 | ); |
| 186 | |
| 187 | await act(async () => emit("Hello wrold")); |
| 188 | expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("Hello wrold"); |
| 189 | |
| 190 | // Send clears the draft; iOS commits the pending autocorrection as the keyboard dismisses. |
| 191 | await act(async () => { |
| 192 | clearComposerDraftContent(DRAFT_KEY); |
| 193 | emit("Hello world"); |
| 194 | }); |
| 195 | expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe(""); |
| 196 | expect(native.text).toBe(""); |
| 197 | |
| 198 | await act(async () => emit("n")); |
| 199 | expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("n"); |
| 200 | expect(native.text).toBe("n"); |
| 201 | }); |
| 202 | |
| 203 | it("keeps a pending event from the previous native editor out of the selected draft", async () => { |
| 204 | (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 205 | const first = "switch-draft:first"; |
| 206 | const second = "switch-draft:second"; |
| 207 | setComposerDraftText(first, "Draft A"); |
| 208 | setComposerDraftText(second, "Draft B"); |
| 209 | const root = createRoot(document.createElement("div")); |
| 210 | await using _root = { [Symbol.asyncDispose]: async () => act(() => root.unmount()) }; |
| 211 | const renderDraft = (draftKey: string) => |
| 212 | root.render( |
| 213 | <RegistryContext.Provider value={appAtomRegistry}> |
| 214 | <ThreadComposer draftKey={draftKey} /> |
| 215 | </RegistryContext.Provider>, |
| 216 | ); |
| 217 | await act(async () => renderDraft(first)); |
| 218 | const deliverPendingEvent = native.deliver!; |
| 219 | const pending = { eventCount: native.eventCount + 1, resetGeneration: native.resetGeneration }; |
| 220 | const mounts = native.mounts; |
| 221 | await act(async () => renderDraft(second)); |
| 222 | // Remounting the native view would drop the keyboard mid-edit. |
| 223 | expect(native.mounts).toBe(mounts); |
| 224 | await act(async () => |
| 225 | deliverPendingEvent({ |
| 226 | value: "Draft A corrected", |
| 227 | selection: { start: 17, end: 17 }, |
| 228 | ...pending, |
| 229 | }), |
| 230 | ); |
| 231 | expect(getComposerDraftSnapshot(second).text).toBe("Draft B"); |
| 232 | expect(native.text).toBe("Draft B"); |
| 233 | }); |
| 234 | |
| 235 | async function renderFlushableComposer() { |
| 236 | (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 237 | const editor = createRef<ComposerEditorHandle>(); |
| 238 | const root = createRoot(document.createElement("div")); |
| 239 | await act(async () => |
| 240 | root.render( |
| 241 | <RegistryContext.Provider value={appAtomRegistry}> |
| 242 | <ThreadComposer editorRef={editor} /> |
| 243 | </RegistryContext.Provider>, |
| 244 | ), |
| 245 | ); |
| 246 | return { |
| 247 | editor, |
| 248 | [Symbol.asyncDispose]: async () => act(() => root.unmount()), |
| 249 | }; |
| 250 | } |
| 251 | |
| 252 | it("sends the autocorrection the Send tap accepted, and its late event cannot refill the draft", async () => { |
| 253 | await using screen = await renderFlushableComposer(); |
| 254 | await act(async () => emit("Hello wrold")); |
| 255 | const lateCorrection = pend("Hello world"); |
| 256 | |
| 257 | await act(async () => screen.editor.current!.flushPendingInput()); |
| 258 | expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("Hello world"); |
| 259 | |
| 260 | await act(async () => { |
| 261 | clearComposerDraftContent(DRAFT_KEY); |
| 262 | native.deliver!(lateCorrection); |
| 263 | }); |
| 264 | expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe(""); |
| 265 | }); |
| 266 | |
| 267 | it("does not flush past a paste whose event has not reached JS", async () => { |
| 268 | await using screen = await renderFlushableComposer(); |
| 269 | await act(async () => emit("Before")); |
| 270 | const paste = pend("Before pasted"); |
| 271 | native.pasteEventCount = paste.eventCount; |
| 272 | const caretMove = pend("Before pasted"); |
| 273 | |
| 274 | await act(async () => screen.editor.current!.flushPendingInput()); |
| 275 | expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("Before"); |
| 276 | |
| 277 | await act(async () => native.deliver!(paste)); |
| 278 | expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("Before pasted"); |
| 279 | await act(async () => native.deliver!(caretMove)); |
| 280 | expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("Before pasted"); |
| 281 | }); |