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 { buildInitialGrokProviderSnapshot, checkGrokProviderStatus } from "./GrokProvider.ts";
10
11const decodeGrokSettings = Schema.decodeSync(GrokSettings);
12
13describe("buildInitialGrokProviderSnapshot", () => {
14 it.effect("returns a disabled snapshot when settings.enabled is false", () =>
15 Effect.gen(function* () {
16 const snapshot = yield* buildInitialGrokProviderSnapshot(
17 decodeGrokSettings({ enabled: false }),
18 );
19 expect(snapshot.enabled).toBe(false);
20 expect(snapshot.status).toBe("disabled");
21 expect(snapshot.installed).toBe(false);
22 expect(snapshot.message).toContain("disabled");
23 }),
24 );
25
26 it.effect("returns a pending snapshot by default", () =>
27 Effect.gen(function* () {
28 const snapshot = yield* buildInitialGrokProviderSnapshot(decodeGrokSettings({}));
29 expect(snapshot.enabled).toBe(true);
30 expect(snapshot.installed).toBe(true);
31 expect(snapshot.status).toBe("warning");
32 expect(snapshot.version).toBeNull();
33 expect(snapshot.message).toContain("Checking Grok");
34 expect(snapshot.requiresNewThreadForModelChange).toBe(true);
35 }),
36 );
37});
38
39it.layer(NodeServices.layer)("checkGrokProviderStatus", (it) => {
40 it.effect("reports the binary as missing when the binary path does not resolve", () =>
41 Effect.gen(function* () {
42 const snapshot = yield* checkGrokProviderStatus(
43 decodeGrokSettings({
44 enabled: true,
45 binaryPath: "/definitely/not/installed/grok-binary",
46 }),
47 );
48 expect(snapshot.enabled).toBe(true);
49 expect(snapshot.installed).toBe(false);
50 expect(snapshot.status).toBe("error");
51 expect(snapshot.message).toMatch(/not installed|not on PATH|Failed to execute/);
52 }),
53 );
54
55 it.effect("reports an installed CLI as unhealthy when --version exits non-zero", () =>
56 Effect.gen(function* () {
57 const secretStderr = "broken grok install: secret-token-value";
58 const snapshot = yield* Effect.scoped(
59 Effect.gen(function* () {
60 const fs = yield* FileSystem.FileSystem;
61 const path = yield* Path.Path;
62 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-grok-version-" });
63 const grokPath = path.join(dir, "grok");
64 yield* fs.writeFileString(
65 grokPath,
66 ["#!/bin/sh", `printf "%s\\n" "${secretStderr}" >&2`, "exit 2", ""].join("\n"),
67 );
68 yield* fs.chmod(grokPath, 0o755);
69
70 return yield* checkGrokProviderStatus(
71 decodeGrokSettings({ enabled: true, binaryPath: grokPath }),
72 );
73 }),
74 );
75
76 expect(snapshot.enabled).toBe(true);
77 expect(snapshot.installed).toBe(true);
78 expect(snapshot.status).toBe("error");
79 expect(snapshot.message).toBe("Grok CLI is installed but failed to run.");
80 expect(snapshot.message).not.toContain(secretStderr);
81 }),
82 );
83
84 it.effect("reports an error when ACP model discovery is unavailable", () =>
85 Effect.gen(function* () {
86 const snapshot = yield* Effect.scoped(
87 Effect.gen(function* () {
88 const fs = yield* FileSystem.FileSystem;
89 const path = yield* Path.Path;
90 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-grok-success-" });
91 const grokPath = path.join(dir, "grok");
92 yield* fs.writeFileString(
93 grokPath,
94 ["#!/bin/sh", 'printf "grok-cli 0.0.99\\n"', "exit 0", ""].join("\n"),
95 );
96 yield* fs.chmod(grokPath, 0o755);
97
98 return yield* checkGrokProviderStatus(
99 decodeGrokSettings({ enabled: true, binaryPath: grokPath }),
100 );
101 }),
102 );
103
104 expect(snapshot.status).toBe("error");
105 expect(snapshot.installed).toBe(true);
106 expect(snapshot.models.map((model) => model.slug)).toEqual(["grok-build"]);
107 expect(snapshot.message).toContain("ACP startup failed");
108 }),
109 );
110});