| 1 | import { ComposerContextId, type ComposerContextRecord } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { upgradeLegacyContextMessage } from "./composerContextLegacy.ts"; |
| 5 | import { formatComposerContextReference } from "./composerContextReferences.ts"; |
| 6 | import { serializeLegacyContextMessage } from "./composerContextLegacySend.ts"; |
| 7 | |
| 8 | const terminal = { |
| 9 | version: 1, |
| 10 | contextId: ComposerContextId.make("terminal_t1"), |
| 11 | kind: "terminal", |
| 12 | label: "Terminal 1 lines 3-4", |
| 13 | terminalId: "terminal-1", |
| 14 | terminalLabel: "Terminal 1", |
| 15 | lineStart: 3, |
| 16 | lineEnd: 4, |
| 17 | text: "boom\nagain", |
| 18 | } satisfies ComposerContextRecord; |
| 19 | |
| 20 | const review = { |
| 21 | version: 1, |
| 22 | contextId: ComposerContextId.make("review-comment_rc1"), |
| 23 | kind: "review-comment", |
| 24 | label: "b.ts L4", |
| 25 | sectionId: "file:a/b.ts", |
| 26 | sectionTitle: "File comment", |
| 27 | filePath: "a/b.ts", |
| 28 | startIndex: 3, |
| 29 | endIndex: 3, |
| 30 | rangeLabel: "L4", |
| 31 | text: "Why this branch?", |
| 32 | diff: "const x = 1;", |
| 33 | fenceLanguage: "ts", |
| 34 | } satisfies ComposerContextRecord; |
| 35 | |
| 36 | const annotation = { |
| 37 | version: 1, |
| 38 | contextId: ComposerContextId.make("preview-annotation_ann1"), |
| 39 | kind: "preview-annotation", |
| 40 | label: "Fix the checkout button", |
| 41 | annotationId: "ann1", |
| 42 | pageUrl: "https://example.com/checkout", |
| 43 | pageTitle: "Checkout", |
| 44 | comment: "Make this button clearer", |
| 45 | targetSummary: "1 selected element", |
| 46 | styleChanges: ["color: red → blue"], |
| 47 | elements: [ |
| 48 | { |
| 49 | pageUrl: "https://example.com/checkout", |
| 50 | pageTitle: "Checkout", |
| 51 | tagName: "button", |
| 52 | selector: "#submit-order", |
| 53 | htmlPreview: '<button id="submit-order">Buy now</button>', |
| 54 | componentName: "SubmitOrderButton", |
| 55 | source: { |
| 56 | functionName: "SubmitOrderButton", |
| 57 | fileName: "src/Checkout.tsx", |
| 58 | lineNumber: 42, |
| 59 | columnNumber: 7, |
| 60 | }, |
| 61 | styles: "color: red;", |
| 62 | }, |
| 63 | ], |
| 64 | } satisfies ComposerContextRecord; |
| 65 | |
| 66 | describe("serializeLegacyContextMessage", () => { |
| 67 | it("carries terminal payloads an older server would otherwise discard", () => { |
| 68 | const text = `Look at ${formatComposerContextReference(terminal)} please`; |
| 69 | const legacy = serializeLegacyContextMessage({ text, records: [terminal] }); |
| 70 | |
| 71 | // An older server forwards text verbatim, so the payload has to be in it. |
| 72 | expect(legacy).not.toContain("t3-context://"); |
| 73 | expect(legacy).toContain("boom"); |
| 74 | |
| 75 | // A newer client reading that message reconstructs the same excerpt. |
| 76 | const upgraded = upgradeLegacyContextMessage(legacy); |
| 77 | expect(upgraded.records).toHaveLength(1); |
| 78 | expect(upgraded.records[0]).toMatchObject({ |
| 79 | kind: "terminal", |
| 80 | terminalLabel: "Terminal 1", |
| 81 | lineStart: 3, |
| 82 | lineEnd: 4, |
| 83 | text: "boom\nagain", |
| 84 | }); |
| 85 | }); |
| 86 | |
| 87 | it("inlines a review comment with its diff intact", () => { |
| 88 | const text = `See ${formatComposerContextReference(review)} here`; |
| 89 | const legacy = serializeLegacyContextMessage({ text, records: [review] }); |
| 90 | expect(legacy).not.toContain("t3-context://"); |
| 91 | |
| 92 | const upgraded = upgradeLegacyContextMessage(legacy); |
| 93 | expect(upgraded.records[0]).toMatchObject({ |
| 94 | kind: "review-comment", |
| 95 | filePath: "a/b.ts", |
| 96 | rangeLabel: "L4", |
| 97 | text: "Why this branch?", |
| 98 | diff: "const x = 1;", |
| 99 | }); |
| 100 | }); |
| 101 | |
| 102 | it.each([" ", "\n", "\n\n"])( |
| 103 | "preserves the separator between a skill and a trailing PR through an older server: %j", |
| 104 | (separator) => { |
| 105 | const pullRequest = { |
| 106 | ...review, |
| 107 | label: "#11440", |
| 108 | sectionId: "pull-request:11440", |
| 109 | sectionTitle: "Pull request", |
| 110 | filePath: "PR #11440", |
| 111 | rangeLabel: "summary", |
| 112 | text: "Audit Mobile Photo Import", |
| 113 | diff: "", |
| 114 | }; |
| 115 | const text = `$pr-audit${separator}${formatComposerContextReference(pullRequest)}`; |
| 116 | const upgraded = upgradeLegacyContextMessage( |
| 117 | serializeLegacyContextMessage({ text, records: [pullRequest] }), |
| 118 | ); |
| 119 | |
| 120 | expect(upgraded.records).toHaveLength(1); |
| 121 | expect(upgraded.text).toBe( |
| 122 | `$pr-audit${separator}${formatComposerContextReference(upgraded.records[0]!)}`, |
| 123 | ); |
| 124 | }, |
| 125 | ); |
| 126 | |
| 127 | it("retains picked-element details for preview annotations sent through an older server", () => { |
| 128 | const text = `Update ${formatComposerContextReference(annotation)}`; |
| 129 | const upgraded = upgradeLegacyContextMessage( |
| 130 | serializeLegacyContextMessage({ text, records: [annotation] }), |
| 131 | ); |
| 132 | |
| 133 | expect(upgraded.records[0]).toMatchObject({ |
| 134 | kind: "preview-annotation", |
| 135 | pageUrl: "https://example.com/checkout", |
| 136 | elements: [ |
| 137 | { |
| 138 | selector: "#submit-order", |
| 139 | htmlPreview: '<button id="submit-order">Buy now</button>', |
| 140 | source: { |
| 141 | fileName: "src/Checkout.tsx", |
| 142 | lineNumber: 42, |
| 143 | columnNumber: 7, |
| 144 | }, |
| 145 | }, |
| 146 | ], |
| 147 | }); |
| 148 | }); |
| 149 | |
| 150 | it("keeps prose without context untouched", () => { |
| 151 | expect(serializeLegacyContextMessage({ text: "just prose", records: [] })).toBe("just prose"); |
| 152 | }); |
| 153 | |
| 154 | it("appends review comments the text never referenced", () => { |
| 155 | const legacy = serializeLegacyContextMessage({ text: "just prose", records: [review] }); |
| 156 | |
| 157 | expect(legacy).toContain("Why this branch?"); |
| 158 | const upgraded = upgradeLegacyContextMessage(legacy); |
| 159 | expect(upgraded.records).toHaveLength(1); |
| 160 | expect(upgraded.records[0]).toMatchObject({ |
| 161 | kind: "review-comment", |
| 162 | filePath: "a/b.ts", |
| 163 | text: "Why this branch?", |
| 164 | }); |
| 165 | }); |
| 166 | |
| 167 | it("numbers terminal output within the declared line range", () => { |
| 168 | const ranged = { |
| 169 | ...terminal, |
| 170 | lineStart: 3, |
| 171 | lineEnd: 4, |
| 172 | text: "boom\nagain\n", |
| 173 | }; |
| 174 | const legacy = serializeLegacyContextMessage({ text: "look", records: [ranged] }); |
| 175 | |
| 176 | expect(legacy).toContain("3 | boom\n 4 | again"); |
| 177 | expect(legacy).not.toContain("5 |"); |
| 178 | }); |
| 179 | }); |