|
1 | | -import fs from "node:fs/promises" |
2 | | -import path from "node:path" |
3 | 1 | import { expect } from "bun:test" |
4 | | -import { Effect, Schedule } from "effect" |
5 | | -import { tmpdir } from "../../core/test/fixture/tmpdir" |
| 2 | +import { SdkPlugins } from "@opencode-ai/core/plugin/sdk" |
| 3 | +import { Plugin } from "@opencode-ai/plugin/effect" |
| 4 | +import { Context, Deferred, Effect, Fiber, Layer } from "effect" |
| 5 | +import { HttpEffect, HttpRouter, HttpServer } from "effect/unstable/http" |
| 6 | +import { tmpdirScoped } from "../../core/test/fixture/tmpdir" |
6 | 7 | import { it } from "../../core/test/lib/effect" |
7 | | -import { startServer } from "./fixture/server" |
| 8 | +import { createRoutes } from "../src/routes" |
8 | 9 |
|
9 | 10 | it.live( |
10 | | - "lists providers without blocking on plugin initialization", |
| 11 | + "lists and gets providers without blocking on plugin initialization", |
11 | 12 | () => |
12 | 13 | Effect.gen(function* () { |
13 | | - const fixture = yield* configuredProvider("opencode-provider-list-endpoint-") |
14 | | - const url = new URL("/api/provider", fixture.server.base) |
15 | | - url.searchParams.set("location[directory]", fixture.path) |
16 | | - yield* Effect.promise(async () => { |
17 | | - const response = await fetch(url, { headers: fixture.server.headers }) |
18 | | - if (response.status !== 200) return false |
19 | | - const body: unknown = await response.json() |
20 | | - return isRecord(body) && Array.isArray(body["data"]) |
21 | | - ? body["data"].some((provider) => isRecord(provider) && provider["id"] === "custom") |
22 | | - : false |
23 | | - }).pipe( |
24 | | - Effect.filterOrFail((found) => found), |
25 | | - Effect.retry(Schedule.spaced("10 millis")), |
26 | | - Effect.timeout("2 seconds"), |
| 14 | + const tmp = yield* tmpdirScoped("opencode-provider-endpoints-") |
| 15 | + const started = yield* Deferred.make<void>() |
| 16 | + const release = yield* Deferred.make<void>() |
| 17 | + const context = yield* Layer.build( |
| 18 | + createRoutes({ |
| 19 | + password: "secret", |
| 20 | + database: { path: ":memory:" }, |
| 21 | + models: { fetch: false }, |
| 22 | + fs: { filewatcher: false }, |
| 23 | + config: { |
| 24 | + directory: tmp.path, |
| 25 | + project: false, |
| 26 | + content: JSON.stringify({ |
| 27 | + providers: { |
| 28 | + custom: { |
| 29 | + name: "Configured Custom Provider", |
| 30 | + package: "@opencode-ai/ai/providers/openai-compatible", |
| 31 | + settings: { apiKey: "secret" }, |
| 32 | + models: { chat: {} }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }), |
| 36 | + }, |
| 37 | + }).pipe(Layer.provide(HttpServer.layerServices)), |
27 | 38 | ) |
28 | | - }), |
29 | | - 15_000, |
30 | | -) |
| 39 | + const sdk = Context.get(context, SdkPlugins.Service) |
| 40 | + yield* sdk.register( |
| 41 | + Plugin.define({ |
| 42 | + id: "slow-plugin", |
| 43 | + effect: () => |
| 44 | + Effect.gen(function* () { |
| 45 | + yield* Deferred.succeed(started, undefined) |
| 46 | + yield* Deferred.await(release) |
| 47 | + }), |
| 48 | + }), |
| 49 | + ) |
| 50 | + const handler = Context.get(context, HttpRouter.HttpRouter) |
| 51 | + .asHttpEffect() |
| 52 | + .pipe(HttpEffect.toWebHandlerWith(context)) |
| 53 | + const request = (method: "GET" | "POST", route: string) => |
| 54 | + Effect.promise((signal) => { |
| 55 | + const url = new URL(route, "http://opencode.local") |
| 56 | + url.searchParams.set("location[directory]", tmp.path) |
| 57 | + return handler( |
| 58 | + new Request(url, { |
| 59 | + method, |
| 60 | + headers: { authorization: `Basic ${btoa("opencode:secret")}` }, |
| 61 | + signal, |
| 62 | + }), |
| 63 | + ) |
| 64 | + }) |
| 65 | + const pending = yield* request("POST", "/api/plugin/await-activation").pipe(Effect.forkScoped) |
| 66 | + yield* Deferred.await(started) |
31 | 67 |
|
32 | | -it.live( |
33 | | - "gets providers without blocking on plugin initialization", |
34 | | - () => |
35 | | - Effect.gen(function* () { |
36 | | - const fixture = yield* configuredProvider("opencode-provider-get-endpoint-") |
37 | | - const url = new URL("/api/provider/custom", fixture.server.base) |
38 | | - url.searchParams.set("location[directory]", fixture.path) |
39 | | - const body: unknown = yield* Effect.tryPromise({ |
40 | | - try: async () => { |
41 | | - const response = await fetch(url, { headers: fixture.server.headers }) |
42 | | - if (response.status !== 200) throw new Error(`Provider not ready: ${response.status}`) |
43 | | - return response.json() |
44 | | - }, |
45 | | - catch: (cause) => cause, |
46 | | - }).pipe(Effect.retry(Schedule.spaced("10 millis")), Effect.timeout("2 seconds")) |
47 | | - if (!isRecord(body) || !isRecord(body["data"])) throw new Error("Expected a provider response") |
48 | | - expect(body["data"]["id"]).toBe("custom") |
| 68 | + // Config providers activate after SDK plugins; reads must return the current snapshot without waiting. |
| 69 | + const list = yield* request("GET", "/api/provider").pipe(Effect.timeout("2 seconds")) |
| 70 | + expect(list.status).toBe(200) |
| 71 | + expect(yield* Effect.promise(() => list.json())).toMatchObject({ |
| 72 | + location: { directory: tmp.path }, |
| 73 | + data: expect.not.arrayContaining([expect.objectContaining({ id: "custom" })]), |
| 74 | + }) |
| 75 | + const get = yield* request("GET", "/api/provider/custom").pipe(Effect.timeout("2 seconds")) |
| 76 | + expect(get.status).toBe(404) |
| 77 | + expect(yield* Effect.promise(() => get.json())).toMatchObject({ |
| 78 | + _tag: "ProviderNotFoundError", |
| 79 | + providerID: "custom", |
| 80 | + }) |
| 81 | + expect(pending.pollUnsafe()).toBeUndefined() |
| 82 | + |
| 83 | + yield* Deferred.succeed(release, undefined) |
| 84 | + expect((yield* Fiber.join(pending)).status).toBe(204) |
| 85 | + const provider = { |
| 86 | + id: "custom", |
| 87 | + name: "Configured Custom Provider", |
| 88 | + activation: "enabled", |
| 89 | + package: "@opencode-ai/ai/providers/openai-compatible", |
| 90 | + settings: { apiKey: "secret" }, |
| 91 | + } |
| 92 | + const configuredList = yield* request("GET", "/api/provider").pipe(Effect.timeout("2 seconds")) |
| 93 | + expect(configuredList.status).toBe(200) |
| 94 | + expect(yield* Effect.promise(() => configuredList.json())).toMatchObject({ |
| 95 | + location: { directory: tmp.path }, |
| 96 | + data: expect.arrayContaining([expect.objectContaining(provider)]), |
| 97 | + }) |
| 98 | + const configuredGet = yield* request("GET", "/api/provider/custom").pipe(Effect.timeout("2 seconds")) |
| 99 | + expect(configuredGet.status).toBe(200) |
| 100 | + expect(yield* Effect.promise(() => configuredGet.json())).toMatchObject({ |
| 101 | + location: { directory: tmp.path }, |
| 102 | + data: provider, |
| 103 | + }) |
49 | 104 | }), |
50 | 105 | 15_000, |
51 | 106 | ) |
52 | | - |
53 | | -const configuredProvider = Effect.fnUntraced(function* (prefix: string) { |
54 | | - const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir(prefix))) |
55 | | - yield* Effect.promise(() => |
56 | | - fs.writeFile( |
57 | | - path.join(tmp.path, "opencode.json"), |
58 | | - JSON.stringify({ |
59 | | - providers: { |
60 | | - custom: { |
61 | | - package: "@opencode-ai/ai/providers/openai-compatible", |
62 | | - settings: { apiKey: "secret" }, |
63 | | - models: { chat: {} }, |
64 | | - }, |
65 | | - }, |
66 | | - }), |
67 | | - ), |
68 | | - ) |
69 | | - return { server: yield* startServer(tmp.path), path: tmp.path } |
70 | | -}) |
71 | | - |
72 | | -function isRecord(value: unknown): value is Record<string, unknown> { |
73 | | - return typeof value === "object" && value !== null && !Array.isArray(value) |
74 | | -} |
|