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

providerSnapshot.test.ts

136 lines4.3 KB

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("preserves a custom slug that collides with a provider alias", () => {
56 const capabilities = createModelCapabilities({ optionDescriptors: [] });
57 const models = providerModelsFromSettings(
58 [
59 {
60 slug: "claude-opus-4-8",
61 name: "Claude Opus 4.8",
62 isCustom: false,
63 capabilities,
64 },
65 ],
66 [" opus "],
67 capabilities,
68 );
69​
70 expect(models.map((model) => model.slug)).toEqual(["claude-opus-4-8", "opus"]);
71 expect(models[1]?.isCustom).toBe(true);
72 });
73});
74​
75describe("ProviderCommandNotFoundError", () => {
76 it("classifies normalized platform failures without parsing messages", () => {
77 expect(
78 isCommandMissingCause(
79 PlatformError.systemError({
80 _tag: "NotFound",
81 module: "ChildProcess",
82 method: "spawn",
83 description: "arbitrary host detail",
84 }),
85 ),
86 ).toBe(true);
87 expect(isCommandMissingCause(new Error("spawn provider ENOENT"))).toBe(false);
88 });
89​
90 it.effect("retains safe failed-command diagnostics without process output", () => {
91 const stderr = "'codex' is not recognized: secret-token-value";
92 const spawner = ChildProcessSpawner.make(() =>
93 Effect.succeed(
94 ChildProcessSpawner.makeHandle({
95 pid: ChildProcessSpawner.ProcessId(1),
96 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(9009)),
97 isRunning: Effect.succeed(false),
98 kill: () => Effect.void,
99 unref: Effect.succeed(Effect.void),
100 stdin: Sink.drain,
101 stdout: Stream.empty,
102 stderr: Stream.encodeText(Stream.make(stderr)),
103 all: Stream.empty,
104 getInputFd: () => Sink.drain,
105 getOutputFd: () => Stream.empty,
106 }),
107 ),
108 );
109 return Effect.gen(function* () {
110 const error = yield* spawnAndCollect(
111 "C:\\tools\\codex.cmd",
112 ChildProcess.make("codex", ["--version"]),
113 ).pipe(
114 Effect.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)),
115 Effect.provideService(HostProcessPlatform, "win32"),
116 Effect.flip,
117 );
118​
119 if (error._tag !== "ProviderCommandNotFoundError") {
120 throw new Error(`Unexpected error: ${error._tag}`);
121 }
122​
123 expect(error.binaryPath).toBe("C:\\tools\\codex.cmd");
124 expect(error.exitCode).toBe(9009);
125 expect(error.stdoutLength).toBe(0);
126 expect(error.stderrLength).toBe(stderr.length);
127 expect(error.message).toBe(
128 "Provider command C:\\tools\\codex.cmd was not found (exit code 9009).",
129 );
130 expect(isCommandMissingCause(error)).toBe(true);
131 expect(error).not.toHaveProperty("stdout");
132 expect(error).not.toHaveProperty("stderr");
133 expect(error.message).not.toContain("secret-token-value");
134 });
135 });
136});