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

composerContextLegacySend.test.ts

177 lines5.6 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 expect(legacy).not.toContain("t3-context://");
72 expect(legacy).toContain("boom");
73​
74 const upgraded = upgradeLegacyContextMessage(legacy);
75 expect(upgraded.records).toHaveLength(1);
76 expect(upgraded.records[0]).toMatchObject({
77 kind: "terminal",
78 terminalLabel: "Terminal 1",
79 lineStart: 3,
80 lineEnd: 4,
81 text: "boom\nagain",
82 });
83 });
84​
85 it("inlines a review comment with its diff intact", () => {
86 const text = `See ${formatComposerContextReference(review)} here`;
87 const legacy = serializeLegacyContextMessage({ text, records: [review] });
88 expect(legacy).not.toContain("t3-context://");
89​
90 const upgraded = upgradeLegacyContextMessage(legacy);
91 expect(upgraded.records[0]).toMatchObject({
92 kind: "review-comment",
93 filePath: "a/b.ts",
94 rangeLabel: "L4",
95 text: "Why this branch?",
96 diff: "const x = 1;",
97 });
98 });
99​
100 it.each([" ", "\n", "\n\n"])(
101 "preserves the separator between a skill and a trailing PR through an older server: %j",
102 (separator) => {
103 const pullRequest = {
104 ...review,
105 label: "#11440",
106 sectionId: "pull-request:11440",
107 sectionTitle: "Pull request",
108 filePath: "PR #11440",
109 rangeLabel: "summary",
110 text: "Audit Mobile Photo Import",
111 diff: "",
112 };
113 const text = `$pr-audit${separator}${formatComposerContextReference(pullRequest)}`;
114 const upgraded = upgradeLegacyContextMessage(
115 serializeLegacyContextMessage({ text, records: [pullRequest] }),
116 );
117​
118 expect(upgraded.records).toHaveLength(1);
119 expect(upgraded.text).toBe(
120 `$pr-audit${separator}${formatComposerContextReference(upgraded.records[0]!)}`,
121 );
122 },
123 );
124​
125 it("retains picked-element details for preview annotations sent through an older server", () => {
126 const text = `Update ${formatComposerContextReference(annotation)}`;
127 const upgraded = upgradeLegacyContextMessage(
128 serializeLegacyContextMessage({ text, records: [annotation] }),
129 );
130​
131 expect(upgraded.records[0]).toMatchObject({
132 kind: "preview-annotation",
133 pageUrl: "https://example.com/checkout",
134 elements: [
135 {
136 selector: "#submit-order",
137 htmlPreview: '<button id="submit-order">Buy now</button>',
138 source: {
139 fileName: "src/Checkout.tsx",
140 lineNumber: 42,
141 columnNumber: 7,
142 },
143 },
144 ],
145 });
146 });
147​
148 it("keeps prose without context untouched", () => {
149 expect(serializeLegacyContextMessage({ text: "just prose", records: [] })).toBe("just prose");
150 });
151​
152 it("appends review comments the text never referenced", () => {
153 const legacy = serializeLegacyContextMessage({ text: "just prose", records: [review] });
154​
155 expect(legacy).toContain("Why this branch?");
156 const upgraded = upgradeLegacyContextMessage(legacy);
157 expect(upgraded.records).toHaveLength(1);
158 expect(upgraded.records[0]).toMatchObject({
159 kind: "review-comment",
160 filePath: "a/b.ts",
161 text: "Why this branch?",
162 });
163 });
164​
165 it("numbers terminal output within the declared line range", () => {
166 const ranged = {
167 ...terminal,
168 lineStart: 3,
169 lineEnd: 4,
170 text: "boom\nagain\n",
171 };
172 const legacy = serializeLegacyContextMessage({ text: "look", records: [ranged] });
173​
174 expect(legacy).toContain("3 | boom\n 4 | again");
175 expect(legacy).not.toContain("5 |");
176 });
177});