Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6import * as Schema from "effect/Schema";
7import { GrokSettings } from "@t3tools/contracts";
8
9import {
10 buildGrokModelCapabilities,
11 buildInitialGrokProviderSnapshot,
12 checkGrokProviderStatus,
13} from "./GrokProvider.ts";
14
15const decodeGrokSettings = Schema.decodeSync(GrokSettings);
16
17describe("buildGrokModelCapabilities", () => {
18 it("preserves ACP-provided reasoning labels and the active default", () => {
19 const capabilities = buildGrokModelCapabilities({
20 modelId: "grok-4.6",
21 name: "Grok 4.6",
22 _meta: {
23 supportsReasoningEffort: true,
24 reasoningEffort: "xhigh",
25 reasoningEfforts: [
26 { value: "xhigh", label: "Extra High Effort", default: true },
27 { value: "high", label: "High Effort", default: true },
28 { value: "medium", label: "Medium Effort" },
29 { value: "low", label: "Low Effort" },
30 ],
31 },
32 });
33
34 expect(capabilities.optionDescriptors).toEqual([
35 {
36 id: "reasoningEffort",
37 label: "Reasoning",
38 type: "select",
39 currentValue: "xhigh",
40 options: [
41 { id: "xhigh", label: "Extra High Effort", isDefault: true },
42 { id: "high", label: "High Effort" },
43 { id: "medium", label: "Medium Effort" },
44 { id: "low", label: "Low Effort" },
45 ],
46 },
47 ]);
48 });
49
50 it("uses raw ACP values when option labels are omitted", () => {
51 const capabilities = buildGrokModelCapabilities({
52 modelId: "grok-4.6",
53 name: "Grok 4.6",
54 _meta: {
55 supportsReasoningEffort: true,
56 reasoningEffort: "xhigh",
57 reasoningEfforts: [{ value: "xhigh" }, { value: "medium" }],
58 },
59 });
60
61 expect(capabilities.optionDescriptors).toEqual([
62 {
63 id: "reasoningEffort",
64 label: "Reasoning",
65 type: "select",
66 currentValue: "xhigh",
67 options: [
68 { id: "xhigh", label: "xhigh" },
69 { id: "medium", label: "medium" },
70 ],
71 },
72 ]);
73 });
74
75 it("keeps ACP current effort separate from its collapsed advertised default", () => {
76 const capabilities = buildGrokModelCapabilities({
77 modelId: "grok-4.6",
78 name: "Grok 4.6",
79 _meta: {
80 supportsReasoningEffort: true,
81 reasoningEffort: "medium",
82 reasoningEfforts: [
83 { value: "xhigh", label: "Extra High Effort", default: true },
84 { value: "high", label: "High Effort", default: true },
85 { value: "medium", label: "Medium Effort" },
86 ],
87 },
88 });
89
90 expect(capabilities.optionDescriptors).toEqual([
91 {
92 id: "reasoningEffort",
93 label: "Reasoning",
94 type: "select",
95 currentValue: "medium",
96 options: [
97 { id: "xhigh", label: "Extra High Effort", isDefault: true },
98 { id: "high", label: "High Effort" },
99 { id: "medium", label: "Medium Effort" },
100 ],
101 },
102 ]);
103 });
104
105 it("preserves ACP descriptions and falls back from invalid values to valid ids", () => {
106 const capabilities = buildGrokModelCapabilities({
107 modelId: "grok-4.6",
108 name: "Grok 4.6",
109 _meta: {
110 supportsReasoningEffort: true,
111 reasoningEffort: "high",
112 reasoningEfforts: [
113 {
114 id: "high",
115 value: "not a token",
116 label: "High Effort",
117 description: "Higher implementation quality",
118 default: true,
119 },
120 { id: "bad id", value: "also invalid", label: "Invalid" },
121 ],
122 },
123 });
124
125 expect(capabilities.optionDescriptors).toEqual([
126 {
127 id: "reasoningEffort",
128 label: "Reasoning",
129 type: "select",
130 currentValue: "high",
131 options: [
132 {
133 id: "high",
134 label: "High Effort",
135 description: "Higher implementation quality",
136 isDefault: true,
137 },
138 ],
139 },
140 ]);
141 });
142
143 it("accepts an advertised ACP menu when the support flag is omitted", () => {
144 const capabilities = buildGrokModelCapabilities({
145 modelId: "grok-4.6",
146 name: "Grok 4.6",
147 _meta: {
148 reasoningEffort: "high",
149 reasoningEfforts: [{ value: "high", label: "High Effort", default: true }],
150 },
151 });
152
153 expect(capabilities.optionDescriptors).toHaveLength(1);
154 });
155
156 it("honors an explicit ACP opt-out even when a menu is present", () => {
157 const capabilities = buildGrokModelCapabilities({
158 modelId: "grok-4.6",
159 name: "Grok 4.6",
160 _meta: {
161 supportsReasoningEffort: false,
162 reasoningEfforts: [{ value: "high", label: "High Effort", default: true }],
163 },
164 });
165
166 expect(capabilities.optionDescriptors).toEqual([]);
167 });
168
169 it("does not synthesize a reasoning menu when ACP omits it", () => {
170 expect(
171 buildGrokModelCapabilities({
172 modelId: "grok-4.6",
173 name: "Grok 4.6",
174 _meta: { supportsReasoningEffort: true, reasoningEffort: "xhigh" },
175 }).optionDescriptors,
176 ).toEqual([]);
177 });
178
179 it("keeps non-reasoning Grok models free of reasoning controls", () => {
180 expect(
181 buildGrokModelCapabilities({ modelId: "grok-build", name: "Grok Build" }).optionDescriptors,
182 ).toEqual([]);
183 });
184});
185
186describe("buildInitialGrokProviderSnapshot", () => {
187 it.effect("returns a disabled snapshot when settings.enabled is false", () =>
188 Effect.gen(function* () {
189 const snapshot = yield* buildInitialGrokProviderSnapshot(
190 decodeGrokSettings({ enabled: false }),
191 );
192 expect(snapshot.enabled).toBe(false);
193 expect(snapshot.status).toBe("disabled");
194 expect(snapshot.installed).toBe(false);
195 expect(snapshot.message).toContain("disabled");
196 }),
197 );
198
199 it.effect("returns a disabled snapshot by default — Grok is opt-in", () =>
200 Effect.gen(function* () {
201 const snapshot = yield* buildInitialGrokProviderSnapshot(decodeGrokSettings({}));
202 expect(snapshot.enabled).toBe(false);
203 expect(snapshot.status).toBe("disabled");
204 }),
205 );
206
207 it.effect("returns a pending snapshot when enabled", () =>
208 Effect.gen(function* () {
209 const snapshot = yield* buildInitialGrokProviderSnapshot(
210 decodeGrokSettings({ enabled: true }),
211 );
212 expect(snapshot.enabled).toBe(true);
213 expect(snapshot.installed).toBe(true);
214 expect(snapshot.status).toBe("warning");
215 expect(snapshot.version).toBeNull();
216 expect(snapshot.message).toContain("Checking Grok");
217 expect(snapshot.requiresNewThreadForModelChange).toBeUndefined();
218 }),
219 );
220});
221
222it.layer(NodeServices.layer)("checkGrokProviderStatus", (it) => {
223 it.effect("reports the binary as missing when the binary path does not resolve", () =>
224 Effect.gen(function* () {
225 const snapshot = yield* checkGrokProviderStatus(
226 decodeGrokSettings({
227 enabled: true,
228 binaryPath: "/definitely/not/installed/grok-binary",
229 }),
230 );
231 expect(snapshot.enabled).toBe(true);
232 expect(snapshot.installed).toBe(false);
233 expect(snapshot.status).toBe("error");
234 expect(snapshot.message).toMatch(/not installed|not on PATH|Failed to execute/);
235 }),
236 );
237
238 it.effect("reports an installed CLI as unhealthy when --version exits non-zero", () =>
239 Effect.gen(function* () {
240 const secretStderr = "broken grok install: secret-token-value";
241 const snapshot = yield* Effect.scoped(
242 Effect.gen(function* () {
243 const fs = yield* FileSystem.FileSystem;
244 const path = yield* Path.Path;
245 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-grok-version-" });
246 const grokPath = path.join(dir, "grok");
247 yield* fs.writeFileString(
248 grokPath,
249 ["#!/bin/sh", `printf "%s\\n" "${secretStderr}" >&2`, "exit 2", ""].join("\n"),
250 );
251 yield* fs.chmod(grokPath, 0o755);
252
253 return yield* checkGrokProviderStatus(
254 decodeGrokSettings({ enabled: true, binaryPath: grokPath }),
255 );
256 }),
257 );
258
259 expect(snapshot.enabled).toBe(true);
260 expect(snapshot.installed).toBe(true);
261 expect(snapshot.status).toBe("error");
262 expect(snapshot.message).toBe("Grok CLI is installed but failed to run.");
263 expect(snapshot.message).not.toContain(secretStderr);
264 }),
265 );
266
267 it.effect("reports an error when ACP model discovery is unavailable", () =>
268 Effect.gen(function* () {
269 const snapshot = yield* Effect.scoped(
270 Effect.gen(function* () {
271 const fs = yield* FileSystem.FileSystem;
272 const path = yield* Path.Path;
273 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-grok-success-" });
274 const grokPath = path.join(dir, "grok");
275 yield* fs.writeFileString(
276 grokPath,
277 ["#!/bin/sh", 'printf "grok-cli 0.0.99\\n"', "exit 0", ""].join("\n"),
278 );
279 yield* fs.chmod(grokPath, 0o755);
280
281 return yield* checkGrokProviderStatus(
282 decodeGrokSettings({ enabled: true, binaryPath: grokPath }),
283 );
284 }),
285 );
286
287 expect(snapshot.status).toBe("error");
288 expect(snapshot.installed).toBe(true);
289 expect(snapshot.models.map((model) => model.slug)).toEqual(["grok-build"]);
290 expect(snapshot.message).toContain("ACP startup failed");
291 }),
292 );
293});