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