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

CodexChatGptModels.test.ts

75 lines2.9 KB

1// @effect-diagnostics preferSchemaOverJson:off - Mock HTTP responses use JSON fixtures.
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import { HttpClient, HttpClientResponse } from "effect/unstable/http";
5import { chatGptModels } from "./CodexChatGptModels.ts";
6​
7it.effect(
8 "uses each selected profile's token and the server's visible catalog order and names",
9 () =>
10 Effect.gen(function* () {
11 const requests: string[] = [];
12 const http = HttpClient.make((request) =>
13 Effect.sync(() => {
14 assert.strictEqual(request.url, "https://api.openai.com/v1/models");
15 requests.push(request.headers.authorization!);
16 return HttpClientResponse.fromWeb(
17 request,
18 new Response(
19 JSON.stringify({
20 models:
21 request.headers.authorization === "Bearer account-a"
22 ? [
23 { slug: "second", display_name: "Second from OpenAI", visibility: "list" },
24 { slug: "hidden", display_name: "Hidden", visibility: "hidden" },
25 { slug: "first", display_name: "First from OpenAI", visibility: "list" },
26 ]
27 : [{ slug: "account-b-only", display_name: "B", visibility: "list" }],
28 }),
29 ),
30 );
31 }),
32 );
33 const native = [
34 {
35 slug: "first",
36 name: "Cached first",
37 isCustom: false,
38 capabilities: { optionDescriptors: [] },
39 },
40 { slug: "not-entitled", name: "Cached other", isCustom: false, capabilities: null },
41 ];
42 const a = yield* chatGptModels("account-a", native).pipe(
43 Effect.provideService(HttpClient.HttpClient, http),
44 );
45 assert.deepEqual(
46 a.map((model) => [model.slug, model.name]),
47 [
48 ["second", "Second from OpenAI"],
49 ["first", "First from OpenAI"],
50 ],
51 );
52 assert.deepEqual(a[1]!.capabilities, native[0]!.capabilities);
53 assert.isNull(a[0]!.capabilities);
54 const b = yield* chatGptModels("account-b", native).pipe(
55 Effect.provideService(HttpClient.HttpClient, http),
56 );
57 assert.deepEqual(
58 b.map((model) => model.slug),
59 ["account-b-only"],
60 );
61 assert.deepEqual(requests, ["Bearer account-a", "Bearer account-b"]);
62 }),
63);
64​
65it.effect("does not present a cached catalog as account entitlements when discovery fails", () =>
66 Effect.gen(function* () {
67 const http = HttpClient.make((request) =>
68 Effect.succeed(HttpClientResponse.fromWeb(request, new Response(null, { status: 503 }))),
69 );
70 const error = yield* Effect.flip(
71 chatGptModels("account-a", []).pipe(Effect.provideService(HttpClient.HttpClient, http)),
72 );
73 assert.strictEqual(error._tag, "ChatGptCatalogError");
74 }),
75);