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

PiTextGeneration.test.ts

88 lines3.4 KB

1import { describe, expect, it } from "@effect/vitest";
2import { ProviderInstanceId } from "@t3tools/contracts";
3import * as Cause from "effect/Cause";
4import * as Deferred from "effect/Deferred";
5import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";
7import * as Queue from "effect/Queue";
8import * as Schema from "effect/Schema";
9import * as Sink from "effect/Sink";
10import * as Stream from "effect/Stream";
11import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
12​
13import { makePiTextGeneration } from "./PiTextGeneration.ts";
14​
15const encoder = new TextEncoder();
16const decodeRecord = Schema.decodeUnknownSync(
17 Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)),
18);
19​
20describe("PiTextGeneration", () => {
21 it.effect("uses an isolated RPC session and parses structured output", () => {
22 const commandTypes: string[] = [];
23 const spawner = ChildProcessSpawner.make(() =>
24 Effect.gen(function* () {
25 const stdout = yield* Queue.unbounded<Uint8Array, Cause.Done<void>>();
26 const exited = yield* Deferred.make<ChildProcessSpawner.ExitCode>();
27 const offer = (record: Record<string, unknown>) =>
28 Queue.offer(stdout, encoder.encode(`${JSON.stringify(record)}\n`));
29 const stdin = Sink.forEach((bytes: Uint8Array) =>
30 Effect.gen(function* () {
31 const command = decodeRecord(new TextDecoder().decode(bytes).trim());
32 const type = String(command["type"]);
33 commandTypes.push(type);
34 yield* offer({
35 type: "response",
36 id: command["id"],
37 command: type,
38 success: true,
39 ...(type === "get_last_assistant_text"
40 ? { data: { text: '{"title":"A useful title"}' } }
41 : {}),
42 });
43 if (type === "prompt") yield* offer({ type: "agent_settled" });
44 }),
45 );
46 return ChildProcessSpawner.makeHandle({
47 pid: ChildProcessSpawner.ProcessId(9876),
48 exitCode: Deferred.await(exited),
49 isRunning: Effect.succeed(true),
50 kill: () =>
51 Queue.end(stdout).pipe(
52 Effect.andThen(Deferred.succeed(exited, ChildProcessSpawner.ExitCode(0))),
53 ),
54 unref: Effect.succeed(Effect.void),
55 stdin,
56 stdout: Stream.fromQueue(stdout),
57 stderr: Stream.empty,
58 all: Stream.empty,
59 getInputFd: () => Sink.drain,
60 getOutputFd: () => Stream.empty,
61 });
62 }),
63 );
64​
65 return Effect.gen(function* () {
66 const textGeneration = yield* makePiTextGeneration(
67 { enabled: true, binaryPath: "pi", launchArgs: "", customModels: [] },
68 process.env,
69 );
70 const result = yield* textGeneration.generateThreadTitle({
71 cwd: process.cwd(),
72 message: "Please investigate the flaky test",
73 modelSelection: {
74 instanceId: ProviderInstanceId.make("pi"),
75 model: "openai-codex/gpt-5.4",
76 options: [{ id: "thinking", value: "max" }],
77 },
78 });
79 expect(result).toEqual({ title: "A useful title" });
80 expect(commandTypes).toEqual([
81 "set_model",
82 "set_thinking_level",
83 "prompt",
84 "get_last_assistant_text",
85 ]);
86 }).pipe(Effect.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)));
87 });
88});