Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { ProviderInstanceId } from "@t3tools/contracts";
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 { TestClock } from "effect/testing";
9
10import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
11import * as OpenCode2AdapterV2Testkit from "../orchestration-v2/Adapters/OpenCode2AdapterV2.testkit.ts";
12import * as OpenCode2Server from "@t3tools/provider-opencode/server/v2/OpenCode2Server";
13import * as OpenCode2TextGeneration from "@t3tools/provider-opencode/server/v2/textGeneration";
14import { OPENCODE2_TITLE_GENERATION } from "./OpenCode2TextGeneration.fixture.ts";
15
16const layer = TestProviderHost.layer().pipe(Layer.provideMerge(NodeServices.layer));
17
18it.layer(layer)("OpenCode2TextGeneration", (it) => {
19 it.effect("generates a title in a temporary session on the free tier and removes it", () =>
20 Effect.gen(function* () {
21 const server = yield* OpenCode2AdapterV2Testkit.replayServer({
22 provider: "opencode",
23 protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
24 version: "2.0.18",
25 scenario: "opencode2_title_generation",
26 entries: [...OPENCODE2_TITLE_GENERATION, { type: "runtime_exit", status: "success" }],
27 });
28 const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
29 Effect.provideService(OpenCode2Server.OpenCode2Server, server),
30 );
31 const title = yield* textGeneration.generateThreadTitle({
32 cwd: process.cwd(),
33 message: "fix the login redirect loop after oauth",
34 modelSelection: {
35 instanceId: ProviderInstanceId.make("opencode"),
36 model: "opencode/big-pickle",
37 },
38 });
39 assert.equal(title.title, "Fix OAuth Login Redirect Loop");
40 }).pipe(Effect.scoped),
41 );
42
43 it.effect("keeps a provider's own error message out of the caller-visible detail", () =>
44 Effect.gen(function* () {
45 const [subscribe, connected, create, created] = OPENCODE2_TITLE_GENERATION;
46 const sessionId = "ses_f0ec85cf6ffebfalpvV2E9nocu";
47 const refusal = {
48 type: "provider.auth",
49 message:
50 "Error from provider (Console): OpenCode's free tier can only be used from within OpenCode",
51 status: 403,
52 };
53 const server = yield* OpenCode2AdapterV2Testkit.replayServer({
54 provider: "opencode",
55 protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
56 version: "2.0.18",
57 scenario: "opencode2_title_generation_refused",
58 entries: [
59 subscribe!,
60 connected!,
61 create!,
62 created!,
63 {
64 type: "expect_outbound",
65 frame: { type: "session.prompt", input: { sessionID: sessionId, text: "<any>" } },
66 },
67 {
68 type: "emit_inbound",
69 frame: {
70 type: "sdk.response",
71 operation: "session.prompt",
72 data: {
73 data: {
74 id: "msg_1",
75 sessionID: sessionId,
76 time: { created: 1 },
77 type: "user",
78 payload: { text: "t" },
79 delivery: "steer",
80 },
81 },
82 },
83 },
84 {
85 type: "emit_inbound",
86 frame: {
87 type: "sdk.event",
88 event: {
89 id: "evt_0eb7f9ae0001VCNDHVLnqTfoPG",
90 created: 1790657403616,
91 type: "session.execution.failed",
92 data: { sessionID: sessionId, error: refusal },
93 durable: { aggregateID: sessionId, seq: 7, version: 1 },
94 },
95 },
96 },
97 {
98 type: "expect_outbound",
99 frame: { type: "session.remove", input: { sessionID: sessionId } },
100 },
101 {
102 type: "emit_inbound",
103 frame: { type: "sdk.response", operation: "session.remove", data: null },
104 },
105 { type: "runtime_exit", status: "success" },
106 ],
107 });
108 const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
109 Effect.provideService(OpenCode2Server.OpenCode2Server, server),
110 );
111 const failure = yield* textGeneration
112 .generateThreadTitle({
113 cwd: process.cwd(),
114 message: "fix the login redirect loop after oauth",
115 modelSelection: {
116 instanceId: ProviderInstanceId.make("opencode"),
117 model: "opencode/big-pickle",
118 },
119 })
120 .pipe(Effect.flip);
121 assert.equal(failure.detail, "OpenCode could not generate the text.");
122 assert.deepEqual(failure.cause, refusal);
123 }).pipe(Effect.scoped),
124 );
125
126 it.effect("times out when the prompt request and its interrupt never answer", () =>
127 Effect.gen(function* () {
128 const [subscribe, connected, create, created] = OPENCODE2_TITLE_GENERATION;
129 const sessionId = "ses_f0ec85cf6ffebfalpvV2E9nocu";
130 const prompted = yield* Deferred.make<void>();
131 const server = yield* OpenCode2AdapterV2Testkit.replayServer(
132 {
133 provider: "opencode",
134 protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
135 version: "2.0.18",
136 scenario: "opencode2_title_generation_stalled",
137 entries: [
138 subscribe!,
139 connected!,
140 create!,
141 created!,
142 {
143 type: "expect_outbound",
144 frame: { type: "session.prompt", input: { sessionID: sessionId, text: "<any>" } },
145 },
146 {
147 type: "emit_inbound",
148 frame: { type: "sdk.response", operation: "session.prompt", data: "<hang>" },
149 },
150 {
151 type: "expect_outbound",
152 frame: { type: "session.interrupt", input: { sessionID: sessionId } },
153 },
154 {
155 type: "emit_inbound",
156 frame: { type: "sdk.response", operation: "session.interrupt", data: "<hang>" },
157 },
158 {
159 type: "expect_outbound",
160 frame: { type: "session.remove", input: { sessionID: sessionId } },
161 },
162 {
163 type: "emit_inbound",
164 frame: { type: "sdk.response", operation: "session.remove", data: null },
165 },
166 { type: "runtime_exit", status: "success" },
167 ],
168 },
169 { onPrompt: () => Deferred.doneUnsafe(prompted, Effect.void) },
170 );
171 const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
172 Effect.provideService(OpenCode2Server.OpenCode2Server, server),
173 );
174 const generation = yield* textGeneration
175 .generateThreadTitle({
176 cwd: process.cwd(),
177 message: "fix the login redirect loop after oauth",
178 modelSelection: {
179 instanceId: ProviderInstanceId.make("opencode"),
180 model: "opencode/big-pickle",
181 },
182 })
183 .pipe(Effect.flip, Effect.forkScoped);
184 yield* Deferred.await(prompted);
185 yield* TestClock.adjust("3 minutes");
186 yield* TestClock.adjust("5 seconds");
187 const failure = yield* Fiber.join(generation);
188 assert.equal(failure.detail, "OpenCode did not finish generating in time.");
189 }).pipe(Effect.scoped),
190 );
191});