Files
t3code/apps/server/src/provider/opencode2/OpenCode2Client.live.test.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

151 lines
5.7 KiB
TypeScript

/**
* Live contract check for `@opencode/client` against a real OpenCode 2 server.
* It re-proves that OpenCode's schemas decode under the workspace Effect
* version, which the pnpm overrides force. Run it after bumping either side:
*
* OPENCODE2_BIN=/path/to/opencode vp test run src/provider/opencode2/OpenCode2Client.live.test.ts
*
* The turn uses the free `opencode/big-pickle` model.
*/
import * as NodeServices from "@effect/platform-node/NodeServices";
import { AbsolutePath, Location, Model, Provider } from "@opencode/client/effect";
import { assert, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Encoding from "effect/Encoding";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Filter from "effect/Filter";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Stream from "effect/Stream";
import { FetchHttpClient } from "effect/unstable/http";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
import * as NodeCrypto from "node:crypto";
import { describe } from "vite-plus/test";
import * as OpenCode2Client from "./OpenCode2Client.ts";
const binaryPath = process.env.OPENCODE2_BIN;
const startServer = Effect.fn("OpenCode2ClientLive.startServer")(function* (binary: string) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode2-live-" });
const directory = path.join(root, "work");
yield* fs.makeDirectory(directory);
// Non-ASCII on purpose: OpenCode decodes Basic credentials as UTF-8.
const password = `${Encoding.encodeBase64Url(NodeCrypto.randomBytes(32))}-pässwörd€`;
const child = yield* spawner.spawn(
ChildProcess.make(binary, ["serve", "--hostname=127.0.0.1", "--port=0"], {
cwd: directory,
env: {
PATH: process.env.PATH,
HOME: root,
XDG_CONFIG_HOME: path.join(root, "config"),
XDG_DATA_HOME: path.join(root, "data"),
XDG_STATE_HOME: path.join(root, "state"),
XDG_CACHE_HOME: path.join(root, "cache"),
OPENCODE_PASSWORD: password,
},
}),
);
// Stop by the PID captured at spawn, before the temp directory is removed.
yield* Effect.addFinalizer(() =>
Effect.sync(() => {
try {
process.kill(Number(child.pid), "SIGTERM");
} catch {
// Already exited.
}
}).pipe(Effect.andThen(child.exitCode), Effect.timeout("10 seconds"), Effect.ignore),
);
const baseUrl = yield* child.stdout.pipe(
Stream.decodeText(),
Stream.splitLines,
Stream.filterMap(
Filter.fromPredicateOption((line: string) =>
Option.fromUndefinedOr(/server listening on\s+(https?:\/\/\S+)/i.exec(line)?.[1]),
),
),
Stream.runHead,
Effect.flatMap(Effect.fromOption),
Effect.timeout("30 seconds"),
);
return { baseUrl, password, directory, pid: Number(child.pid) };
});
describe.runIf(binaryPath !== undefined)("OpenCode2Client live", () => {
it.live(
"reads server info, runs one turn, and pages the history",
() =>
Effect.gen(function* () {
const server = yield* startServer(binaryPath!);
const opencode = yield* OpenCode2Client.OpenCode2Client;
const { client, events } = yield* opencode.connect({
baseUrl: server.baseUrl,
password: server.password,
});
const info = yield* client.server.info();
assert.strictEqual(info.version, "2.0.18");
assert.strictEqual(info.pid, server.pid);
const session = yield* client.session.create({
title: "t3 client live check",
location: Location.PublicRef.make({ directory: AbsolutePath.make(server.directory) }),
model: Model.Ref.make({
providerID: Provider.ID.make("opencode"),
id: Model.ID.make("big-pickle"),
}),
});
const finished = yield* (yield* events).pipe(
Stream.filter(
(event) =>
event.type !== "unreadable.execution.ended" &&
event.type !== "unreadable.execution.started" &&
(event.data as { readonly sessionID?: string }).sessionID === session.id &&
event.type.startsWith("session.execution.") &&
event.type !== "session.execution.started",
),
Stream.runHead,
Effect.forkChild,
);
yield* client.session.prompt({
sessionID: session.id,
text: "Reply with exactly: OPENCODE2_CLIENT_OK",
});
const terminal = yield* Fiber.join(finished).pipe(
Effect.flatMap(Effect.fromOption),
Effect.timeout("120 seconds"),
);
assert.strictEqual(terminal.type, "session.execution.succeeded");
// One message per page makes every request after the first send only a cursor.
const history = yield* OpenCode2Client.paginate(
{ sessionID: session.id, order: "asc", limit: 1 },
client.message.list,
).pipe(Stream.runCollect);
assert.deepStrictEqual(
history.map((message) => message.type),
["user", "assistant", "idle"],
);
const assistant = history[1];
assert(assistant?.type === "assistant");
const reply = assistant.content.flatMap((part) =>
part.type === "text" ? [part.text] : [],
);
assert.include(reply.join(""), "OPENCODE2_CLIENT_OK");
}).pipe(
Effect.scoped,
Effect.provide(
OpenCode2Client.layer.pipe(
Layer.provideMerge(Layer.mergeAll(NodeServices.layer, FetchHttpClient.layer)),
),
),
),
180_000,
);
});