test: caminhos-base de components
This commit is contained in:
1 parent
9a0e0205dc
commit
60c98fc51e
11 files changed
+1148
-1
No files matched your search
@@ -0,0 +1,134 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||
import { ModelSelector } from './model-selector';
|
||||
|
||||
// Comandos Tauri (pull_model etc.) e o evento download-progress viram fakes.
|
||||
const { invokeMock, emit } = vi.hoisted(() => {
|
||||
const listeners: Record<string, (event: { payload: string }) => void> = {};
|
||||
return {
|
||||
invokeMock: vi.fn(),
|
||||
emit: Object.assign((payload: unknown) => listeners['download-progress']({ payload: typeof payload === 'string' ? payload : JSON.stringify(payload) }), { listeners }),
|
||||
};
|
||||
});
|
||||
vi.mock('@tauri-apps/api/core', () => ({ invoke: invokeMock }));
|
||||
vi.mock('@tauri-apps/api/event', () => ({
|
||||
listen: vi.fn(async (name: string, cb: (event: { payload: string }) => void) => {
|
||||
emit.listeners[name] = cb;
|
||||
return () => {};
|
||||
}),
|
||||
}));
|
||||
|
||||
let installed = false;
|
||||
let pull: () => Promise<void> = () => Promise.withResolvers<void>().promise; // download em andamento
|
||||
|
||||
beforeEach(() => {
|
||||
installed = false;
|
||||
pull = () => Promise.withResolvers<void>().promise;
|
||||
invokeMock.mockReset();
|
||||
invokeMock.mockImplementation(async (cmd: string) => {
|
||||
if (cmd === 'check_if_model_installed') return installed;
|
||||
if (cmd === 'pull_model') return pull();
|
||||
throw new Error(`comando inesperado: ${cmd}`);
|
||||
});
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
async function startDownload(modelId = 'llama3.2:3b') {
|
||||
const onComplete = vi.fn();
|
||||
render(<ModelSelector recommendation={{ modelId, reason: 'cabe na sua RAM', minRam: 8 }} onComplete={onComplete} />);
|
||||
await waitFor(() => expect(emit.listeners['download-progress']).toBeDefined());
|
||||
fireEvent.click(screen.getByRole('button', { name: /Baixar e Instalar/ }));
|
||||
await screen.findByText('Iniciando download...');
|
||||
return onComplete;
|
||||
}
|
||||
|
||||
describe('ModelSelector', () => {
|
||||
it('modelo já instalado: oferece ir para o chat e chama onComplete', async () => {
|
||||
installed = true;
|
||||
const onComplete = vi.fn();
|
||||
render(<ModelSelector recommendation={{ modelId: 'llama3.2:1b', reason: 'r', minRam: 4 }} onComplete={onComplete} />);
|
||||
fireEvent.click(await screen.findByRole('button', { name: /Ir para o Chat/ }));
|
||||
expect(onComplete).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('recomendação fora da lista: botão usa o tamanho do primeiro modelo', async () => {
|
||||
render(<ModelSelector recommendation={{ modelId: 'desconhecido', reason: 'r', minRam: 4 }} onComplete={vi.fn()} />);
|
||||
expect(await screen.findByRole('button', { name: 'Baixar e Instalar (1.3GB)' })).toBeTruthy();
|
||||
});
|
||||
|
||||
it('pull_model falha: volta ao botão de baixar habilitado e zera o progresso', async () => {
|
||||
const { promise, reject: fail } = Promise.withResolvers<void>();
|
||||
pull = () => promise;
|
||||
await startDownload();
|
||||
await act(async () => fail(new Error('sem rede')));
|
||||
const button = await screen.findByRole('button', { name: /Baixar e Instalar/ });
|
||||
expect(button).toHaveProperty('disabled', false);
|
||||
expect(screen.queryByText('0%')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('ModelSelector: evento download-progress', () => {
|
||||
it.each([
|
||||
['pulling', 'Baixando modelo...'],
|
||||
['verifying', 'Verificando...'],
|
||||
['writing', 'Salvando...'],
|
||||
['outro', 'Baixando...'],
|
||||
])('status %s sem percent: texto "%s"', async (status, text) => {
|
||||
await startDownload();
|
||||
act(() => emit({ status, raw: '' }));
|
||||
expect(screen.getByText(text, { selector: 'span' })).toBeTruthy();
|
||||
});
|
||||
|
||||
it('percent informado: mostra o percentual e tamanho/velocidade', async () => {
|
||||
await startDownload();
|
||||
act(() => emit({ status: 'pulling', percent: 42, downloaded: '1 GB', total: '2 GB', speed: '5 MB/s', raw: '' }));
|
||||
expect(screen.getByText('42%')).toBeTruthy();
|
||||
expect(screen.getByText('1 GB / 2 GB')).toBeTruthy();
|
||||
expect(screen.getByText('5 MB/s')).toBeTruthy();
|
||||
});
|
||||
|
||||
it('pulling sem percent: progresso sobe para no mínimo 10%', async () => {
|
||||
await startDownload();
|
||||
act(() => emit({ status: 'pulling', raw: '' }));
|
||||
expect(screen.getByText('10%')).toBeTruthy();
|
||||
});
|
||||
|
||||
it('pulling sem percent depois de 42%: não regride', async () => {
|
||||
await startDownload();
|
||||
act(() => emit({ status: 'pulling', percent: 42, raw: '' }));
|
||||
act(() => emit({ status: 'pulling', raw: '' }));
|
||||
expect(screen.getByText('42%')).toBeTruthy();
|
||||
});
|
||||
|
||||
it('verifying sem percent: 90%; writing sem percent: 95%', async () => {
|
||||
await startDownload();
|
||||
act(() => emit({ status: 'verifying', raw: '' }));
|
||||
expect(screen.getByText('90%')).toBeTruthy();
|
||||
act(() => emit({ status: 'writing', raw: '' }));
|
||||
expect(screen.getByText('95%')).toBeTruthy();
|
||||
});
|
||||
|
||||
it('payload que não é JSON: trata como download em andamento sem mudar o progresso', async () => {
|
||||
await startDownload();
|
||||
act(() => emit('pulling manifest'));
|
||||
expect(screen.getByText('Baixando...', { selector: 'span' })).toBeTruthy();
|
||||
expect(screen.getByText('0%')).toBeTruthy();
|
||||
});
|
||||
|
||||
it('downloaded sem total: mostra só o baixado', async () => {
|
||||
await startDownload();
|
||||
act(() => emit({ status: 'pulling', percent: 5, downloaded: '300 MB', raw: '' }));
|
||||
expect(screen.getByText('300 MB')).toBeTruthy();
|
||||
});
|
||||
|
||||
it.each([
|
||||
['percent 100', { status: 'pulling', percent: 100, raw: '' }],
|
||||
['status success', { status: 'success', raw: '' }],
|
||||
])('%s: após 500 ms marca instalado e oferece ir para o chat', async (_n, payload) => {
|
||||
await startDownload();
|
||||
installed = true;
|
||||
act(() => emit(payload));
|
||||
expect(screen.queryByRole('button', { name: /Ir para o Chat/ })).toBeNull();
|
||||
expect(await screen.findByRole('button', { name: /Ir para o Chat/ }, { timeout: 2000 })).toBeTruthy();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user