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

driver.test.ts

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