|
| 1 | +import fs from "node:fs/promises" |
| 2 | +import path from "node:path" |
| 3 | +import { expect } from "bun:test" |
| 4 | +import { Effect } from "effect" |
| 5 | +import { tmpdir } from "../../core/test/fixture/tmpdir" |
| 6 | +import { it } from "../../core/test/lib/effect" |
| 7 | +import { startServer } from "./fixture/server" |
| 8 | + |
| 9 | +it.live( |
| 10 | + "waits for plugin initialization on the first provider list request", |
| 11 | + () => |
| 12 | + 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 | + const response = yield* Effect.promise(() => fetch(url, { headers: fixture.server.headers })) |
| 17 | + |
| 18 | + expect(response.status).toBe(200) |
| 19 | + const body: unknown = yield* Effect.promise(() => response.json()) |
| 20 | + if (!isRecord(body) || !Array.isArray(body["data"])) throw new Error("Expected a provider list response") |
| 21 | + expect(body["data"].some((provider) => isRecord(provider) && provider["id"] === "custom")).toBeTrue() |
| 22 | + }), |
| 23 | + 15_000, |
| 24 | +) |
| 25 | + |
| 26 | +it.live( |
| 27 | + "waits for plugin initialization on the first provider get request", |
| 28 | + () => |
| 29 | + Effect.gen(function* () { |
| 30 | + const fixture = yield* configuredProvider("opencode-provider-get-endpoint-") |
| 31 | + const url = new URL("/api/provider/custom", fixture.server.base) |
| 32 | + url.searchParams.set("location[directory]", fixture.path) |
| 33 | + const response = yield* Effect.promise(() => fetch(url, { headers: fixture.server.headers })) |
| 34 | + |
| 35 | + expect(response.status).toBe(200) |
| 36 | + const body: unknown = yield* Effect.promise(() => response.json()) |
| 37 | + if (!isRecord(body) || !isRecord(body["data"])) throw new Error("Expected a provider response") |
| 38 | + expect(body["data"]["id"]).toBe("custom") |
| 39 | + }), |
| 40 | + 15_000, |
| 41 | +) |
| 42 | + |
| 43 | +const configuredProvider = Effect.fnUntraced(function* (prefix: string) { |
| 44 | + const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir(prefix))) |
| 45 | + yield* Effect.promise(() => |
| 46 | + fs.writeFile( |
| 47 | + path.join(tmp.path, "opencode.json"), |
| 48 | + JSON.stringify({ |
| 49 | + providers: { |
| 50 | + custom: { |
| 51 | + package: "@opencode-ai/ai/providers/openai-compatible", |
| 52 | + settings: { apiKey: "secret" }, |
| 53 | + models: { chat: {} }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }), |
| 57 | + ), |
| 58 | + ) |
| 59 | + return { server: yield* startServer(tmp.path), path: tmp.path } |
| 60 | +}) |
| 61 | + |
| 62 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 63 | + return typeof value === "object" && value !== null && !Array.isArray(value) |
| 64 | +} |
0 commit comments