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