import { describe, expect, test } from 'vitest'; import { toPageRef } from '@embedpdf/core'; import type { SearchHit } from '../src/contract'; import { appendHits, cancelSession, clearSession, completeSession, failSession, initialSearchModel, pagesWithHits, setActiveHit, startSession, type SearchModel, } from '../src/model'; const seg = (rect: { x: number; y: number; width: number; height: number }) => ({ quad: { upperLeft: { x: rect.x, y: rect.y }, upperRight: { x: rect.x + rect.width, y: rect.y }, lowerLeft: { x: rect.x, y: rect.y + rect.height }, lowerRight: { x: rect.x + rect.width, y: rect.y + rect.height }, }, rect, advance: 1 as const, }); const hit = (pageObjectNumber: number, start: number): SearchHit => ({ page: toPageRef(pageObjectNumber), pageIndex: 0, start, count: 4, segments: [seg({ x: 0, y: 0, width: 10, height: 10 })], bounds: { x: 0, y: 0, width: 10, height: 10 }, }); const started = (): SearchModel => startSession(initialSearchModel(), { text: 'test' }, 'session#1'); describe('search transitions', () => { test('startSession resets everything and enters searching with the operation id', () => { const dirty: SearchModel = { ...initialSearchModel(), hits: [hit(5, 0)], hitsByPage: { 5: [hit(5, 0)] }, activeHitIndex: 0, status: 'complete', }; const state = startSession(dirty, { text: 'x', matchCase: true }, 'session#2'); expect(state.status).toBe('searching'); expect(state.hits).toEqual([]); expect(state.hitsByPage).toEqual({}); expect(state.activeHitIndex).toBe(-1); expect(state.operationId).toBe('session#2'); expect(state.query).toEqual({ text: 'x', matchCase: true }); }); test('appendHits accumulates, keeps per-page arrays reference-stable, activates the first hit', () => { let state = started(); state = appendHits(state, [hit(5, 0), hit(5, 9)], { pagesSearched: 1, pageCount: 8 }); const page5 = state.hitsByPage[5]; state = appendHits(state, [hit(7, 2)], { pagesSearched: 3, pageCount: 8 }); expect(state.hits.length).toBe(3); expect(state.hitsByPage[5]).toBe(page5); // an untouched page keeps its array expect(state.hitsByPage[7].map((found) => found.start)).toEqual([2]); expect(state.activeHitIndex).toBe(0); expect(state.progress).toEqual({ pagesSearched: 3, pageCount: 8 }); // In document order, which moving a page changes: page 7 is first here. const pages = [{ ref: toPageRef(7) }, { ref: toPageRef(6) }, { ref: toPageRef(5) }]; expect(pagesWithHits(state, pages).map((page) => page.objectNumber)).toEqual([7, 5]); }); test('an empty slice only advances progress; an explicit active index survives appends', () => { let state = started(); state = appendHits(state, [], { pagesSearched: 4, pageCount: 8 }); expect(state.hits.length).toBe(0); expect(state.activeHitIndex).toBe(-1); state = appendHits(state, [hit(5, 0), hit(5, 9)], { pagesSearched: 5, pageCount: 8 }); state = setActiveHit(state, 1); state = appendHits(state, [hit(7, 2)], { pagesSearched: 6, pageCount: 8 }); expect(state.activeHitIndex).toBe(1); }); test('progress with the same counts keeps its object, and an empty slice with it changes nothing', () => { let state = appendHits(started(), [hit(5, 0)], { pagesSearched: 1, pageCount: 8 }); const { progress } = state; state = appendHits(state, [hit(5, 9)], { pagesSearched: 1, pageCount: 8 }); expect(state.progress).toBe(progress); expect(state.hits.length).toBe(2); expect(appendHits(state, [], { pagesSearched: 1, pageCount: 8 })).toBe(state); }); test('complete, cancelled and failed are terminal; clearing returns to idle', () => { const state = started(); expect(completeSession(state).status).toBe('complete'); expect(cancelSession(state).status).toBe('cancelled'); const failed = failSession(state, { code: 'operation-failed', message: 'boom', capability: 'search', permission: null, }); expect(failed.status).toBe('error'); expect(failed.error?.message).toBe('boom'); expect(clearSession(failed)).toEqual(initialSearchModel()); }); });