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

composerContext.test.ts

229 lines6.8 KB

1import { describe, expect, it } from "vite-plus/test";
2import * as Option from "effect/Option";
3import * as Schema from "effect/Schema";
4​
5import {
6 COMPOSER_CONTEXT_KINDS,
7 ComposerContextRecord,
8 OrchestrationMessageContext,
9} from "./composerContext.ts";
10​
11const decodeRecord = Schema.decodeUnknownOption(ComposerContextRecord);
12const decodeContext = Schema.decodeUnknownSync(OrchestrationMessageContext);
13​
14const base = { version: 1, contextId: "ctx_1" } as const;
15​
16const knownRecords: Record<(typeof COMPOSER_CONTEXT_KINDS)[number], Record<string, unknown>> = {
17 image: {
18 ...base,
19 kind: "image",
20 label: "checkout-error.png",
21 attachmentId: "att_1",
22 name: "checkout-error.png",
23 mimeType: "image/png",
24 sizeBytes: 1234,
25 },
26 file: {
27 ...base,
28 kind: "file",
29 label: "notes.txt",
30 attachmentId: "att_2",
31 name: "notes.txt",
32 mimeType: "text/plain",
33 sizeBytes: 12,
34 },
35 terminal: {
36 ...base,
37 kind: "terminal",
38 label: "Terminal 1 lines 509-514",
39 terminalId: "term-1",
40 terminalLabel: "Terminal 1",
41 lineStart: 509,
42 lineEnd: 514,
43 text: "error: boom\n at main.ts:1",
44 },
45 element: {
46 ...base,
47 kind: "element",
48 label: "<Button>",
49 pageUrl: "http://localhost:3000/checkout",
50 pageTitle: "Checkout",
51 tagName: "button",
52 selector: "#pay",
53 htmlPreview: '<button id="pay">Pay</button>',
54 componentName: "Button",
55 source: { functionName: "Button", fileName: "Button.tsx", lineNumber: 12, columnNumber: 3 },
56 styles: "color: red;",
57 },
58 "preview-annotation": {
59 ...base,
60 kind: "preview-annotation",
61 label: "Checkout",
62 annotationId: "ann_1",
63 pageUrl: "http://localhost:3000/checkout",
64 pageTitle: "Checkout",
65 comment: "Make this bigger",
66 targetSummary: "1 selected element",
67 styleChanges: ["font-size: 20px"],
68 elements: [
69 {
70 pageUrl: "http://localhost:3000/checkout",
71 pageTitle: "Checkout",
72 tagName: "button",
73 selector: "#pay",
74 htmlPreview: "<button>Pay</button>",
75 componentName: "Button",
76 source: null,
77 styles: "",
78 },
79 ],
80 screenshotContextId: "ctx_2",
81 },
82 "review-comment": {
83 ...base,
84 kind: "review-comment",
85 label: "ChatComposer.tsx L4118",
86 sectionId: "diff-1",
87 sectionTitle: "Changes",
88 filePath: "apps/web/src/ChatComposer.tsx",
89 startIndex: 4118,
90 endIndex: 4118,
91 rangeLabel: "L4118",
92 text: "Why is this here?",
93 diff: "+ const x = 1;",
94 fenceLanguage: "diff",
95 pullRequest: {
96 number: 42,
97 title: "Improve context chips",
98 url: "https://github.com/pingdotgg/t3code/pull/42",
99 headBranch: "feat/context-chips",
100 baseBranch: "main",
101 state: "open",
102 isDraft: false,
103 },
104 },
105 mention: { ...base, kind: "mention", label: "@src/index.ts", path: "src/index.ts" },
106 skill: { ...base, kind: "skill", label: "$pinchtab", name: "pinchtab" },
107 thread: {
108 ...base,
109 kind: "thread",
110 label: "Fix login flow",
111 environmentId: "environment-1",
112 threadId: "thread-1",
113 title: "Fix login flow",
114 },
115};
116​
117describe("ComposerContextRecord", () => {
118 it("keeps unknown kinds with their payload", () => {
119 const decoded = decodeRecord({
120 ...base,
121 kind: "future-thing",
122 label: "Future",
123 payload: { anything: [1, 2, 3] },
124 });
125 expect(Option.getOrThrow(decoded)).toEqual({
126 version: 1,
127 contextId: "ctx_1",
128 kind: "future-thing",
129 label: "Future",
130 payload: { anything: [1, 2, 3] },
131 });
132 });
133​
134 it("does not let a malformed known kind slide through as unknown", () => {
135 expect(Option.isNone(decodeRecord({ ...base, kind: "image", label: "x" }))).toBe(true);
136 });
137​
138 it("bounds the serialized payload of future context kinds", () => {
139 const unknown = { ...base, kind: "future-thing", label: "Future" };
140 expect(Option.isSome(decodeRecord({ ...unknown, payload: "x".repeat(63_998) }))).toBe(true);
141 expect(Option.isNone(decodeRecord({ ...unknown, payload: "x".repeat(63_999) }))).toBe(true);
142 expect(Option.isNone(decodeRecord({ ...unknown, payload: '"'.repeat(32_000) }))).toBe(true);
143 expect(
144 decodeContext({
145 version: 1,
146 records: [
147 { ...unknown, payload: "x".repeat(64_000) },
148 { ...knownRecords.skill, contextId: "ctx_skill" },
149 ],
150 }).records,
151 ).toEqual([{ ...knownRecords.skill, contextId: "ctx_skill" }]);
152 });
153});
154​
155describe("OrchestrationMessageContext", () => {
156 it("rejects aggregate context size even when each record is valid", () => {
157 const record = {
158 ...knownRecords["preview-annotation"],
159 styleChangeDetails: Array.from({ length: 200 }, () => ({
160 targetId: "target",
161 selector: null,
162 property: "content",
163 previousValue: "x".repeat(8_000),
164 value: "y".repeat(8_000),
165 })),
166 };
167 expect(Option.isSome(decodeRecord(record))).toBe(true);
168 expect(() => decodeContext({ version: 1, records: [record] })).not.toThrow();
169 expect(() =>
170 decodeContext({
171 version: 1,
172 records: Array.from({ length: 6 }, (_, index) => ({
173 ...record,
174 contextId: `ctx_${index}`,
175 })),
176 }),
177 ).toThrow();
178 });
179​
180 it("sends a message without the records the wire cannot carry", () => {
181 const encodeOrchestrationMessageContext = Schema.encodeUnknownSync(
182 Schema.toCodecJson(OrchestrationMessageContext),
183 );
184​
185 const wire = encodeOrchestrationMessageContext({
186 version: 1,
187 records: [
188 decodeContext({ version: 1, records: [knownRecords.terminal] }).records[0],
189 {
190 ...base,
191 contextId: "ctx_2",
192 kind: "future-kind",
193 label: "x",
194 payload: { count: Number.NaN },
195 },
196 { ...knownRecords["review-comment"], contextId: "ctx_3", fenceLanguage: undefined },
197 { ...base, contextId: "ctx_4", kind: "future-kind", label: "y", payload: { n: 1n } },
198 ],
199 });
200 expect(decodeContext(wire).records.map((record) => record.contextId)).toEqual(["ctx_1"]);
201 });
202​
203 it("rejects oversized arrays before dropping malformed records", () => {
204 expect(() =>
205 decodeContext({ version: 1, records: Array.from({ length: 201 }, () => ({})) }),
206 ).toThrow();
207 });
208​
209 it("drops undecodable records and keeps valid siblings", () => {
210 const context = decodeContext({
211 version: 1,
212 records: [
213 knownRecords.skill,
214 { version: 1, kind: "image" },
215 { ...knownRecords.terminal, contextId: "ctx_2" },
216 ],
217 });
218 expect(context.records.map((record) => record.kind)).toEqual(["skill", "terminal"]);
219 });
220​
221 it("rejects duplicate normalized context identities", () => {
222 expect(() =>
223 decodeContext({
224 version: 1,
225 records: [knownRecords.skill, { ...knownRecords.terminal, contextId: " ctx_1 " }],
226 }),
227 ).toThrow();
228 });
229});