|
| 1 | +import * as NodeAssert from "node:assert/strict"; |
| 2 | + |
| 3 | +import { describe, it } from "vite-plus/test"; |
| 4 | + |
| 5 | +import { parseModelsCliOutput, parseAgentListCliOutput } from "./opencodeRuntime.ts"; |
| 6 | + |
| 7 | +describe("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 = 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 = 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 = 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 = 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 = 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 = 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 | + |
| 130 | +describe("parseAgentListCliOutput", () => { |
| 131 | + it("parses a single agent", () => { |
| 132 | + const stdout = [ |
| 133 | + "build (primary)", |
| 134 | + " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]), |
| 135 | + ].join("\n"); |
| 136 | + |
| 137 | + const result = parseAgentListCliOutput(stdout); |
| 138 | + NodeAssert.equal(result.length, 1); |
| 139 | + NodeAssert.equal(result[0]!.name, "build"); |
| 140 | + NodeAssert.equal(result[0]!.mode, "primary"); |
| 141 | + NodeAssert.equal(result[0]!.permission.length, 1); |
| 142 | + }); |
| 143 | + |
| 144 | + it("parses multiple agents", () => { |
| 145 | + const stdout = [ |
| 146 | + "build (primary)", |
| 147 | + " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]), |
| 148 | + "explore (subagent)", |
| 149 | + " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]), |
| 150 | + "plan (primary)", |
| 151 | + " " + JSON.stringify([{ permission: "edit", action: "ask", pattern: "*.md" }]), |
| 152 | + ].join("\n"); |
| 153 | + |
| 154 | + const result = parseAgentListCliOutput(stdout); |
| 155 | + NodeAssert.equal(result.length, 3); |
| 156 | + NodeAssert.equal(result[0]!.name, "build"); |
| 157 | + NodeAssert.equal(result[0]!.mode, "primary"); |
| 158 | + NodeAssert.equal(result[1]!.name, "explore"); |
| 159 | + NodeAssert.equal(result[1]!.mode, "subagent"); |
| 160 | + NodeAssert.equal(result[2]!.name, "plan"); |
| 161 | + NodeAssert.equal(result[2]!.mode, "primary"); |
| 162 | + }); |
| 163 | + |
| 164 | + it("handles empty input", () => { |
| 165 | + const result = parseAgentListCliOutput(""); |
| 166 | + NodeAssert.equal(result.length, 0); |
| 167 | + }); |
| 168 | + |
| 169 | + it("skips agents with unparseable permission JSON", () => { |
| 170 | + const stdout = [ |
| 171 | + "build (primary)", |
| 172 | + " not valid json {", |
| 173 | + "explore (subagent)", |
| 174 | + " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]), |
| 175 | + ].join("\n"); |
| 176 | + |
| 177 | + const result = parseAgentListCliOutput(stdout); |
| 178 | + NodeAssert.equal(result.length, 1); |
| 179 | + NodeAssert.equal(result[0]!.name, "explore"); |
| 180 | + }); |
| 181 | + |
| 182 | + it("handles real-world permission blocks with nested paths", () => { |
| 183 | + const permissions = [ |
| 184 | + { permission: "*", action: "allow", pattern: "*" }, |
| 185 | + { |
| 186 | + permission: "external_directory", |
| 187 | + pattern: "C:\\Users\\test\\.local\\*", |
| 188 | + action: "allow", |
| 189 | + }, |
| 190 | + { permission: "read", pattern: "*.env", action: "ask" }, |
| 191 | + ]; |
| 192 | + const stdout = ["build (primary)", " " + JSON.stringify(permissions)].join("\n"); |
| 193 | + |
| 194 | + const result = parseAgentListCliOutput(stdout); |
| 195 | + NodeAssert.equal(result.length, 1); |
| 196 | + NodeAssert.equal(result[0]!.permission.length, 3); |
| 197 | + NodeAssert.equal(result[0]!.permission[0]!.action, "allow"); |
| 198 | + NodeAssert.equal(result[0]!.permission[2]!.action, "ask"); |
| 199 | + }); |
| 200 | + |
| 201 | + it("handles agent names with spaces", () => { |
| 202 | + const stdout = [ |
| 203 | + "code reviewer (subagent)", |
| 204 | + " " + JSON.stringify([{ permission: "read", action: "allow", pattern: "*" }]), |
| 205 | + "my custom agent (primary)", |
| 206 | + " " + JSON.stringify([{ permission: "edit", action: "ask", pattern: "*.ts" }]), |
| 207 | + ].join("\n"); |
| 208 | + |
| 209 | + const result = parseAgentListCliOutput(stdout); |
| 210 | + NodeAssert.equal(result.length, 2); |
| 211 | + NodeAssert.equal(result[0]!.name, "code reviewer"); |
| 212 | + NodeAssert.equal(result[0]!.mode, "subagent"); |
| 213 | + NodeAssert.equal(result[1]!.name, "my custom agent"); |
| 214 | + NodeAssert.equal(result[1]!.mode, "primary"); |
| 215 | + }); |
| 216 | + |
| 217 | + it("marks known hidden agents", () => { |
| 218 | + const stdout = [ |
| 219 | + "compaction (primary)", |
| 220 | + " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]), |
| 221 | + "build (primary)", |
| 222 | + " " + JSON.stringify([{ permission: "*", action: "allow", pattern: "*" }]), |
| 223 | + ].join("\n"); |
| 224 | + |
| 225 | + const result = parseAgentListCliOutput(stdout); |
| 226 | + NodeAssert.equal(result[0]!.hidden, true); |
| 227 | + NodeAssert.equal(result[1]!.hidden, false); |
| 228 | + }); |
| 229 | +}); |
0 commit comments