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";
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 environment: {
116 ...base,
117 kind: "environment",
118 label: "vps",
119 environmentId: "environment-vps",
120 },
121};
122
123describe("ComposerContextRecord", () => {
124 it("keeps unknown kinds with their payload", () => {
125 const decoded = decodeRecord({
126 ...base,
127 kind: "future-thing",
128 label: "Future",
129 payload: { anything: [1, 2, 3] },
130 });
131 expect(Option.getOrThrow(decoded)).toEqual({
132 version: 1,
133 contextId: "ctx_1",
134 kind: "future-thing",
135 label: "Future",
136 payload: { anything: [1, 2, 3] },
137 });
138 });
139
140 it("does not let a malformed known kind slide through as unknown", () => {
141 expect(Option.isNone(decodeRecord({ ...base, kind: "image", label: "x" }))).toBe(true);
142 });
143
144 it("bounds the serialized payload of future context kinds", () => {
145 const unknown = { ...base, kind: "future-thing", label: "Future" };
146 expect(Option.isSome(decodeRecord({ ...unknown, payload: "x".repeat(63_998) }))).toBe(true);
147 expect(Option.isNone(decodeRecord({ ...unknown, payload: "x".repeat(63_999) }))).toBe(true);
148 expect(Option.isNone(decodeRecord({ ...unknown, payload: '"'.repeat(32_000) }))).toBe(true);
149 expect(
150 decodeContext({
151 version: 1,
152 records: [
153 { ...unknown, payload: "x".repeat(64_000) },
154 { ...knownRecords.skill, contextId: "ctx_skill" },
155 ],
156 }).records,
157 ).toEqual([{ ...knownRecords.skill, contextId: "ctx_skill" }]);
158 });
159});
160
161describe("OrchestrationMessageContext", () => {
162 it("rejects aggregate context size even when each record is valid", () => {
163 const record = {
164 ...knownRecords["preview-annotation"],
165 styleChangeDetails: Array.from({ length: 200 }, () => ({
166 targetId: "target",
167 selector: null,
168 property: "content",
169 previousValue: "x".repeat(8_000),
170 value: "y".repeat(8_000),
171 })),
172 };
173 expect(Option.isSome(decodeRecord(record))).toBe(true);
174 expect(() => decodeContext({ version: 1, records: [record] })).not.toThrow();
175 expect(() =>
176 decodeContext({
177 version: 1,
178 records: Array.from({ length: 6 }, (_, index) => ({
179 ...record,
180 contextId: `ctx_${index}`,
181 })),
182 }),
183 ).toThrow();
184 });
185
186 it("sends a message without the records the wire cannot carry", () => {
187 const encodeOrchestrationMessageContext = Schema.encodeUnknownSync(
188 Schema.toCodecJson(OrchestrationMessageContext),
189 );
190
191 const wire = encodeOrchestrationMessageContext({
192 version: 1,
193 records: [
194 decodeContext({ version: 1, records: [knownRecords.terminal] }).records[0],
195 {
196 ...base,
197 contextId: "ctx_2",
198 kind: "future-kind",
199 label: "x",
200 payload: { count: Number.NaN },
201 },
202 { ...knownRecords["review-comment"], contextId: "ctx_3", fenceLanguage: undefined },
203 { ...base, contextId: "ctx_4", kind: "future-kind", label: "y", payload: { n: 1n } },
204 ],
205 });
206 expect(decodeContext(wire).records.map((record) => record.contextId)).toEqual(["ctx_1"]);
207 });
208
209 it("rejects oversized arrays before dropping malformed records", () => {
210 expect(() =>
211 decodeContext({ version: 1, records: Array.from({ length: 201 }, () => ({})) }),
212 ).toThrow();
213 });
214
215 it("drops undecodable records and keeps valid siblings", () => {
216 const context = decodeContext({
217 version: 1,
218 records: [
219 knownRecords.skill,
220 { version: 1, kind: "image" },
221 { ...knownRecords.terminal, contextId: "ctx_2" },
222 ],
223 });
224 expect(context.records.map((record) => record.kind)).toEqual(["skill", "terminal"]);
225 });
226
227 it("rejects duplicate normalized context identities", () => {
228 expect(() =>
229 decodeContext({
230 version: 1,
231 records: [knownRecords.skill, { ...knownRecords.terminal, contextId: " ctx_1 " }],
232 }),
233 ).toThrow();
234 });
235});