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

snapshotProbe.test.ts

161 lines5.2 KB

1import { describe, expect, it } from "@effect/vitest";
2import type { ModelCapabilities } from "@t3tools/contracts";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import { createModelCapabilities } from "@t3tools/shared/model";
5import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";
7import * as Sink from "effect/Sink";
8import * as Stream from "effect/Stream";
9import * as ChildProcess from "effect/process/ChildProcess";
10import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11​
12import {
13 isCommandMissingCause,
14 parseGenericCliVersion,
15 providerModelsFromSettings,
16 spawnAndCollect,
17} from "./snapshotProbe.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("keeps an entry's own name and capabilities over the driver default", () => {
40 const capabilities = createModelCapabilities({
41 optionDescriptors: [{ id: "fastMode", label: "Fast Mode", type: "boolean" }],
42 });
43 const models = providerModelsFromSettings(
44 [],
45 ["bare", { slug: "named", name: "Named", capabilities }],
46 OPENCODE_CUSTOM_MODEL_CAPABILITIES,
47 );
48​
49 expect(models).toEqual([
50 {
51 slug: "bare",
52 name: "bare",
53 isCustom: true,
54 capabilities: OPENCODE_CUSTOM_MODEL_CAPABILITIES,
55 },
56 { slug: "named", name: "Named", isCustom: true, capabilities },
57 ]);
58 });
59​
60 it("renames a built-in model when a named entry uses its slug", () => {
61 const capabilities = createModelCapabilities({ optionDescriptors: [] });
62 const models = providerModelsFromSettings(
63 [
64 {
65 slug: "gpt-5.5",
66 name: "GPT-5.5",
67 shortName: "5.5",
68 isCustom: false,
69 capabilities,
70 },
71 ],
72 [{ slug: "gpt-5.5", name: "Team GPT" }],
73 capabilities,
74 );
75​
76 expect(models).toEqual([{ slug: "gpt-5.5", name: "Team GPT", isCustom: false, capabilities }]);
77 });
78​
79 it("preserves a custom slug that collides with a provider alias", () => {
80 const capabilities = createModelCapabilities({ optionDescriptors: [] });
81 const models = providerModelsFromSettings(
82 [
83 {
84 slug: "claude-opus-4-8",
85 name: "Claude Opus 4.8",
86 isCustom: false,
87 capabilities,
88 },
89 ],
90 [" opus "],
91 capabilities,
92 );
93​
94 expect(models.map((model) => model.slug)).toEqual(["claude-opus-4-8", "opus"]);
95 expect(models[1]?.isCustom).toBe(true);
96 });
97});
98​
99describe("parseGenericCliVersion", () => {
100 it("parses a v-prefixed version", () => {
101 expect(parseGenericCliVersion("opencode v2.0.3")).toBe("2.0.3");
102 expect(parseGenericCliVersion("v22.19.0")).toBe("22.19.0");
103 });
104​
105 it("parses a version embedded in other output", () => {
106 expect(parseGenericCliVersion("codex-cli 0.53.0 (build abc)")).toBe("0.53.0");
107 });
108​
109 it("ignores versions glued to other word characters", () => {
110 expect(parseGenericCliVersion("build2.0.3artifact")).toBeNull();
111 });
112});
113​
114describe("ProviderCommandNotFoundError", () => {
115 it.effect("retains safe failed-command diagnostics without process output", () => {
116 const stderr = "'codex' is not recognized: secret-token-value";
117 const spawner = ChildProcessSpawner.make(() =>
118 Effect.succeed(
119 ChildProcessSpawner.makeHandle({
120 pid: ChildProcessSpawner.ProcessId(1),
121 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(9009)),
122 isRunning: Effect.succeed(false),
123 kill: () => Effect.void,
124 unref: Effect.succeed(Effect.void),
125 stdin: Sink.drain,
126 stdout: Stream.empty,
127 stderr: Stream.encodeText(Stream.make(stderr)),
128 all: Stream.empty,
129 getInputFd: () => Sink.drain,
130 getOutputFd: () => Stream.empty,
131 }),
132 ),
133 );
134 return Effect.gen(function* () {
135 const error = yield* spawnAndCollect(
136 "C:\\tools\\codex.cmd",
137 ChildProcess.make("codex", ["--version"]),
138 ).pipe(
139 Effect.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)),
140 Effect.provideService(HostProcess.Platform, "win32"),
141 Effect.flip,
142 );
143​
144 if (error._tag !== "ProviderCommandNotFoundError") {
145 throw new Error(`Unexpected error: ${error._tag}`);
146 }
147​
148 expect(error.binaryPath).toBe("C:\\tools\\codex.cmd");
149 expect(error.exitCode).toBe(9009);
150 expect(error.stdoutLength).toBe(0);
151 expect(error.stderrLength).toBe(stderr.length);
152 expect(error.message).toBe(
153 "Provider command C:\\tools\\codex.cmd was not found (exit code 9009).",
154 );
155 expect(isCommandMissingCause(error)).toBe(true);
156 expect(error).not.toHaveProperty("stdout");
157 expect(error).not.toHaveProperty("stderr");
158 expect(error.message).not.toContain("secret-token-value");
159 });
160 });
161});