import fs from "node:fs/promises" import path from "node:path" import { expect } from "bun:test" import { Effect } from "effect" import { tmpdir } from "../../core/test/fixture/tmpdir" import { it } from "../../core/test/lib/effect" import { serveRoutes } from "./fixture/routes" it.live("lists configured models filtered by type", () => Effect.gen(function* () { const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-model-endpoint-"))) yield* Effect.promise(() => fs.writeFile( path.join(tmp.path, "opencode.json"), JSON.stringify({ providers: { custom: { package: "aisdk:@ai-sdk/openai-compatible", settings: { apiKey: "secret" }, models: { chat: {} }, }, }, }), ), ) const server = yield* serveRoutes({ database: { path: ":memory:" }, config: { directory: tmp.path }, fs: { filewatcher: false }, models: { fetch: false }, }) yield* server.pluginsLoaded(tmp.path) const url = new URL("/api/model", "http://opencode.local") url.searchParams.set("location[directory]", tmp.path) const response = yield* Effect.promise(() => server.fetch(url)) expect(response.status).toBe(200) const body: unknown = yield* Effect.promise(() => response.json()) if (!isRecord(body) || !Array.isArray(body["data"])) throw new Error("Expected a model list response") expect( body["data"].some((model) => isRecord(model) && model["providerID"] === "custom" && model["id"] === "chat"), ).toBeTrue() url.searchParams.set("types", "image") const images = yield* Effect.promise(() => server.fetch(url)) expect(images.status).toBe(200) const imageBody: unknown = yield* Effect.promise(() => images.json()) if (!isRecord(imageBody) || !Array.isArray(imageBody["data"])) throw new Error("Expected a model list response") expect(imageBody["data"].some((model) => isRecord(model) && model["providerID"] === "custom")).toBeFalse() url.searchParams.append("types", "chat") const both = yield* Effect.promise(() => server.fetch(url)) expect(both.status).toBe(200) const bothBody: unknown = yield* Effect.promise(() => both.json()) if (!isRecord(bothBody) || !Array.isArray(bothBody["data"])) throw new Error("Expected a model list response") expect(bothBody["data"].some((model) => isRecord(model) && model["id"] === "chat")).toBeTrue() }), ) function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value) }