import { beforeEach, describe, expect, it, vi } from 'vitest'; import { detectModelContextWindow } from './model-context'; import { useSettingsStore } from '@/store/settings-store'; // Fake do /api/show do Ollama: nenhuma chamada sai do processo. function stubShow(body: unknown, ok = true) { const fetchMock = vi.fn(async () => ({ ok, json: async () => body }) as Response); vi.stubGlobal('fetch', fetchMock); return fetchMock; } describe('detectModelContextWindow', () => { beforeEach(() => { useSettingsStore.setState({ contextWindow: 2048 }); vi.spyOn(console, 'warn').mockImplementation(() => {}); }); it('modelfile com context_length: usa o valor declarado', async () => { const fetchMock = stubShow({ modelfile: 'PARAMETER context_length: 32000' }); expect(await detectModelContextWindow('llama3')).toBe(32000); expect(fetchMock).toHaveBeenCalledWith('http://localhost:11434/api/show', expect.objectContaining({ method: 'POST' })); }); it('nome vazio: usa contextWindow das settings sem chamar a API', async () => { const fetchMock = stubShow({}); expect(await detectModelContextWindow('')).toBe(2048); expect(fetchMock).not.toHaveBeenCalled(); }); it('nome vazio e contextWindow 0: cai em 4096', async () => { useSettingsStore.setState({ contextWindow: 0 }); stubShow({}); expect(await detectModelContextWindow('')).toBe(4096); }); it('resposta HTTP não ok: usa as settings', async () => { stubShow({ modelfile: 'context_length 9999' }, false); expect(await detectModelContextWindow('m')).toBe(2048); }); it.each([ ['7b', 8192], ['8b', 8192], ['13b', 16384], ['14b', 16384], ['32k', 32768], ['128k', 32768], ])('sem context_length e size "%s": infere %i', async (size, expected) => { stubShow({ modelfile: 'FROM x', size }); expect(await detectModelContextWindow('m')).toBe(expected); }); it('sem modelfile nem size: usa as settings', async () => { stubShow({}); expect(await detectModelContextWindow('m')).toBe(2048); }); it('fetch rejeita (Ollama fora do ar): usa as settings', async () => { vi.stubGlobal('fetch', vi.fn(async () => { throw new TypeError('connection refused'); })); expect(await detectModelContextWindow('m')).toBe(2048); }); it('fallback final com contextWindow 0: cai em 4096', async () => { useSettingsStore.setState({ contextWindow: 0 }); stubShow({ size: '3b' }); expect(await detectModelContextWindow('m')).toBe(4096); }); });