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

GrokDriver.test.ts

97 lines4.0 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import { ProviderInstanceId } from "@t3tools/contracts";
4import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Layer from "effect/Layer";
8import * as Path from "effect/Path";
9import { HttpClient } from "effect/unstable/http";
10import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
11​
12import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
13import { ServerConfig } from "../../config.ts";
14import { ServerSettingsService } from "../../serverSettings.ts";
15import { NoOpProviderEventLoggers, ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
16import { GrokDriver } from "./GrokDriver.ts";
17​
18const testLayer = ServerConfig.layerTest(process.cwd(), {
19 prefix: "t3-grok-driver-update-",
20}).pipe(
21 Layer.provideMerge(NodeServices.layer),
22 Layer.provideMerge(ServerSettingsService.layerTest()),
23 Layer.provideMerge(
24 Layer.mock(BackgroundPolicy.BackgroundPolicy)({
25 shouldRunScopeWork: () => Effect.succeed(false),
26 }),
27 ),
28 Layer.provideMerge(Layer.succeed(ProviderEventLoggers, NoOpProviderEventLoggers)),
29 Layer.provideMerge(
30 Layer.succeed(
31 HttpClient.HttpClient,
32 HttpClient.make(() => Effect.die("Disabled Grok must not make an HTTP request")),
33 ),
34 ),
35);
36​
37const noSpawner = ChildProcessSpawner.make(() =>
38 Effect.die("Disabled Grok must not spawn a process"),
39);
40​
41// The `#!/bin/sh` stub below cannot be resolved as an executable on Windows.
42const windowsHost = HostProcessPlatform.defaultValue() === "win32";
43​
44it.layer(testLayer)("GrokDriver", (it) => {
45 it.effect.skipIf(windowsHost)("updates through the configured executable's own updater", () =>
46 Effect.gen(function* () {
47 const fs = yield* FileSystem.FileSystem;
48 const path = yield* Path.Path;
49 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-grok-driver-" });
50 const grokHome = path.join(tempDir, "Grok Home");
51 const binaryPath = path.join(grokHome, "bin", "grok");
52 yield* fs.makeDirectory(path.dirname(binaryPath), { recursive: true });
53 yield* fs.writeFileString(binaryPath, "#!/bin/sh\n");
54 yield* fs.chmod(binaryPath, 0o755);
55​
56 const instance = yield* GrokDriver.create({
57 instanceId: ProviderInstanceId.make("grok-update"),
58 displayName: "Grok test",
59 enabled: false,
60 environment: [{ name: "GROK_HOME", value: grokHome, sensitive: false }],
61 config: { ...GrokDriver.defaultConfig(), binaryPath },
62 });
63​
64 const capabilities = yield* instance.snapshot.resolveMaintenance();
65 expect(capabilities.packageName).toBe("@xai-official/grok");
66 expect(capabilities.update).toMatchObject({
67 command: `'${binaryPath}' update`,
68 executable: binaryPath,
69 args: ["update"],
70 });
71 // `grok update` installs under GROK_HOME, so it must target this instance's home.
72 expect(capabilities.update?.env?.GROK_HOME).toBe(grokHome);
73 }).pipe(
74 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawner),
75 Effect.scoped,
76 ),
77 );
78​
79 it.effect("stays manual-only when the configured executable does not exist", () =>
80 Effect.gen(function* () {
81 const fs = yield* FileSystem.FileSystem;
82 const path = yield* Path.Path;
83 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-grok-missing-" });
84 const instance = yield* GrokDriver.create({
85 instanceId: ProviderInstanceId.make("grok-missing"),
86 displayName: "Grok test",
87 enabled: false,
88 environment: [],
89 config: { ...GrokDriver.defaultConfig(), binaryPath: path.join(tempDir, "grok") },
90 });
91 expect((yield* instance.snapshot.resolveMaintenance()).update).toBeNull();
92 }).pipe(
93 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawner),
94 Effect.scoped,
95 ),
96 );
97});