Open History to see the last commit
1import * as NodeAssert from "node:assert/strict";
2
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import type { OpencodeClient } from "@opencode-ai/sdk/v2";
5import { it } from "@effect/vitest";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as Path from "effect/Path";
10import {
11 HostProcessEnvironment,
12 HostProcessExecutablePath,
13 HostProcessPlatform,
14} from "@t3tools/shared/hostProcess";
15
16import { OpenCodeRuntime, OpenCodeRuntimeLive } from "./opencodeRuntime.ts";
17
18const testLayer = OpenCodeRuntimeLive.pipe(Layer.provideMerge(NodeServices.layer));
19
20it.layer(testLayer)("OpenCodeRuntime inventory", (it) => {
21 it.effect("keeps provider inventory when agent discovery fails", () =>
22 Effect.gen(function* () {
23 const runtime = yield* OpenCodeRuntime;
24 const client = {
25 provider: {
26 list: () =>
27 Promise.resolve({
28 data: {
29 connected: ["openai"],
30 all: [],
31 default: {},
32 },
33 }),
34 },
35 app: {
36 agents: () => Promise.reject(new Error("agents endpoint unavailable")),
37 skills: () => Promise.resolve({ data: [] }),
38 },
39 } as unknown as OpencodeClient;
40
41 const inventory = yield* runtime.loadOpenCodeInventory(client);
42
43 NodeAssert.deepEqual(inventory.providerList.connected, ["openai"]);
44 NodeAssert.deepEqual(inventory.agents, []);
45 NodeAssert.deepEqual(inventory.skills, []);
46 }),
47 );
48
49 it.effect("keeps provider inventory when skill discovery fails", () =>
50 Effect.gen(function* () {
51 const runtime = yield* OpenCodeRuntime;
52 const client = {
53 provider: {
54 list: () =>
55 Promise.resolve({
56 data: {
57 connected: ["openai"],
58 all: [],
59 default: {},
60 },
61 }),
62 },
63 app: {
64 agents: () => Promise.resolve({ data: [] }),
65 skills: () => Promise.reject(new Error("skills endpoint unavailable")),
66 },
67 } as unknown as OpencodeClient;
68
69 const inventory = yield* runtime.loadOpenCodeInventory(client);
70
71 NodeAssert.deepEqual(inventory.providerList.connected, ["openai"]);
72 NodeAssert.deepEqual(inventory.agents, []);
73 NodeAssert.deepEqual(inventory.skills, []);
74 }),
75 );
76
77 it.effect("keeps only SDK skill metadata in inventory", () =>
78 Effect.gen(function* () {
79 const runtime = yield* OpenCodeRuntime;
80 const client = {
81 provider: {
82 list: () =>
83 Promise.resolve({
84 data: {
85 connected: ["openai"],
86 all: [],
87 default: {},
88 },
89 }),
90 },
91 app: {
92 agents: () => Promise.resolve({ data: [] }),
93 skills: () =>
94 Promise.resolve({
95 data: [
96 {
97 name: "review",
98 description: "Review code changes",
99 location: "/skills/review/SKILL.md",
100 content: "unused skill content",
101 },
102 ],
103 }),
104 },
105 } as unknown as OpencodeClient;
106
107 const inventory = yield* runtime.loadOpenCodeInventory(client);
108
109 NodeAssert.deepEqual(inventory.skills, [
110 {
111 name: "review",
112 description: "Review code changes",
113 location: "/skills/review/SKILL.md",
114 },
115 ]);
116 }),
117 );
118
119 it.effect("drops oversized CLI skill output without losing the model inventory", () =>
120 Effect.gen(function* () {
121 const fs = yield* FileSystem.FileSystem;
122 const path = yield* Path.Path;
123 const hostEnvironment = yield* HostProcessEnvironment;
124 const executablePath = yield* HostProcessExecutablePath;
125 const hostPlatform = yield* HostProcessPlatform;
126 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-inventory-" });
127 const isWindows = hostPlatform === "win32";
128 const binaryPath = path.join(tempDir, isWindows ? "opencode.cmd" : "opencode");
129 const scriptPath = path.join(tempDir, "opencode.mjs");
130 const oversizedContentBytes = 8 * 1024 * 1024 + 1;
131
132 yield* fs.writeFileString(
133 scriptPath,
134 [
135 'if (process.argv[2] === "models") {',
136 ' process.stdout.write(`openai/gpt-test\\n{"id":"gpt-test","providerID":"openai","name":"GPT Test"}\\n`);',
137 '} else if (process.argv[2] === "debug") {',
138 ` const content = "x".repeat(${oversizedContentBytes});`,
139 ' process.stdout.write(`[{"name":"oversized","content":"${content}"}]`);',
140 "}",
141 "",
142 ].join("\n"),
143 );
144 yield* fs.writeFileString(
145 binaryPath,
146 [
147 ...(isWindows ? ["@echo off"] : ["#!/bin/sh"]),
148 isWindows
149 ? '"%T3_TEST_NODE_BINARY%" "%T3_TEST_OPENCODE_SCRIPT%" %*'
150 : 'exec "$T3_TEST_NODE_BINARY" "$T3_TEST_OPENCODE_SCRIPT" "$@"',
151 "",
152 ].join("\n"),
153 );
154 if (!isWindows) {
155 yield* fs.chmod(binaryPath, 0o755);
156 }
157
158 const runtime = yield* OpenCodeRuntime;
159 const inventory = yield* runtime.loadInventoryFromCli({
160 binaryPath,
161 cwd: tempDir,
162 environment: {
163 ...hostEnvironment,
164 T3_TEST_NODE_BINARY: executablePath,
165 T3_TEST_OPENCODE_SCRIPT: scriptPath,
166 },
167 });
168
169 NodeAssert.deepEqual(inventory.providerList.connected, ["openai"]);
170 NodeAssert.equal(inventory.skills.length, 0);
171 }),
172 );
173
174 it.effect("caps and drains command stdout and stderr when requested", () =>
175 Effect.gen(function* () {
176 const runtime = yield* OpenCodeRuntime;
177 const executablePath = yield* HostProcessExecutablePath;
178 const outputBytes = 2 * 1024 * 1024;
179 const result = yield* runtime.runOpenCodeCommand({
180 binaryPath: executablePath,
181 args: [
182 "-e",
183 `process.stdout.write("o".repeat(${outputBytes})); process.stderr.write("e".repeat(${outputBytes}));`,
184 ],
185 maxOutputBytes: 64,
186 });
187
188 NodeAssert.equal(result.stdout, "o".repeat(64));
189 NodeAssert.equal(result.stderr, "e".repeat(64));
190 NodeAssert.equal(result.code, 0);
191 }),
192 );
193});