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

OpenCode2Client.test.ts

169 lines6.4 KB

1import { assert, it } from "@effect/vitest";
2import { Session } from "@opencode/client/effect";
3import * as Cause from "effect/Cause";
4import * as Deferred from "effect/Deferred";
5import * as Effect from "effect/Effect";
6import * as Fiber from "effect/Fiber";
7import * as Layer from "effect/Layer";
8import * as Redacted from "effect/Redacted";
9import * as Schema from "effect/Schema";
10import * as TestClock from "effect/testing/TestClock";
11import * as HttpClient from "effect/http/HttpClient";
12import * as HttpClientResponse from "effect/http/HttpClientResponse";
13import { describe } from "vite-plus/test";
14​
15import * as OpenCode2Client from "./OpenCode2Client.ts";
16​
17const INFO_BODY = '{"version":"2.0.18","pid":4242,"urls":[],"paths":{"tmp":"/tmp"}}';
18​
19const authorizationFor = (password: string | Redacted.Redacted) => {
20 const seen: Array<string | undefined> = [];
21 const layerCapturing = Layer.succeed(
22 HttpClient.HttpClient,
23 HttpClient.make((request) =>
24 Effect.sync(() => {
25 seen.push(request.headers.authorization);
26 return HttpClientResponse.fromWeb(
27 request,
28 new Response(INFO_BODY, { headers: { "content-type": "application/json" } }),
29 );
30 }),
31 ),
32 );
33 return Effect.gen(function* () {
34 const opencode = yield* OpenCode2Client.OpenCode2Client;
35 const { client } = yield* opencode.connect({ baseUrl: "http://127.0.0.1:4096", password });
36 yield* client.server.info();
37 return seen[0];
38 }).pipe(Effect.provide(OpenCode2Client.layer.pipe(Layer.provide(layerCapturing))));
39};
40​
41const utf8Basic = (password: string) =>
42 `Basic ${Buffer.from(`opencode:${password}`, "utf8").toString("base64")}`;
43​
44describe("OpenCode2Client authorization", () => {
45 // OpenCode requires UTF-8 Basic auth; btoa rejects or misencodes non-ASCII.
46 it.effect("encodes non-ASCII passwords as UTF-8", () =>
47 Effect.gen(function* () {
48 assert.strictEqual(yield* authorizationFor("pässwörd"), utf8Basic("pässwörd"));
49 assert.strictEqual(yield* authorizationFor("pass€word"), utf8Basic("pass€word"));
50 assert.strictEqual(yield* authorizationFor(Redacted.make("pässwörd")), utf8Basic("pässwörd"));
51 }),
52 );
53});
54​
55describe("OpenCode2Client event subscription", () => {
56 it.effect("times out and cancels a subscription with no response headers", () =>
57 Effect.gen(function* () {
58 const cancelled = yield* Deferred.make<void>();
59 const transport = Layer.succeed(
60 HttpClient.HttpClient,
61 HttpClient.make((request) => {
62 assert.strictEqual(request.url, "http://127.0.0.1:4096/api/event");
63 return Effect.never.pipe(Effect.ensuring(Deferred.succeed(cancelled, undefined)));
64 }),
65 );
66 const opencode = yield* OpenCode2Client.OpenCode2Client.pipe(
67 Effect.provide(OpenCode2Client.layer.pipe(Layer.provide(transport))),
68 );
69 const { events } = yield* opencode.connect({
70 baseUrl: "http://127.0.0.1:4096",
71 password: "test-password",
72 });
73 const opening = yield* events.pipe(Effect.flip, Effect.forkScoped);
74​
75 yield* TestClock.adjust("10 seconds");
76 assert.isTrue(yield* Deferred.isDone(cancelled));
77 const error = yield* Fiber.join(opening);
78 assert.strictEqual(error._tag, "HttpClientError");
79 assert.strictEqual(error.reason._tag, "TransportError");
80 assert.isTrue(Cause.isTimeoutError(error.cause));
81 }).pipe(Effect.scoped),
82 );
83});
84​
85const previous = {
86 id: "msg_repro088",
87 type: "synthetic",
88 text: "Fixture message 88",
89 time: { created: 1088 },
90};
91​
92const compaction = (fields: Record<string, unknown>) => ({
93 type: "compaction",
94 id: "msg_repro089",
95 time: { created: 1089 },
96 reason: "manual",
97 summary: "Fixture compaction",
98 ...fields,
99});
100​
101const listHistory = (messages: ReadonlyArray<unknown>) =>
102 Effect.gen(function* () {
103 const transport = Layer.succeed(
104 HttpClient.HttpClient,
105 HttpClient.make((request) =>
106 Effect.sync(() => {
107 assert.include(request.url, "/api/session/ses_repro/message");
108 return HttpClientResponse.fromWeb(
109 request,
110 new Response(JSON.stringify({ data: messages, cursor: {} }), {
111 headers: { "content-type": "application/json" },
112 }),
113 );
114 }),
115 ),
116 );
117 const opencode = yield* OpenCode2Client.OpenCode2Client.pipe(
118 Effect.provide(OpenCode2Client.layer.pipe(Layer.provide(transport))),
119 );
120 const { client } = yield* opencode.connect({
121 baseUrl: "http://127.0.0.1:4096",
122 password: "test-password",
123 });
124 return yield* client.message.list({ sessionID: Session.ID.make("ses_repro"), order: "asc" });
125 });
126​
127describe("OpenCode2Client message history", () => {
128 it.effect.each([
129 { status: "running", shape: "native keepID", fields: { metadata: { keepID: previous.id } } },
130 { status: "completed", shape: "native keepID", fields: { metadata: { keepID: previous.id } } },
131 { status: "running", shape: "native without metadata", fields: {} },
132 { status: "completed", shape: "native without metadata", fields: {} },
133 {
134 status: "completed",
135 shape: "legacy metadata",
136 fields: { metadata: { recent: previous.id } },
137 },
138 { status: "running", shape: "top-level", fields: { recent: previous.id } },
139 ] as const)("decodes a $status compaction in the $shape format", ({ status, fields }) =>
140 Effect.gen(function* () {
141 const history = yield* listHistory([previous, compaction({ status, ...fields })]);
142​
143 assert.deepStrictEqual(
144 history.data.map((message) => message.id),
145 [previous.id, "msg_repro089"],
146 );
147 assert.deepInclude(history.data[1], {
148 type: "compaction",
149 status,
150 summary: "Fixture compaction",
151 recent:
152 "metadata" in fields && "recent" in fields.metadata ? previous.id : (fields.recent ?? ""),
153 });
154 }),
155 );
156​
157 it.effect.each([
158 { field: "recent", fields: { status: "completed", recent: 42 } },
159 { field: "reason", fields: { status: "completed", reason: "scheduled", recent: previous.id } },
160 ] as const)("keeps a compaction with an invalid $field an error", ({ field, fields }) =>
161 Effect.gen(function* () {
162 const error = yield* listHistory([compaction(fields)]).pipe(Effect.flip);
163​
164 assert.strictEqual(error._tag, "ClientError");
165 assert.isTrue(Schema.isSchemaError(error.cause));
166 assert.include(String(error.cause), field);
167 }),
168 );
169});