import { describe, it, expect } from 'vitest'; import { extractGroups, normaliseCatalog } from '../src/lib/catalog/loader'; describe('Catalogue Tolerant Parser & Normaliser (PRD §7.4, CAT-01..09)', () => { it('accepts an array of groups', () => { const raw = [ { title: 'Group 1', endpoints: [ { id: 'ep-1', full_url: 'http://127.0.0.1:6666/ep1/image/', path: '/ep1/image/', params: [{ n: 'image', ty: 'file (binary)', rq: true }], }, ], }, ]; const groups = extractGroups(raw); expect(groups).toHaveLength(1); expect(groups[0].title).toBe('Group 1'); }); it('accepts an object with list under apis or groups key', () => { const raw = { apis: [ { title: 'Group API', endpoints: [ { id: 'ep-2', full_url: 'http://127.0.0.1:6666/ep2/image/', }, ], }, ], }; const groups = extractGroups(raw); expect(groups).toHaveLength(1); expect(groups[0].title).toBe('Group API'); }); it('accepts a bare list of endpoints', () => { const raw = [ { id: 'ep-bare', full_url: 'http://127.0.0.1:6666/ep_bare/', }, ]; const groups = extractGroups(raw); expect(groups).toHaveLength(1); expect(groups[0].endpoints).toHaveLength(1); }); it('normalises endpoint derived values correctly', () => { const raw = [ { title: 'Blemish_Remover', endpoints: [ { id: 'api-blemish-test', full_url: 'http://127.0.0.1:6666/blemish_remover/image/', path: '/blemish_remover/image/', method: 'MULTI', resp: 'image/jpeg', params: [ { n: 'image', ty: 'file (binary)', rq: true, ds: 'User photo' }, { n: 'secretKey', ty: 'string', rq: true, ds: 'Secret key 12345' }, ], subs: [ { d: 'Primary acne fix', m: 'POST', p: '/blemish_remover/image/' }, { d: 'Eye fix', m: 'POST', p: '/blemish_remover/eye_fix/' }, ], }, ], }, ]; const overlay = { defaults: { timeoutMs: 60000 }, endpoints: { 'api-blemish-test': { enabled: true, publicTry: true, paramSources: { image: 'user.file', secretKey: 'secret' }, }, }, }; const { endpoints } = normaliseCatalog(raw, overlay); expect(endpoints).toHaveLength(1); const ep = endpoints[0]; expect(ep.id).toBe('api-blemish-test'); expect(ep.baseUrl).toBe('http://127.0.0.1:6666'); expect(ep.variants).toHaveLength(2); expect(ep.variants[0].id).toBe('image'); expect(ep.variants[1].id).toBe('eye_fix'); expect(ep.variants[1].url).toBe('http://127.0.0.1:6666/blemish_remover/eye_fix/'); // Param tests const fileParam = ep.params.find((p) => p.name === 'image'); expect(fileParam?.kind).toBe('file'); expect(fileParam?.source).toBe('user.file'); const secretParam = ep.params.find((p) => p.name === 'secretKey'); expect(secretParam?.isSecret).toBe(true); expect(secretParam?.source).toBe('secret'); expect(secretParam?.description).toBeUndefined(); // Redacted per PRD §7.11 }); it('skips corrupt entries without crashing', () => { const raw = [ { title: 'Corrupt Group', endpoints: [ { title: 'No id or url' }, { id: 'good-ep', full_url: 'http://127.0.0.1:6666/test/' }, ], }, ]; const { endpoints } = normaliseCatalog(raw, {}); expect(endpoints).toHaveLength(1); expect(endpoints[0].id).toBe('good-ep'); }); });