|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + parseRemoteModelCatalogBundle, |
| 4 | + validateAndSanitizeRemoteModelCatalogBundle, |
| 5 | +} from "./remote-catalog-bundle.js"; |
| 6 | + |
| 7 | +const validBundle = { |
| 8 | + schemaVersion: 1, |
| 9 | + generatedAt: 1_753_500_000_000, |
| 10 | + minVersion: "2026.7.0", |
| 11 | + sourceCommit: "abc123", |
| 12 | + providers: { |
| 13 | + anthropic: { |
| 14 | + baseUrl: "https://evil.test", |
| 15 | + headers: { Authorization: "bad" }, |
| 16 | + models: [ |
| 17 | + { |
| 18 | + id: "claude-test", |
| 19 | + baseUrl: "https://evil.test/model", |
| 20 | + headers: { "X-Evil": "yes" }, |
| 21 | + compat: { nested: { baseUrl: "https://evil.test/nested", headers: { X: "y" } } }, |
| 22 | + }, |
| 23 | + ], |
| 24 | + }, |
| 25 | + }, |
| 26 | +} as const; |
| 27 | + |
| 28 | +describe("remote model catalog bundle", () => { |
| 29 | + it("accepts schema v1 and deeply strips endpoint and header fields", () => { |
| 30 | + const parsed = validateAndSanitizeRemoteModelCatalogBundle(validBundle); |
| 31 | + const anthropic = parsed.providers.anthropic; |
| 32 | + expect(anthropic).toBeDefined(); |
| 33 | + if (!anthropic) { |
| 34 | + throw new Error("expected anthropic provider"); |
| 35 | + } |
| 36 | + expect(anthropic).not.toHaveProperty("baseUrl"); |
| 37 | + expect(anthropic).not.toHaveProperty("headers"); |
| 38 | + expect(anthropic.models[0]).not.toHaveProperty("baseUrl"); |
| 39 | + expect(anthropic.models[0]).not.toHaveProperty("headers"); |
| 40 | + expect(anthropic.models[0]?.compat).toEqual({ nested: {} }); |
| 41 | + }); |
| 42 | + |
| 43 | + it("rejects unsupported versions, invalid timestamps, and malformed providers", () => { |
| 44 | + expect(() => parseRemoteModelCatalogBundle({ ...validBundle, schemaVersion: 2 })).toThrow(); |
| 45 | + expect(() => parseRemoteModelCatalogBundle({ ...validBundle, generatedAt: 0 })).toThrow(); |
| 46 | + expect(() => |
| 47 | + parseRemoteModelCatalogBundle({ |
| 48 | + ...validBundle, |
| 49 | + generatedAt: Date.now() + 2 * 24 * 60 * 60_000, |
| 50 | + }), |
| 51 | + ).toThrow("generatedAt is implausibly far in the future"); |
| 52 | + expect(() => |
| 53 | + parseRemoteModelCatalogBundle({ ...validBundle, providers: { anthropic: { models: [] } } }), |
| 54 | + ).toThrow(); |
| 55 | + expect(() => |
| 56 | + parseRemoteModelCatalogBundle({ |
| 57 | + ...validBundle, |
| 58 | + providers: { anthropic: { models: [{ id: " duplicate " }, { id: "duplicate" }] } }, |
| 59 | + }), |
| 60 | + ).toThrow("duplicate model id: duplicate"); |
| 61 | + }); |
| 62 | +}); |
0 commit comments