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

OpenCode2Client.live.test.ts

139 lines5.3 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { AbsolutePath, Location, Model, Provider } from "@opencode/client/effect";
3import { assert, it } from "@effect/vitest";
4import * as Crypto from "effect/Crypto";
5import * as Effect from "effect/Effect";
6import * as Base64Url from "effect/encoding/Base64Url";
7import * as Fiber from "effect/Fiber";
8import * as FileSystem from "effect/FileSystem";
9import * as Filter from "effect/Filter";
10import * as Layer from "effect/Layer";
11import * as Option from "effect/Option";
12import * as Path from "effect/Path";
13import * as Stream from "effect/Stream";
14import * as FetchHttpClient from "effect/http/FetchHttpClient";
15import * as ChildProcess from "effect/process/ChildProcess";
16import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
17import { describe } from "vite-plus/test";
18​
19import * as OpenCode2Client from "./OpenCode2Client.ts";
20​
21const binaryPath = process.env.OPENCODE2_BIN;
22​
23const startServer = Effect.fn("OpenCode2ClientLive.startServer")(function* (binary: string) {
24 const fs = yield* FileSystem.FileSystem;
25 const path = yield* Path.Path;
26 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
27 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode2-live-" });
28 const directory = path.join(root, "work");
29 yield* fs.makeDirectory(directory);
30 // Non-ASCII on purpose: OpenCode decodes Basic credentials as UTF-8.
31 const crypto = yield* Crypto.Crypto;
32 const password = `${Base64Url.encode(yield* crypto.randomBytes(32))}-pässwörd€`;
33 const child = yield* spawner.spawn(
34 ChildProcess.make(binary, ["serve", "--hostname=127.0.0.1", "--port=0"], {
35 cwd: directory,
36 env: {
37 PATH: process.env.PATH,
38 HOME: root,
39 XDG_CONFIG_HOME: path.join(root, "config"),
40 XDG_DATA_HOME: path.join(root, "data"),
41 XDG_STATE_HOME: path.join(root, "state"),
42 XDG_CACHE_HOME: path.join(root, "cache"),
43 OPENCODE_PASSWORD: password,
44 },
45 }),
46 );
47 yield* Effect.addFinalizer(() =>
48 Effect.sync(() => {
49 try {
50 process.kill(Number(child.pid), "SIGTERM");
51 } catch {}
52 }).pipe(Effect.andThen(child.exitCode), Effect.timeout("10 seconds"), Effect.ignore),
53 );
54 const baseUrl = yield* child.stdout.pipe(
55 Stream.decodeText(),
56 Stream.splitLines,
57 Stream.filterMap(
58 Filter.fromPredicateOption((line: string) =>
59 Option.fromUndefinedOr(/server listening on\s+(https?:\/\/\S+)/i.exec(line)?.[1]),
60 ),
61 ),
62 Stream.runHead,
63 Effect.flatMap(Effect.fromOption),
64 Effect.timeout("30 seconds"),
65 );
66 return { baseUrl, password, directory, pid: Number(child.pid) };
67});
68​
69describe.runIf(binaryPath !== undefined)("OpenCode2Client live", () => {
70 it.live(
71 "reads server info, runs one turn, and pages the history",
72 () =>
73 Effect.gen(function* () {
74 const server = yield* startServer(binaryPath!);
75 const opencode = yield* OpenCode2Client.OpenCode2Client;
76 const { client, events } = yield* opencode.connect({
77 baseUrl: server.baseUrl,
78 password: server.password,
79 });
80​
81 const info = yield* client.server.info();
82 assert.strictEqual(info.version, "2.0.28");
83 assert.strictEqual(info.pid, server.pid);
84​
85 const session = yield* client.session.create({
86 title: "t3 client live check",
87 location: Location.PublicRef.make({ directory: AbsolutePath.make(server.directory) }),
88 model: Model.Ref.make({
89 providerID: Provider.ID.make("opencode"),
90 id: Model.ID.make("big-pickle"),
91 }),
92 });
93 const finished = yield* (yield* events).pipe(
94 Stream.filter(
95 (event) =>
96 event.type !== "unreadable.execution.ended" &&
97 event.type !== "unreadable.execution.started" &&
98 (event.data as { readonly sessionID?: string }).sessionID === session.id &&
99 event.type.startsWith("session.execution.") &&
100 event.type !== "session.execution.started",
101 ),
102 Stream.runHead,
103 Effect.forkChild,
104 );
105 yield* client.session.prompt({
106 sessionID: session.id,
107 text: "Reply with exactly: OPENCODE2_CLIENT_OK",
108 });
109 const terminal = yield* Fiber.join(finished).pipe(
110 Effect.flatMap(Effect.fromOption),
111 Effect.timeout("120 seconds"),
112 );
113 assert.strictEqual(terminal.type, "session.execution.succeeded");
114​
115 const history = yield* OpenCode2Client.paginate(
116 { sessionID: session.id, order: "asc", limit: 1 },
117 client.message.list,
118 ).pipe(Stream.runCollect);
119 assert.deepStrictEqual(
120 history.map((message) => message.type),
121 ["user", "assistant", "idle"],
122 );
123 const assistant = history[1];
124 assert(assistant?.type === "assistant");
125 const reply = assistant.content.flatMap((part) =>
126 part.type === "text" ? [part.text] : [],
127 );
128 assert.include(reply.join(""), "OPENCODE2_CLIENT_OK");
129 }).pipe(
130 Effect.scoped,
131 Effect.provide(
132 OpenCode2Client.layer.pipe(
133 Layer.provideMerge(Layer.mergeAll(NodeServices.layer, FetchHttpClient.layer)),
134 ),
135 ),
136 ),
137 180_000,
138 );
139});