import { describe, it, expect, vi, beforeEach } from 'vitest' import { searchTemplates } from './searchTemplates' describe('searchTemplates', () => { beforeEach(() => { vi.restoreAllMocks() }) it('gibt Ergebnisse zurück wenn KLAUSUR_SERVICE verfügbar', async () => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: () => Promise.resolve([{ id: 't1', score: 0.9, text: 'Inhalt', document_title: 'DSE Template', template_type: 'privacy_policy', clause_category: null, language: 'de', jurisdiction: 'de', license_id: 'mit', license_name: 'MIT', license_url: null, attribution_required: false, attribution_text: null, source_name: 'Test', source_url: null, source_repo: null, placeholders: [], is_complete_document: true, is_modular: false, requires_customization: false, output_allowed: true, modification_allowed: true, distortion_prohibited: false, }]), })) const results = await searchTemplates({ query: 'Datenschutzerklärung' }) expect(results).toHaveLength(1) expect(results[0].documentTitle).toBe('DSE Template') }) it('fällt auf RAG zurück wenn KLAUSUR_SERVICE fehlschlägt', async () => { vi.stubGlobal('fetch', vi.fn() .mockRejectedValueOnce(new Error('KLAUSUR down')) .mockResolvedValueOnce({ ok: true, json: () => Promise.resolve({ results: [{ regulation_name: 'DSGVO Art. 13', text: 'Informationspflichten', regulation_code: 'DSGVO-13', score: 0.8, }], }), }) ) const results = await searchTemplates({ query: 'test' }) expect(results).toHaveLength(1) expect(results[0].documentTitle).toBe('DSGVO Art. 13') expect((results[0] as any).source).toBe('rag') }) it('gibt [] zurück wenn beide Services down sind', async () => { vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('all down'))) const results = await searchTemplates({ query: 'test' }) expect(results).toEqual([]) }) })