Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import type { ModelCapabilities } from "@t3tools/contracts";
3import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
4import { createModelCapabilities } from "@t3tools/shared/model";
5import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";
7import * as PlatformError from "effect/PlatformError";
8import * as Sink from "effect/Sink";
9import * as Stream from "effect/Stream";
10import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
11
12import {
13 isCommandMissingCause,
14 parseGenericCliVersion,
15 providerModelsFromSettings,
16 spawnAndCollect,
17} from "./providerSnapshot.ts";
18
19const OPENCODE_CUSTOM_MODEL_CAPABILITIES: ModelCapabilities = createModelCapabilities({
20 optionDescriptors: [
21 {
22 id: "variant",
23 label: "Reasoning",
24 type: "select",
25 options: [{ id: "medium", label: "Medium", isDefault: true }],
26 currentValue: "medium",
27 },
28 {
29 id: "agent",
30 label: "Agent",
31 type: "select",
32 options: [{ id: "build", label: "Build", isDefault: true }],
33 currentValue: "build",
34 },
35 ],
36});
37
38describe("providerModelsFromSettings", () => {
39 it("applies the provided capabilities to custom models", () => {
40 const models = providerModelsFromSettings(
41 [],
42 ["openai/gpt-5"],
43 OPENCODE_CUSTOM_MODEL_CAPABILITIES,
44 );
45
46 expect(models).toEqual([
47 {
48 slug: "openai/gpt-5",
49 name: "openai/gpt-5",
50 isCustom: true,
51 capabilities: OPENCODE_CUSTOM_MODEL_CAPABILITIES,
52 },
53 ]);
54 });
55
56 it("keeps an entry's own name and capabilities over the driver default", () => {
57 const capabilities = createModelCapabilities({
58 optionDescriptors: [{ id: "fastMode", label: "Fast Mode", type: "boolean" }],
59 });
60 const models = providerModelsFromSettings(
61 [],
62 ["bare", { slug: "named", name: "Named", capabilities }],
63 OPENCODE_CUSTOM_MODEL_CAPABILITIES,
64 );
65
66 expect(models).toEqual([
67 {
68 slug: "bare",
69 name: "bare",
70 isCustom: true,
71 capabilities: OPENCODE_CUSTOM_MODEL_CAPABILITIES,
72 },
73 { slug: "named", name: "Named", isCustom: true, capabilities },
74 ]);
75 });
76
77 it("preserves a custom slug that collides with a provider alias", () => {
78 const capabilities = createModelCapabilities({ optionDescriptors: [] });
79 const models = providerModelsFromSettings(
80 [
81 {
82 slug: "claude-opus-4-8",
83 name: "Claude Opus 4.8",
84 isCustom: false,
85 capabilities,
86 },
87 ],
88 [" opus "],
89 capabilities,
90 );
91
92 expect(models.map((model) => model.slug)).toEqual(["claude-opus-4-8", "opus"]);
93 expect(models[1]?.isCustom).toBe(true);
94 });
95});
96
97describe("parseGenericCliVersion", () => {
98 it("parses a bare version", () => {
99 expect(parseGenericCliVersion("1.14.19")).toBe("1.14.19");
100 });
101
102 it("parses a v-prefixed version", () => {
103 expect(parseGenericCliVersion("opencode v2.0.3")).toBe("2.0.3");
104 expect(parseGenericCliVersion("v22.19.0")).toBe("22.19.0");
105 });
106
107 it("parses a version embedded in other output", () => {
108 expect(parseGenericCliVersion("codex-cli 0.53.0 (build abc)")).toBe("0.53.0");
109 });
110
111 it("returns null when no version is present", () => {
112 expect(parseGenericCliVersion("no version here")).toBeNull();
113 });
114
115 it("ignores versions glued to other word characters", () => {
116 expect(parseGenericCliVersion("build2.0.3artifact")).toBeNull();
117 });
118});
119
120describe("ProviderCommandNotFoundError", () => {
121 it("classifies normalized platform failures without parsing messages", () => {
122 expect(
123 isCommandMissingCause(
124 PlatformError.systemError({
125 _tag: "NotFound",
126 module: "ChildProcess",
127 method: "spawn",
128 description: "arbitrary host detail",
129 }),
130 ),
131 ).toBe(true);
132 expect(isCommandMissingCause(new Error("spawn provider ENOENT"))).toBe(false);
133 });
134
135 it.effect("retains safe failed-command diagnostics without process output", () => {
136 const stderr = "'codex' is not recognized: secret-token-value";
137 const spawner = ChildProcessSpawner.make(() =>
138 Effect.succeed(
139 ChildProcessSpawner.makeHandle({
140 pid: ChildProcessSpawner.ProcessId(1),
141 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(9009)),
142 isRunning: Effect.succeed(false),
143 kill: () => Effect.void,
144 unref: Effect.succeed(Effect.void),
145 stdin: Sink.drain,
146 stdout: Stream.empty,
147 stderr: Stream.encodeText(Stream.make(stderr)),
148 all: Stream.empty,
149 getInputFd: () => Sink.drain,
150 getOutputFd: () => Stream.empty,
151 }),
152 ),
153 );
154 return Effect.gen(function* () {
155 const error = yield* spawnAndCollect(
156 "C:\\tools\\codex.cmd",
157 ChildProcess.make("codex", ["--version"]),
158 ).pipe(
159 Effect.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)),
160 Effect.provideService(HostProcessPlatform, "win32"),
161 Effect.flip,
162 );
163
164 if (error._tag !== "ProviderCommandNotFoundError") {
165 throw new Error(`Unexpected error: ${error._tag}`);
166 }
167
168 expect(error.binaryPath).toBe("C:\\tools\\codex.cmd");
169 expect(error.exitCode).toBe(9009);
170 expect(error.stdoutLength).toBe(0);
171 expect(error.stderrLength).toBe(stderr.length);
172 expect(error.message).toBe(
173 "Provider command C:\\tools\\codex.cmd was not found (exit code 9009).",
174 );
175 expect(isCommandMissingCause(error)).toBe(true);
176 expect(error).not.toHaveProperty("stdout");
177 expect(error).not.toHaveProperty("stderr");
178 expect(error.message).not.toContain("secret-token-value");
179 });
180 });
181});