Skip to content
Source
Sign inSign up
Open History to see the last commit

openCode2Catalog.test.ts

112 lines5.6 KB

1import { assert, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Fiber from "effect/Fiber";
4import * as Layer from "effect/Layer";
5import * as TestClock from "effect/testing/TestClock";
6import { HttpClient, HttpClientResponse } from "effect/http";
7import { describe } from "vite-plus/test";
8​
9import { loadOpenCode2Catalog } from "./openCode2Catalog.ts";
10import * as OpenCode2Client from "./v2/OpenCode2Client.ts";
11​
12const 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}}]}`;
13​
14const 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"}}]}`;
15​
16const layerServerReplying = (
17 bodies: Readonly<Record<string, string>>,
18 hanging: ReadonlyArray<string> = [],
19) =>
20 Layer.succeed(
21 HttpClient.HttpClient,
22 HttpClient.make((request) =>
23 Effect.suspend(() => {
24 const path = new URL(request.url).pathname;
25 if (hanging.includes(path)) {
26 return Effect.never;
27 }
28 const body = bodies[path];
29 return Effect.succeed(
30 HttpClientResponse.fromWeb(
31 request,
32 new Response(body ?? '{"_tag":"NotFoundError","message":"not recorded"}', {
33 status: body === undefined ? 404 : 200,
34 headers: { "content-type": "application/json" },
35 }),
36 ),
37 );
38 }),
39 ),
40 );
41​
42const readCatalog = (
43 bodies: Readonly<Record<string, string>>,
44 hanging: ReadonlyArray<string> = [],
45) =>
46 Effect.gen(function* () {
47 const opencode = yield* OpenCode2Client.OpenCode2Client;
48 const { client } = yield* opencode.connect({
49 baseUrl: "http://127.0.0.1:4096",
50 password: "secret",
51 });
52 return yield* loadOpenCode2Catalog(client, "/project");
53 }).pipe(
54 Effect.provide(OpenCode2Client.layer.pipe(Layer.provide(layerServerReplying(bodies, hanging)))),
55 );
56​
57describe("loadOpenCode2Catalog", () => {
58 it.effect("names the provider each listed model came from", () =>
59 Effect.gen(function* () {
60 const models = yield* readCatalog({
61 "/api/model": MODELS_BODY,
62 "/api/provider": PROVIDERS_BODY,
63 });
64​
65 assert.deepStrictEqual(
66 models.map((model) => [`${model.providerID}/${model.id}`, model.providerName]),
67 [
68 ["opencode-go/step-5-preview-free", "OpenCode Go"],
69 ["opencode/step-5-preview-free", "OpenCode Zen"],
70 ],
71 );
72 }),
73 );
74​
75 it.effect("leaves a model unnamed when the provider list fails", () =>
76 Effect.gen(function* () {
77 const models = yield* readCatalog({ "/api/model": MODELS_BODY });
78​
79 assert.deepStrictEqual(
80 models.map((model) => [`${model.providerID}/${model.id}`, model.providerName]),
81 [
82 ["opencode-go/step-5-preview-free", undefined],
83 ["opencode/step-5-preview-free", undefined],
84 ],
85 );
86 }),
87 );
88​
89 it.effect("lists no models yet when the model list does not answer", () =>
90 Effect.gen(function* () {
91 const fiber = yield* Effect.forkChild(
92 readCatalog({ "/api/provider": PROVIDERS_BODY }, ["/api/model"]),
93 );
94 yield* TestClock.adjust("5 seconds");
95​
96 assert.deepStrictEqual(yield* Fiber.join(fiber), []);
97 }),
98 );
99​
100 // regression: #17424 only the provider list is optional; a model-list failure fails the read
101 it.effect("fails the read when the model list fails", () =>
102 Effect.gen(function* () {
103 const error = yield* readCatalog({
104 "/api/model": '{"_tag":"InternalServerError","message":"catalog unavailable"}',
105 "/api/provider": PROVIDERS_BODY,
106 }).pipe(Effect.flip);
107​
108 assert.strictEqual(error.operation, "model.list");
109 assert.match(error.detail, /could not list its models/i);
110 }),
111 );
112});