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

composerContextLegacySend.test.ts

154 lines4.9 KB

1import { ComposerContextId, type ComposerContextRecord } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { upgradeLegacyContextMessage } from "./composerContextLegacy.ts";
5import { formatComposerContextReference } from "./composerContextReferences.ts";
6import { serializeLegacyContextMessage } from "./composerContextLegacySend.ts";
7​
8const 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​
20const 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​
36const 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​
66describe("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("retains picked-element details for preview annotations sent through an older server", () => {
103 const text = `Update ${formatComposerContextReference(annotation)}`;
104 const upgraded = upgradeLegacyContextMessage(
105 serializeLegacyContextMessage({ text, records: [annotation] }),
106 );
107​
108 expect(upgraded.records[0]).toMatchObject({
109 kind: "preview-annotation",
110 pageUrl: "https://example.com/checkout",
111 elements: [
112 {
113 selector: "#submit-order",
114 htmlPreview: '<button id="submit-order">Buy now</button>',
115 source: {
116 fileName: "src/Checkout.tsx",
117 lineNumber: 42,
118 columnNumber: 7,
119 },
120 },
121 ],
122 });
123 });
124​
125 it("keeps prose without context untouched", () => {
126 expect(serializeLegacyContextMessage({ text: "just prose", records: [] })).toBe("just prose");
127 });
128​
129 it("appends review comments the text never referenced", () => {
130 const legacy = serializeLegacyContextMessage({ text: "just prose", records: [review] });
131​
132 expect(legacy).toContain("Why this branch?");
133 const upgraded = upgradeLegacyContextMessage(legacy);
134 expect(upgraded.records).toHaveLength(1);
135 expect(upgraded.records[0]).toMatchObject({
136 kind: "review-comment",
137 filePath: "a/b.ts",
138 text: "Why this branch?",
139 });
140 });
141​
142 it("numbers terminal output within the declared line range", () => {
143 const ranged = {
144 ...terminal,
145 lineStart: 3,
146 lineEnd: 4,
147 text: "boom\nagain\n",
148 };
149 const legacy = serializeLegacyContextMessage({ text: "look", records: [ranged] });
150​
151 expect(legacy).toContain("3 | boom\n 4 | again");
152 expect(legacy).not.toContain("5 |");
153 });
154});