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

GrokProvider.test.ts

120 lines4.5 KB

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