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

status.test.ts

167 lines6.2 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 * as ChildProcess from "effect/process/ChildProcess";
10import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11​
12import { checkPiProviderStatus, MINIMUM_PI_VERSION } from "./status.ts";
13​
14const encoder = new TextEncoder();
15const decodeRpcRequest = Schema.decodeSync(
16 Schema.fromJsonString(Schema.Struct({ id: Schema.String, type: Schema.String })),
17);
18const encodeJsonLine = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
19​
20function processHandle(input: {
21 readonly stdout?: string;
22 readonly stderr?: string;
23 readonly exitCode?: number;
24}) {
25 const bytes = (value: string | undefined) =>
26 value === undefined || value.length === 0
27 ? Stream.empty
28 : Stream.succeed(encoder.encode(value));
29 return ChildProcessSpawner.makeHandle({
30 pid: ChildProcessSpawner.ProcessId(900_000_001),
31 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(input.exitCode ?? 0)),
32 isRunning: Effect.succeed(false),
33 kill: () => Effect.void,
34 unref: Effect.succeed(Effect.void),
35 stdin: Sink.drain,
36 stdout: bytes(input.stdout),
37 stderr: bytes(input.stderr),
38 all: Stream.empty,
39 getInputFd: () => Sink.drain,
40 getOutputFd: () => Stream.empty,
41 });
43​
44function piProbeSpawner(version: string) {
45 return ChildProcessSpawner.make((command) => {
46 const args = ChildProcess.isStandardCommand(command) ? command.args : [];
47 return Effect.succeed(
48 args.includes("--version")
49 ? processHandle({ stdout: `pi ${version}\n` })
50 : processHandle({ stderr: "RPC startup failed", exitCode: 1 }),
51 );
52 });
54​
55const piDiscoverySpawner = (inventory: unknown) =>
56 Effect.gen(function* () {
57 const stdout = yield* Queue.unbounded<Uint8Array, Cause.Done>();
58 let stdinBuffer = "";
59 const stdin = Sink.forEach((chunk: Uint8Array) =>
60 Effect.gen(function* () {
61 stdinBuffer += new TextDecoder().decode(chunk);
62 while (true) {
63 const newline = stdinBuffer.indexOf("\n");
64 if (newline === -1) return;
65 const line = stdinBuffer.slice(0, newline);
66 stdinBuffer = stdinBuffer.slice(newline + 1);
67 if (line.length === 0) continue;
68 const request = decodeRpcRequest(line);
69 const data =
70 request.type === "get_available_models"
71 ? inventory
72 : request.type === "get_commands"
73 ? { commands: [] }
74 : {};
75 yield* Queue.offer(
76 stdout,
77 encoder.encode(
78 `${encodeJsonLine({ type: "response", id: request.id, command: request.type, success: true, data })}\n`,
79 ),
80 );
81 }
82 }),
83 );
84 return ChildProcessSpawner.make((command) => {
85 const args = ChildProcess.isStandardCommand(command) ? command.args : [];
86 return Effect.succeed(
87 args.includes("--version")
88 ? processHandle({ stdout: "pi 0.84.3\n" })
89 : ChildProcessSpawner.makeHandle({
90 ...processHandle({}),
91 exitCode: Effect.never,
92 isRunning: Effect.succeed(true),
93 stdin,
94 stdout: Stream.fromQueue(stdout),
95 }),
96 );
97 });
98 });
99​
100const settings = {
101 enabled: true,
102 binaryPath: "pi",
103 launchArgs: "",
104 customModels: [],
105} as const;
106​
107describe("PiProvider", () => {
108 it.effect("requires the first published Pi version with entries and settlement hooks", () =>
109 Effect.gen(function* () {
110 const snapshot = yield* checkPiProviderStatus(settings).pipe(
111 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, piProbeSpawner("0.80.3")),
112 );
113 assert.equal(snapshot.status, "error");
114 assert.equal(snapshot.version, "0.80.3");
115 assert.include(snapshot.message ?? "", `Pi ${MINIMUM_PI_VERSION} or newer`);
116 }).pipe(Effect.provide(NodeServices.layer)),
117 );
118​
119 it.effect("keeps compatible Pi selectable when optional discovery fails", () =>
120 Effect.gen(function* () {
121 const snapshot = yield* checkPiProviderStatus(settings).pipe(
122 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, piProbeSpawner("0.84.3")),
123 );
124 assert.equal(snapshot.status, "ready");
125 assert.equal(snapshot.auth.status, "unknown");
126 assert.deepEqual(
127 snapshot.models.map((model) => model.slug),
128 ["default"],
129 );
130 assert.include(snapshot.message ?? "", "could not refresh its models and commands");
131 }).pipe(Effect.provide(NodeServices.layer)),
132 );
133​
134 it.effect("reports a known unauthenticated inventory when Pi returns no usable models", () =>
135 Effect.gen(function* () {
136 const spawner = yield* piDiscoverySpawner({ models: [] });
137 const snapshot = yield* checkPiProviderStatus(settings).pipe(
138 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
139 );
140 assert.equal(snapshot.status, "warning");
141 assert.equal(snapshot.auth.status, "unauthenticated");
142 assert.deepEqual(
143 snapshot.models.map((model) => model.slug),
144 ["default"],
145 );
146 }).pipe(Effect.provide(NodeServices.layer)),
147 );
148​
149 // regression: #18498 a malformed model inventory must not restore stale Pi models
150 it.effect.each([
151 ["missing models", {}],
152 ["missing model id", { models: [{ provider: "anthropic" }] }],
153 [
154 "mixed valid and invalid models",
155 { models: [{ provider: "anthropic", id: "claude-sonnet-4" }, { provider: "openrouter" }] },
156 ],
157 ] as const)("reports unknown authentication when Pi discovery returns %s", (_label, inventory) =>
158 Effect.gen(function* () {
159 const spawner = yield* piDiscoverySpawner(inventory);
160 const snapshot = yield* checkPiProviderStatus(settings).pipe(
161 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
162 );
163 assert.equal(snapshot.status, "ready");
164 assert.equal(snapshot.auth.status, "unknown");
165 }).pipe(Effect.provide(NodeServices.layer)),
166 );
167});