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

PiProvider.test.ts

164 lines5.8 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import * as Cause from "effect/Cause";
4import * as Effect from "effect/Effect";
5import * as Queue from "effect/Queue";
6import * as Schema from "effect/Schema";
7import * as Sink from "effect/Sink";
8import * as Stream from "effect/Stream";
9import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
10​
11import {
12 checkPiProviderStatus,
13 discoverPiWorkspaceCommands,
14 MINIMUM_PI_VERSION,
15} from "./PiProvider.ts";
16​
17const encoder = new TextEncoder();
18const decodeRecordLine = Schema.decodeSync(
19 Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)),
20);
21const encodeJsonLine = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
22​
23function processHandle(input: {
24 readonly stdout?: string;
25 readonly stderr?: string;
26 readonly exitCode?: number;
27}) {
28 const bytes = (value: string | undefined) =>
29 value === undefined || value.length === 0
30 ? Stream.empty
31 : Stream.succeed(encoder.encode(value));
32 return ChildProcessSpawner.makeHandle({
33 pid: ChildProcessSpawner.ProcessId(900_000_001),
34 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(input.exitCode ?? 0)),
35 isRunning: Effect.succeed(false),
36 kill: () => Effect.void,
37 unref: Effect.succeed(Effect.void),
38 stdin: Sink.drain,
39 stdout: bytes(input.stdout),
40 stderr: bytes(input.stderr),
41 all: Stream.empty,
42 getInputFd: () => Sink.drain,
43 getOutputFd: () => Stream.empty,
44 });
46​
47function piProbeSpawner(version: string) {
48 return ChildProcessSpawner.make((command) => {
49 const args = ChildProcess.isStandardCommand(command) ? command.args : [];
50 return Effect.succeed(
51 args.includes("--version")
52 ? processHandle({ stdout: `pi ${version}\n` })
53 : processHandle({ stderr: "RPC startup failed", exitCode: 1 }),
54 );
55 });
57​
58/**
59 * A `pi --mode rpc` that answers `get_commands` with `commands` and records
60 * the directory each process was spawned in.
61 */
62function piCommandsSpawner(commands: ReadonlyArray<unknown>, spawnedIn: Array<string | undefined>) {
63 return ChildProcessSpawner.make((command) =>
64 Effect.gen(function* () {
65 if (ChildProcess.isStandardCommand(command)) spawnedIn.push(command.options.cwd);
66 const stdout = yield* Queue.unbounded<Uint8Array, Cause.Done>();
67 let buffer = "";
68 return ChildProcessSpawner.makeHandle({
69 pid: ChildProcessSpawner.ProcessId(900_000_001),
70 exitCode: Effect.never,
71 isRunning: Effect.succeed(true),
72 kill: () => Effect.void,
73 unref: Effect.succeed(Effect.void),
74 stdin: Sink.forEach((chunk: Uint8Array) =>
75 Effect.gen(function* () {
76 buffer += new TextDecoder().decode(chunk);
77 let newline = buffer.indexOf("\n");
78 while (newline !== -1) {
79 const record = decodeRecordLine(buffer.slice(0, newline));
80 buffer = buffer.slice(newline + 1);
81 const response = { type: "response", id: record["id"], success: true };
82 const data = record["type"] === "get_commands" ? { commands } : undefined;
83 yield* Queue.offer(
84 stdout,
85 encoder.encode(`${encodeJsonLine({ ...response, data })}\n`),
86 );
87 newline = buffer.indexOf("\n");
88 }
89 }),
90 ),
91 stdout: Stream.fromQueue(stdout),
92 stderr: Stream.empty,
93 all: Stream.empty,
94 getInputFd: () => Sink.drain,
95 getOutputFd: () => Stream.empty,
96 });
97 }),
98 );
100​
101const settings = {
102 enabled: true,
103 binaryPath: "pi",
104 launchArgs: "",
105 customModels: [],
106} as const;
107​
108describe("PiProvider", () => {
109 it.effect("requires the first published Pi version with entries and settlement hooks", () =>
110 Effect.gen(function* () {
111 const snapshot = yield* checkPiProviderStatus(settings).pipe(
112 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, piProbeSpawner("0.80.3")),
113 );
114 assert.equal(snapshot.status, "error");
115 assert.equal(snapshot.version, "0.80.3");
116 assert.include(snapshot.message ?? "", `Pi ${MINIMUM_PI_VERSION} or newer`);
117 }).pipe(Effect.provide(NodeServices.layer)),
118 );
119​
120 it.effect("keeps compatible Pi selectable when optional discovery fails", () =>
121 Effect.gen(function* () {
122 const snapshot = yield* checkPiProviderStatus(settings).pipe(
123 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, piProbeSpawner("0.84.3")),
124 );
125 assert.equal(snapshot.status, "ready");
126 assert.equal(snapshot.auth.status, "unknown");
127 assert.deepEqual(
128 snapshot.models.map((model) => model.slug),
129 ["default"],
130 );
131 assert.include(snapshot.message ?? "", "could not refresh its models and commands");
132 }).pipe(Effect.provide(NodeServices.layer)),
133 );
134​
135 it.effect("discovers a workspace's project skills from Pi running in that workspace", () =>
136 Effect.gen(function* () {
137 const spawnedIn: Array<string | undefined> = [];
138 const discovered = yield* discoverPiWorkspaceCommands(settings, {}, "/workspace").pipe(
139 Effect.provideService(
140 ChildProcessSpawner.ChildProcessSpawner,
141 piCommandsSpawner(
142 [
143 {
144 name: "skill:project-deploy",
145 source: "skill",
146 sourceInfo: { path: "/workspace/.pi/skills/project-deploy/SKILL.md" },
147 },
148 ],
149 spawnedIn,
150 ),
151 ),
152 );
153 assert.deepEqual(spawnedIn, ["/workspace"]);
154 assert.deepEqual(
155 discovered.skills.map((skill) => skill.name),
156 ["project-deploy"],
157 );
158 assert.deepEqual(
159 discovered.slashCommands.map((command) => command.name),
160 ["compact", "copy", "export", "share"],
161 );
162 }),
163 );
164});