test: caminhos-base de lib, hooks e services

This commit is contained in:
evandro committed 2026-11-16 00:26:52 -03:00
1 parent 9d911603f7
commit adc37e1921
18 files changed
+2961 -142

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { act, renderHook } from '@testing-library/react';
import { useDeepResearch } from './use-deep-research';
// Logger (invoke) e análise contextual viram fakes; o Ollama responde por fetch fake.
vi.mock('@tauri-apps/api/core', () => ({ invoke: vi.fn(async () => undefined) }));
vi.mock('@/lib/contextual-analyzer', () => ({
analyzeQueryContext: vi.fn(async () => {
throw new Error('sem análise contextual neste teste');
}),
}));
function llmReplies(response: string) {
vi.stubGlobal('fetch', vi.fn(async () => ({ ok: true, json: async () => ({ response }) })));
}
const QUERY = 'historia da linguagem rust';
async function decompose(response: string) {
llmReplies(response);
const { result } = renderHook(() => useDeepResearch());
let plan: string[] = [];
await act(async () => {
plan = await result.current.decompose(QUERY, 'llama3');
});
return { plan, error: result.current.state.logs[0]?.error };
}
beforeEach(() => {
for (const level of ['log', 'warn', 'error', 'info'] as const) vi.spyOn(console, level).mockImplementation(() => {});
});
describe('extractQueriesFromResponse (via decompose)', () => {
it('array JSON puro: mantém só strings não vazias', async () => {
const r = await decompose('["rust origem", "", 3, "rust mozilla"]');
expect(r).toEqual({ plan: ['rust origem', 'rust mozilla'], error: undefined });
});
it('bloco ```json: remove a cerca antes de interpretar', async () => {
expect((await decompose('```json\n["rust 1.0"]\n```')).plan).toEqual(['rust 1.0']);
});
it('JSON no meio de texto: extrai o trecho', async () => {
expect((await decompose('Aqui está o plano: ["rust graydon"] espero ajudar')).plan).toEqual(['rust graydon']);
});
it('trecho parecido com JSON mas inválido: cai na pergunta original com erro registrado', async () => {
const r = await decompose('plano: [rust sem aspas]');
expect(r).toEqual({ plan: [QUERY], error: 'JSON Parse Failed: Could not extract JSON from response' });
});
it('resposta sem JSON: cai na pergunta original com erro registrado', async () => {
const r = await decompose('não sei');
expect(r).toEqual({ plan: [QUERY], error: 'JSON Parse Failed: No JSON found in response' });
});
it('objeto com chave conhecida: usa o array dessa chave', async () => {
expect((await decompose('{"search_queries": ["rust história", " "]}')).plan).toEqual(['rust história']);
});
it('chave conhecida que não é array: segue para a próxima chave', async () => {
expect((await decompose('{"query": "rust", "plan": ["rust plano"]}')).plan).toEqual(['rust plano']);
});
it('objeto sem chave conhecida: usa o primeiro array de strings', async () => {
expect((await decompose('{"nums": [1], "vazio": [], "itens": ["rust itens"]}')).plan).toEqual(['rust itens']);
});
it('objeto sem nenhum array de strings: cai na pergunta original', async () => {
const r = await decompose('{"nums": [1, 2], "texto": "rust"}');
expect(r).toEqual({ plan: [QUERY], error: 'JSON Parse Failed: No array of queries found in response' });
});
});