Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

OpenCodeRuntime.cliParsers.test.ts

161 lines5.7 KB

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 // ZIP file parts fail Anthropic before turn start; send only the prompt's path line.
128 attachment("application/zip"),
129 // OpenCode inlines only text/plain; other text MIME parts fail Anthropic/OpenAI.
130 attachment("text/html"),
131 attachment("text/markdown"),
132 attachment("application/octet-stream"),
133​
134 attachment("image/bmp"),
135 attachment("image/svg+xml"),
136​
137 attachment("application/pdf", 21 * 1024 * 1024),
138 ],
139 resolveAttachmentPath: () => "/tmp/attachment",
140 });
141​
142 NodeAssert.deepEqual(
143 parts.map((part) => part.mime),
144 ["application/pdf", "text/plain", "image/png"],
145 );
146 });
147​
148 it("keeps folded clipboard text on the lazy path fallback", () => {
149 const parts = OpenCodeRuntime.toOpenCodeFileParts({
150 attachments: [
151 {
152 ...attachment("text/plain"),
153 source: { _tag: "pasted-text" as const },
154 },
155 ],
156 resolveAttachmentPath: () => "/tmp/pasted-text.txt",
157 });
158​
159 NodeAssert.deepEqual(parts, []);
160 });
161});