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 { 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};
111
112describe("ComposerContextRecord", () => {
113 it.each(COMPOSER_CONTEXT_KINDS)("round-trips a %s record", (kind) => {
114 const decoded = decodeRecord(knownRecords[kind]);
115 expect(Option.isSome(decoded)).toBe(true);
116 expect(Option.getOrThrow(decoded)).toEqual(knownRecords[kind]);
117 });
118
119 it("keeps unknown kinds with their payload", () => {
120 const decoded = decodeRecord({
121 ...base,
122 kind: "future-thing",
123 label: "Future",
124 payload: { anything: [1, 2, 3] },
125 });
126 expect(Option.getOrThrow(decoded)).toEqual({
127 version: 1,
128 contextId: "ctx_1",
129 kind: "future-thing",
130 label: "Future",
131 payload: { anything: [1, 2, 3] },
132 });
133 });
134
135 it("does not let a malformed known kind slide through as unknown", () => {
136 expect(Option.isNone(decodeRecord({ ...base, kind: "image", label: "x" }))).toBe(true);
137 });
138
139 it("bounds the serialized payload of future context kinds", () => {
140 const unknown = { ...base, kind: "future-thing", label: "Future" };
141 expect(Option.isSome(decodeRecord({ ...unknown, payload: "x".repeat(63_998) }))).toBe(true);
142 expect(Option.isNone(decodeRecord({ ...unknown, payload: "x".repeat(63_999) }))).toBe(true);
143 expect(Option.isNone(decodeRecord({ ...unknown, payload: '"'.repeat(32_000) }))).toBe(true);
144 expect(
145 decodeContext({
146 version: 1,
147 records: [
148 { ...unknown, payload: "x".repeat(64_000) },
149 { ...knownRecords.skill, contextId: "ctx_skill" },
150 ],
151 }).records,
152 ).toEqual([{ ...knownRecords.skill, contextId: "ctx_skill" }]);
153 });
154
155 it("rejects bad ids and versions", () => {
156 expect(Option.isNone(decodeRecord({ ...knownRecords.skill, contextId: "has space" }))).toBe(
157 true,
158 );
159 expect(Option.isNone(decodeRecord({ ...knownRecords.skill, version: 2 }))).toBe(true);
160 expect(Option.isNone(decodeRecord({ ...knownRecords.skill, kind: "Bad Kind" }))).toBe(true);
161 });
162});
163
164describe("OrchestrationMessageContext", () => {
165 it("rejects aggregate context size even when each record is valid", () => {
166 const record = {
167 ...knownRecords["preview-annotation"],
168 styleChangeDetails: Array.from({ length: 200 }, () => ({
169 targetId: "target",
170 selector: null,
171 property: "content",
172 previousValue: "x".repeat(8_000),
173 value: "y".repeat(8_000),
174 })),
175 };
176 expect(Option.isSome(decodeRecord(record))).toBe(true);
177 expect(() => decodeContext({ version: 1, records: [record] })).not.toThrow();
178 expect(() =>
179 decodeContext({
180 version: 1,
181 records: Array.from({ length: 6 }, (_, index) => ({
182 ...record,
183 contextId: `ctx_${index}`,
184 })),
185 }),
186 ).toThrow();
187 });
188
189 it("normalizes decoded record identifiers", () => {
190 const context = decodeContext({
191 version: 1,
192 records: [{ ...knownRecords.skill, contextId: " ctx_1 ", name: " review " }],
193 });
194 expect(context.records[0]).toMatchObject({ contextId: "ctx_1", name: "review" });
195 });
196
197 it("rejects oversized arrays before dropping malformed records", () => {
198 expect(() =>
199 decodeContext({ version: 1, records: Array.from({ length: 201 }, () => ({})) }),
200 ).toThrow();
201 });
202
203 it("drops undecodable records and keeps valid siblings", () => {
204 const context = decodeContext({
205 version: 1,
206 records: [
207 knownRecords.skill,
208 { version: 1, kind: "image" },
209 { ...knownRecords.terminal, contextId: "ctx_2" },
210 ],
211 });
212 expect(context.records.map((record) => record.kind)).toEqual(["skill", "terminal"]);
213 });
214
215 it("rejects duplicate normalized context identities", () => {
216 expect(() =>
217 decodeContext({
218 version: 1,
219 records: [knownRecords.skill, { ...knownRecords.terminal, contextId: " ctx_1 " }],
220 }),
221 ).toThrow();
222 });
223
224 it("is optional on messages and turn-start commands", () => {
225 const message = {
226 id: "m1",
227 role: "user",
228 text: "hi",
229 turnId: null,
230 streaming: false,
231 createdAt: "2026-01-01T00:00:00.000Z",
232 updatedAt: "2026-01-01T00:00:00.000Z",
233 };
234 expect(decodeMessage(message).context).toBeUndefined();
235 const withContext = decodeMessage({
236 ...message,
237 context: { version: 1, records: [knownRecords.mention] },
238 });
239 expect(withContext.context?.records).toHaveLength(1);
240
241 const command = decodeTurnStart({
242 type: "thread.turn.start",
243 commandId: "c1",
244 threadId: "t1",
245 message: {
246 messageId: "m1",
247 role: "user",
248 text: "hi",
249 attachments: [],
250 context: { version: 1, records: [knownRecords.image] },
251 },
252 createdAt: "2026-01-01T00:00:00.000Z",
253 });
254 expect(command.message.context?.records[0]?.kind).toBe("image");
255 });
256});