|
1 | 1 | import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
2 | | -import { ComposerContextId, EnvironmentId } from "@t3tools/contracts"; |
| 2 | +import { |
| 3 | + COMPOSER_CONTEXT_MAX_RECORDS, |
| 4 | + ComposerContextId, |
| 5 | + EnvironmentId, |
| 6 | + ThreadId, |
| 7 | +} from "@t3tools/contracts"; |
3 | 8 | import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard"; |
| 9 | +import { |
| 10 | + collectComposerContextReferences, |
| 11 | + formatComposerContextReference, |
| 12 | +} from "@t3tools/shared/composerContextReferences"; |
4 | 13 |
|
5 | 14 | const mocks = vi.hoisted(() => ({ |
6 | 15 | execute: vi.fn(), |
@@ -44,6 +53,7 @@ vi.mock("./composerImages", () => ({ |
44 | 53 | vi.mock("./uuid", () => ({ uuidv4: () => `import-${++mocks.sequence}` })); |
45 | 54 |
|
46 | 55 | import { importComposerContextClipboard } from "./composerContextClipboard"; |
| 56 | +import { createComposerContextHistory } from "./composerContext"; |
47 | 57 |
|
48 | 58 | const image = { |
49 | 59 | version: 1 as const, |
@@ -76,6 +86,42 @@ const clipboard = { |
76 | 86 | html: "", |
77 | 87 | }; |
78 | 88 |
|
| 89 | +const localThread = { |
| 90 | + version: 1 as const, |
| 91 | + kind: "thread" as const, |
| 92 | + contextId: ComposerContextId.make("thread_local-thread"), |
| 93 | + label: "Local discussion", |
| 94 | + environmentId: EnvironmentId.make("destination"), |
| 95 | + threadId: ThreadId.make("local-thread"), |
| 96 | + title: "Local discussion", |
| 97 | +}; |
| 98 | +const foreignThread = { |
| 99 | + ...localThread, |
| 100 | + contextId: ComposerContextId.make("thread_foreign-thread"), |
| 101 | + label: "Remote discussion", |
| 102 | + environmentId: EnvironmentId.make("source"), |
| 103 | + threadId: ThreadId.make("foreign-thread"), |
| 104 | + title: "Remote discussion", |
| 105 | +}; |
| 106 | +const mixedRecords = [foreignThread, localThread, terminal, image]; |
| 107 | +const mixedClipboard = { |
| 108 | + text: `Compare these findings: ${mixedRecords.map(formatComposerContextReference).join(" ")} before changing anything.`, |
| 109 | + fragment: encodeComposerContextFragment({ |
| 110 | + version: 1, |
| 111 | + source: { environmentId: EnvironmentId.make("source") }, |
| 112 | + records: [ |
| 113 | + ...mixedRecords, |
| 114 | + { |
| 115 | + ...foreignThread, |
| 116 | + contextId: ComposerContextId.make("thread_uncopied-thread"), |
| 117 | + threadId: ThreadId.make("uncopied-thread"), |
| 118 | + title: "Not in the copied selection", |
| 119 | + }, |
| 120 | + ], |
| 121 | + })!, |
| 122 | + html: "", |
| 123 | +}; |
| 124 | + |
79 | 125 | beforeEach(() => { |
80 | 126 | vi.clearAllMocks(); |
81 | 127 | mocks.sequence = 0; |
@@ -142,6 +188,114 @@ describe("mobile context clipboard imports", () => { |
142 | 188 | expect(mocks.download).not.toHaveBeenCalled(); |
143 | 189 | }); |
144 | 190 |
|
| 191 | + it("pastes local threads, text context and files at the cap after excluding foreign threads", async () => { |
| 192 | + const result = await importComposerContextClipboard( |
| 193 | + mixedClipboard, |
| 194 | + 0, |
| 195 | + new AbortController().signal, |
| 196 | + COMPOSER_CONTEXT_MAX_RECORDS - 3, |
| 197 | + EnvironmentId.make("destination"), |
| 198 | + ); |
| 199 | + expect(result).not.toBeNull(); |
| 200 | + expect(result!.context.records).toEqual([ |
| 201 | + { ...localThread, contextId: expect.any(String) }, |
| 202 | + { ...terminal, contextId: expect.any(String) }, |
| 203 | + { ...image, contextId: expect.any(String), attachmentId: result!.attachments[0]!.id }, |
| 204 | + ]); |
| 205 | + expect(result!.attachments).toEqual([ |
| 206 | + expect.objectContaining({ name: image.name, fileUri: "file:///owned.png" }), |
| 207 | + ]); |
| 208 | + expect(result!.failures).toEqual([]); |
| 209 | + expect(result!.skippedThreads).toEqual([foreignThread.title]); |
| 210 | + const references = collectComposerContextReferences(result!.text); |
| 211 | + expect(references.map((reference) => reference.label)).toEqual( |
| 212 | + mixedRecords.map((record) => record.label), |
| 213 | + ); |
| 214 | + expect(result!.context.records.map((record) => record.contextId)).toEqual( |
| 215 | + references.slice(1).map((reference) => reference.contextId), |
| 216 | + ); |
| 217 | + expect( |
| 218 | + references.every( |
| 219 | + (reference) => !mixedRecords.some((record) => record.contextId === reference.contextId), |
| 220 | + ), |
| 221 | + ).toBe(true); |
| 222 | + expect( |
| 223 | + result!.context.records.some((record) => record.contextId === references[0]!.contextId), |
| 224 | + ).toBe(false); |
| 225 | + expect(result!.text).toMatch(/^Compare these findings: .* before changing anything\.$/); |
| 226 | + |
| 227 | + const restoreHistory = createComposerContextHistory(); |
| 228 | + restoreHistory(formatComposerContextReference(foreignThread), { |
| 229 | + version: 1, |
| 230 | + records: [foreignThread], |
| 231 | + }); |
| 232 | + expect(restoreHistory(`${result!.text} Next step`, result!.context)).toEqual(result!.context); |
| 233 | + |
| 234 | + mocks.download.mockClear(); |
| 235 | + await expect( |
| 236 | + importComposerContextClipboard( |
| 237 | + mixedClipboard, |
| 238 | + 0, |
| 239 | + new AbortController().signal, |
| 240 | + COMPOSER_CONTEXT_MAX_RECORDS - 2, |
| 241 | + EnvironmentId.make("destination"), |
| 242 | + ), |
| 243 | + ).rejects.toThrow("Remove some context"); |
| 244 | + expect(mocks.download).not.toHaveBeenCalled(); |
| 245 | + }); |
| 246 | + |
| 247 | + it("reports foreign threads and failed files together while keeping the rest of a paste", async () => { |
| 248 | + mocks.download.mockRejectedValueOnce(new Error("source disconnected")); |
| 249 | + const result = await importComposerContextClipboard( |
| 250 | + mixedClipboard, |
| 251 | + 0, |
| 252 | + new AbortController().signal, |
| 253 | + 0, |
| 254 | + EnvironmentId.make("destination"), |
| 255 | + ); |
| 256 | + expect(result).not.toBeNull(); |
| 257 | + expect(result!.context.records).toEqual([ |
| 258 | + { ...localThread, contextId: expect.any(String) }, |
| 259 | + { ...terminal, contextId: expect.any(String) }, |
| 260 | + ]); |
| 261 | + expect(result!.attachments).toEqual([]); |
| 262 | + expect(result!.failures).toEqual([image.name]); |
| 263 | + expect(result!.skippedThreads).toEqual([foreignThread.title]); |
| 264 | + const references = collectComposerContextReferences(result!.text); |
| 265 | + expect(references.map((reference) => reference.label)).toEqual( |
| 266 | + mixedRecords.map((record) => record.label), |
| 267 | + ); |
| 268 | + expect( |
| 269 | + references |
| 270 | + .filter( |
| 271 | + (reference) => |
| 272 | + !result!.context.records.some((record) => record.contextId === reference.contextId), |
| 273 | + ) |
| 274 | + .map((reference) => reference.label), |
| 275 | + ).toEqual([foreignThread.label, image.label]); |
| 276 | + expect(result!.text).toMatch(/^Compare these findings: .* before changing anything\.$/); |
| 277 | + }); |
| 278 | + |
| 279 | + it("preserves both machines' thread references when pasting without a destination", async () => { |
| 280 | + const result = await importComposerContextClipboard( |
| 281 | + mixedClipboard, |
| 282 | + 0, |
| 283 | + new AbortController().signal, |
| 284 | + ); |
| 285 | + expect(result).not.toBeNull(); |
| 286 | + expect(result!.context.records).toEqual([ |
| 287 | + { ...foreignThread, contextId: expect.any(String) }, |
| 288 | + { ...localThread, contextId: expect.any(String) }, |
| 289 | + { ...terminal, contextId: expect.any(String) }, |
| 290 | + { ...image, contextId: expect.any(String), attachmentId: result!.attachments[0]!.id }, |
| 291 | + ]); |
| 292 | + expect(result!.attachments).toHaveLength(1); |
| 293 | + expect(result!.failures).toEqual([]); |
| 294 | + expect( |
| 295 | + collectComposerContextReferences(result!.text).map((reference) => reference.contextId), |
| 296 | + ).toEqual(result!.context.records.map((record) => record.contextId)); |
| 297 | + }); |
| 298 | + |
145 | 299 | it("refuses an overflowing context paste before copying files", async () => { |
146 | 300 | await expect( |
147 | 301 | importComposerContextClipboard(clipboard, 0, new AbortController().signal, 200), |
|
0 commit comments