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