import { beforeEach, describe, expect, it } from 'vitest' import type { NavigationEntry } from '../../src/api/types' import { activeEntry, firstControllerPath, homePath, loadNavigation, navigation, railEntries, setNavigation, useNavigation, } from '../../src/state/useNavigation' import { clone, navigationFixture } from '../fixtures/typed' import { API, mockApi, requestsTo, resetState } from '../helpers' function entry(code: string, order: number, controllers: string[], controller = controllers[0] ?? 'x.y.z'): NavigationEntry { return { code, label: code, icon: 'tags', order, controller, sideMenu: controllers.map((id, index) => ({ code: `${code}-${index}`, label: id, icon: 'tags', order: 0, controller: id, sideMenu: [] })), } } beforeEach(() => { resetState() }) describe('loadNavigation', () => { it('stores the server-filtered entries', async () => { const calls = mockApi({ [`GET ${API}/navigation`]: { body: navigationFixture } }) expect(await loadNavigation()).toHaveLength(3) expect(navigation.value.map((item) => item.code)).toEqual(['demo', 'tools', 'hidden']) expect(requestsTo(calls, 'GET', `${API}/navigation`)).toHaveLength(1) }) it('empties the navigation when the request fails', async () => { setNavigation(navigationFixture.data) mockApi({ [`GET ${API}/navigation`]: { status: 403, body: { error: { code: 'forbidden', message: 'x', details: {} } } } }) expect(await loadNavigation()).toEqual([]) expect(navigation.value).toEqual([]) }) }) describe('rail entries', () => { it('omits plugins without a permitted side-menu item and orders by order, then position', () => { setNavigation([entry('b', 20, ['a.b.c']), entry('empty', 1, []), entry('a', 10, ['a.a.c']), entry('c', 20, ['a.c.c'])]) expect(railEntries.value.map((item) => item.code)).toEqual(['a', 'b', 'c']) }) it('opens the first valid side-menu controller, else the entry controller', () => { expect(firstControllerPath(entry('p', 0, ['bad id', 'acme.demo.gadgets']))).toBe('/acme/demo/gadgets') expect(firstControllerPath({ ...entry('p', 0, ['bad id']), controller: 'acme.demo.widgets' })).toBe('/acme/demo/widgets') expect(firstControllerPath({ ...entry('p', 0, []), controller: 'nope' })).toBeNull() }) it('finds the active plugin by its own controller or any side-menu controller', () => { setNavigation(navigationFixture.data) expect(activeEntry('acme', 'demo')?.code).toBe('demo') expect(activeEntry('acme', 'tools')?.code).toBe('tools') expect(activeEntry('acme', 'hidden')).toBeNull() expect(activeEntry('acme', 'missing')).toBeNull() const shared = clone(navigationFixture.data) shared[0]!.controller = 'other.root.index' setNavigation(shared) expect(activeEntry('acme', 'demo')?.code).toBe('demo') }) it('goes home to the first rail plugin, skipping plugins without a valid path', () => { expect(homePath()).toBeNull() setNavigation([{ ...entry('broken', 1, ['bad id']), controller: 'bad' }, entry('ok', 2, ['acme.tools.hammers'])]) expect(homePath()).toBe('/acme/tools/hammers') setNavigation(navigationFixture.data) expect(homePath()).toBe('/acme/demo/widgets') expect(useNavigation().homePath).toBe(homePath) }) })