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

GrokTextGeneration.test.ts

136 lines5.2 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodePath from "node:path";
3import * as NodeOS from "node:os";
4import * as NodeURL from "node:url";
5import * as NodeFS from "node:fs";
6​
7import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
8import * as NodeServices from "@effect/platform-node/NodeServices";
9import { it } from "@effect/vitest";
10import * as Effect from "effect/Effect";
11import * as Layer from "effect/Layer";
12import * as Schema from "effect/Schema";
13import { createModelSelection } from "@t3tools/shared/model";
14import { expect } from "vite-plus/test";
15import { ProviderInstanceId } from "@t3tools/contracts";
16import { GrokSettings } from "@t3tools/provider-grok/settings";
17​
18import * as ServerConfig from "../config.ts";
19import * as TextGeneration from "./TextGeneration.ts";
20import { makeGrokTextGeneration } from "@t3tools/provider-grok/testing";
21import { execScriptSource, writeFakeCli } from "@t3tools/provider-testing/fakeCli";
22const decodeGrokSettings = Schema.decodeSync(GrokSettings);
23​
24const __dirname = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
25const mockAgentPath = NodePath.join(__dirname, "../provider/testFixtures/grok-text-mock-agent.mjs");
26​
27const layerGrokTextGenerationTest = ServerConfig.ServerConfig.layerTest(process.cwd(), {
28 prefix: "t3code-grok-text-generation-test-",
29}).pipe(Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)));
30​
31function makeAcpGrokWrapper(dir: string, env: Record<string, string>): string {
32 return writeFakeCli({
33 directory: NodePath.join(dir, "bin"),
34 name: "grok",
35 env,
36 source: execScriptSource({
37 scriptPath: mockAgentPath,
38 expectedArgs: ["agent", "stdio"],
39 }),
40 });
42​
43function withFakeAcpGrok<A, E, R>(
44 env: Record<string, string>,
45 effectFn: (textGeneration: TextGeneration.TextGeneration["Service"]) => Effect.Effect<A, E, R>,
46) {
47 return Effect.gen(function* () {
48 const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3code-grok-text-acp-"));
49 yield* Effect.addFinalizer(() =>
50 Effect.sync(() => {
51 NodeFS.rmSync(tempDir, { recursive: true, force: true });
52 }),
53 );
54 const binaryPath = makeAcpGrokWrapper(tempDir, env);
55 const config = decodeGrokSettings({ binaryPath });
56 const textGeneration = yield* makeGrokTextGeneration(config);
57 return yield* effectFn(textGeneration);
58 }).pipe(Effect.scoped);
60​
61function readJsonRpcRequests(
62 filePath: string,
63): ReadonlyArray<{ readonly method?: string; readonly params?: Record<string, unknown> }> {
64 return NodeFS.readFileSync(filePath, "utf8")
65 .trim()
66 .split("\n")
67 .filter((line) => line.length > 0)
68 .map((line) => JSON.parse(line) as { method?: string; params?: Record<string, unknown> });
70​
71it.layer(layerGrokTextGenerationTest)("GrokTextGeneration", (it) => {
72 it.effect("uses ACP with disabled tool capabilities and forwards the requested model id", () => {
73 const requestLogDir = NodeFS.mkdtempSync(
74 NodePath.join(NodeOS.tmpdir(), "t3code-grok-text-log-"),
75 );
76 const requestLogPath = NodePath.join(requestLogDir, "requests.ndjson");
77​
78 return withFakeAcpGrok(
79 {
80 T3_ACP_REQUEST_LOG_PATH: requestLogPath,
81 T3_ACP_PROMPT_RESPONSE_TEXT: JSON.stringify({
82 subject: "Add Grok provider",
83 body: "Wire up the ACP runtime and headless text generation path.",
84 }),
85 },
86 (textGeneration) =>
87 Effect.gen(function* () {
88 const generated = yield* textGeneration.generateCommitMessage({
89 cwd: process.cwd(),
90 branch: "feature/grok",
91 stagedSummary: "M apps/server/src/provider/Drivers/GrokDriver.ts",
92 stagedPatch: "diff --git a/.../GrokDriver.ts b/.../GrokDriver.ts",
93 modelSelection: createModelSelection(ProviderInstanceId.make("grok"), "grok-mock-alt"),
94 });
95​
96 expect(generated.subject).toBe("Add Grok provider");
97 expect(generated.body).toBe("Wire up the ACP runtime and headless text generation path.");
98​
99 const requests = readJsonRpcRequests(requestLogPath);
100 expect(
101 requests.find((request) => request.method === "initialize")?.params?.clientCapabilities,
102 ).toMatchObject({
103 fs: { readTextFile: false, writeTextFile: false },
104 terminal: false,
105 });
106 expect(
107 requests.some(
108 (request) =>
109 request.method === "session/set_model" &&
110 request.params?.modelId === "grok-mock-alt",
111 ),
112 ).toBe(true);
113 }),
114 );
115 });
116​
117 it.effect("extracts the JSON object when Grok wraps it in conversational text", () =>
118 withFakeAcpGrok(
119 {
120 T3_ACP_PROMPT_RESPONSE_TEXT:
121 "Sure! Here's a thread title:\n\n" +
122 JSON.stringify({ title: "Investigate failing CI" }) +
123 "\n\nLet me know if you need anything else.",
124 },
125 (textGeneration) =>
126 Effect.gen(function* () {
127 const generated = yield* textGeneration.generateThreadTitle({
128 cwd: process.cwd(),
129 message: "the lint job is red",
130 modelSelection: createModelSelection(ProviderInstanceId.make("grok"), "grok-mock-alt"),
131 });
132 expect(generated.title).toBe("Investigate failing CI");
133 }),
134 ),
135 );
136});