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

OpenCode2TextGeneration.test.ts

262 lines9.6 KB

1import { assert, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { ProviderInstanceId } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6​
7import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
8import * as OpenCode2AdapterV2Testkit from "../orchestration-v2/Adapters/OpenCode2AdapterV2.testkit.ts";
9import * as OpenCode2Server from "@t3tools/provider-opencode/server/v2/OpenCode2Server";
10import * as OpenCode2TextGeneration from "@t3tools/provider-opencode/server/v2/textGeneration";
11import { OPENCODE2_TITLE_GENERATION } from "./OpenCode2TextGeneration.fixture.ts";
12​
13const layer = TestProviderHost.layer().pipe(Layer.provideMerge(NodeServices.layer));
14​
15it.layer(layer)("OpenCode2TextGeneration", (it) => {
16 it.effect("generates a title in a temporary session on the free tier and removes it", () =>
17 Effect.gen(function* () {
18 const server = yield* OpenCode2AdapterV2Testkit.replayServer({
19 provider: "opencode",
20 protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
21 version: "2.0.18",
22 scenario: "opencode2_title_generation",
23 entries: [...OPENCODE2_TITLE_GENERATION, { type: "runtime_exit", status: "success" }],
24 });
25 const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
26 Effect.provideService(OpenCode2Server.OpenCode2Server, server),
27 );
28 const title = yield* textGeneration.generateThreadTitle({
29 cwd: process.cwd(),
30 message: "fix the login redirect loop after oauth",
31 modelSelection: {
32 instanceId: ProviderInstanceId.make("opencode"),
33 model: "opencode/big-pickle",
34 },
35 });
36 assert.equal(title.title, "Fix OAuth Login Redirect Loop");
37 }).pipe(Effect.scoped),
38 );
39​
40 it.effect("fails at once when the event stream drops before the reply", () =>
41 Effect.gen(function* () {
42 const [subscribe, connected, create, created] = OPENCODE2_TITLE_GENERATION;
43 const sessionId = "ses_f0ec85cf6ffebfalpvV2E9nocu";
44 const server = yield* OpenCode2AdapterV2Testkit.replayServer({
45 provider: "opencode",
46 protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
47 version: "2.0.18",
48 scenario: "opencode2_title_generation_stream_lost",
49 entries: [
50 subscribe!,
51 connected!,
52 create!,
53 created!,
54 {
55 type: "expect_outbound",
56 frame: { type: "session.prompt", input: { sessionID: sessionId, text: "<any>" } },
57 },
58 {
59 type: "emit_inbound",
60 frame: {
61 type: "sdk.response",
62 operation: "session.prompt",
63 data: {
64 data: {
65 id: "msg_1",
66 sessionID: sessionId,
67 time: { created: 1 },
68 type: "user",
69 payload: { text: "t" },
70 delivery: "steer",
71 },
72 },
73 },
74 },
75 { type: "runtime_exit", status: "success" },
76 {
77 type: "expect_outbound",
78 frame: { type: "session.remove", input: { sessionID: sessionId } },
79 },
80 {
81 type: "emit_inbound",
82 frame: { type: "sdk.response", operation: "session.remove", data: null },
83 },
84 ],
85 });
86 const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
87 Effect.provideService(OpenCode2Server.OpenCode2Server, server),
88 );
89 const failure = yield* textGeneration
90 .generateThreadTitle({
91 cwd: process.cwd(),
92 message: "fix the login redirect loop after oauth",
93 modelSelection: {
94 instanceId: ProviderInstanceId.make("opencode"),
95 model: "opencode/big-pickle",
96 },
97 })
98 .pipe(Effect.flip, Effect.timeout("10 seconds"));
99 assert.equal(failure._tag, "TextGenerationError");
100 }).pipe(Effect.scoped),
101 );
102​
103 it.effect("keeps a provider's own error message out of the caller-visible detail", () =>
104 Effect.gen(function* () {
105 const [subscribe, connected, create, created] = OPENCODE2_TITLE_GENERATION;
106 const sessionId = "ses_f0ec85cf6ffebfalpvV2E9nocu";
107 const refusal = {
108 type: "provider.auth",
109 message:
110 "Error from provider (Console): OpenCode's free tier can only be used from within OpenCode",
111 status: 403,
112 };
113 const server = yield* OpenCode2AdapterV2Testkit.replayServer({
114 provider: "opencode",
115 protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
116 version: "2.0.18",
117 scenario: "opencode2_title_generation_refused",
118 entries: [
119 subscribe!,
120 connected!,
121 create!,
122 created!,
123 {
124 type: "expect_outbound",
125 frame: { type: "session.prompt", input: { sessionID: sessionId, text: "<any>" } },
126 },
127 {
128 type: "emit_inbound",
129 frame: {
130 type: "sdk.response",
131 operation: "session.prompt",
132 data: {
133 data: {
134 id: "msg_1",
135 sessionID: sessionId,
136 time: { created: 1 },
137 type: "user",
138 payload: { text: "t" },
139 delivery: "steer",
140 },
141 },
142 },
143 },
144 {
145 type: "emit_inbound",
146 frame: {
147 type: "sdk.event",
148 event: {
149 id: "evt_0eb7f9ae0001VCNDHVLnqTfoPG",
150 created: 1790657403616,
151 type: "session.execution.failed",
152 data: { sessionID: sessionId, error: refusal },
153 durable: { aggregateID: sessionId, seq: 7, version: 1 },
154 },
155 },
156 },
157 {
158 type: "expect_outbound",
159 frame: { type: "session.remove", input: { sessionID: sessionId } },
160 },
161 {
162 type: "emit_inbound",
163 frame: { type: "sdk.response", operation: "session.remove", data: null },
164 },
165 { type: "runtime_exit", status: "success" },
166 ],
167 });
168 const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
169 Effect.provideService(OpenCode2Server.OpenCode2Server, server),
170 );
171 const failure = yield* textGeneration
172 .generateThreadTitle({
173 cwd: process.cwd(),
174 message: "fix the login redirect loop after oauth",
175 modelSelection: {
176 instanceId: ProviderInstanceId.make("opencode"),
177 model: "opencode/big-pickle",
178 },
179 })
180 .pipe(Effect.flip);
181 assert.equal(failure.detail, "OpenCode could not generate the text.");
182 assert.deepEqual(failure.cause, refusal);
183 }).pipe(Effect.scoped),
184 );
185​
186 it.effect("ends at once on an execution end this build cannot decode", () =>
187 Effect.gen(function* () {
188 const [subscribe, connected, create, created] = OPENCODE2_TITLE_GENERATION;
189 const sessionId = "ses_f0ec85cf6ffebfalpvV2E9nocu";
190 const server = yield* OpenCode2AdapterV2Testkit.replayServer({
191 provider: "opencode",
192 protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
193 version: "2.0.18",
194 scenario: "opencode2_title_generation_unreadable_end",
195 entries: [
196 subscribe!,
197 connected!,
198 create!,
199 created!,
200 {
201 type: "expect_outbound",
202 frame: { type: "session.prompt", input: { sessionID: sessionId, text: "<any>" } },
203 },
204 {
205 type: "emit_inbound",
206 frame: {
207 type: "sdk.response",
208 operation: "session.prompt",
209 data: {
210 data: {
211 id: "msg_1",
212 sessionID: sessionId,
213 time: { created: 1 },
214 type: "user",
215 payload: { text: "t" },
216 delivery: "steer",
217 },
218 },
219 },
220 },
221 // A reason added after 2.0.18: the full schema rejects the frame.
222 {
223 type: "emit_inbound",
224 frame: {
225 type: "sdk.event",
226 event: {
227 id: "evt_0eb7f9ae0001VCNDHVLnqTfoPG",
228 created: 1790657403616,
229 type: "session.execution.interrupted",
230 data: { sessionID: sessionId, reason: "budget" },
231 durable: { aggregateID: sessionId, seq: 7, version: 1 },
232 },
233 },
234 },
235 {
236 type: "expect_outbound",
237 frame: { type: "session.remove", input: { sessionID: sessionId } },
238 },
239 {
240 type: "emit_inbound",
241 frame: { type: "sdk.response", operation: "session.remove", data: null },
242 },
243 { type: "runtime_exit", status: "success" },
244 ],
245 });
246 const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
247 Effect.provideService(OpenCode2Server.OpenCode2Server, server),
248 );
249 const failure = yield* textGeneration
250 .generateThreadTitle({
251 cwd: process.cwd(),
252 message: "fix the login redirect loop after oauth",
253 modelSelection: {
254 instanceId: ProviderInstanceId.make("opencode"),
255 model: "opencode/big-pickle",
256 },
257 })
258 .pipe(Effect.flip, Effect.timeout("10 seconds"));
259 assert.equal(failure._tag, "TextGenerationError");
260 }).pipe(Effect.scoped),
261 );
262});