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 skill 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.resolve({ data: [] }),
37 skills: () => Promise.reject(new Error("skills endpoint unavailable")),
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 only SDK skill metadata in inventory", () =>
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: () =>
66 Promise.resolve({
67 data: [
68 {
69 name: "review",
70 description: "Review code changes",
71 location: "/skills/review/SKILL.md",
72 content: "unused skill content",
73 },
74 ],
75 }),
76 },
77 } as unknown as OpencodeClient;
78
79 const inventory = yield* runtime.loadOpenCodeInventory(client);
80
81 NodeAssert.deepEqual(inventory.skills, [
82 {
83 name: "review",
84 description: "Review code changes",
85 location: "/skills/review/SKILL.md",
86 },
87 ]);
88 }),
89 );
90
91 it.effect("drops oversized CLI skill output without losing the model inventory", () =>
92 Effect.gen(function* () {
93 const fs = yield* FileSystem.FileSystem;
94 const path = yield* Path.Path;
95 const hostEnvironment = yield* HostProcessEnvironment;
96 const executablePath = yield* HostProcessExecutablePath;
97 const hostPlatform = yield* HostProcessPlatform;
98 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-inventory-" });
99 const isWindows = hostPlatform === "win32";
100 const binaryPath = path.join(tempDir, isWindows ? "opencode.cmd" : "opencode");
101 const scriptPath = path.join(tempDir, "opencode.mjs");
102 const oversizedContentBytes = 8 * 1024 * 1024 + 1;
103
104 yield* fs.writeFileString(
105 scriptPath,
106 [
107 'if (process.argv[2] === "models") {',
108 ' process.stdout.write(`openai/gpt-test\\n{"id":"gpt-test","providerID":"openai","name":"GPT Test"}\\n`);',
109 '} else if (process.argv[2] === "debug") {',
110 ` const content = "x".repeat(${oversizedContentBytes});`,
111 ' process.stdout.write(`[{"name":"oversized","content":"${content}"}]`);',
112 "}",
113 "",
114 ].join("\n"),
115 );
116 yield* fs.writeFileString(
117 binaryPath,
118 [
119 ...(isWindows ? ["@echo off"] : ["#!/bin/sh"]),
120 isWindows
121 ? '"%T3_TEST_NODE_BINARY%" "%T3_TEST_OPENCODE_SCRIPT%" %*'
122 : 'exec "$T3_TEST_NODE_BINARY" "$T3_TEST_OPENCODE_SCRIPT" "$@"',
123 "",
124 ].join("\n"),
125 );
126 if (!isWindows) {
127 yield* fs.chmod(binaryPath, 0o755);
128 }
129
130 const runtime = yield* OpenCodeRuntime;
131 const inventory = yield* runtime.loadInventoryFromCli({
132 binaryPath,
133 cwd: tempDir,
134 environment: {
135 ...hostEnvironment,
136 T3_TEST_NODE_BINARY: executablePath,
137 T3_TEST_OPENCODE_SCRIPT: scriptPath,
138 },
139 });
140
141 NodeAssert.deepEqual(inventory.providerList.connected, ["openai"]);
142 NodeAssert.equal(inventory.skills.length, 0);
143 }),
144 );
145
146 it.effect("caps and drains command stdout and stderr when requested", () =>
147 Effect.gen(function* () {
148 const runtime = yield* OpenCodeRuntime;
149 const executablePath = yield* HostProcessExecutablePath;
150 const outputBytes = 2 * 1024 * 1024;
151 const result = yield* runtime.runOpenCodeCommand({
152 binaryPath: executablePath,
153 args: [
154 "-e",
155 `process.stdout.write("o".repeat(${outputBytes})); process.stderr.write("e".repeat(${outputBytes}));`,
156 ],
157 maxOutputBytes: 64,
158 });
159
160 NodeAssert.equal(result.stdout, "o".repeat(64));
161 NodeAssert.equal(result.stderr, "e".repeat(64));
162 NodeAssert.equal(result.code, 0);
163 }),
164 );
165});