|
| 1 | +import { DEFAULT_CATALOG_URL, CatalogFetchError } from '@moonshot-ai/kimi-code-sdk'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { fetchCatalogOrBuiltIn } from '#/utils/catalog-fetch'; |
| 5 | + |
| 6 | +const BUILT_IN = JSON.stringify({ |
| 7 | + anthropic: { |
| 8 | + id: 'anthropic', |
| 9 | + name: 'Anthropic', |
| 10 | + models: { 'claude-test': { id: 'claude-test', limit: { context: 200000 } } }, |
| 11 | + }, |
| 12 | +}); |
| 13 | + |
| 14 | +function jsonResponse(body: unknown, status = 200): Response { |
| 15 | + return new Response(JSON.stringify(body), { |
| 16 | + status, |
| 17 | + headers: { 'Content-Type': 'application/json' }, |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +describe('fetchCatalogOrBuiltIn', () => { |
| 22 | + it('returns the network catalog when models.dev is reachable', async () => { |
| 23 | + const network = { openai: { id: 'openai', models: {} } }; |
| 24 | + const fetchImpl = vi.fn(async () => jsonResponse(network)); |
| 25 | + |
| 26 | + const result = await fetchCatalogOrBuiltIn(DEFAULT_CATALOG_URL, { |
| 27 | + fetchImpl: fetchImpl as unknown as typeof fetch, |
| 28 | + builtInJson: BUILT_IN, |
| 29 | + }); |
| 30 | + |
| 31 | + expect(result.fromBuiltIn).toBe(false); |
| 32 | + expect(result.catalog).toEqual(network); |
| 33 | + }); |
| 34 | + |
| 35 | + it('falls back to the built-in snapshot when the default URL fetch fails', async () => { |
| 36 | + const fetchImpl = vi.fn(async () => jsonResponse('no', 503)); |
| 37 | + |
| 38 | + const result = await fetchCatalogOrBuiltIn(DEFAULT_CATALOG_URL, { |
| 39 | + fetchImpl: fetchImpl as unknown as typeof fetch, |
| 40 | + builtInJson: BUILT_IN, |
| 41 | + }); |
| 42 | + |
| 43 | + expect(result.fromBuiltIn).toBe(true); |
| 44 | + expect(result.catalog).toEqual(JSON.parse(BUILT_IN)); |
| 45 | + }); |
| 46 | + |
| 47 | + it('does not fall back for a custom catalog URL', async () => { |
| 48 | + const fetchImpl = vi.fn(async () => jsonResponse('no', 500)); |
| 49 | + |
| 50 | + await expect( |
| 51 | + fetchCatalogOrBuiltIn('https://example.test/private.json', { |
| 52 | + fetchImpl: fetchImpl as unknown as typeof fetch, |
| 53 | + builtInJson: BUILT_IN, |
| 54 | + }), |
| 55 | + ).rejects.toBeInstanceOf(CatalogFetchError); |
| 56 | + }); |
| 57 | + |
| 58 | + it('does not fall back when the caller aborted the request', async () => { |
| 59 | + const controller = new AbortController(); |
| 60 | + controller.abort(); |
| 61 | + const fetchImpl = vi.fn(async () => { |
| 62 | + throw new DOMException('Aborted', 'AbortError'); |
| 63 | + }); |
| 64 | + |
| 65 | + await expect( |
| 66 | + fetchCatalogOrBuiltIn(DEFAULT_CATALOG_URL, { |
| 67 | + signal: controller.signal, |
| 68 | + fetchImpl: fetchImpl as unknown as typeof fetch, |
| 69 | + builtInJson: BUILT_IN, |
| 70 | + }), |
| 71 | + ).rejects.toThrow(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('rethrows when fetch fails and no built-in snapshot is available', async () => { |
| 75 | + const fetchImpl = vi.fn(async () => jsonResponse('no', 500)); |
| 76 | + |
| 77 | + await expect( |
| 78 | + fetchCatalogOrBuiltIn(DEFAULT_CATALOG_URL, { |
| 79 | + fetchImpl: fetchImpl as unknown as typeof fetch, |
| 80 | + builtInJson: '', |
| 81 | + }), |
| 82 | + ).rejects.toBeInstanceOf(CatalogFetchError); |
| 83 | + }); |
| 84 | +}); |
0 commit comments