Open History to see the last commit
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
159describe("parseAgentListCliOutput", () => {
160 it("parses a single agent", () => {
161 const stdout = [
162 "build (primary)",
163 " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]),
164 ].join("\n");
165
166 const result = parseAgentListCliOutput(stdout);
167 NodeAssert.equal(result.length, 1);
168 NodeAssert.equal(result[0]!.name, "build");
169 NodeAssert.equal(result[0]!.mode, "primary");
170 NodeAssert.equal(result[0]!.permission.length, 1);
171 });
172
173 it("parses multiple agents", () => {
174 const stdout = [
175 "build (primary)",
176 " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]),
177 "explore (subagent)",
178 " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]),
179 "plan (primary)",
180 " " + JSON.stringify([{ permission: "edit", action: "ask", pattern: "*.md" }]),
181 ].join("\n");
182
183 const result = parseAgentListCliOutput(stdout);
184 NodeAssert.equal(result.length, 3);
185 NodeAssert.equal(result[0]!.name, "build");
186 NodeAssert.equal(result[0]!.mode, "primary");
187 NodeAssert.equal(result[1]!.name, "explore");
188 NodeAssert.equal(result[1]!.mode, "subagent");
189 NodeAssert.equal(result[2]!.name, "plan");
190 NodeAssert.equal(result[2]!.mode, "primary");
191 });
192
193 it("handles empty input", () => {
194 const result = parseAgentListCliOutput("");
195 NodeAssert.equal(result.length, 0);
196 });
197
198 it("skips agents with unparseable permission JSON", () => {
199 const stdout = [
200 "build (primary)",
201 " not valid json {",
202 "explore (subagent)",
203 " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]),
204 ].join("\n");
205
206 const result = parseAgentListCliOutput(stdout);
207 NodeAssert.equal(result.length, 1);
208 NodeAssert.equal(result[0]!.name, "explore");
209 });
210
211 it("handles real-world permission blocks with nested paths", () => {
212 const permissions = [
213 { permission: "*", action: "allow", pattern: "*" },
214 {
215 permission: "external_directory",
216 pattern: "C:\\Users\\test\\.local\\*",
217 action: "allow",
218 },
219 { permission: "read", pattern: "*.env", action: "ask" },
220 ];
221 const stdout = ["build (primary)", " " + JSON.stringify(permissions)].join("\n");
222
223 const result = parseAgentListCliOutput(stdout);
224 NodeAssert.equal(result.length, 1);
225 NodeAssert.equal(result[0]!.permission.length, 3);
226 NodeAssert.equal(result[0]!.permission[0]!.action, "allow");
227 NodeAssert.equal(result[0]!.permission[2]!.action, "ask");
228 });
229
230 it("handles agent names with spaces", () => {
231 const stdout = [
232 "code reviewer (subagent)",
233 " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]),
234 "my custom agent (primary)",
235 " " + JSON.stringify([{ permission: "edit", action: "ask", pattern: "*.ts" }]),
236 ].join("\n");
237
238 const result = parseAgentListCliOutput(stdout);
239 NodeAssert.equal(result.length, 2);
240 NodeAssert.equal(result[0]!.name, "code reviewer");
241 NodeAssert.equal(result[0]!.mode, "subagent");
242 NodeAssert.equal(result[1]!.name, "my custom agent");
243 NodeAssert.equal(result[1]!.mode, "primary");
244 });
245
246 it("marks known hidden agents", () => {
247 const stdout = [
248 "compaction (primary)",
249 " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]),
250 "build (primary)",
251 " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]),
252 ].join("\n");
253
254 const result = parseAgentListCliOutput(stdout);
255 NodeAssert.equal(result[0]!.hidden, true);
256 NodeAssert.equal(result[1]!.hidden, false);
257 });
258});
259
260describe("parseSkillsCliOutput", () => {
261 it("parses skill metadata from the CLI JSON output", () => {
262 const result = parseSkillsCliOutput(
263 JSON.stringify([
264 {
265 name: "review-pr",
266 description: "Review a pull request.",
267 location: "/tmp/review-pr/SKILL.md",
268 content: "---\nname: review-pr\n---\n",
269 },
270 ]),
271 );
272
273 NodeAssert.deepEqual(result, [
274 {
275 name: "review-pr",
276 description: "Review a pull request.",
277 location: "/tmp/review-pr/SKILL.md",
278 content: "---\nname: review-pr\n---\n",
279 },
280 ]);
281 });
282
283 it("degrades malformed output to an empty skill list", () => {
284 NodeAssert.deepEqual(parseSkillsCliOutput("not json"), []);
285 });
286});