import { describe, expect, it } from 'vitest' import type { ElementKey } from '@faunosteo/core-wasm/types' import { initialNavigation, navigationReducer, targetOfKey, type EditorTarget, type NavigationState, } from '../../src/editor/navigation' const view: ElementKey = { kind: 'view', id: 'v1' } const finding: ElementKey = { kind: 'finding', zone_id: 'z1', path: ['a'] } function run( state: NavigationState, ...actions: Parameters[1][] ) { return actions.reduce(navigationReducer, state) } describe('targetOfKey', () => { const tab = (t: string, section: string | null, key: ElementKey) => ({ place: 'tab', tab: t, section, key, }) const cases: [ElementKey, EditorTarget][] = [ [ { kind: 'sheet' }, tab('general', null, { kind: 'sheet' }) as EditorTarget, ], [ { kind: 'zone', id: 'z' }, tab('findings', null, { kind: 'zone', id: 'z' }) as EditorTarget, ], [finding, tab('findings', null, finding) as EditorTarget], [ { kind: 'annotation', id: 'a' }, tab('annotations', 'drawing', { kind: 'annotation', id: 'a', }) as EditorTarget, ], [view, tab('annotations', 'views', view) as EditorTarget], [ { kind: 'category', id: 'c' }, tab('annotations', 'categories', { kind: 'category', id: 'c', }) as EditorTarget, ], [ { kind: 'region', code: 'r' }, tab('annotations', 'regions', { kind: 'region', code: 'r', }) as EditorTarget, ], [ { kind: 'reference', id: 'x' }, { place: 'references', key: { kind: 'reference', id: 'x' }, citedBy: null, }, ], ] it.each(cases)('maps %j', (key, expected) => { expect(targetOfKey(key)).toEqual(expected) }) }) describe('navigationReducer', () => { it('starts on the given tab with the drawing section', () => { const state = initialNavigation('annotations') expect(state.visitedTabs).toEqual(['annotations']) expect(state.visitedSections).toEqual(['drawing']) expect(initialNavigation('general').visitedSections).toEqual([]) expect(state.referencesMounted).toBe(false) }) it('go_to sets the place and increments the serial', () => { const first = run(initialNavigation('general'), { type: 'go_to', target: targetOfKey(view), }) expect(first).toMatchObject({ tab: 'annotations', section: 'views' }) expect(first.focus).toEqual({ target: targetOfKey(view), serial: 1 }) const again = run(first, { type: 'go_to', target: targetOfKey(finding) }) expect(again.tab).toBe('findings') expect(again.section).toBe('views') expect(again.focus?.serial).toBe(2) }) it('go_to a reference opens the panel over the current place', () => { const base = run(initialNavigation('annotations'), { type: 'open_section', section: 'views', }) const state = run(base, { type: 'go_to', target: targetOfKey({ kind: 'reference', id: 'x' }), }) expect(state).toMatchObject({ tab: 'annotations', section: 'views', referencesMounted: true, }) expect(state.references.open).toBe(true) }) it('go_to a tab closes the panel', () => { const state = run( initialNavigation('general'), { type: 'open_references', citedBy: view }, { type: 'go_to', target: targetOfKey(finding) }, ) expect(state.references).toEqual({ open: false, citedBy: null }) expect(state.referencesMounted).toBe(true) }) it('select_tab keeps section and panel and issues no request', () => { const state = run( initialNavigation('annotations'), { type: 'open_section', section: 'regions' }, { type: 'open_references', citedBy: view }, { type: 'select_tab', tab: 'findings' }, ) expect(state).toMatchObject({ tab: 'findings', section: 'regions', focus: undefined, }) expect(state.references).toEqual({ open: true, citedBy: view }) }) it('clear_cited_by drops the filter and keeps the panel open', () => { const state = run( initialNavigation('annotations'), { type: 'open_references', citedBy: view }, { type: 'clear_cited_by' }, ) expect(state.references).toEqual({ open: true, citedBy: null }) expect(state.focus).toBeUndefined() }) it('opens a section and goes back to the drawing', () => { const state = run(initialNavigation('annotations'), { type: 'open_section', section: 'views', }) expect(state.section).toBe('views') const back = run(state, { type: 'back_to_drawing' }) expect(back.section).toBe('drawing') expect(back.visitedSections).toEqual(['drawing', 'views']) }) it('open and close references keep tab and section', () => { const opened = run( initialNavigation('annotations'), { type: 'open_section', section: 'views' }, { type: 'open_references', citedBy: view }, ) expect(opened).toMatchObject({ tab: 'annotations', section: 'views' }) expect(opened.references).toEqual({ open: true, citedBy: view }) const closed = run(opened, { type: 'close_references' }) expect(closed).toMatchObject({ tab: 'annotations', section: 'views', referencesMounted: true, }) expect(closed.references.open).toBe(false) }) it('focus_consumed clears only its serial and serials are not reused', () => { const asked = run(initialNavigation('general'), { type: 'go_to', target: targetOfKey(view), }) expect( run(asked, { type: 'focus_consumed', serial: 9 }).focus, ).toBeDefined() const cleared = run(asked, { type: 'focus_consumed', serial: 1 }) expect(cleared.focus).toBeUndefined() const next = run(cleared, { type: 'go_to', target: targetOfKey(view) }) expect(next.focus?.serial).toBe(2) }) it('visited lists only grow and record places as they are shown', () => { const state = run( initialNavigation('general'), { type: 'select_tab', tab: 'annotations' }, { type: 'open_section', section: 'categories' }, { type: 'select_tab', tab: 'findings' }, { type: 'select_tab', tab: 'general' }, { type: 'back_to_drawing' }, ) expect(state.visitedTabs).toEqual(['general', 'annotations', 'findings']) expect(state.visitedSections).toEqual(['drawing', 'categories']) }) })