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

opencodeRuntime.cliParsers.test.ts

348 lines12.6 KB

1import * as NodeAssert from "node:assert/strict";
2​
3import { describe, it } from "vite-plus/test";
4​
5import {
6 parseAgentListCliOutput,
7 parseModelsCliOutput,
8 parseSkillsCliOutput,
9} from "./opencodeRuntime.ts";
10​
11describe("parseModelsCliOutput", () => {
12 it("parses a single model from a single provider", () => {
13 const stdout = [
14 "anthropic/claude-sonnet-4-5",
15 JSON.stringify({
16 id: "claude-sonnet-4-5",
17 providerID: "anthropic",
18 name: "Claude Sonnet 4.5",
19 capabilities: { temperature: true, reasoning: true, toolcall: true },
20 cost: { input: 3, output: 15 },
21 limit: { context: 200000, output: 8192 },
22 status: "active",
23 options: {},
24 headers: {},
25 release_date: "2025-01-01",
26 }),
27 ].join("\n");
28​
29 const result = parseModelsCliOutput(stdout);
30 NodeAssert.equal(result.providers.size, 1);
31 NodeAssert.equal(result.connected.length, 1);
32 NodeAssert.equal(result.connected[0], "anthropic");
33​
34 const provider = result.providers.get("anthropic")!;
35 NodeAssert.ok(provider);
36 NodeAssert.equal(provider.id, "anthropic");
37 NodeAssert.equal(provider.name, "anthropic");
38 NodeAssert.equal(Object.keys(provider.models).length, 1);
39​
40 const model = provider.models["claude-sonnet-4-5"]!;
41 NodeAssert.ok(model);
42 NodeAssert.equal(model.id, "claude-sonnet-4-5");
43 NodeAssert.equal(model.providerID, "anthropic");
44 NodeAssert.equal(model.name, "Claude Sonnet 4.5");
45 });
46​
47 it("parses multiple models from multiple providers", () => {
48 const stdout = [
49 "anthropic/claude-sonnet-4-5",
50 JSON.stringify({ id: "claude-sonnet-4-5", providerID: "anthropic", name: "Sonnet 4.5" }),
51 "anthropic/claude-haiku-4-5",
52 JSON.stringify({ id: "claude-haiku-4-5", providerID: "anthropic", name: "Haiku 4.5" }),
53 "openai/gpt-4o",
54 JSON.stringify({ id: "gpt-4o", providerID: "openai", name: "GPT-4o" }),
55 ].join("\n");
56​
57 const result = parseModelsCliOutput(stdout);
58 NodeAssert.equal(result.providers.size, 2);
59 NodeAssert.equal(result.connected.length, 2);
60 NodeAssert.equal([...result.connected].sort().join(","), "anthropic,openai");
61 NodeAssert.equal(Object.keys(result.providers.get("anthropic")!.models).length, 2);
62 NodeAssert.equal(Object.keys(result.providers.get("openai")!.models).length, 1);
63 });
64​
65 it("handles empty input", () => {
66 const result = parseModelsCliOutput("");
67 NodeAssert.equal(result.providers.size, 0);
68 NodeAssert.equal(result.connected.length, 0);
69 });
70​
71 it("skips unparseable JSON blocks", () => {
72 const stdout = [
73 "anthropic/claude-sonnet-4-5",
74 "this is not valid json {{{",
75 "anthropic/claude-haiku-4-5",
76 JSON.stringify({ id: "claude-haiku-4-5", providerID: "anthropic", name: "Haiku 4.5" }),
77 ].join("\n");
78​
79 const result = parseModelsCliOutput(stdout);
80 NodeAssert.equal(result.providers.size, 1);
81 const provider = result.providers.get("anthropic")!;
82 NodeAssert.equal(Object.keys(provider.models).length, 1);
83 NodeAssert.ok(provider.models["claude-haiku-4-5"]);
84 });
85​
86 it("handles Windows-style CRLF line endings", () => {
87 const stdout =
88 "anthropic/claude-sonnet-4-5\r\n" +
89 JSON.stringify({ id: "claude-sonnet-4-5", providerID: "anthropic", name: "Sonnet" }) +
90 "\r\n";
91​
92 const result = parseModelsCliOutput(stdout);
93 NodeAssert.equal(result.providers.size, 1);
94 NodeAssert.ok(result.providers.get("anthropic")!.models["claude-sonnet-4-5"]);
95 });
96​
97 it("handles model JSON with variants and nested fields", () => {
98 const stdout = [
99 "opencode/gpt-5.4",
100 JSON.stringify({
101 id: "gpt-5.4",
102 providerID: "opencode",
103 name: "GPT-5.4",
104 family: "gpt",
105 capabilities: {
106 temperature: true,
107 reasoning: true,
108 attachment: false,
109 toolcall: true,
110 input: { text: true, audio: false, image: false, video: false, pdf: false },
111 output: { text: true, audio: false, image: false, video: false, pdf: false },
112 interleaved: false,
113 },
114 cost: { input: 0, output: 0, cache: { read: 0, write: 0 } },
115 limit: { context: 200000, input: 160000, output: 32000 },
116 status: "active",
117 options: {},
118 headers: {},
119 release_date: "2025-01-01",
120 variants: { none: {}, low: {}, medium: {}, high: {} },
121 }),
122 ].join("\n");
123​
124 const result = parseModelsCliOutput(stdout);
125 const model = result.providers.get("opencode")!.models["gpt-5.4"]!;
126 NodeAssert.ok(model);
127 NodeAssert.ok(model.capabilities);
128 NodeAssert.equal(model.capabilities!.reasoning, true);
129 NodeAssert.ok(model.variants);
130 NodeAssert.equal(model.variants!["medium"] !== undefined, true);
131 });
132​
133 it("keeps a model whose JSON body has a slash and no interior whitespace", () => {
134 // OpenRouter-style: the model id contains a `/` and no string value has a
135 // space, so the JSON body line itself matches the slug regex. It must still
136 // be treated as the body of the preceding slug, not a new slug.
137 const stdout = [
138 "openrouter/qwen/qwen3-coder",
139 JSON.stringify({
140 id: "qwen/qwen3-coder",
141 providerID: "openrouter",
142 name: "qwen3-coder",
143 status: "active",
144 }),
145 ].join("\n");
146​
147 const result = parseModelsCliOutput(stdout);
148 NodeAssert.equal(result.providers.size, 1);
149 NodeAssert.deepEqual([...result.connected], ["openrouter"]);
150 const provider = result.providers.get("openrouter")!;
151 NodeAssert.ok(provider);
152 const model = provider.models["qwen/qwen3-coder"]!;
153 NodeAssert.ok(model);
154 NodeAssert.equal(model.id, "qwen/qwen3-coder");
155 NodeAssert.equal(model.providerID, "openrouter");
156 });
157​
158 it("strips OSC title escapes from model slugs (opencode CLI leak)", () => {
159 const stdout = [
160 "\x1b]0;t3code: ready\x07opencode/big-pickle",
161 JSON.stringify({ id: "big-pickle", providerID: "opencode", name: "Big Pickle" }),
162 "\x1b]0;tmp: ready\x07anthropic/claude-sonnet-4-5",
163 JSON.stringify({ id: "claude-sonnet-4-5", providerID: "anthropic", name: "Sonnet" }),
164 ].join("\n");
165​
166 const result = parseModelsCliOutput(stdout);
167 NodeAssert.equal(result.providers.size, 2);
168 NodeAssert.ok(result.providers.get("opencode")!.models["big-pickle"]);
169 NodeAssert.ok(result.providers.get("anthropic")!.models["claude-sonnet-4-5"]);
170 });
171​
172 it("strips ANSI escapes from model slugs", () => {
173 const stdout = [
174 "\x1b[33mopencode/gpt-5.4\x1b[0m",
175 JSON.stringify({ id: "gpt-5.4", providerID: "opencode", name: "GPT-5.4" }),
176 ].join("\n");
177​
178 const result = parseModelsCliOutput(stdout);
179 NodeAssert.ok(result.providers.get("opencode")!.models["gpt-5.4"]);
180 });
181});
182​
183describe("parseAgentListCliOutput", () => {
184 it("parses a single agent", () => {
185 const stdout = [
186 "build (primary)",
187 " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]),
188 ].join("\n");
189​
190 const result = parseAgentListCliOutput(stdout);
191 NodeAssert.equal(result.length, 1);
192 NodeAssert.equal(result[0]!.name, "build");
193 NodeAssert.equal(result[0]!.mode, "primary");
194 NodeAssert.equal(result[0]!.permission.length, 1);
195 });
196​
197 it("parses multiple agents", () => {
198 const stdout = [
199 "build (primary)",
200 " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]),
201 "explore (subagent)",
202 " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]),
203 "plan (primary)",
204 " " + JSON.stringify([{ permission: "edit", action: "ask", pattern: "*.md" }]),
205 ].join("\n");
206​
207 const result = parseAgentListCliOutput(stdout);
208 NodeAssert.equal(result.length, 3);
209 NodeAssert.equal(result[0]!.name, "build");
210 NodeAssert.equal(result[0]!.mode, "primary");
211 NodeAssert.equal(result[1]!.name, "explore");
212 NodeAssert.equal(result[1]!.mode, "subagent");
213 NodeAssert.equal(result[2]!.name, "plan");
214 NodeAssert.equal(result[2]!.mode, "primary");
215 });
216​
217 it("handles empty input", () => {
218 const result = parseAgentListCliOutput("");
219 NodeAssert.equal(result.length, 0);
220 });
221​
222 it("skips agents with unparseable permission JSON", () => {
223 const stdout = [
224 "build (primary)",
225 " not valid json {",
226 "explore (subagent)",
227 " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]),
228 ].join("\n");
229​
230 const result = parseAgentListCliOutput(stdout);
231 NodeAssert.equal(result.length, 1);
232 NodeAssert.equal(result[0]!.name, "explore");
233 });
234​
235 it("handles real-world permission blocks with nested paths", () => {
236 const permissions = [
237 { permission: "*", action: "allow", pattern: "*" },
238 {
239 permission: "external_directory",
240 pattern: "C:\\Users\\test\\.local\\*",
241 action: "allow",
242 },
243 { permission: "read", pattern: "*.env", action: "ask" },
244 ];
245 const stdout = ["build (primary)", " " + JSON.stringify(permissions)].join("\n");
246​
247 const result = parseAgentListCliOutput(stdout);
248 NodeAssert.equal(result.length, 1);
249 NodeAssert.equal(result[0]!.permission.length, 3);
250 NodeAssert.equal(result[0]!.permission[0]!.action, "allow");
251 NodeAssert.equal(result[0]!.permission[2]!.action, "ask");
252 });
253​
254 it("handles agent names with spaces", () => {
255 const stdout = [
256 "code reviewer (subagent)",
257 " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]),
258 "my custom agent (primary)",
259 " " + JSON.stringify([{ permission: "edit", action: "ask", pattern: "*.ts" }]),
260 ].join("\n");
261​
262 const result = parseAgentListCliOutput(stdout);
263 NodeAssert.equal(result.length, 2);
264 NodeAssert.equal(result[0]!.name, "code reviewer");
265 NodeAssert.equal(result[0]!.mode, "subagent");
266 NodeAssert.equal(result[1]!.name, "my custom agent");
267 NodeAssert.equal(result[1]!.mode, "primary");
268 });
269​
270 it("marks known hidden agents", () => {
271 const stdout = [
272 "compaction (primary)",
273 " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]),
274 "build (primary)",
275 " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]),
276 ].join("\n");
277​
278 const result = parseAgentListCliOutput(stdout);
279 NodeAssert.equal(result[0]!.hidden, true);
280 NodeAssert.equal(result[1]!.hidden, false);
281 });
282​
283 it("strips OSC title escapes leaked by opencode CLI", () => {
284 // opencode <=1.18 writes `ESC ]0;<cwd>: ready BEL` to stdout for every
285 // non-help command — even when stdout is a pipe. Without stripping, the
286 // agent name becomes `ESC]0;...BELbuild` and later fails with
287 // `Agent not found: "ESC]0;...build"`.
288 const stdout = [
289 "\x1b]0;t3code: ready\x07build (primary)",
290 " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]),
291 "\x1b]0;tmp: ready\x07explore (subagent)",
292 " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]),
293 ].join("\n");
294​
295 const result = parseAgentListCliOutput(stdout);
296 NodeAssert.equal(result.length, 2);
297 NodeAssert.equal(result[0]!.name, "build");
298 NodeAssert.equal(result[0]!.mode, "primary");
299 NodeAssert.equal(result[1]!.name, "explore");
300 NodeAssert.equal(result[1]!.mode, "subagent");
301 });
302​
303 it("strips ANSI CSI color escapes from agent headers", () => {
304 const stdout = [
305 "\x1b[31mbuild (primary)\x1b[0m",
306 " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]),
307 ].join("\n");
308​
309 const result = parseAgentListCliOutput(stdout);
310 NodeAssert.equal(result.length, 1);
311 NodeAssert.equal(result[0]!.name, "build");
312 });
313});
314​
315describe("parseSkillsCliOutput", () => {
316 it("strips OSC escapes before JSON parsing (opencode CLI leak)", () => {
317 const polluted = "\x1b]0;tmp: ready\x07" + JSON.stringify([{ name: "review-pr", location: "/tmp/x", description: "d", content: "c" }]);
318 const result = parseSkillsCliOutput(polluted);
319 NodeAssert.equal(result.length, 1);
320 NodeAssert.equal(result[0]!.name, "review-pr");
321 });
322​
323 it("parses skill metadata from the CLI JSON output", () => {
324 const result = parseSkillsCliOutput(
325 JSON.stringify([
326 {
327 name: "review-pr",
328 description: "Review a pull request.",
329 location: "/tmp/review-pr/SKILL.md",
330 content: "---\nname: review-pr\n---\n",
331 },
332 ]),
333 );
334​
335 NodeAssert.deepEqual(result, [
336 {
337 name: "review-pr",
338 description: "Review a pull request.",
339 location: "/tmp/review-pr/SKILL.md",
340 content: "---\nname: review-pr\n---\n",
341 },
342 ]);
343 });
344​
345 it("degrades malformed output to an empty skill list", () => {
346 NodeAssert.deepEqual(parseSkillsCliOutput("not json"), []);
347 });
348});