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 toOpenCodeFileParts,
10} from "./opencodeRuntime.ts";
11
12describe("parseModelsCliOutput", () => {
13 it("parses a single model from a single provider", () => {
14 const stdout = [
15 "anthropic/claude-sonnet-4-5",
16 JSON.stringify({
17 id: "claude-sonnet-4-5",
18 providerID: "anthropic",
19 name: "Claude Sonnet 4.5",
20 capabilities: { temperature: true, reasoning: true, toolcall: true },
21 cost: { input: 3, output: 15 },
22 limit: { context: 200000, output: 8192 },
23 status: "active",
24 options: {},
25 headers: {},
26 release_date: "2025-01-01",
27 }),
28 ].join("\n");
29
30 const result = parseModelsCliOutput(stdout);
31 NodeAssert.equal(result.providers.size, 1);
32 NodeAssert.equal(result.connected.length, 1);
33 NodeAssert.equal(result.connected[0], "anthropic");
34
35 const provider = result.providers.get("anthropic")!;
36 NodeAssert.ok(provider);
37 NodeAssert.equal(provider.id, "anthropic");
38 NodeAssert.equal(provider.name, "anthropic");
39 NodeAssert.equal(Object.keys(provider.models).length, 1);
40
41 const model = provider.models["claude-sonnet-4-5"]!;
42 NodeAssert.ok(model);
43 NodeAssert.equal(model.id, "claude-sonnet-4-5");
44 NodeAssert.equal(model.providerID, "anthropic");
45 NodeAssert.equal(model.name, "Claude Sonnet 4.5");
46 });
47
48 it("parses multiple models from multiple providers", () => {
49 const stdout = [
50 "anthropic/claude-sonnet-4-5",
51 JSON.stringify({ id: "claude-sonnet-4-5", providerID: "anthropic", name: "Sonnet 4.5" }),
52 "anthropic/claude-haiku-4-5",
53 JSON.stringify({ id: "claude-haiku-4-5", providerID: "anthropic", name: "Haiku 4.5" }),
54 "openai/gpt-4o",
55 JSON.stringify({ id: "gpt-4o", providerID: "openai", name: "GPT-4o" }),
56 ].join("\n");
57
58 const result = parseModelsCliOutput(stdout);
59 NodeAssert.equal(result.providers.size, 2);
60 NodeAssert.equal(result.connected.length, 2);
61 NodeAssert.equal([...result.connected].sort().join(","), "anthropic,openai");
62 NodeAssert.equal(Object.keys(result.providers.get("anthropic")!.models).length, 2);
63 NodeAssert.equal(Object.keys(result.providers.get("openai")!.models).length, 1);
64 });
65
66 it("handles empty input", () => {
67 const result = parseModelsCliOutput("");
68 NodeAssert.equal(result.providers.size, 0);
69 NodeAssert.equal(result.connected.length, 0);
70 });
71
72 it("skips unparseable JSON blocks", () => {
73 const stdout = [
74 "anthropic/claude-sonnet-4-5",
75 "this is not valid json {{{",
76 "anthropic/claude-haiku-4-5",
77 JSON.stringify({ id: "claude-haiku-4-5", providerID: "anthropic", name: "Haiku 4.5" }),
78 ].join("\n");
79
80 const result = parseModelsCliOutput(stdout);
81 NodeAssert.equal(result.providers.size, 1);
82 const provider = result.providers.get("anthropic")!;
83 NodeAssert.equal(Object.keys(provider.models).length, 1);
84 NodeAssert.ok(provider.models["claude-haiku-4-5"]);
85 });
86
87 it("handles Windows-style CRLF line endings", () => {
88 const stdout =
89 "anthropic/claude-sonnet-4-5\r\n" +
90 JSON.stringify({ id: "claude-sonnet-4-5", providerID: "anthropic", name: "Sonnet" }) +
91 "\r\n";
92
93 const result = parseModelsCliOutput(stdout);
94 NodeAssert.equal(result.providers.size, 1);
95 NodeAssert.ok(result.providers.get("anthropic")!.models["claude-sonnet-4-5"]);
96 });
97
98 it("handles model JSON with variants and nested fields", () => {
99 const stdout = [
100 "opencode/gpt-5.4",
101 JSON.stringify({
102 id: "gpt-5.4",
103 providerID: "opencode",
104 name: "GPT-5.4",
105 family: "gpt",
106 capabilities: {
107 temperature: true,
108 reasoning: true,
109 attachment: false,
110 toolcall: true,
111 input: { text: true, audio: false, image: false, video: false, pdf: false },
112 output: { text: true, audio: false, image: false, video: false, pdf: false },
113 interleaved: false,
114 },
115 cost: { input: 0, output: 0, cache: { read: 0, write: 0 } },
116 limit: { context: 200000, input: 160000, output: 32000 },
117 status: "active",
118 options: {},
119 headers: {},
120 release_date: "2025-01-01",
121 variants: { none: {}, low: {}, medium: {}, high: {} },
122 }),
123 ].join("\n");
124
125 const result = parseModelsCliOutput(stdout);
126 const model = result.providers.get("opencode")!.models["gpt-5.4"]!;
127 NodeAssert.ok(model);
128 NodeAssert.ok(model.capabilities);
129 NodeAssert.equal(model.capabilities!.reasoning, true);
130 NodeAssert.ok(model.variants);
131 NodeAssert.equal(model.variants!["medium"] !== undefined, true);
132 });
133
134 it("keeps a model whose JSON body has a slash and no interior whitespace", () => {
135 // OpenRouter-style: the model id contains a `/` and no string value has a
136 // space, so the JSON body line itself matches the slug regex. It must still
137 // be treated as the body of the preceding slug, not a new slug.
138 const stdout = [
139 "openrouter/qwen/qwen3-coder",
140 JSON.stringify({
141 id: "qwen/qwen3-coder",
142 providerID: "openrouter",
143 name: "qwen3-coder",
144 status: "active",
145 }),
146 ].join("\n");
147
148 const result = parseModelsCliOutput(stdout);
149 NodeAssert.equal(result.providers.size, 1);
150 NodeAssert.deepEqual([...result.connected], ["openrouter"]);
151 const provider = result.providers.get("openrouter")!;
152 NodeAssert.ok(provider);
153 const model = provider.models["qwen/qwen3-coder"]!;
154 NodeAssert.ok(model);
155 NodeAssert.equal(model.id, "qwen/qwen3-coder");
156 NodeAssert.equal(model.providerID, "openrouter");
157 });
158});
159
160describe("parseAgentListCliOutput", () => {
161 it("parses a single agent", () => {
162 const stdout = [
163 "build (primary)",
164 " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]),
165 ].join("\n");
166
167 const result = parseAgentListCliOutput(stdout);
168 NodeAssert.equal(result.length, 1);
169 NodeAssert.equal(result[0]!.name, "build");
170 NodeAssert.equal(result[0]!.mode, "primary");
171 NodeAssert.equal(result[0]!.permission.length, 1);
172 });
173
174 it("parses multiple agents", () => {
175 const stdout = [
176 "build (primary)",
177 " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]),
178 "explore (subagent)",
179 " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]),
180 "plan (primary)",
181 " " + JSON.stringify([{ permission: "edit", action: "ask", pattern: "*.md" }]),
182 ].join("\n");
183
184 const result = parseAgentListCliOutput(stdout);
185 NodeAssert.equal(result.length, 3);
186 NodeAssert.equal(result[0]!.name, "build");
187 NodeAssert.equal(result[0]!.mode, "primary");
188 NodeAssert.equal(result[1]!.name, "explore");
189 NodeAssert.equal(result[1]!.mode, "subagent");
190 NodeAssert.equal(result[2]!.name, "plan");
191 NodeAssert.equal(result[2]!.mode, "primary");
192 });
193
194 it("handles empty input", () => {
195 const result = parseAgentListCliOutput("");
196 NodeAssert.equal(result.length, 0);
197 });
198
199 it("skips agents with unparseable permission JSON", () => {
200 const stdout = [
201 "build (primary)",
202 " not valid json {",
203 "explore (subagent)",
204 " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]),
205 ].join("\n");
206
207 const result = parseAgentListCliOutput(stdout);
208 NodeAssert.equal(result.length, 1);
209 NodeAssert.equal(result[0]!.name, "explore");
210 });
211
212 it("handles real-world permission blocks with nested paths", () => {
213 const permissions = [
214 { permission: "*", action: "allow", pattern: "*" },
215 {
216 permission: "external_directory",
217 pattern: "C:\\Users\\test\\.local\\*",
218 action: "allow",
219 },
220 { permission: "read", pattern: "*.env", action: "ask" },
221 ];
222 const stdout = ["build (primary)", " " + JSON.stringify(permissions)].join("\n");
223
224 const result = parseAgentListCliOutput(stdout);
225 NodeAssert.equal(result.length, 1);
226 NodeAssert.equal(result[0]!.permission.length, 3);
227 NodeAssert.equal(result[0]!.permission[0]!.action, "allow");
228 NodeAssert.equal(result[0]!.permission[2]!.action, "ask");
229 });
230
231 it("handles agent names with spaces", () => {
232 const stdout = [
233 "code reviewer (subagent)",
234 " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]),
235 "my custom agent (primary)",
236 " " + JSON.stringify([{ permission: "edit", action: "ask", pattern: "*.ts" }]),
237 ].join("\n");
238
239 const result = parseAgentListCliOutput(stdout);
240 NodeAssert.equal(result.length, 2);
241 NodeAssert.equal(result[0]!.name, "code reviewer");
242 NodeAssert.equal(result[0]!.mode, "subagent");
243 NodeAssert.equal(result[1]!.name, "my custom agent");
244 NodeAssert.equal(result[1]!.mode, "primary");
245 });
246
247 it("marks known hidden agents", () => {
248 const stdout = [
249 "compaction (primary)",
250 " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]),
251 "build (primary)",
252 " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]),
253 ].join("\n");
254
255 const result = parseAgentListCliOutput(stdout);
256 NodeAssert.equal(result[0]!.hidden, true);
257 NodeAssert.equal(result[1]!.hidden, false);
258 });
259});
260
261describe("parseSkillsCliOutput", () => {
262 it("parses only skill metadata from the CLI JSON output", () => {
263 const result = parseSkillsCliOutput(
264 JSON.stringify([
265 {
266 name: "review-pr",
267 description: "Review a pull request.",
268 location: "/tmp/review-pr/SKILL.md",
269 content: "---\nname: review-pr\n---\n",
270 },
271 ]),
272 );
273
274 NodeAssert.deepEqual(result, [
275 {
276 name: "review-pr",
277 description: "Review a pull request.",
278 location: "/tmp/review-pr/SKILL.md",
279 },
280 ]);
281 });
282
283 it("degrades malformed output to an empty skill list", () => {
284 NodeAssert.deepEqual(parseSkillsCliOutput("not json"), []);
285 });
286});
287
288describe("toOpenCodeFileParts", () => {
289 const attachment = (mimeType: string, sizeBytes = 12) => ({
290 type: "file" as const,
291 id: "thread-1-00000000-0000-4000-8000-000000000001-bin",
292 name: "attachment",
293 mimeType,
294 sizeBytes,
295 });
296
297 it("sends supported images, text, and PDFs natively and skips what models reject", () => {
298 const parts = toOpenCodeFileParts({
299 attachments: [
300 attachment("application/pdf"),
301 attachment("text/markdown"),
302 attachment("image/png"),
303 // A ZIP file part makes OpenCode's Anthropic path throw before the
304 // turn starts; it must ride only as the prompt's file path line.
305 attachment("application/zip"),
306 attachment("application/octet-stream"),
307 // Image formats the model APIs reject stay on the fallback path too.
308 attachment("image/bmp"),
309 attachment("image/svg+xml"),
310 // Over the direct-attachment limit: path fallback even for a PDF.
311 attachment("application/pdf", 21 * 1024 * 1024),
312 ],
313 resolveAttachmentPath: () => "/tmp/attachment",
314 });
315
316 NodeAssert.deepEqual(
317 parts.map((part) => part.mime),
318 ["application/pdf", "text/markdown", "image/png"],
319 );
320 });
321
322 it("keeps folded clipboard text on the lazy path fallback", () => {
323 const parts = toOpenCodeFileParts({
324 attachments: [
325 {
326 ...attachment("text/plain"),
327 source: { _tag: "pasted-text" as const },
328 },
329 ],
330 resolveAttachmentPath: () => "/tmp/pasted-text.txt",
331 });
332
333 NodeAssert.deepEqual(parts, []);
334 });
335});