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

OpenCode2Client.test.ts

135 lines5.0 KB

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