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