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 { |
| 2 | type ComposerContextId, |
| 3 | EnvironmentId, |
| 4 | MessageId, |
| 5 | OrchestrationMessageContext, |
| 6 | ThreadId, |
| 7 | TurnItemId, |
| 8 | type PreviewAnnotationPayload, |
| 9 | } from "@t3tools/contracts"; |
| 10 | import * as Schema from "effect/Schema"; |
| 11 | import { upgradeLegacyContextMessage } from "@t3tools/shared/composerContextLegacy"; |
| 12 | |
| 13 | import { |
| 14 | formatInlineContextReference, |
| 15 | removeInlineContextReference, |
| 16 | } from "./composerContextReferences"; |
| 17 | import { describe, expect, it } from "vite-plus/test"; |
| 18 | |
| 19 | import { |
| 20 | asKnownContextRecord, |
| 21 | buildMessageContext, |
| 22 | composerContextImportLookupIds, |
| 23 | isSameComposerContextPayload, |
| 24 | previewAnnotationContextRecord, |
| 25 | previewAnnotationFromRecord, |
| 26 | pullRequestContextKindLabel, |
| 27 | resolveUserMessageContext, |
| 28 | responseContextRecord, |
| 29 | reviewCommentContextLabel, |
| 30 | selectedMessageContextFragment, |
| 31 | reviewCommentContextRecord, |
| 32 | terminalContextRecord, |
| 33 | terminalContextReference, |
| 34 | terminalContextDraftFromRecord, |
| 35 | uploadedAttachmentContextRecord, |
| 36 | } from "./composerContextRecords"; |
| 37 | |
| 38 | const decodeMessageContext = Schema.decodeUnknownSync(OrchestrationMessageContext); |
| 39 | |
| 40 | const annotation: PreviewAnnotationPayload = { |
| 41 | id: "ann_1", |
| 42 | pageUrl: "http://localhost:3000/checkout", |
| 43 | pageTitle: "Checkout", |
| 44 | comment: " Make this bigger ", |
| 45 | elements: [ |
| 46 | { |
| 47 | id: "el_1", |
| 48 | rect: { x: 0, y: 0, width: 10, height: 10 }, |
| 49 | element: { |
| 50 | pageUrl: "http://localhost:3000/checkout", |
| 51 | pageTitle: "Checkout", |
| 52 | tagName: "BUTTON", |
| 53 | selector: "#pay", |
| 54 | htmlPreview: '<button id="pay">Pay</button>', |
| 55 | componentName: "Button", |
| 56 | source: null, |
| 57 | stack: [], |
| 58 | styles: "color: red;", |
| 59 | pickedAt: "2026-01-01T00:00:00.000Z", |
| 60 | }, |
| 61 | }, |
| 62 | ], |
| 63 | regions: [], |
| 64 | strokes: [], |
| 65 | styleChanges: [ |
| 66 | { targetId: "el_1", selector: "#pay", property: "font-size", previousValue: "", value: "20px" }, |
| 67 | ], |
| 68 | screenshot: null, |
| 69 | createdAt: "2026-01-01T00:00:00.000Z", |
| 70 | }; |
| 71 | |
| 72 | describe("composerContextRecords", () => { |
| 73 | it("copies only ready or persisted server-side attachment IDs", () => { |
| 74 | const environmentId = EnvironmentId.make("env"); |
| 75 | const image = { |
| 76 | type: "image" as const, |
| 77 | id: "local-image", |
| 78 | name: "shot.png", |
| 79 | mimeType: "image/png", |
| 80 | sizeBytes: 1, |
| 81 | file: new File(["x"], "shot.png"), |
| 82 | previewUrl: "blob:shot", |
| 83 | }; |
| 84 | expect(uploadedAttachmentContextRecord(image, undefined)).toBeNull(); |
| 85 | expect( |
| 86 | uploadedAttachmentContextRecord(image, { status: "uploading", environmentId, progress: 0.5 }), |
| 87 | ).toBeNull(); |
| 88 | expect( |
| 89 | uploadedAttachmentContextRecord(image, { |
| 90 | status: "failed", |
| 91 | environmentId, |
| 92 | reason: "offline", |
| 93 | attachmentId: "unfinished", |
| 94 | }), |
| 95 | ).toBeNull(); |
| 96 | expect( |
| 97 | uploadedAttachmentContextRecord(image, { |
| 98 | status: "ready", |
| 99 | environmentId, |
| 100 | attachmentId: "uploaded-image", |
| 101 | }), |
| 102 | ).toMatchObject({ attachmentId: "uploaded-image", contextId: "image_local-image" }); |
| 103 | const file = { |
| 104 | type: "file" as const, |
| 105 | id: "local-file", |
| 106 | name: "notes.txt", |
| 107 | mimeType: "text/plain", |
| 108 | sizeBytes: 1, |
| 109 | file: null, |
| 110 | }; |
| 111 | expect(uploadedAttachmentContextRecord(file, undefined)).toBeNull(); |
| 112 | expect( |
| 113 | uploadedAttachmentContextRecord( |
| 114 | { ...file, uploadedAttachmentId: "persisted-file", uploadEnvironmentId: environmentId }, |
| 115 | undefined, |
| 116 | ), |
| 117 | ).toMatchObject({ attachmentId: "persisted-file", contextId: "file_local-file" }); |
| 118 | }); |
| 119 | it("does not bind an annotation screenshot to a same-ID file", () => { |
| 120 | const context = buildMessageContext({ |
| 121 | terminalContexts: [], |
| 122 | reviewComments: [], |
| 123 | previewAnnotations: [annotation], |
| 124 | attachments: [ |
| 125 | { |
| 126 | attachment: { |
| 127 | type: "file", |
| 128 | id: annotation.id, |
| 129 | name: "notes.txt", |
| 130 | mimeType: "text/plain", |
| 131 | sizeBytes: 1, |
| 132 | file: null, |
| 133 | }, |
| 134 | attachmentId: "uploaded-file", |
| 135 | }, |
| 136 | ], |
| 137 | })!; |
| 138 | expect(context.records.map((record) => record.kind)).toEqual(["preview-annotation", "file"]); |
| 139 | expect(context.records[0]).not.toHaveProperty("screenshotContextId"); |
| 140 | }); |
| 141 | it.each(["x", "terminal_x"])( |
| 142 | "preserves canonical terminal IDs across repeated imports: %s", |
| 143 | (id) => { |
| 144 | const threadId = ThreadId.make("t1"); |
| 145 | const record = terminalContextRecord({ |
| 146 | id, |
| 147 | threadId, |
| 148 | terminalId: "default", |
| 149 | terminalLabel: "Terminal", |
| 150 | lineStart: 1, |
| 151 | lineEnd: 1, |
| 152 | text: "output", |
| 153 | createdAt: "2026-01-01T00:00:00.000Z", |
| 154 | }); |
| 155 | const restored = terminalContextRecord(terminalContextDraftFromRecord(record, threadId)); |
| 156 | expect(restored).toEqual(record); |
| 157 | expect(terminalContextRecord(terminalContextDraftFromRecord(restored, threadId))).toEqual( |
| 158 | record, |
| 159 | ); |
| 160 | }, |
| 161 | ); |
| 162 | it("scopes colliding producer ids and links the annotation to its screenshot record", () => { |
| 163 | const id = "same.id:1"; |
| 164 | const context = buildMessageContext({ |
| 165 | terminalContexts: [ |
| 166 | { |
| 167 | id, |
| 168 | threadId: ThreadId.make("t1"), |
| 169 | terminalId: "default", |
| 170 | terminalLabel: "Terminal", |
| 171 | lineStart: 1, |
| 172 | lineEnd: 1, |
| 173 | text: "output", |
| 174 | createdAt: "2026-01-01T00:00:00.000Z", |
| 175 | }, |
| 176 | ], |
| 177 | reviewComments: [ |
| 178 | { |
| 179 | id, |
| 180 | sectionId: "s", |
| 181 | sectionTitle: "Review", |
| 182 | filePath: "file.ts", |
| 183 | startIndex: 0, |
| 184 | endIndex: 0, |
| 185 | rangeLabel: "L1", |
| 186 | text: "Review", |
| 187 | diff: "", |
| 188 | }, |
| 189 | ], |
| 190 | previewAnnotations: [{ ...annotation, id }], |
| 191 | attachments: [ |
| 192 | { |
| 193 | attachment: { |
| 194 | type: "image", |
| 195 | id, |
| 196 | name: "shot.png", |
| 197 | mimeType: "image/png", |
| 198 | sizeBytes: 1, |
| 199 | file: new File(["x"], "shot.png"), |
| 200 | previewUrl: "blob:shot", |
| 201 | }, |
| 202 | attachmentId: "uploaded-image", |
| 203 | }, |
| 204 | { |
| 205 | attachment: { |
| 206 | type: "file", |
| 207 | id, |
| 208 | name: "file.txt", |
| 209 | mimeType: "text/plain", |
| 210 | sizeBytes: 1, |
| 211 | file: null, |
| 212 | }, |
| 213 | attachmentId: "uploaded-file", |
| 214 | }, |
| 215 | ], |
| 216 | })!; |
| 217 | expect(decodeMessageContext(context).records).toHaveLength(5); |
| 218 | const preview = context.records.find((record) => record.kind === "preview-annotation"); |
| 219 | const image = context.records.find((record) => record.kind === "image"); |
| 220 | expect(preview).toMatchObject({ screenshotContextId: image!.contextId }); |
| 221 | expect(image).toMatchObject({ attachmentId: "uploaded-image" }); |
| 222 | }); |
| 223 | |
| 224 | it("shortens a long annotation comment label without splitting an emoji", () => { |
| 225 | const record = previewAnnotationContextRecord({ |
| 226 | ...annotation, |
| 227 | comment: `${"a".repeat(46)}😀 and make the button bigger`, |
| 228 | }); |
| 229 | expect(record.label).toBe(`${"a".repeat(46)}…`); |
| 230 | }); |
| 231 | |
| 232 | it("shortens a long reply label without splitting an emoji", () => { |
| 233 | const text = `${"a".repeat(63)}😀 trailing`; |
| 234 | const record = responseContextRecord({ |
| 235 | citation: { |
| 236 | version: 1, |
| 237 | environmentId: EnvironmentId.make("environment-1"), |
| 238 | threadId: ThreadId.make("thread-1"), |
| 239 | messageId: MessageId.make("message-1"), |
| 240 | text, |
| 241 | start: 0, |
| 242 | end: text.length, |
| 243 | prefix: "", |
| 244 | suffix: "", |
| 245 | }, |
| 246 | itemId: TurnItemId.make("item-1"), |
| 247 | subagents: [], |
| 248 | }); |
| 249 | expect(record.label).toBe("a".repeat(63)); |
| 250 | }); |
| 251 | |
| 252 | it("restores multiple edits on one target without parsing display strings", () => { |
| 253 | const styleChanges = [ |
| 254 | ...annotation.styleChanges, |
| 255 | { |
| 256 | targetId: "el_1", |
| 257 | selector: "#pay", |
| 258 | property: "content", |
| 259 | previousValue: "a → b", |
| 260 | value: "first\nsecond → third", |
| 261 | }, |
| 262 | ]; |
| 263 | const restored = previewAnnotationFromRecord( |
| 264 | previewAnnotationContextRecord({ ...annotation, styleChanges }), |
| 265 | ); |
| 266 | expect(restored.styleChanges).toEqual(styleChanges); |
| 267 | expect(restored.elements[0]?.id).toBe("el_1"); |
| 268 | }); |
| 269 | |
| 270 | it("clamps an oversized annotation comment so the record still decodes", () => { |
| 271 | const record = previewAnnotationContextRecord({ ...annotation, comment: "c".repeat(8_001) }); |
| 272 | expect(record.comment.endsWith("… truncated …")).toBe(true); |
| 273 | expect(decodeMessageContext({ version: 1, records: [record] }).records).toEqual([record]); |
| 274 | }); |
| 275 | |
| 276 | it("removes an expired terminal chip by its kind-scoped id, not the producer id", () => { |
| 277 | const context = { |
| 278 | id: "term-1", |
| 279 | threadId: ThreadId.make("t"), |
| 280 | createdAt: "2026-01-01T00:00:00.000Z", |
| 281 | terminalId: "default", |
| 282 | terminalLabel: "Terminal 1", |
| 283 | lineStart: 3, |
| 284 | lineEnd: 4, |
| 285 | text: "boom", |
| 286 | }; |
| 287 | const reference = terminalContextReference(context); |
| 288 | expect(reference.contextId).toBe("terminal_term-1"); |
| 289 | |
| 290 | const prompt = `prose ${formatInlineContextReference(reference)} tail`; |
| 291 | expect(removeInlineContextReference(prompt, context.id).prompt).toBe(prompt); |
| 292 | expect(removeInlineContextReference(prompt, reference.contextId).prompt).toBe("prose tail"); |
| 293 | }); |
| 294 | |
| 295 | it("clamps oversized review and terminal selections so the records still encode", () => { |
| 296 | const build = (diffLength: number) => |
| 297 | reviewCommentContextRecord({ |
| 298 | id: "rc-big", |
| 299 | sectionId: "file:a/b.ts", |
| 300 | sectionTitle: "File comment", |
| 301 | filePath: "a/b.ts", |
| 302 | startIndex: 0, |
| 303 | endIndex: 1, |
| 304 | rangeLabel: "L1", |
| 305 | text: "Why?", |
| 306 | diff: "d".repeat(diffLength), |
| 307 | }); |
| 308 | |
| 309 | const atLimit = build(32_000); |
| 310 | expect(atLimit.diff).toHaveLength(32_000); |
| 311 | const overLimit = build(32_001); |
| 312 | expect(overLimit.diff.length).toBeLessThanOrEqual(32_000); |
| 313 | expect(overLimit.diff.endsWith("… truncated …")).toBe(true); |
| 314 | expect(() => decodeMessageContext({ version: 1, records: [atLimit] })).not.toThrow(); |
| 315 | expect(() => decodeMessageContext({ version: 1, records: [overLimit] })).not.toThrow(); |
| 316 | |
| 317 | const terminal = terminalContextRecord({ |
| 318 | id: "term-big", |
| 319 | threadId: ThreadId.make("t"), |
| 320 | createdAt: "2026-01-01T00:00:00.000Z", |
| 321 | terminalId: "default", |
| 322 | terminalLabel: "Terminal 1", |
| 323 | lineStart: 1, |
| 324 | lineEnd: 2_000, |
| 325 | text: `${"t".repeat(63_985)}${"😀".repeat(8)}`, |
| 326 | }); |
| 327 | expect(terminal.text).toBe(`${"t".repeat(63_985)}\n… truncated …`); |
| 328 | expect(decodeMessageContext({ version: 1, records: [terminal] }).records).toMatchObject([ |
| 329 | { kind: "terminal", text: terminal.text }, |
| 330 | ]); |
| 331 | }); |
| 332 | |
| 333 | it("finds the destination collision for different legacy terminal messages", () => { |
| 334 | const legacy = (text: string) => |
| 335 | asKnownContextRecord( |
| 336 | upgradeLegacyContextMessage( |
| 337 | `Inspect this\n\n<terminal_context>\n- Terminal 1 line 1:\n 1 | ${text}\n</terminal_context>`, |
| 338 | ).records[0], |
| 339 | )!; |
| 340 | const first = legacy("A"); |
| 341 | const second = legacy("B"); |
| 342 | if (first.kind !== "terminal" || second.kind !== "terminal") { |
| 343 | throw new Error("Expected legacy terminal records"); |
| 344 | } |
| 345 | const destinationId = terminalContextReference( |
| 346 | terminalContextDraftFromRecord(first, ThreadId.make("t")), |
| 347 | ).contextId; |
| 348 | |
| 349 | expect(destinationId).toBe("terminal_legacy_terminal_1"); |
| 350 | expect(composerContextImportLookupIds(second)[0]).toBe(destinationId); |
| 351 | expect(isSameComposerContextPayload(first, second)).toBe(false); |
| 352 | }); |
| 353 | |
| 354 | it("treats an imported legacy id and its canonical reconstruction as the same excerpt", () => { |
| 355 | const draft = { |
| 356 | id: "term-1", |
| 357 | threadId: ThreadId.make("t"), |
| 358 | createdAt: "2026-01-01T00:00:00.000Z", |
| 359 | terminalId: "default", |
| 360 | terminalLabel: "Terminal 1", |
| 361 | lineStart: 1, |
| 362 | lineEnd: 2, |
| 363 | text: "A", |
| 364 | }; |
| 365 | const canonical = terminalContextRecord(draft); |
| 366 | const imported = { ...canonical, contextId: "legacy_terminal_1" as ComposerContextId }; |
| 367 | |
| 368 | expect(isSameComposerContextPayload(canonical, imported)).toBe(true); |
| 369 | expect(isSameComposerContextPayload(canonical, { ...canonical, text: "B" })).toBe(false); |
| 370 | }); |
| 371 | |
| 372 | it("resolves structured context directly and upgrades legacy text otherwise", () => { |
| 373 | const structured = resolveUserMessageContext({ |
| 374 | text: "hi [b.ts L4](t3-context://v1/review-comment/rc-1)", |
| 375 | context: { |
| 376 | version: 1, |
| 377 | records: [ |
| 378 | reviewCommentContextRecord({ |
| 379 | id: "rc-1", |
| 380 | sectionId: "s", |
| 381 | sectionTitle: "t", |
| 382 | filePath: "a/b.ts", |
| 383 | startIndex: 3, |
| 384 | endIndex: 3, |
| 385 | rangeLabel: "L4", |
| 386 | text: "", |
| 387 | diff: "", |
| 388 | }), |
| 389 | ], |
| 390 | }, |
| 391 | }); |
| 392 | expect(structured.recordsById.get("review-comment_rc-1")?.kind).toBe("review-comment"); |
| 393 | const legacy = resolveUserMessageContext({ |
| 394 | text: "hi\n\n<terminal_context>\n- T line 1:\n 1 | x\n</terminal_context>", |
| 395 | }); |
| 396 | expect(legacy.text).toBe("hi\n\n[T line 1](t3-context://v1/terminal/legacy_terminal_1)"); |
| 397 | expect(legacy.recordsById.get("legacy_terminal_1")?.kind).toBe("terminal"); |
| 398 | }); |
| 399 | }); |
| 400 | |
| 401 | describe("pullRequestContextKindLabel", () => { |
| 402 | const comment = (url: string, isDraft: boolean) => ({ |
| 403 | id: "pull-request-context:7", |
| 404 | sectionId: "pull-request:7", |
| 405 | sectionTitle: "PR #7", |
| 406 | filePath: "PR #7", |
| 407 | startIndex: 0, |
| 408 | endIndex: 0, |
| 409 | rangeLabel: "Title", |
| 410 | text: "", |
| 411 | diff: "", |
| 412 | pullRequest: { |
| 413 | number: 7, |
| 414 | title: "Title", |
| 415 | url, |
| 416 | headBranch: "feature", |
| 417 | baseBranch: "main", |
| 418 | state: "open" as const, |
| 419 | isDraft, |
| 420 | }, |
| 421 | }); |
| 422 | |
| 423 | it("names the change request the way its host does", () => { |
| 424 | expect(pullRequestContextKindLabel(comment("https://github.com/acme/app/pull/7", false))).toBe( |
| 425 | "Open pull request", |
| 426 | ); |
| 427 | expect( |
| 428 | pullRequestContextKindLabel(comment("https://gitlab.com/acme/app/-/merge_requests/7", true)), |
| 429 | ).toBe("Draft merge request"); |
| 430 | }); |
| 431 | }); |
| 432 | |
| 433 | describe("reviewCommentContextLabel", () => { |
| 434 | const selection = (sectionId: string) => ({ |
| 435 | id: "selection", |
| 436 | sectionId, |
| 437 | sectionTitle: "Review", |
| 438 | filePath: "apps/web/src/page.tsx", |
| 439 | startIndex: 0, |
| 440 | endIndex: 0, |
| 441 | rangeLabel: "+12 to +18", |
| 442 | text: "", |
| 443 | diff: "", |
| 444 | }); |
| 445 | |
| 446 | it("names the pull request a selection came from", () => { |
| 447 | expect(reviewCommentContextLabel(selection("pull-request:42"))).toBe("#42 page.tsx L12 to L18"); |
| 448 | expect(reviewCommentContextLabel(selection("turn-diff"))).toBe("page.tsx L12 to L18"); |
| 449 | }); |
| 450 | }); |
| 451 | |
| 452 | describe("producer ids that do not fit the grammar", () => { |
| 453 | it("folds review comment ids and keeps the raw id in the draft shape", () => { |
| 454 | const record = reviewCommentContextRecord({ |
| 455 | id: "pull-request-finding:42", |
| 456 | sectionId: "s", |
| 457 | sectionTitle: "t", |
| 458 | filePath: "a/b.ts", |
| 459 | startIndex: 0, |
| 460 | endIndex: 0, |
| 461 | rangeLabel: "L1", |
| 462 | text: "", |
| 463 | diff: "", |
| 464 | }); |
| 465 | expect(record.contextId).toMatch(/^review-comment_pull-request-finding-42-[0-9a-f]{16}$/); |
| 466 | expect( |
| 467 | resolveUserMessageContext({ |
| 468 | text: `[b.ts L1](t3-context://v1/review-comment/${record.contextId})`, |
| 469 | context: { version: 1, records: [record] }, |
| 470 | }).recordsById.has(record.contextId), |
| 471 | ).toBe(true); |
| 472 | }); |
| 473 | }); |
| 474 | |
| 475 | describe("selectedMessageContextFragment", () => { |
| 476 | const terminal = terminalContextRecord({ |
| 477 | id: "term-1", |
| 478 | threadId: ThreadId.make("t"), |
| 479 | createdAt: "2026-01-01T00:00:00.000Z", |
| 480 | terminalId: "default", |
| 481 | terminalLabel: "Terminal 1", |
| 482 | lineStart: 1, |
| 483 | lineEnd: 1, |
| 484 | text: "A", |
| 485 | }); |
| 486 | const review = reviewCommentContextRecord({ |
| 487 | id: "rc-1", |
| 488 | sectionId: "s", |
| 489 | sectionTitle: "t", |
| 490 | filePath: "a/b.ts", |
| 491 | startIndex: 3, |
| 492 | endIndex: 3, |
| 493 | rangeLabel: "L4", |
| 494 | text: "", |
| 495 | diff: "", |
| 496 | }); |
| 497 | const input = { |
| 498 | records: [terminal, review], |
| 499 | environmentId: EnvironmentId.make("env"), |
| 500 | threadId: ThreadId.make("t"), |
| 501 | messageId: MessageId.make("msg-1"), |
| 502 | }; |
| 503 | |
| 504 | it("carries only records for chips inside the selection", () => { |
| 505 | const fragment = selectedMessageContextFragment({ |
| 506 | ...input, |
| 507 | markdown: `see [b.ts L4](t3-context://v1/review-comment/${review.contextId})`, |
| 508 | }); |
| 509 | |
| 510 | expect(fragment).toContain(review.contextId); |
| 511 | expect(fragment).not.toContain(terminal.contextId); |
| 512 | }); |
| 513 | }); |