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

ModelManifest.test.ts

472 lines14.5 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { ProviderDriverKind, type ServerProviderModel } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Path from "effect/Path";
8import * as TestClock from "effect/testing/TestClock";
9import { HttpClient, HttpClientResponse } from "effect/unstable/http";
10​
11import * as ServerConfig from "../config.ts";
12import * as ServerSettings from "../serverSettings.ts";
13import {
14 applyManifestDefault,
15 BUNDLED_MODEL_MANIFEST,
16 classifyModels,
17 make,
18 resolveProviderCatalog,
19 type ModelManifestData,
20 encodeManifestCache,
21} from "./ModelManifest.ts";
22​
23/**
24 * Test policy: this file covers manifest machinery, not manifest contents.
25 * Do not add assertions for real model slugs, names, status, aliases, or
26 * profiles when editing model-manifest.json. Add tests only when fetch/cache
27 * behavior or the provider-neutral resolver semantics change, and use
28 * synthetic models for resolver coverage.
29 */
30​
31const CODEX = ProviderDriverKind.make("codex");
32const model = (overrides: Partial<ServerProviderModel>): ServerProviderModel => ({
33 slug: "gpt-test",
34 name: "GPT Test",
35 isCustom: false,
36 capabilities: null,
37 ...overrides,
38});
39​
40describe("classifyModels", () => {
41 it("classifies qualified Codex families without changing their wire ids", () => {
42 const manifest: ModelManifestData = { version: 1, currentModels: { codex: ["gpt-test"] } };
43 const models = [
44 model({ slug: "openai.gpt-test", isLegacy: true }),
45 model({ slug: "openai.gpt-old" }),
46 ];
47 assert.deepStrictEqual(
48 classifyModels(models, manifest, CODEX).map((entry) => [entry.slug, entry.isLegacy ?? false]),
49 [
50 ["openai.gpt-test", false],
51 ["openai.gpt-old", true],
52 ],
53 );
54 });
55 it("flags non-current models, clears stale flags, and skips custom models", () => {
56 const manifest: ModelManifestData = {
57 version: 1,
58 currentModels: { codex: ["current-a", "current-b"] },
59 };
60 const models = [
61 model({ slug: "current-a" }),
62 // Stale flag from a previous classification pass must be cleared.
63 model({ slug: "current-b", isLegacy: true }),
64 model({ slug: "old-model" }),
65 // Custom models are user-defined and never reclassified.
66 model({ slug: "my-own-model", isCustom: true }),
67 ];
68 assert.deepStrictEqual(
69 classifyModels(models, manifest, CODEX).map((entry) => [entry.slug, entry.isLegacy ?? false]),
70 [
71 ["current-a", false],
72 ["current-b", false],
73 ["old-model", true],
74 ["my-own-model", false],
75 ],
76 );
77 });
78});
79​
80describe("applyManifestDefault", () => {
81 it("resolves the manifest default to the qualified live model", () => {
82 const manifest: ModelManifestData = {
83 version: 1,
84 currentModels: {},
85 providers: { codex: { models: [], profiles: {}, defaults: { chat: "gpt-test" } } },
86 };
87 const models = [
88 model({ slug: "openai.gpt-old", isDefault: true }),
89 model({ slug: "openai.gpt-test" }),
90 ];
91 assert.strictEqual(
92 applyManifestDefault(models, manifest, CODEX).find((entry) => entry.isDefault)?.slug,
93 "openai.gpt-test",
94 );
95 });
96 it("moves the default flag and its aliases to the manifest's chat default", () => {
97 const driver = ProviderDriverKind.make("antigravity");
98 const manifest: ModelManifestData = {
99 version: 1,
100 currentModels: {},
101 providers: {
102 antigravity: {
103 defaults: { chat: "gemini-new" },
104 profiles: {},
105 models: [{ slug: "gemini-new", name: "New", status: "current" }],
106 },
107 },
108 };
109 const models = [
110 model({ slug: "gemini-old", isDefault: true, aliases: ["antigravity-default"] }),
111 model({ slug: "gemini-new" }),
112 ];
113 assert.deepStrictEqual(applyManifestDefault(models, manifest, driver), [
114 model({ slug: "gemini-old" }),
115 model({ slug: "gemini-new", isDefault: true, aliases: ["antigravity-default"] }),
116 ]);
117 // The account does not offer the manifest default: keep the runtime's choice.
118 assert.deepStrictEqual(
119 applyManifestDefault(models.slice(0, 1), manifest, driver),
120 models.slice(0, 1),
121 );
122 });
123});
124​
125describe("resolveProviderCatalog", () => {
126 it("resolves generic model presentation through a reusable profile", () => {
127 const manifest: ModelManifestData = {
128 version: 1,
129 currentModels: {},
130 providers: {
131 synthetic: {
132 defaults: { chat: "model-next" },
133 profiles: {
134 standard: {
135 capabilities: {
136 optionDescriptors: [
137 {
138 id: "mode",
139 label: "Mode",
140 type: "select",
141 options: [{ id: "fast", label: "Fast", isDefault: true }],
142 },
143 ],
144 },
145 adapter: { opaque: true },
146 },
147 },
148 models: [
149 {
150 slug: "model-next",
151 name: "Model Next",
152 aliases: ["next"],
153 status: "current",
154 badge: "new",
155 profile: "standard",
156 },
157 ],
158 },
159 },
160 };
161​
162 const catalog = resolveProviderCatalog(manifest, ProviderDriverKind.make("synthetic"));
163 assert.deepStrictEqual(catalog?.models[0], {
164 model: {
165 slug: "model-next",
166 name: "Model Next",
167 aliases: ["next"],
168 badge: "new",
169 isCustom: false,
170 isDefault: true,
171 capabilities: manifest.providers!.synthetic!.profiles.standard!.capabilities!,
172 },
173 adapter: undefined,
174 profileAdapter: { opaque: true },
175 });
176 });
177​
178 it("rejects invalid catalog references", () => {
179 const invalidCatalog = (input: {
180 readonly models: NonNullable<ModelManifestData["providers"]>[string]["models"];
181 readonly defaultChat?: string;
182 }): ModelManifestData => ({
183 version: 1,
184 currentModels: {},
185 providers: {
186 synthetic: {
187 ...(input.defaultChat ? { defaults: { chat: input.defaultChat } } : {}),
188 profiles: {},
189 models: input.models,
190 },
191 },
192 });
193​
194 for (const invalid of [
195 invalidCatalog({
196 models: [
197 { slug: "duplicate", name: "First", status: "current" },
198 { slug: "duplicate", name: "Second", status: "current" },
199 ],
200 }),
201 invalidCatalog({
202 models: [
203 {
204 slug: "missing-profile",
205 name: "Missing Profile",
206 status: "current",
207 profile: "missing",
208 },
209 ],
210 }),
211 invalidCatalog({
212 models: [{ slug: "present", name: "Present", status: "current" }],
213 defaultChat: "absent",
214 }),
215 ]) {
216 assert.isNull(resolveProviderCatalog(invalid, ProviderDriverKind.make("synthetic")));
217 }
218 });
219});
220​
221// Remote fixtures date after the bundle so a fetch still outranks it.
222const REMOTE_UPDATED_AT = "2099-01-01T00:00:00Z";
223​
224const REMOTE_MANIFEST: ModelManifestData = {
225 version: 1,
226 updatedAt: REMOTE_UPDATED_AT,
227 currentModels: {
228 codex: ["remote-model"],
229 claudeAgent: ["remote-agent-model"],
230 },
231};
232​
233const REMOTE_CLAUDE_MANIFEST: ModelManifestData = {
234 version: 1,
235 updatedAt: REMOTE_UPDATED_AT,
236 currentModels: {},
237 providers: {
238 claudeAgent: {
239 profiles: {
240 synthetic: {
241 adapter: { claudeCode: { effortMap: { extreme: "high" } } },
242 },
243 },
244 models: [
245 {
246 slug: "remote-only-model",
247 name: "Remote Only Model",
248 status: "current",
249 profile: "synthetic",
250 },
251 ],
252 },
253 },
254};
255​
256const remoteClaudeManifestWithCompatibility = (compatibility: unknown): ModelManifestData => ({
257 ...REMOTE_CLAUDE_MANIFEST,
258 providers: {
259 claudeAgent: {
260 profiles: REMOTE_CLAUDE_MANIFEST.providers!.claudeAgent!.profiles,
261 models: REMOTE_CLAUDE_MANIFEST.providers!.claudeAgent!.models.map((model) => ({
262 ...model,
263 adapter: { claudeCode: compatibility },
264 })),
265 },
266 },
267});
268​
269const INVALID_REMOTE_MANIFESTS: ReadonlyArray<ModelManifestData> = [
270 {
271 ...REMOTE_CLAUDE_MANIFEST,
272 providers: {
273 claudeAgent: {
274 profiles: {
275 synthetic: {
276 adapter: { claudeCode: { effortMap: { extreme: 123 } } },
277 },
278 },
279 models: REMOTE_CLAUDE_MANIFEST.providers!.claudeAgent!.models,
280 },
281 },
282 },
283 {
284 ...REMOTE_CLAUDE_MANIFEST,
285 providers: {
286 claudeAgent: {
287 profiles: {},
288 models: REMOTE_CLAUDE_MANIFEST.providers!.claudeAgent!.models,
289 },
290 },
291 },
292 {
293 ...REMOTE_CLAUDE_MANIFEST,
294 providers: {
295 claudeAgent: {
296 profiles: REMOTE_CLAUDE_MANIFEST.providers!.claudeAgent!.profiles,
297 models: [
298 ...REMOTE_CLAUDE_MANIFEST.providers!.claudeAgent!.models,
299 {
300 slug: "remote-only-model",
301 name: "Duplicate Remote Model",
302 status: "current",
303 profile: "synthetic",
304 },
305 ],
306 },
307 },
308 },
309 {
310 ...REMOTE_CLAUDE_MANIFEST,
311 providers: {
312 claudeAgent: {
313 defaults: { chat: "absent-model" },
314 profiles: REMOTE_CLAUDE_MANIFEST.providers!.claudeAgent!.profiles,
315 models: REMOTE_CLAUDE_MANIFEST.providers!.claudeAgent!.models,
316 },
317 },
318 },
319 remoteClaudeManifestWithCompatibility({ minVersion: "2.x" }),
320 remoteClaudeManifestWithCompatibility({ maxVersionExclusive: "2.x" }),
321 remoteClaudeManifestWithCompatibility({
322 minVersion: "2.2",
323 maxVersionExclusive: "2.1",
324 }),
325];
326​
327const httpClientLayer = (handler: () => Response) =>
328 Layer.succeed(
329 HttpClient.HttpClient,
330 HttpClient.make((request) => Effect.succeed(HttpClientResponse.fromWeb(request, handler()))),
331 );
332​
333const serviceLayers = (input: {
334 readonly prefix: string;
335 readonly response: () => Response;
336 readonly settings?: Parameters<typeof ServerSettings.layerTest>[0];
337}) =>
338 ServerConfig.layerTest(process.cwd(), { prefix: input.prefix }).pipe(
339 Layer.provideMerge(NodeServices.layer),
340 Layer.provideMerge(ServerSettings.layerTest(input.settings ?? {})),
341 Layer.provideMerge(httpClientLayer(input.response)),
342 );
343​
344describe("ModelManifest service", () => {
345 it.live("prefers a fetched manifest over the bundle and caches it to disk", () =>
346 Effect.gen(function* () {
347 const service = yield* make;
348 const refreshed = yield* service.refresh;
349 assert.deepStrictEqual(refreshed, REMOTE_MANIFEST);
350​
351 // A fresh service instance sees the disk cache without another fetch:
352 // its HTTP layer is still stubbed, but `current` never fetches at all.
353 const rebooted = yield* make;
354 assert.deepStrictEqual(yield* rebooted.current, REMOTE_MANIFEST);
355 }).pipe(
356 Effect.scoped,
357 Effect.provide(
358 serviceLayers({
359 prefix: "model-manifest-fetch-test",
360 response: () => Response.json(REMOTE_MANIFEST),
361 }),
362 ),
363 ),
364 );
365​
366 it.live("keeps the bundled manifest when the remote payload is malformed", () =>
367 Effect.gen(function* () {
368 const service = yield* make;
369 assert.deepStrictEqual(yield* service.refresh, BUNDLED_MODEL_MANIFEST);
370 }).pipe(
371 Effect.scoped,
372 Effect.provide(
373 serviceLayers({
374 prefix: "model-manifest-malformed-test",
375 response: () => Response.json({ version: 999, nonsense: true }),
376 }),
377 ),
378 ),
379 );
380​
381 it.effect("preserves the last-good remote cache when later payloads are invalid", () => {
382 let responseIndex = 0;
383 const responses = [REMOTE_CLAUDE_MANIFEST, ...INVALID_REMOTE_MANIFESTS];
384​
385 return Effect.gen(function* () {
386 const service = yield* make;
387 assert.deepStrictEqual(yield* service.refresh, REMOTE_CLAUDE_MANIFEST);
388​
389 for (const _invalid of INVALID_REMOTE_MANIFESTS) {
390 yield* TestClock.adjust("1 hour");
391 responseIndex += 1;
392 assert.deepStrictEqual(yield* service.refresh, REMOTE_CLAUDE_MANIFEST);
393 }
394​
395 const rebooted = yield* make;
396 assert.deepStrictEqual(yield* rebooted.current, REMOTE_CLAUDE_MANIFEST);
397 }).pipe(
398 Effect.scoped,
399 Effect.provide(
400 serviceLayers({
401 prefix: "model-manifest-last-good-test",
402 response: () => Response.json(responses[responseIndex]),
403 }),
404 ),
405 );
406 });
407​
408 it.live("drops a disk cache of a manifest older than the bundled one", () =>
409 Effect.gen(function* () {
410 const fs = yield* FileSystem.FileSystem;
411 const path = yield* Path.Path;
412 const config = yield* ServerConfig.ServerConfig;
413 const cachePath = path.join(config.stateDir, "model-manifest.json");
414 // A cache of the manifest as it was before the release edited it. The
415 // fetch time is irrelevant: the remote may be unreachable now, so
416 // `current` must already prefer the bundle.
417 const { updatedAt: _undated, ...undatedManifest } = REMOTE_MANIFEST;
418 for (const stale of [
419 undatedManifest,
420 { ...REMOTE_MANIFEST, updatedAt: "2000-01-01T00:00:00Z" },
421 ]) {
422 yield* fs.writeFileString(
423 cachePath,
424 yield* encodeManifestCache({ fetchedAtMs: 0, manifest: stale }),
425 );
426 const service = yield* make;
427 assert.deepStrictEqual(yield* service.current, BUNDLED_MODEL_MANIFEST);
428 }
429​
430 // A cache of a newer edit still outranks the bundle.
431 yield* fs.writeFileString(
432 cachePath,
433 yield* encodeManifestCache({ fetchedAtMs: 0, manifest: REMOTE_MANIFEST }),
434 );
435 const later = yield* make;
436 assert.deepStrictEqual(yield* later.current, REMOTE_MANIFEST);
437 }).pipe(
438 Effect.scoped,
439 Effect.provide(
440 serviceLayers({
441 prefix: "model-manifest-newer-bundle-test",
442 response: () => Response.json(REMOTE_MANIFEST),
443 }),
444 ),
445 ),
446 );
447​
448 it.live("does not fetch when provider update checks are disabled", () =>
449 Effect.gen(function* () {
450 let fetchCount = 0;
451 const service = yield* make.pipe(
452 Effect.provide(
453 httpClientLayer(() => {
454 fetchCount += 1;
455 return Response.json(REMOTE_MANIFEST);
456 }),
457 ),
458 );
459 assert.deepStrictEqual(yield* service.refresh, BUNDLED_MODEL_MANIFEST);
460 assert.strictEqual(fetchCount, 0);
461 }).pipe(
462 Effect.scoped,
463 Effect.provide(
464 serviceLayers({
465 prefix: "model-manifest-optout-test",
466 response: () => Response.json(REMOTE_MANIFEST),
467 settings: { enableProviderUpdateChecks: false },
468 }),
469 ),
470 ),
471 );
472});