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

composerContext.test.ts

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