Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import { HttpClient, HttpClientResponse } from "effect/http";
5import { describe } from "vite-plus/test";
6
7import { loadOpenCode2Catalog } from "./openCode2Catalog.ts";
8import * as OpenCode2Client from "./v2/OpenCode2Client.ts";
9
10const MODELS_BODY = `{"location":{"directory":"/project"},"data":[{"id":"step-5-preview-free","modelID":"step-5-preview-free","providerID":"opencode-go","name":"Step 5 Preview Free","compatibility":{"reasoningField":"reasoning_content"},"package":"@opencode/ai/providers/openai-compatible","settings":{"apiKey":"sk-demo-not-real","baseURL":"http://127.0.0.1:48990/v1","provider":"opencode-go"},"capabilities":{"tools":true,"input":["text","image","video"],"output":["text"]},"variants":[{"id":"low","settings":{"reasoningEffort":"low"}},{"id":"medium","settings":{"reasoningEffort":"medium"}},{"id":"high","settings":{"reasoningEffort":"high"}}],"time":{"released":1789516800000},"cost":[{"input":0,"output":0,"cache":{"read":0,"write":0}}],"status":"active","enabled":true,"limit":{"context":1000000,"input":1000000,"output":65536}},{"id":"step-5-preview-free","modelID":"step-5-preview-free","providerID":"opencode","name":"Step 5 Preview Free","compatibility":{"reasoningField":"reasoning_content"},"package":"@opencode/ai/providers/openai-compatible","settings":{"apiKey":"public","baseURL":"https://opencode.ai/zen/v1","provider":"opencode"},"capabilities":{"tools":true,"input":["text","image","video"],"output":["text"]},"variants":[{"id":"low","settings":{"reasoningEffort":"low"}},{"id":"medium","settings":{"reasoningEffort":"medium"}},{"id":"high","settings":{"reasoningEffort":"high"}}],"time":{"released":1789516800000},"cost":[{"input":0,"output":0,"cache":{"read":0,"write":0}}],"status":"active","enabled":true,"limit":{"context":1000000,"input":1000000,"output":65536}}]}`;
11
12const PROVIDERS_BODY = `{"location":{"directory":"/project"},"data":[{"id":"opencode-go","integrationID":"opencode-go","name":"OpenCode Go","activation":"enabled","package":"@opencode/ai/providers/openai-compatible","settings":{"apiKey":"sk-demo-not-real","baseURL":"http://127.0.0.1:48990/v1","provider":"opencode-go"}},{"id":"opencode","integrationID":"opencode","name":"OpenCode Zen","activation":"enabled","package":"@opencode/ai/providers/openai-compatible","settings":{"apiKey":"public","baseURL":"https://opencode.ai/zen/v1"}}]}`;
13
14const layerServerReplying = (
15 bodies: Readonly<Record<string, string>>,
16 hanging: ReadonlyArray<string> = [],
17) =>
18 Layer.succeed(
19 HttpClient.HttpClient,
20 HttpClient.make((request) =>
21 Effect.suspend(() => {
22 const path = new URL(request.url).pathname;
23 if (hanging.includes(path)) {
24 return Effect.never;
25 }
26 const body = bodies[path];
27 return Effect.succeed(
28 HttpClientResponse.fromWeb(
29 request,
30 new Response(body ?? '{"_tag":"NotFoundError","message":"not recorded"}', {
31 status: body === undefined ? 404 : 200,
32 headers: { "content-type": "application/json" },
33 }),
34 ),
35 );
36 }),
37 ),
38 );
39
40const readCatalog = (
41 bodies: Readonly<Record<string, string>>,
42 hanging: ReadonlyArray<string> = [],
43) =>
44 Effect.gen(function* () {
45 const opencode = yield* OpenCode2Client.OpenCode2Client;
46 const { client } = yield* opencode.connect({
47 baseUrl: "http://127.0.0.1:4096",
48 password: "secret",
49 });
50 return yield* loadOpenCode2Catalog(client, "/project");
51 }).pipe(
52 Effect.provide(OpenCode2Client.layer.pipe(Layer.provide(layerServerReplying(bodies, hanging)))),
53 );
54
55describe("loadOpenCode2Catalog", () => {
56 it.effect("names the provider each listed model came from", () =>
57 Effect.gen(function* () {
58 const models = yield* readCatalog({
59 "/api/model": MODELS_BODY,
60 "/api/provider": PROVIDERS_BODY,
61 });
62
63 assert.deepStrictEqual(
64 models.map((model) => [`${model.providerID}/${model.id}`, model.providerName]),
65 [
66 ["opencode-go/step-5-preview-free", "OpenCode Go"],
67 ["opencode/step-5-preview-free", "OpenCode Zen"],
68 ],
69 );
70 }),
71 );
72
73 it.effect("leaves a model unnamed when the provider list fails", () =>
74 Effect.gen(function* () {
75 const models = yield* readCatalog({ "/api/model": MODELS_BODY });
76
77 assert.deepStrictEqual(
78 models.map((model) => [`${model.providerID}/${model.id}`, model.providerName]),
79 [
80 ["opencode-go/step-5-preview-free", undefined],
81 ["opencode/step-5-preview-free", undefined],
82 ],
83 );
84 }),
85 );
86
87 // regression: #17424 only the provider list is optional; a model-list failure fails the read
88 it.effect("fails the read when the model list fails", () =>
89 Effect.gen(function* () {
90 const error = yield* readCatalog({
91 "/api/model": '{"_tag":"InternalServerError","message":"catalog unavailable"}',
92 "/api/provider": PROVIDERS_BODY,
93 }).pipe(Effect.flip);
94
95 assert.strictEqual(error.operation, "model.list");
96 assert.match(error.detail, /could not list its models/i);
97 }),
98 );
99});