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

PiSessionCommands.test.ts

192 lines7.1 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import { ThreadId } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Queue from "effect/Queue";
7import * as Sink from "effect/Sink";
8import * as Stream from "effect/Stream";
9import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
10​
11import { PiRpcError, type PiRpcConnection, type PiRpcRecord } from "./PiRpc.ts";
12import { runPiSessionCommand } from "./PiSessionCommands.ts";
13​
14const threadId = ThreadId.make("pi-session-commands");
15const html = "<!doctype html><html><body>Pi session</body></html>";
16​
17const makeHarness = Effect.fnUntraced(function* (text: string | null = "Last response") {
18 const fs = yield* FileSystem.FileSystem;
19 const requests: PiRpcRecord[] = [];
20 let exportPath = "";
21 let gistArgs: readonly string[] = [];
22 let gistCode = 0;
23 let gistStderr = "";
24 const connection = {
25 request: (record: PiRpcRecord) =>
26 Effect.gen(function* () {
27 requests.push(record);
28 if (record.type === "get_last_assistant_text") return { text };
29 assert.equal(record.type, "export_html");
30 assert.isString(record.outputPath);
31 exportPath = String(record.outputPath);
32 yield* fs.writeFileString(exportPath, html).pipe(Effect.orDie);
33 return { path: exportPath };
34 }),
35 send: () => Effect.die("Session utilities must never send a prompt"),
36 events: yield* Queue.unbounded<PiRpcRecord, PiRpcError>(),
37 exited: Effect.never,
38 terminate: Effect.void,
39 } satisfies PiRpcConnection;
40 const spawner = ChildProcessSpawner.make((command) =>
41 Effect.sync(() => {
42 assert.isTrue(ChildProcess.isStandardCommand(command));
43 if (ChildProcess.isStandardCommand(command)) gistArgs = command.args;
44 return ChildProcessSpawner.makeHandle({
45 pid: ChildProcessSpawner.ProcessId(999_999_999),
46 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(gistCode)),
47 isRunning: Effect.succeed(false),
48 kill: () => Effect.void,
49 unref: Effect.succeed(Effect.void),
50 stdin: Sink.drain,
51 stdout: Stream.succeed(
52 new TextEncoder().encode("https://gist.github.com/davis/abcdef123456\n"),
53 ),
54 stderr: Stream.succeed(new TextEncoder().encode(gistStderr)),
55 all: Stream.empty,
56 getInputFd: () => Sink.drain,
57 getOutputFd: () => Stream.empty,
58 });
59 }),
60 );
61 return {
62 connection,
63 spawner,
64 requests,
65 exportPath: () => exportPath,
66 gistArgs: () => gistArgs,
67 failGist: () => {
68 gistCode = 1;
69 gistStderr = "Run gh auth login";
70 },
71 };
72});
73​
74describe("Pi session utilities", () => {
75 it.effect("rejects arguments to copy before making an RPC request", () =>
76 Effect.gen(function* () {
77 const harness = yield* makeHarness();
78 const error = yield* runPiSessionCommand(
79 { threadId, command: "copy", outputPath: "extra" },
80 harness.connection,
81 {},
82 undefined,
83 ).pipe(Effect.flip);
84 assert.include(error.message, "does not accept arguments");
85 assert.deepEqual(harness.requests, []);
86 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
87 );
88 it.effect("reads the last assistant text without submitting a prompt", () =>
89 Effect.gen(function* () {
90 const harness = yield* makeHarness();
91 const result = yield* runPiSessionCommand(
92 { threadId, command: "copy" },
93 harness.connection,
94 {},
95 undefined,
96 );
97 assert.deepEqual(result, { command: "copy", text: "Last response" });
98 assert.deepEqual(harness.requests, [{ type: "get_last_assistant_text" }]);
99 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
100 );
101​
102 it.effect("fails copy when the session has no assistant response", () =>
103 Effect.gen(function* () {
104 const harness = yield* makeHarness(null);
105 const error = yield* runPiSessionCommand(
106 { threadId, command: "copy" },
107 harness.connection,
108 {},
109 undefined,
110 ).pipe(Effect.flip);
111 assert.include(error.message, "No assistant response");
112 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
113 );
114​
115 it.effect("returns downloadable HTML and removes the temporary export", () =>
116 Effect.gen(function* () {
117 const fs = yield* FileSystem.FileSystem;
118 const harness = yield* makeHarness();
119 const result = yield* runPiSessionCommand(
120 { threadId, command: "export" },
121 harness.connection,
122 {},
123 undefined,
124 ).pipe(Effect.scoped);
125 assert.deepEqual(result, { command: "export", fileName: "session.html", html });
126 assert.isFalse(yield* fs.exists(harness.exportPath()));
127 }).pipe(Effect.provide(NodeServices.layer)),
128 );
129​
130 it.effect("preserves an explicit export path in the workspace", () =>
131 Effect.gen(function* () {
132 const fs = yield* FileSystem.FileSystem;
133 const cwd = yield* fs.makeTempDirectoryScoped();
134 const harness = yield* makeHarness();
135 const result = yield* runPiSessionCommand(
136 { threadId, command: "export", outputPath: "my session.html" },
137 harness.connection,
138 {},
139 cwd,
140 ).pipe(Effect.scoped);
141 assert.equal(result.command, "export");
142 assert.equal(yield* fs.readFileString(`${cwd}/my session.html`), html);
143 assert.equal(harness.exportPath(), `${cwd}/my session.html`);
144 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
145 );
146​
147 it.effect("shares an unlisted gist and returns the Pi viewer URL", () =>
148 Effect.gen(function* () {
149 const fs = yield* FileSystem.FileSystem;
150 const harness = yield* makeHarness();
151 const result = yield* runPiSessionCommand(
152 { threadId, command: "share" },
153 harness.connection,
154 {},
155 undefined,
156 ).pipe(
157 Effect.scoped,
158 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, harness.spawner),
159 );
160 assert.deepEqual(result, { command: "share", url: "https://pi.dev/session/#abcdef123456" });
161 assert.deepEqual(harness.gistArgs(), [
162 "gist",
163 "create",
164 "--public=false",
165 harness.exportPath(),
166 ]);
167 assert.isFalse(yield* fs.exists(harness.exportPath()));
168 }).pipe(Effect.provide(NodeServices.layer)),
169 );
170​
171 it.effect("reports a sharing failure and still removes the temporary export", () =>
172 Effect.gen(function* () {
173 const fs = yield* FileSystem.FileSystem;
174 const harness = yield* makeHarness();
175 harness.failGist();
176 const error = yield* runPiSessionCommand(
177 { threadId, command: "share" },
178 harness.connection,
179 {},
180 undefined,
181 ).pipe(
182 Effect.scoped,
183 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, harness.spawner),
184 Effect.flip,
185 );
186 assert.equal(error.message, "Pi RPC share failed: GitHub CLI sharing failed.");
187 assert.notInclude(error.message, "gh auth login");
188 assert.isDefined(error.cause);
189 assert.isFalse(yield* fs.exists(harness.exportPath()));
190 }).pipe(Effect.provide(NodeServices.layer)),
191 );
192});