The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 2 | import { |
| 3 | COMPOSER_CONTEXT_MAX_RECORDS, |
| 4 | ComposerContextId, |
| 5 | EnvironmentId, |
| 6 | ThreadId, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard"; |
| 9 | import { |
| 10 | collectComposerContextReferences, |
| 11 | formatComposerContextReference, |
| 12 | } from "@t3tools/shared/composerContextReferences"; |
| 13 | |
| 14 | const mocks = vi.hoisted(() => ({ |
| 15 | execute: vi.fn(), |
| 16 | download: vi.fn(), |
| 17 | persist: vi.fn(), |
| 18 | remove: vi.fn(), |
| 19 | local: vi.fn(), |
| 20 | preview: vi.fn(), |
| 21 | dispose: vi.fn(), |
| 22 | sequence: 0, |
| 23 | })); |
| 24 | vi.mock("expo", () => ({ requireNativeModule: vi.fn() })); |
| 25 | vi.mock("expo-file-system", () => ({ |
| 26 | File: class { |
| 27 | size = 42; |
| 28 | }, |
| 29 | })); |
| 30 | vi.mock("../state/atom-registry", () => ({ |
| 31 | appAtomRegistry: { |
| 32 | get: (atom: unknown) => |
| 33 | atom === "environments" |
| 34 | ? [] |
| 35 | : { _tag: "Some", value: { httpBaseUrl: "https://source.example" } }, |
| 36 | }, |
| 37 | })); |
| 38 | vi.mock("../state/workspace", () => ({ |
| 39 | workspaceConnections: { environmentsAtom: "environments" }, |
| 40 | })); |
| 41 | vi.mock("../state/session", () => ({ |
| 42 | environmentSession: { preparedConnectionValueAtom: vi.fn() }, |
| 43 | })); |
| 44 | vi.mock("../state/assets", () => ({ assetEnvironment: { createUrl: (value: unknown) => value } })); |
| 45 | vi.mock("../state/threads", () => ({ |
| 46 | environmentThreadShells: { threadShellAtom: (value: unknown) => value }, |
| 47 | })); |
| 48 | vi.mock("../state/use-composer-drafts", () => ({ |
| 49 | waitForComposerDraftsLoaded: async () => {}, |
| 50 | findLocalComposerClipboardAttachment: mocks.local, |
| 51 | })); |
| 52 | vi.mock("@t3tools/client-runtime/state/runtime", () => ({ |
| 53 | executeAtomQuery: mocks.execute, |
| 54 | squashAtomCommandFailure: () => new Error("offline"), |
| 55 | })); |
| 56 | vi.mock("./attachmentDownload", () => ({ downloadAttachmentForPreview: mocks.download })); |
| 57 | vi.mock("./localAttachmentPreview", () => ({ loadLocalAttachmentPreview: mocks.preview })); |
| 58 | vi.mock("./composerImages", () => ({ |
| 59 | persistComposerAttachmentFile: mocks.persist, |
| 60 | removePersistedComposerAttachmentFile: mocks.remove, |
| 61 | })); |
| 62 | vi.mock("./uuid", () => ({ uuidv4: () => `import-${++mocks.sequence}` })); |
| 63 | |
| 64 | import { importComposerContextClipboard } from "./composerContextClipboard"; |
| 65 | import { createComposerContextHistory } from "./composerContext"; |
| 66 | |
| 67 | const image = { |
| 68 | version: 1 as const, |
| 69 | kind: "image" as const, |
| 70 | contextId: ComposerContextId.make("image-source"), |
| 71 | label: "Checkout", |
| 72 | name: "checkout.png", |
| 73 | mimeType: "image/png", |
| 74 | sizeBytes: 7, |
| 75 | attachmentId: "source-file", |
| 76 | source: { |
| 77 | kind: "snap-shot" as const, |
| 78 | capturedAt: "2026-01-01T00:00:00.000Z", |
| 79 | appName: "Finder", |
| 80 | windowTitle: "Downloads", |
| 81 | accessibleText: "Downloads", |
| 82 | }, |
| 83 | }; |
| 84 | const terminal = { |
| 85 | version: 1 as const, |
| 86 | kind: "terminal" as const, |
| 87 | contextId: ComposerContextId.make("terminal-source"), |
| 88 | label: "Build", |
| 89 | terminalId: "main", |
| 90 | terminalLabel: "Terminal", |
| 91 | lineStart: 1, |
| 92 | lineEnd: 1, |
| 93 | text: "Build failed", |
| 94 | }; |
| 95 | const clipboard = { |
| 96 | text: " [Build](t3-context://v1/terminal/terminal-source)", |
| 97 | fragment: encodeComposerContextFragment({ |
| 98 | version: 1, |
| 99 | source: { environmentId: EnvironmentId.make("source") }, |
| 100 | records: [image, terminal], |
| 101 | })!, |
| 102 | html: "", |
| 103 | }; |
| 104 | |
| 105 | const localThread = { |
| 106 | version: 1 as const, |
| 107 | kind: "thread" as const, |
| 108 | contextId: ComposerContextId.make("thread_local-thread"), |
| 109 | label: "Local discussion", |
| 110 | environmentId: EnvironmentId.make("destination"), |
| 111 | threadId: ThreadId.make("local-thread"), |
| 112 | title: "Local discussion", |
| 113 | }; |
| 114 | const foreignThread = { |
| 115 | ...localThread, |
| 116 | contextId: ComposerContextId.make("thread_foreign-thread"), |
| 117 | label: "Remote discussion", |
| 118 | environmentId: EnvironmentId.make("source"), |
| 119 | threadId: ThreadId.make("foreign-thread"), |
| 120 | title: "Remote discussion", |
| 121 | }; |
| 122 | const mixedRecords = [foreignThread, localThread, terminal, image]; |
| 123 | const mixedClipboard = { |
| 124 | text: `Compare these findings: ${mixedRecords.map(formatComposerContextReference).join(" ")} before changing anything.`, |
| 125 | fragment: encodeComposerContextFragment({ |
| 126 | version: 1, |
| 127 | source: { environmentId: EnvironmentId.make("source") }, |
| 128 | records: [ |
| 129 | ...mixedRecords, |
| 130 | { |
| 131 | ...foreignThread, |
| 132 | contextId: ComposerContextId.make("thread_uncopied-thread"), |
| 133 | threadId: ThreadId.make("uncopied-thread"), |
| 134 | title: "Not in the copied selection", |
| 135 | }, |
| 136 | ], |
| 137 | })!, |
| 138 | html: "", |
| 139 | }; |
| 140 | |
| 141 | beforeEach(() => { |
| 142 | vi.clearAllMocks(); |
| 143 | mocks.sequence = 0; |
| 144 | mocks.local.mockReturnValue(undefined); |
| 145 | mocks.execute.mockResolvedValue({ _tag: "Success", value: { relativeUrl: "/assets/source" } }); |
| 146 | mocks.download.mockResolvedValue({ uri: "file:///download.png", dispose: mocks.dispose }); |
| 147 | mocks.persist.mockResolvedValue("file:///owned.png"); |
| 148 | mocks.remove.mockResolvedValue(undefined); |
| 149 | }); |
| 150 | |
| 151 | describe("mobile context clipboard imports", () => { |
| 152 | it("copies original bytes and rewrites file bindings using a signal without throwIfAborted", async () => { |
| 153 | const signal = { aborted: false } as AbortSignal; |
| 154 | const result = await importComposerContextClipboard(clipboard, 0, signal); |
| 155 | expect(result?.failures).toEqual([]); |
| 156 | expect(result?.attachments[0]).toMatchObject({ |
| 157 | fileUri: "file:///owned.png", |
| 158 | sizeBytes: 42, |
| 159 | source: image.source, |
| 160 | }); |
| 161 | expect(result?.context.records[0]).toMatchObject({ |
| 162 | contextId: "import-1", |
| 163 | attachmentId: result?.attachments[0]?.id, |
| 164 | }); |
| 165 | expect(result?.context.records[1]).toMatchObject({ text: "Build failed" }); |
| 166 | expect(result?.text).toContain("/image/import-1)"); |
| 167 | expect(mocks.dispose).toHaveBeenCalledOnce(); |
| 168 | }); |
| 169 | |
| 170 | it("keeps failed attachment references visibly unavailable without dropping text context", async () => { |
| 171 | mocks.download.mockRejectedValue(new Error("source disconnected")); |
| 172 | const result = await importComposerContextClipboard(clipboard, 0, new AbortController().signal); |
| 173 | expect(result?.attachments).toEqual([]); |
| 174 | expect(result?.failures).toEqual(["checkout.png"]); |
| 175 | expect(result?.text).toContain("/image/import-1)"); |
| 176 | expect(result?.context.records).toEqual([{ ...terminal, contextId: "import-2" }]); |
| 177 | }); |
| 178 | |
| 179 | it("releases a newly owned file if the destination closes during its copy", async () => { |
| 180 | const controller = new AbortController(); |
| 181 | mocks.persist.mockImplementationOnce(async () => { |
| 182 | controller.abort(); |
| 183 | return "file:///owned.png"; |
| 184 | }); |
| 185 | await expect(importComposerContextClipboard(clipboard, 0, controller.signal)).rejects.toThrow( |
| 186 | "cancelled", |
| 187 | ); |
| 188 | expect(mocks.remove).toHaveBeenCalledWith("file:///owned.png"); |
| 189 | expect(mocks.dispose).toHaveBeenCalledOnce(); |
| 190 | }); |
| 191 | |
| 192 | it("releases its files if cancelled after the last copy finishes", async () => { |
| 193 | const controller = new AbortController(); |
| 194 | mocks.dispose.mockImplementationOnce(() => controller.abort()); |
| 195 | const imageOnly = { |
| 196 | text: "", |
| 197 | fragment: encodeComposerContextFragment({ |
| 198 | version: 1, |
| 199 | source: { environmentId: EnvironmentId.make("source") }, |
| 200 | records: [image], |
| 201 | })!, |
| 202 | html: "", |
| 203 | }; |
| 204 | await expect(importComposerContextClipboard(imageOnly, 0, controller.signal)).rejects.toThrow( |
| 205 | "cancelled", |
| 206 | ); |
| 207 | expect(mocks.remove).toHaveBeenCalledWith("file:///owned.png"); |
| 208 | }); |
| 209 | |
| 210 | it("can copy an image before its source upload completes", async () => { |
| 211 | mocks.local.mockReturnValue({ |
| 212 | id: "source-file", |
| 213 | type: "image", |
| 214 | dataUrl: "data:image/png;base64,YWJj", |
| 215 | previewUri: "data:image/png;base64,YWJj", |
| 216 | name: "checkout.png", |
| 217 | mimeType: "image/png", |
| 218 | sizeBytes: 3, |
| 219 | }); |
| 220 | const result = await importComposerContextClipboard(clipboard, 0, new AbortController().signal); |
| 221 | expect(result?.attachments[0]).toMatchObject({ |
| 222 | dataUrl: "data:image/png;base64,YWJj", |
| 223 | uploadedAttachmentId: undefined, |
| 224 | }); |
| 225 | expect(mocks.execute).not.toHaveBeenCalled(); |
| 226 | expect(mocks.download).not.toHaveBeenCalled(); |
| 227 | }); |
| 228 | |
| 229 | it("pastes local threads, text context and files at the cap after excluding foreign threads", async () => { |
| 230 | const result = await importComposerContextClipboard( |
| 231 | mixedClipboard, |
| 232 | 0, |
| 233 | new AbortController().signal, |
| 234 | COMPOSER_CONTEXT_MAX_RECORDS - 3, |
| 235 | EnvironmentId.make("destination"), |
| 236 | ); |
| 237 | expect(result).not.toBeNull(); |
| 238 | expect(result!.context.records).toEqual([ |
| 239 | { ...localThread, contextId: expect.any(String) }, |
| 240 | { ...terminal, contextId: expect.any(String) }, |
| 241 | { ...image, contextId: expect.any(String), attachmentId: result!.attachments[0]!.id }, |
| 242 | ]); |
| 243 | expect(result!.attachments).toEqual([ |
| 244 | expect.objectContaining({ name: image.name, fileUri: "file:///owned.png" }), |
| 245 | ]); |
| 246 | expect(result!.failures).toEqual([]); |
| 247 | expect(result!.skippedThreads).toEqual([foreignThread.title]); |
| 248 | const references = collectComposerContextReferences(result!.text); |
| 249 | expect(references.map((reference) => reference.label)).toEqual( |
| 250 | mixedRecords.map((record) => record.label), |
| 251 | ); |
| 252 | expect(result!.context.records.map((record) => record.contextId)).toEqual( |
| 253 | references.slice(1).map((reference) => reference.contextId), |
| 254 | ); |
| 255 | expect( |
| 256 | references.every( |
| 257 | (reference) => !mixedRecords.some((record) => record.contextId === reference.contextId), |
| 258 | ), |
| 259 | ).toBe(true); |
| 260 | expect( |
| 261 | result!.context.records.some((record) => record.contextId === references[0]!.contextId), |
| 262 | ).toBe(false); |
| 263 | expect(result!.text).toMatch(/^Compare these findings: .* before changing anything\.$/); |
| 264 | |
| 265 | const restoreHistory = createComposerContextHistory(); |
| 266 | restoreHistory(formatComposerContextReference(foreignThread), { |
| 267 | version: 1, |
| 268 | records: [foreignThread], |
| 269 | }); |
| 270 | expect(restoreHistory(`${result!.text} Next step`, result!.context)).toEqual(result!.context); |
| 271 | |
| 272 | mocks.download.mockClear(); |
| 273 | await expect( |
| 274 | importComposerContextClipboard( |
| 275 | mixedClipboard, |
| 276 | 0, |
| 277 | new AbortController().signal, |
| 278 | COMPOSER_CONTEXT_MAX_RECORDS - 2, |
| 279 | EnvironmentId.make("destination"), |
| 280 | ), |
| 281 | ).rejects.toThrow("Remove some context"); |
| 282 | expect(mocks.download).not.toHaveBeenCalled(); |
| 283 | }); |
| 284 | |
| 285 | // regression: #18322 don't send or paste attached threads a machine can't read |
| 286 | it("reports foreign threads and failed files together while keeping the rest of a paste", async () => { |
| 287 | mocks.download.mockRejectedValueOnce(new Error("source disconnected")); |
| 288 | const result = await importComposerContextClipboard( |
| 289 | mixedClipboard, |
| 290 | 0, |
| 291 | new AbortController().signal, |
| 292 | 0, |
| 293 | EnvironmentId.make("destination"), |
| 294 | ); |
| 295 | expect(result).not.toBeNull(); |
| 296 | expect(result!.context.records).toEqual([ |
| 297 | { ...localThread, contextId: expect.any(String) }, |
| 298 | { ...terminal, contextId: expect.any(String) }, |
| 299 | ]); |
| 300 | expect(result!.attachments).toEqual([]); |
| 301 | expect(result!.failures).toEqual([image.name]); |
| 302 | expect(result!.skippedThreads).toEqual([foreignThread.title]); |
| 303 | const references = collectComposerContextReferences(result!.text); |
| 304 | expect(references.map((reference) => reference.label)).toEqual( |
| 305 | mixedRecords.map((record) => record.label), |
| 306 | ); |
| 307 | expect( |
| 308 | references |
| 309 | .filter( |
| 310 | (reference) => |
| 311 | !result!.context.records.some((record) => record.contextId === reference.contextId), |
| 312 | ) |
| 313 | .map((reference) => reference.label), |
| 314 | ).toEqual([foreignThread.label, image.label]); |
| 315 | expect(result!.text).toMatch(/^Compare these findings: .* before changing anything\.$/); |
| 316 | }); |
| 317 | |
| 318 | it("keeps a thread from a machine the destination links to", async () => { |
| 319 | const result = await importComposerContextClipboard( |
| 320 | mixedClipboard, |
| 321 | 0, |
| 322 | new AbortController().signal, |
| 323 | 0, |
| 324 | EnvironmentId.make("destination"), |
| 325 | new Map([[foreignThread.environmentId, "Source"]]), |
| 326 | ); |
| 327 | expect(result!.skippedThreads).toEqual([]); |
| 328 | expect(result!.context.records.filter((record) => record.kind === "thread")).toEqual([ |
| 329 | { ...foreignThread, contextId: expect.any(String) }, |
| 330 | { ...localThread, contextId: expect.any(String) }, |
| 331 | ]); |
| 332 | }); |
| 333 | |
| 334 | it("preserves both machines' thread references when pasting without a destination", async () => { |
| 335 | const result = await importComposerContextClipboard( |
| 336 | mixedClipboard, |
| 337 | 0, |
| 338 | new AbortController().signal, |
| 339 | ); |
| 340 | expect(result).not.toBeNull(); |
| 341 | expect(result!.context.records).toEqual([ |
| 342 | { ...foreignThread, contextId: expect.any(String) }, |
| 343 | { ...localThread, contextId: expect.any(String) }, |
| 344 | { ...terminal, contextId: expect.any(String) }, |
| 345 | { ...image, contextId: expect.any(String), attachmentId: result!.attachments[0]!.id }, |
| 346 | ]); |
| 347 | expect(result!.attachments).toHaveLength(1); |
| 348 | expect(result!.failures).toEqual([]); |
| 349 | expect( |
| 350 | collectComposerContextReferences(result!.text).map((reference) => reference.contextId), |
| 351 | ).toEqual(result!.context.records.map((record) => record.contextId)); |
| 352 | }); |
| 353 | |
| 354 | it("pastes a thread link as a chip only where the destination's agent reads its machine", async () => { |
| 355 | const text = [ |
| 356 | "https://app.t3.codes/threads/destination/local-thread", |
| 357 | "<t3-thread://v2/source/foreign-thread>", |
| 358 | ].join(" "); |
| 359 | const result = await importComposerContextClipboard( |
| 360 | { text, fragment: "", html: "" }, |
| 361 | 0, |
| 362 | new AbortController().signal, |
| 363 | 0, |
| 364 | EnvironmentId.make("destination"), |
| 365 | ); |
| 366 | const [record] = result!.context.records; |
| 367 | expect(record).toMatchObject({ |
| 368 | kind: "thread", |
| 369 | environmentId: "destination", |
| 370 | threadId: "local-thread", |
| 371 | }); |
| 372 | expect(result!.text).toBe( |
| 373 | `[Thread](t3-context://v1/thread/${record!.contextId}) <t3-thread://v2/source/foreign-thread>`, |
| 374 | ); |
| 375 | expect( |
| 376 | await importComposerContextClipboard( |
| 377 | { text: "plain text", fragment: "", html: "" }, |
| 378 | 0, |
| 379 | new AbortController().signal, |
| 380 | ), |
| 381 | ).toBeNull(); |
| 382 | }); |
| 383 | }); |