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

GrokDriver.test.ts

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