Something went wrong. Try again.
The Serifu editor
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293import { describe, it, expect, vi } from 'vitest';import { render, screen } from '@testing-library/svelte';import userEvent from '@testing-library/user-event';import CameraImportDialog from './CameraImportDialog.svelte';import type { CameraImportState } from '../camera/cameraImportController';import { fakeImageSrc } from '../vision/imageSrc';
const ENDPOINT = { url: 'http://10.0.0.5:9999/c/K7M2PQXR', host: '10.0.0.5', port: 9999 };
function renderDialog(state: Partial<CameraImportState>) { const onRetry = vi.fn(); const onDone = vi.fn(); const full: CameraImportState = { status: 'serving', endpoint: ENDPOINT, photos: [], error: null, ...state, }; return { onRetry, onDone, ...render(CameraImportDialog, { state: full, imageSrc: fakeImageSrc, onRetry, onDone }) };}
describe('CameraImportDialog', () => { it('reports that the server is coming up', () => { renderDialog({ status: 'starting', endpoint: null }); expect(screen.getByTestId('camera-status').textContent).toContain('Starting'); expect(screen.queryByTestId('camera-qr')).toBeNull(); });
it('shows a QR code and the typed-out URL once serving', () => { renderDialog({}); const qr = screen.getByTestId('camera-qr'); // A real code, not an empty frame: the path must carry module geometry. expect(qr.querySelector('path')?.getAttribute('d')).toMatch(/^M\d+ \d+h\d+/); expect(qr.getAttribute('aria-label')).toContain(ENDPOINT.url); expect(screen.getByTestId('camera-url').textContent).toBe(ENDPOINT.url); });
it('waits visibly before the first page', () => { renderDialog({}); expect(screen.getByTestId('camera-count').textContent).toContain('Waiting'); expect(screen.queryByTestId('camera-strip')).toBeNull(); });
it('counts pages and shows them through the imageSrc seam', () => { renderDialog({ photos: [ { path: '/shots/page-0001.jpg', seq: 1 }, { path: '/shots/page-0002.jpg', seq: 2 }, ], }); expect(screen.getByTestId('camera-count').textContent).toContain('2 pages received'); const thumbs = screen.getByTestId('camera-strip').querySelectorAll('img'); expect(thumbs).toHaveLength(2); // Newest first, so the page just shot is the one you see. expect(thumbs[0].getAttribute('src')).toBe('fake-image:///shots/page-0002.jpg'); });
it('says "page" for a single capture', () => { renderDialog({ photos: [{ path: '/shots/page-0001.jpg', seq: 1 }] }); expect(screen.getByTestId('camera-count').textContent).toContain('1 page received'); });
it('caps the strip so a long session cannot overflow the dialog', () => { const photos = Array.from({ length: 20 }, (_, i) => ({ path: `/shots/page-${String(i + 1).padStart(4, '0')}.jpg`, seq: i + 1, })); renderDialog({ photos }); expect(screen.getByTestId('camera-strip').querySelectorAll('img')).toHaveLength(8); });
it('explains a failed start and offers a retry', async () => { const { onRetry } = renderDialog({ status: 'error', endpoint: null, error: 'This Mac isn’t on a network.', }); expect(screen.getByTestId('camera-error').textContent).toContain('isn’t on a network'); await userEvent.click(screen.getByRole('button', { name: 'Try again' })); expect(onRetry).toHaveBeenCalledOnce(); });
it('ends the session from Done and from Escape', async () => { const { onDone } = renderDialog({}); await userEvent.click(screen.getByTestId('camera-done')); expect(onDone).toHaveBeenCalledOnce();
await userEvent.keyboard('{Escape}'); expect(onDone).toHaveBeenCalledTimes(2); });});