Open History to see the last commit
1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, it } from "@effect/vitest";
4import { ProviderInstanceId } from "@t3tools/contracts";
5import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
6import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
7import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers";
8import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
9import * as HostProcess from "@t3tools/shared/HostProcess";
10import * as Effect from "effect/Effect";
11import * as Layer from "effect/Layer";
12import * as Path from "effect/Path";
13import * as Schema from "effect/Schema";
14import * as Sink from "effect/Sink";
15import * as Stream from "effect/Stream";
16import * as ChildProcess from "effect/process/ChildProcess";
17import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
18
19import { FxSettings } from "../settings.ts";
20import { FxDriver } from "./driver.ts";
21
22const homeRelativeSettings = Schema.decodeSync(FxSettings)({ binaryPath: "~/.local/bin/fx" });
23
24const layerTest = TestProviderHost.layer({ runBackgroundWork: false }).pipe(
25 Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
26 Layer.provideMerge(IdAllocator.layer),
27 Layer.provideMerge(McpProviderSessions.layer),
28 Layer.provideMerge(
29 Layer.succeed(
30 ProviderEventLoggers.ProviderEventLoggers,
31 ProviderEventLoggers.NoOpProviderEventLoggers,
32 ),
33 ),
34);
35
36/** Records each spawned command and answers only `--version`. */
37function recordingSpawner(launches: Array<string>) {
38 return ChildProcessSpawner.make((command) => {
39 const standard = ChildProcess.isStandardCommand(command);
40 if (standard) launches.push(command.command);
41 const version = standard && command.args.join(" ") === "--version";
42 return Effect.succeed(
43 ChildProcessSpawner.makeHandle({
44 pid: ChildProcessSpawner.ProcessId(1),
45 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(version ? 0 : 2)),
46 isRunning: Effect.succeed(false),
47 kill: () => Effect.void,
48 unref: Effect.succeed(Effect.void),
49 stdin: Sink.drain,
50 stdout: version ? Stream.encodeText(Stream.make("fx 0.0.13\n")) : Stream.empty,
51 stderr: Stream.empty,
52 all: Stream.empty,
53 getInputFd: () => Sink.drain,
54 getOutputFd: () => Stream.empty,
55 }),
56 );
57 });
58}
59
60it.layer(layerTest)("FxDriver binary path", (it) => {
61 it.effect("probes a ~/ binary path from the home directory", () =>
62 Effect.gen(function* () {
63 const path = yield* Path.Path;
64 const launches: Array<string> = [];
65 const instance = yield* FxDriver.create({
66 instanceId: ProviderInstanceId.make("fx-home"),
67 displayName: undefined,
68 enabled: true,
69 environment: [],
70 config: homeRelativeSettings,
71 }).pipe(
72 Effect.provideService(HostProcess.HomeDirectory, "/test-home"),
73 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, recordingSpawner(launches)),
74 );
75 const snapshot = yield* instance.snapshot.refresh;
76 assert.isNotEmpty(launches);
77 assert.deepEqual([...new Set(launches)], [path.join("/test-home", ".local", "bin", "fx")]);
78 assert.isTrue(snapshot.installed);
79 }),
80 );
81});