Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerContextLegacy.test.ts

504 lines17.5 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { upgradeLegacyContextMessage } from "./composerContextLegacy.ts";
4​
5describe("upgradeLegacyContextMessage", () => {
6 const review =
7 '<review_comment sectionId="s" filePath="f.ts" startIndex="1" endIndex="1">note</review_comment>';
8​
9 it.each([
10 {
11 tag: "terminal_context",
12 body: `- Build line 1:\n ${review}`,
13 payload: { kind: "terminal", text: review },
14 },
15 {
16 tag: "element_context",
17 body: `- <div>:\n html:\n ${review}`,
18 payload: { kind: "element", htmlPreview: review },
19 },
20 {
21 tag: "preview_annotation",
22 body: `Page: Example\nComment: ${review}`,
23 payload: { kind: "preview-annotation", comment: review },
24 },
25 {
26 tag: "preview_annotation",
27 body: `Page: Example\n<element_context>\n- <div>:\n html:\n ${review}\n</element_context>`,
28 payload: {
29 kind: "preview-annotation",
30 elements: [expect.objectContaining({ htmlPreview: review })],
31 },
32 },
33 ])("preserves review-shaped payloads in $tag", ({ tag, body, payload }) => {
34 for (const suffix of ["", `\n${review}`]) {
35 const result = upgradeLegacyContextMessage(
36 `Before ${review} after\n<${tag}>\n${body}\n</${tag}>${suffix}`,
37 );
38 expect(result.records[0]).toMatchObject(payload);
39 expect(result.records.filter((record) => record.kind === "review-comment")).toHaveLength(
40 suffix ? 2 : 1,
41 );
42 expect(result.text).not.toContain("\uE000");
43 expect(result.text).toContain(
44 "Before [f.ts line](t3-context://v1/review-comment/legacy_review-comment_1) after",
45 );
46 }
47 });
48​
49 it.each([3, 4, 6])("preserves closing review tags inside a %i-backtick fence", (length) => {
50 const fence = "`".repeat(length);
51 const diff = '+ const tag = "</review_comment>";\n</review_comment>\n+ final line';
52 const block = `<review_comment sectionId="s" filePath="f.ts" startIndex="1" endIndex="1">\nComment\n${fence}diff\n${diff}\n${fence}\n</review_comment>`;
53 const result = upgradeLegacyContextMessage(`Before ${block} between ${review} after`);
54 expect(result.text).toBe(
55 "Before [f.ts line](t3-context://v1/review-comment/legacy_review-comment_1) between [f.ts line](t3-context://v1/review-comment/legacy_review-comment_2) after",
56 );
57 expect(result.records).toHaveLength(2);
58 expect(result.records[0]).toMatchObject({ text: "Comment", diff, fenceLanguage: "diff" });
59 });
60​
61 it("keeps whitespace in review comment file paths and section ids", () => {
62 const text =
63 '<review_comment sectionId="file:foo.txt " filePath="foo.txt " startIndex="1" endIndex="1">note</review_comment>';
64 expect(upgradeLegacyContextMessage(text).records[0]).toMatchObject({
65 filePath: "foo.txt ",
66 sectionId: "file:foo.txt ",
67 });
68 });
69​
70 it("does not close a review fence at a shorter backtick run", () => {
71 const diff = "```\n</review_comment>\n+ remaining source";
72 const text = `<review_comment sectionId="s" filePath="f.ts" startIndex="1" endIndex="1">\nNote\n\`\`\`\`diff\n${diff}\n\`\`\`\`\n</review_comment>`;
73 expect(upgradeLegacyContextMessage(text).records[0]).toMatchObject({ text: "Note", diff });
74 });
75​
76 it("preserves a review with an unterminated fence", () => {
77 const text =
78 '<review_comment sectionId="s" filePath="f.ts" startIndex="1" endIndex="1">\nNote\n```diff\n+ source\n</review_comment>';
79 expect(upgradeLegacyContextMessage(text)).toEqual({ text, records: [] });
80 });
81​
82 it.each([
83 `- ${"x".repeat(256)} line 1:\n output`,
84 `- Build line 1:\n ${"x".repeat(64_001)}`,
85 "- Build lines 9-1:\n output",
86 Array.from({ length: 201 }, () => "- Build line 1:\n output").join("\n"),
87 ])("keeps terminal source when conversion exceeds the wire contract", (body) => {
88 const text = `Prompt\n<terminal_context>\n${body}\n</terminal_context>`;
89 expect(upgradeLegacyContextMessage(text)).toEqual({ text, records: [] });
90 });
91​
92 it.each(["email@build:7", "@build:7foo", "@build:7.foo"])(
93 "does not replace terminal labels embedded in %s",
94 (prompt) => {
95 const upgraded = upgradeLegacyContextMessage(
96 `${prompt}\n<terminal_context>\n- Build line 7:\n output\n</terminal_context>`,
97 );
98 expect(upgraded.text).toBe(
99 `${prompt}\n\n[Build line 7](t3-context://v1/terminal/legacy_terminal_1)`,
100 );
101 },
102 );
103​
104 it.each(["terminal_context", "element_context"])(
105 "preserves malformed trailing %s blocks as text",
106 (tag) => {
107 for (const body of ["partial output", "- Unrecognized header:\n partial output"]) {
108 const text = `message\n<${tag}>\n${body}\n</${tag}>`;
109 expect(upgradeLegacyContextMessage(text)).toEqual({ text, records: [] });
110 }
111 },
112 );
113​
114 it.each(["terminal_context", "element_context"])(
115 "preserves partially parsed %s blocks",
116 (tag) => {
117 const entry =
118 tag === "terminal_context"
119 ? "- Build line 7:\n output"
120 : "- <button>:\n html:\n <button>Pay</button>";
121 for (const body of [
122 `unexpected prefix\n${entry}`,
123 `${entry}\nunexpected suffix`,
124 `${entry}\nunexpected middle\n${entry}`,
125 ` orphaned body\n${entry}`,
126 ]) {
127 const text = `Prompt\n<${tag}>\n${body}\n</${tag}>`;
128 expect(upgradeLegacyContextMessage(text)).toEqual({ text, records: [] });
129 }
130 },
131 );
132​
133 it.each(["x".repeat(16_001), `note\n\`\`\`diff\n${"+x".repeat(16_001)}\n\`\`\``])(
134 "preserves oversized legacy reviews instead of creating dangling references",
135 (body) => {
136 const text = `<review_comment sectionId="s" filePath="f.ts" startIndex="1" endIndex="1">${body}</review_comment>`;
137 expect(upgradeLegacyContextMessage(text)).toEqual({ text, records: [] });
138 },
139 );
140​
141 it("preserves literal review tokens even beside real review blocks", () => {
142 const review =
143 '<review_comment sectionId="s" filePath="f.ts" startIndex="1" endIndex="1">note</review_comment>';
144 const upgraded = upgradeLegacyContextMessage(
145 `Literal \uE0000\uE000 before ${review} after \uE0007\uE000`,
146 );
147 expect(upgraded.text).toBe(
148 "Literal \uE0000\uE000 before [f.ts line](t3-context://v1/review-comment/legacy_review-comment_1) after \uE0007\uE000",
149 );
150 expect(upgraded.records).toHaveLength(1);
151 const unmatched =
152 "Literal \uE0000\uE000 <review_comment>invalid</review_comment> \uE00099\uE000";
153 expect(upgradeLegacyContextMessage(unmatched)).toEqual({ text: unmatched, records: [] });
154 });
155​
156 it("matches complete terminal line ranges rather than numeric prefixes", () => {
157 const upgraded = upgradeLegacyContextMessage(
158 [
159 "Compare @terminal:10 and @terminal:1-2 with @terminal:1",
160 "<terminal_context>",
161 "- Terminal line 1:",
162 " 1 | one",
163 "- Terminal line 10:",
164 " 10 | ten",
165 "- Terminal lines 1-2:",
166 " 1 | one",
167 " 2 | two",
168 "</terminal_context>",
169 ].join("\n"),
170 );
171 expect(upgraded.text).toBe(
172 "Compare [Terminal line 10](t3-context://v1/terminal/legacy_terminal_2) and [Terminal lines 1-2](t3-context://v1/terminal/legacy_terminal_3) with [Terminal line 1](t3-context://v1/terminal/legacy_terminal_1)",
173 );
174 });
175​
176 it("preserves Markdown hard breaks while removing legacy context", () => {
177 const text = [
178 "Line with hard break ",
179 "next",
180 "",
181 "<terminal_context>",
182 "- Build line 7:",
183 " 7 | done",
184 "</terminal_context>",
185 ].join("\n");
186​
187 expect(upgradeLegacyContextMessage(text).text).toContain("Line with hard break \nnext");
188 });
189​
190 it("binds U+FFFC placeholders to trailing terminal entries in order", () => {
191 const text = [
192 "Look at  and  please",
193 "",
194 "<terminal_context>",
195 "- Terminal 1 lines 509-510:",
196 " 509 | error: boom",
197 " 510 | at main.ts:1",
198 "",
199 "- Build line 7:",
200 " 7 | done",
201 "</terminal_context>",
202 ].join("\n");
203 const upgraded = upgradeLegacyContextMessage(text);
204 expect(upgraded.text).toBe(
205 "Look at [Terminal 1 lines 509-510](t3-context://v1/terminal/legacy_terminal_1) and [Build line 7](t3-context://v1/terminal/legacy_terminal_2) please",
206 );
207 expect(upgraded.records).toEqual([
208 {
209 version: 1,
210 contextId: "legacy_terminal_1",
211 kind: "terminal",
212 label: "Terminal 1 lines 509-510",
213 terminalId: "terminal-1",
214 terminalLabel: "Terminal 1",
215 lineStart: 509,
216 lineEnd: 510,
217 text: "error: boom\n at main.ts:1",
218 },
219 {
220 version: 1,
221 contextId: "legacy_terminal_2",
222 kind: "terminal",
223 label: "Build line 7",
224 terminalId: "build",
225 terminalLabel: "Build",
226 lineStart: 7,
227 lineEnd: 7,
228 text: "done",
229 },
230 ]);
231 });
232​
233 it("appends references for terminal entries without placeholders and drops extra placeholders", () => {
234 const text = " and \n\n<terminal_context>\n- T line 1:\n 1 | x\n</terminal_context>";
235 expect(upgradeLegacyContextMessage(text).text).toBe(
236 "[T line 1](t3-context://v1/terminal/legacy_terminal_1) and",
237 );
238 const noPlaceholder = "hi\n\n<terminal_context>\n- T line 1:\n 1 | x\n</terminal_context>";
239 expect(upgradeLegacyContextMessage(noPlaceholder).text).toBe(
240 "hi\n\n[T line 1](t3-context://v1/terminal/legacy_terminal_1)",
241 );
242 });
243​
244 it("upgrades a trailing element block into element records", () => {
245 const text = [
246 "fix this",
247 "",
248 "<element_context>",
249 "- <Button> (Button.tsx:12):",
250 " url: http://localhost:3000/checkout",
251 " selector: #pay",
252 " source: src/Button.tsx:12:3",
253 " html:",
254 ' <button id="pay">Pay</button>',
255 " styles:",
256 " color: red;",
257 "</element_context>",
258 ].join("\n");
259 const upgraded = upgradeLegacyContextMessage(text);
260 expect(upgraded.text).toBe("fix this\n\n[<Button>](t3-context://v1/element/legacy_element_1)");
261 expect(upgraded.records).toEqual([
262 {
263 version: 1,
264 contextId: "legacy_element_1",
265 kind: "element",
266 label: "<Button>",
267 pageUrl: "http://localhost:3000/checkout",
268 pageTitle: null,
269 tagName: "button",
270 selector: "#pay",
271 htmlPreview: '<button id="pay">Pay</button>',
272 componentName: "Button",
273 source: { functionName: null, fileName: "src/Button.tsx", lineNumber: 12, columnNumber: 3 },
274 styles: "color: red;",
275 },
276 ]);
277 });
278​
279 it("keeps every line of a multiline legacy annotation comment", () => {
280 const text = [
281 "<preview_annotation>",
282 "Preview annotation:",
283 "Id: ann_2",
284 "Page: Checkout",
285 "Comment: First instruction",
286 "Second instruction",
287 "Targets: 1 selected element.",
288 "</preview_annotation>",
289 ].join("\n");
290 const upgraded = upgradeLegacyContextMessage(text);
291 expect(upgraded.records[0]).toMatchObject({
292 kind: "preview-annotation",
293 comment: "First instruction\nSecond instruction",
294 });
295 });
296​
297 it("keeps markup inside a legacy annotation comment", () => {
298 const text = [
299 "<preview_annotation>",
300 "Preview annotation:",
301 "Comment: Change this",
302 "<div>keep me</div>",
303 "and this too",
304 "</preview_annotation>",
305 ].join("\n");
306 expect(upgradeLegacyContextMessage(text).records[0]).toMatchObject({
307 comment: "Change this\n<div>keep me</div>\nand this too",
308 });
309 });
310​
311 it("upgrades trailing preview annotation blocks", () => {
312 const text = [
313 "bigger",
314 "",
315 "<preview_annotation>",
316 "Preview annotation:",
317 "Id: ann_1",
318 "Page: Checkout",
319 "Comment: Make it bigger",
320 "Targets: 1 selected element.",
321 "Requested visual changes:",
322 "- font-size: 12px → 20px",
323 "The attached screenshot is the annotated preview crop.",
324 "<element_context>",
325 "- <button>:",
326 " url: http://localhost:3000/",
327 " selector: #pay",
328 " source: src/Pay.tsx:12:3",
329 " html:",
330 " <button>Pay</button>",
331 "</element_context>",
332 "</preview_annotation>",
333 ].join("\n");
334 const upgraded = upgradeLegacyContextMessage(text);
335 expect(upgraded.text).toBe(
336 "bigger\n\n[Checkout](t3-context://v1/preview-annotation/legacy_preview-annotation_1)",
337 );
338 expect(upgraded.records).toEqual([
339 {
340 version: 1,
341 contextId: "legacy_preview-annotation_1",
342 kind: "preview-annotation",
343 label: "Checkout",
344 annotationId: "ann_1",
345 pageUrl: "http://localhost:3000/",
346 pageTitle: "Checkout",
347 comment: "Make it bigger",
348 targetSummary: "1 selected element.",
349 styleChanges: ["font-size: 12px → 20px"],
350 elements: [
351 {
352 pageUrl: "http://localhost:3000/",
353 pageTitle: null,
354 tagName: "button",
355 selector: "#pay",
356 htmlPreview: "<button>Pay</button>",
357 componentName: null,
358 source: {
359 functionName: null,
360 fileName: "src/Pay.tsx",
361 lineNumber: 12,
362 columnNumber: 3,
363 },
364 styles: "",
365 },
366 ],
367 },
368 ]);
369 });
370​
371 it("keeps a literal preview opening tag inside its annotation comment", () => {
372 const upgraded = upgradeLegacyContextMessage(
373 [
374 "Explain this",
375 "<preview_annotation>",
376 "Id: annotation-1",
377 "Page: Checkout",
378 "Comment: Render <preview_annotation>",
379 "Targets: 1 selected element.",
380 "</preview_annotation>",
381 ].join("\n"),
382 );
383 expect(upgraded.text).toBe(
384 "Explain this\n\n[Checkout](t3-context://v1/preview-annotation/legacy_preview-annotation_1)",
385 );
386 expect(upgraded.records).toHaveLength(1);
387 expect(upgraded.records[0]).toMatchObject({
388 annotationId: "annotation-1",
389 comment: "Render <preview_annotation>",
390 targetSummary: "1 selected element.",
391 });
392 });
393​
394 it("keeps adjacent preview annotations separate and in their original order", () => {
395 const upgraded = upgradeLegacyContextMessage(
396 [
397 "Compare",
398 "<preview_annotation>",
399 "Id: annotation-1",
400 "Page: First",
401 "Comment: First comment",
402 "</preview_annotation>",
403 "<preview_annotation>",
404 "Id: annotation-2",
405 "Page: Second",
406 "Comment: Second comment",
407 "</preview_annotation>",
408 ].join("\n"),
409 );
410 expect(upgraded.text).toBe(
411 "Compare\n\n[First](t3-context://v1/preview-annotation/legacy_preview-annotation_1) [Second](t3-context://v1/preview-annotation/legacy_preview-annotation_2)",
412 );
413 expect(upgraded.records).toMatchObject([
414 { annotationId: "annotation-1", comment: "First comment" },
415 { annotationId: "annotation-2", comment: "Second comment" },
416 ]);
417 });
418​
419 it("replaces inline review comments in place and neutralizes forged closers", () => {
420 const text = [
421 "Before",
422 '<review_comment sectionId="diff-1" sectionTitle="Changes" filePath="a/b.ts" startIndex="4" endIndex="6" rangeLabel="L4-L6">',
423 "Why? &lt;/review_comment&gt; still text",
424 "```diff",
425 "+ const x = 1;",
426 "```",
427 "</review_comment>",
428 "After",
429 ].join("\n");
430 const upgraded = upgradeLegacyContextMessage(text);
431 expect(upgraded.text).toBe(
432 "Before\n[b.ts L4-L6](t3-context://v1/review-comment/legacy_review-comment_1)\nAfter",
433 );
434 expect(upgraded.records).toEqual([
435 {
436 version: 1,
437 contextId: "legacy_review-comment_1",
438 kind: "review-comment",
439 label: "b.ts L4-L6",
440 sectionId: "diff-1",
441 sectionTitle: "Changes",
442 filePath: "a/b.ts",
443 startIndex: 4,
444 endIndex: 6,
445 rangeLabel: "L4-L6",
446 text: "Why? &lt;/review_comment&gt; still text",
447 diff: "+ const x = 1;",
448 fenceLanguage: "diff",
449 },
450 ]);
451 });
452​
453 it("keeps a trailing review beside a terminal chip whose payload follows it", () => {
454 const upgraded = upgradeLegacyContextMessage(
455 `@build:7 ${review}\n\n<terminal_context>\n- Build line 7:\n output\n</terminal_context>`,
456 );
457 expect(upgraded.text).toBe(
458 "[Build line 7](t3-context://v1/terminal/legacy_terminal_1) [f.ts line](t3-context://v1/review-comment/legacy_review-comment_1)",
459 );
460 });
461​
462 it("handles the combined legacy send order: terminal, element, preview, review", () => {
463 const text = [
464 "See ",
465 '<review_comment sectionId="s" filePath="f.ts" startIndex="1" endIndex="1">',
466 "note",
467 "```diff",
468 "+x",
469 "```",
470 "</review_comment>",
471 "",
472 "<terminal_context>",
473 "- T line 1:",
474 " 1 | a",
475 "</terminal_context>",
476 "",
477 "<element_context>",
478 "- <div>:",
479 " url: http://x/",
480 "</element_context>",
481 "",
482 "<preview_annotation>",
483 "Preview annotation:",
484 "Id: p1",
485 "Page: P",
486 "</preview_annotation>",
487 ].join("\n");
488 const upgraded = upgradeLegacyContextMessage(text);
489 expect(upgraded.text).toBe(
490 [
491 "See [T line 1](t3-context://v1/terminal/legacy_terminal_1)",
492 "[f.ts line](t3-context://v1/review-comment/legacy_review-comment_1)",
493 "",
494 "[<div>](t3-context://v1/element/legacy_element_1) [P](t3-context://v1/preview-annotation/legacy_preview-annotation_1)",
495 ].join("\n"),
496 );
497 expect(upgraded.records.map((record) => record.contextId)).toEqual([
498 "legacy_terminal_1",
499 "legacy_element_1",
500 "legacy_preview-annotation_1",
501 "legacy_review-comment_1",
502 ]);
503 });
504});