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 multiple models from multiple providers", () => {
9 const stdout = [
10 "anthropic/claude-sonnet-4-5",
11 JSON.stringify({ id: "claude-sonnet-4-5", providerID: "anthropic", name: "Sonnet 4.5" }),
12 "anthropic/claude-haiku-4-5",
13 JSON.stringify({ id: "claude-haiku-4-5", providerID: "anthropic", name: "Haiku 4.5" }),
14 "openai/gpt-4o",
15 JSON.stringify({ id: "gpt-4o", providerID: "openai", name: "GPT-4o" }),
16 ].join("\n");
17
18 const result = OpenCodeRuntime.parseModelsCliOutput(stdout);
19 NodeAssert.equal(result.providers.size, 2);
20 NodeAssert.equal(result.connected.length, 2);
21 NodeAssert.equal([...result.connected].sort().join(","), "anthropic,openai");
22 NodeAssert.equal(Object.keys(result.providers.get("anthropic")!.models).length, 2);
23 NodeAssert.equal(Object.keys(result.providers.get("openai")!.models).length, 1);
24 });
25
26 it("skips unparseable JSON blocks", () => {
27 const stdout = [
28 "anthropic/claude-sonnet-4-5",
29 "this is not valid json {{{",
30 "anthropic/claude-haiku-4-5",
31 JSON.stringify({ id: "claude-haiku-4-5", providerID: "anthropic", name: "Haiku 4.5" }),
32 ].join("\n");
33
34 const result = OpenCodeRuntime.parseModelsCliOutput(stdout);
35 NodeAssert.equal(result.providers.size, 1);
36 const provider = result.providers.get("anthropic")!;
37 NodeAssert.equal(Object.keys(provider.models).length, 1);
38 NodeAssert.ok(provider.models["claude-haiku-4-5"]);
39 });
40
41 it("handles Windows-style CRLF line endings", () => {
42 const stdout =
43 "anthropic/claude-sonnet-4-5\r\n" +
44 JSON.stringify({ id: "claude-sonnet-4-5", providerID: "anthropic", name: "Sonnet" }) +
45 "\r\n";
46
47 const result = OpenCodeRuntime.parseModelsCliOutput(stdout);
48 NodeAssert.equal(result.providers.size, 1);
49 NodeAssert.ok(result.providers.get("anthropic")!.models["claude-sonnet-4-5"]);
50 });
51
52 it("keeps a model whose JSON body has a slash and no interior whitespace", () => {
53 const stdout = [
54 "openrouter/qwen/qwen3-coder",
55 JSON.stringify({
56 id: "qwen/qwen3-coder",
57 providerID: "openrouter",
58 name: "qwen3-coder",
59 status: "active",
60 }),
61 ].join("\n");
62
63 const result = OpenCodeRuntime.parseModelsCliOutput(stdout);
64 NodeAssert.equal(result.providers.size, 1);
65 NodeAssert.deepEqual([...result.connected], ["openrouter"]);
66 const provider = result.providers.get("openrouter")!;
67 NodeAssert.ok(provider);
68 const model = provider.models["qwen/qwen3-coder"]!;
69 NodeAssert.ok(model);
70 NodeAssert.equal(model.id, "qwen/qwen3-coder");
71 NodeAssert.equal(model.providerID, "openrouter");
72 });
73});
74
75describe("parseAgentListCliOutput", () => {
76 it("skips agents with unparseable permission JSON", () => {
77 const stdout = [
78 "build (primary)",
79 " not valid json {",
80 "explore (subagent)",
81 " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]),
82 ].join("\n");
83
84 const result = OpenCodeRuntime.parseAgentListCliOutput(stdout);
85 NodeAssert.equal(result.length, 1);
86 NodeAssert.equal(result[0]!.name, "explore");
87 });
88
89 it("handles agent names with spaces", () => {
90 const stdout = [
91 "code reviewer (subagent)",
92 " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]),
93 "my custom agent (primary)",
94 " " + JSON.stringify([{ permission: "edit", action: "ask", pattern: "*.ts" }]),
95 ].join("\n");
96
97 const result = OpenCodeRuntime.parseAgentListCliOutput(stdout);
98 NodeAssert.equal(result.length, 2);
99 NodeAssert.equal(result[0]!.name, "code reviewer");
100 NodeAssert.equal(result[0]!.mode, "subagent");
101 NodeAssert.equal(result[1]!.name, "my custom agent");
102 NodeAssert.equal(result[1]!.mode, "primary");
103 });
104});
105
106describe("parseSkillsCliOutput", () => {
107 it("degrades malformed output to an empty skill list", () => {
108 NodeAssert.deepEqual(OpenCodeRuntime.parseSkillsCliOutput("not json"), []);
109 });
110});
111
112describe("toOpenCodeFileParts", () => {
113 const attachment = (mimeType: string, sizeBytes = 12) => ({
114 type: "file" as const,
115 id: "thread-1-00000000-0000-4000-8000-000000000001-bin",
116 name: "attachment",
117 mimeType,
118 sizeBytes,
119 });
120
121 it("sends supported images, text, and PDFs natively and skips what models reject", () => {
122 const parts = OpenCodeRuntime.toOpenCodeFileParts({
123 attachments: [
124 attachment("application/pdf"),
125 attachment("text/plain"),
126 attachment("image/png"),
127 // A ZIP file part makes OpenCode's Anthropic path throw before the
128 // turn starts; it must ride only as the prompt's file path line.
129 attachment("application/zip"),
130 // OpenCode inlines only text/plain itself. Other text types reach the
131 // model API as raw file parts, which Anthropic and OpenAI reject.
132 attachment("text/html"),
133 attachment("text/markdown"),
134 attachment("application/octet-stream"),
135
136 attachment("image/bmp"),
137 attachment("image/svg+xml"),
138
139 attachment("application/pdf", 21 * 1024 * 1024),
140 ],
141 resolveAttachmentPath: () => "/tmp/attachment",
142 });
143
144 NodeAssert.deepEqual(
145 parts.map((part) => part.mime),
146 ["application/pdf", "text/plain", "image/png"],
147 );
148 });
149
150 it("keeps folded clipboard text on the lazy path fallback", () => {
151 const parts = OpenCodeRuntime.toOpenCodeFileParts({
152 attachments: [
153 {
154 ...attachment("text/plain"),
155 source: { _tag: "pasted-text" as const },
156 },
157 ],
158 resolveAttachmentPath: () => "/tmp/pasted-text.txt",
159 });
160
161 NodeAssert.deepEqual(parts, []);
162 });
163});