Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Sink from "effect/Sink";
5import * as Stream from "effect/Stream";
6import { ChildProcess, ChildProcessSpawner } from "effect/process";
7
8import { checkPiProviderStatus, MINIMUM_PI_VERSION } from "./PiProvider.ts";
9
10const encoder = new TextEncoder();
11
12function processHandle(input: {
13 readonly stdout?: string;
14 readonly stderr?: string;
15 readonly exitCode?: number;
16}) {
17 const bytes = (value: string | undefined) =>
18 value === undefined || value.length === 0
19 ? Stream.empty
20 : Stream.succeed(encoder.encode(value));
21 return ChildProcessSpawner.makeHandle({
22 pid: ChildProcessSpawner.ProcessId(900_000_001),
23 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(input.exitCode ?? 0)),
24 isRunning: Effect.succeed(false),
25 kill: () => Effect.void,
26 unref: Effect.succeed(Effect.void),
27 stdin: Sink.drain,
28 stdout: bytes(input.stdout),
29 stderr: bytes(input.stderr),
30 all: Stream.empty,
31 getInputFd: () => Sink.drain,
32 getOutputFd: () => Stream.empty,
33 });
34}
35
36function piProbeSpawner(version: string) {
37 return ChildProcessSpawner.make((command) => {
38 const args = ChildProcess.isStandardCommand(command) ? command.args : [];
39 return Effect.succeed(
40 args.includes("--version")
41 ? processHandle({ stdout: `pi ${version}\n` })
42 : processHandle({ stderr: "RPC startup failed", exitCode: 1 }),
43 );
44 });
45}
46
47const settings = {
48 enabled: true,
49 binaryPath: "pi",
50 launchArgs: "",
51 customModels: [],
52} as const;
53
54describe("PiProvider", () => {
55 it.effect("requires the first published Pi version with entries and settlement hooks", () =>
56 Effect.gen(function* () {
57 const snapshot = yield* checkPiProviderStatus(settings).pipe(
58 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, piProbeSpawner("0.80.3")),
59 );
60 assert.equal(snapshot.status, "error");
61 assert.equal(snapshot.version, "0.80.3");
62 assert.include(snapshot.message ?? "", `Pi ${MINIMUM_PI_VERSION} or newer`);
63 }).pipe(Effect.provide(NodeServices.layer)),
64 );
65
66 it.effect("keeps compatible Pi selectable when optional discovery fails", () =>
67 Effect.gen(function* () {
68 const snapshot = yield* checkPiProviderStatus(settings).pipe(
69 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, piProbeSpawner("0.84.3")),
70 );
71 assert.equal(snapshot.status, "ready");
72 assert.equal(snapshot.auth.status, "unknown");
73 assert.deepEqual(
74 snapshot.models.map((model) => model.slug),
75 ["default"],
76 );
77 assert.include(snapshot.message ?? "", "could not refresh its models and commands");
78 }).pipe(Effect.provide(NodeServices.layer)),
79 );
80});