Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

14.9 KB
Raw Blame
/**
 * Tests for mobile-navigation.js — pure helpers on the MobileNavigation class.
 *
 * checkViewport drives whether the mobile bottom-nav is shown at all,
 * and isCurrentPage decides which tab is highlighted. Both are pure
 * reads of `window.innerWidth` / `window.location.pathname`, so they
 * are testable without touching the rest of the class wiring.
 *
 * The module auto-instantiates a singleton when imported (`initMobileNav`
 * runs immediately because happy-dom reports `document.readyState === 'complete'`).
 * That constructor path renders HTML that references the global `URLS`
 * config, so we stub it before the dynamic import.
 */

let MobileNavigation;

beforeAll(async () => {
    // Minimal URLS stub — only the PAGES keys the constructor's bottom-nav
    // and sheet-menu templates dereference.
    globalThis.URLS = {
        PAGES: {
            HOME: '/',
            HISTORY: '/history/',
            LIBRARY: '/library',
            NEWS: '/news',
            NEWS_SUBSCRIPTIONS: '/news/subscriptions',
            COLLECTIONS: '/library/collections',
            METRICS: '/metrics',
            BENCHMARK: '/benchmark',
            BENCHMARK_RESULTS: '/benchmark/results',
            EMBEDDING_SETTINGS: '/settings/embedding',
            SETTINGS: '/settings',
        },
    };

    await import('@js/mobile-navigation.js');
    MobileNavigation = window.MobileNavigation;
});

describe('MobileNavigation.checkViewport', () => {
    let originalInnerWidth;

    beforeEach(() => {
        originalInnerWidth = window.innerWidth;
    });

    afterEach(() => {
        Object.defineProperty(window, 'innerWidth', {
            value: originalInnerWidth,
            configurable: true,
            writable: true,
        });
    });

    function setInnerWidth(px) {
        Object.defineProperty(window, 'innerWidth', {
            value: px,
            configurable: true,
            writable: true,
        });
    }

    it('returns true and sets state.isVisible=true below the breakpoint', () => {
        const nav = new MobileNavigation();
        setInnerWidth(500);
        expect(nav.checkViewport()).toBe(true);
        expect(nav.state.isVisible).toBe(true);
    });

    it('returns false and sets state.isVisible=false at the breakpoint (768)', () => {
        // Uses strict < (not <=) — 768px is tablet, sidebar should be visible.
        const nav = new MobileNavigation();
        setInnerWidth(768);
        expect(nav.checkViewport()).toBe(false);
        expect(nav.state.isVisible).toBe(false);
    });

    it('returns false above the breakpoint', () => {
        const nav = new MobileNavigation();
        setInnerWidth(1280);
        expect(nav.checkViewport()).toBe(false);
        expect(nav.state.isVisible).toBe(false);
    });

    it('respects a custom breakpoint passed via options', () => {
        const nav = new MobileNavigation({ breakpoint: 1024 });
        setInnerWidth(900);
        expect(nav.checkViewport()).toBe(true);
        setInnerWidth(1024);
        expect(nav.checkViewport()).toBe(false);
    });
});

describe('MobileNavigation.isCurrentPage', () => {
    let originalLocation;

    beforeAll(() => {
        originalLocation = window.location;
    });

    afterAll(() => {
        Object.defineProperty(window, 'location', {
            value: originalLocation,
            configurable: true,
        });
    });

    function setPath(pathname) {
        Object.defineProperty(window, 'location', {
            value: { pathname },
            configurable: true,
            writable: true,
        });
    }

    function nav() {
        return new MobileNavigation();
    }

    it('treats the research tab as active only on the bare root path', () => {
        setPath('/');
        expect(nav().isCurrentPage({ id: 'research' })).toBe(true);

        setPath('/something');
        expect(nav().isCurrentPage({ id: 'research' })).toBe(false);
    });

    it('matches the history tab on /history and any sub-path', () => {
        setPath('/history');
        expect(nav().isCurrentPage({ id: 'history' })).toBe(true);

        setPath('/history/abc-123');
        expect(nav().isCurrentPage({ id: 'history' })).toBe(true);

        setPath('/');
        expect(nav().isCurrentPage({ id: 'history' })).toBe(false);
    });

    it('matches the library tab on /library prefix', () => {
        setPath('/library');
        expect(nav().isCurrentPage({ id: 'library' })).toBe(true);

        setPath('/library/collections/42');
        expect(nav().isCurrentPage({ id: 'library' })).toBe(true);
    });

    it('matches the metrics tab on /metrics prefix', () => {
        setPath('/metrics');
        expect(nav().isCurrentPage({ id: 'metrics' })).toBe(true);

        setPath('/metrics/dashboard');
        expect(nav().isCurrentPage({ id: 'metrics' })).toBe(true);
    });

    it('matches the news tab on /news prefix', () => {
        setPath('/news');
        expect(nav().isCurrentPage({ id: 'news' })).toBe(true);

        setPath('/news/subscriptions');
        expect(nav().isCurrentPage({ id: 'news' })).toBe(true);
    });

    it('returns false for unknown tab ids', () => {
        setPath('/');
        expect(nav().isCurrentPage({ id: 'unknown' })).toBe(false);
    });

    it('does not cross-match: /library should not light up history or news', () => {
        setPath('/library');
        expect(nav().isCurrentPage({ id: 'history' })).toBe(false);
        expect(nav().isCurrentPage({ id: 'news' })).toBe(false);
    });
});

describe('MobileNavigation username rendering', () => {
    afterEach(() => {
        document.querySelectorAll('.ldr-user-info').forEach(element => element.remove());
    });

    it('keeps hostile account text inert when building the sheet menu', () => {
        const userInfo = document.createElement('div');
        userInfo.className = 'ldr-user-info';
        userInfo.textContent = 'Alice <img src=x onerror=alert(1)> & "Admin"';
        document.body.appendChild(userInfo);

        const nav = new MobileNavigation();
        const username = nav.getUsername();
        nav.createSheetMenu();

        const userItem = nav.elements.sheet.querySelector('[data-item-id="user"]');
        expect(username).toContain('Alice');
        expect(username).not.toMatch(/[<>&"']/);
        expect(userItem.querySelector('img')).toBeNull();
        expect(userItem.querySelector('.ldr-mobile-sheet-label').textContent).toBe(
            username,
        );
    });
});

describe('MobileNavigation interactive lifecycle', () => {
    let nav;
    let originalInnerWidth;
    let animationFrames;

    function setInnerWidth(px) {
        Object.defineProperty(window, 'innerWidth', {
            value: px,
            configurable: true,
            writable: true,
        });
    }

    function flushSheetFrames() {
        while (animationFrames.length > 0) {
            animationFrames.shift()();
        }
    }

    function touchEvent(type, clientY) {
        const event = new Event(type, { bubbles: true, cancelable: true });
        Object.defineProperty(event, 'touches', {
            value: [{ clientY }],
        });
        return event;
    }

    beforeEach(() => {
        window.mobileNav?.destroy();
        delete window.mobileNav;
        document.body.replaceChildren();
        document.body.className = '';
        document.body.style.overflow = '';
        localStorage.clear();
        originalInnerWidth = window.innerWidth;
        animationFrames = [];
        vi.useFakeTimers();
        vi.stubGlobal('requestAnimationFrame', vi.fn(callback => {
            animationFrames.push(callback);
            return animationFrames.length;
        }));
        vi.stubGlobal('URLValidator', { safeAssign: vi.fn() });
    });

    afterEach(() => {
        nav?.destroy();
        nav = null;
        vi.clearAllTimers();
        vi.useRealTimers();
        vi.restoreAllMocks();
        vi.unstubAllGlobals();
        Object.defineProperty(window, 'innerWidth', {
            value: originalInnerWidth,
            configurable: true,
            writable: true,
        });
        localStorage.clear();
        document.body.replaceChildren();
        document.body.className = '';
        document.body.style.overflow = '';
    });

    it('opens accessibly and restores the desktop layout after a resize', async () => {
        setInnerWidth(500);
        document.body.innerHTML = '<aside class="ldr-sidebar"></aside>';
        const sidebar = document.querySelector('.ldr-sidebar');
        nav = new MobileNavigation({
            enableGestures: false,
            persistState: false,
        });

        nav.init();

        expect(nav.initialized).toBe(true);
        expect(nav.elements.nav.classList).toContain('visible');
        expect(document.body.classList).toContain('ldr-has-mobile-nav');
        expect(sidebar.style.display).toBe('none');
        expect(sidebar.dataset.mobileHidden).toBe('true');

        nav.openSheet();
        flushSheetFrames();

        const firstItem = nav.getFirstFocusableElement();
        expect(nav.state.sheetOpen).toBe(true);
        expect(nav.elements.sheet.classList).toContain('active');
        expect(nav.elements.sheet.hasAttribute('aria-hidden')).toBe(false);
        expect(nav.elements.overlay.classList).toContain('active');
        expect(document.activeElement).toBe(firstItem);
        expect(nav.elements.nav.querySelector('[data-tab-id="more"]')
            .getAttribute('aria-expanded')).toBe('true');

        setInnerWidth(900);
        window.dispatchEvent(new Event('resize'));
        await vi.advanceTimersByTimeAsync(250);

        expect(nav.state.isVisible).toBe(false);
        expect(nav.state.sheetOpen).toBe(false);
        expect(nav.elements.nav.classList).not.toContain('visible');
        expect(document.body.classList).not.toContain('ldr-has-mobile-nav');
        expect(document.body.style.overflow).toBe('');
        expect(sidebar.style.display).toBe('');
        expect(sidebar.hasAttribute('data-mobile-hidden')).toBe(false);

        await vi.advanceTimersByTimeAsync(350);
        expect(nav.elements.sheet.style.display).toBe('none');
    });

    it('traps keyboard focus and returns it to More when Escape closes the sheet', () => {
        setInnerWidth(500);
        nav = new MobileNavigation({ persistState: false });
        nav.init();
        nav.openSheet();
        flushSheetFrames();
        const focusable = nav.getFocusableElements();
        const first = focusable[0];
        const last = focusable[focusable.length - 1];

        last.focus();
        const forwardTab = new KeyboardEvent('keydown', {
            key: 'Tab',
            bubbles: true,
            cancelable: true,
        });
        document.dispatchEvent(forwardTab);
        expect(forwardTab.defaultPrevented).toBe(true);
        expect(document.activeElement).toBe(first);

        const backwardTab = new KeyboardEvent('keydown', {
            key: 'Tab',
            shiftKey: true,
            bubbles: true,
            cancelable: true,
        });
        document.dispatchEvent(backwardTab);
        expect(backwardTab.defaultPrevented).toBe(true);
        expect(document.activeElement).toBe(last);

        const escape = new KeyboardEvent('keydown', {
            key: 'Escape',
            bubbles: true,
            cancelable: true,
        });
        document.dispatchEvent(escape);
        const more = nav.elements.nav.querySelector('[data-tab-id="more"]');
        expect(escape.defaultPrevented).toBe(true);
        expect(nav.state.sheetOpen).toBe(false);
        expect(document.activeElement).toBe(more);
        expect(more.getAttribute('aria-expanded')).toBe('false');
    });

    it.each([
        ['hands logout to the submit handler', true, 0],
        ['falls back to a plain logout POST without a handler', false, 1],
    ])('where requestSubmit is unavailable, %s', (_name, handled, posts) => {
        document.body.innerHTML = '<form id="logout-form"></form>';
        const form = document.getElementById('logout-form');
        // Safari before 16 has no requestSubmit.
        Object.defineProperty(form, 'requestSubmit', { value: undefined });
        const submit = vi.spyOn(form, 'submit').mockImplementation(() => {});
        const seen = [];
        const listener = event => {
            seen.push(event.type);
            if (handled) event.preventDefault();
        };
        document.addEventListener('submit', listener);
        try {
            MobileNavigation.prototype.handleLogout.call({});
        } finally {
            document.removeEventListener('submit', listener);
        }
        expect(seen).toEqual(['submit']);
        expect(submit).toHaveBeenCalledTimes(posts);
    });

    it('delegates tab, sheet, and logout actions through safe navigation', () => {
        setInnerWidth(500);
        document.body.innerHTML = '<form id="logout-form"></form>';
        const submit = vi.spyOn(
            document.getElementById('logout-form'),
            'requestSubmit',
        ).mockImplementation(() => {});
        nav = new MobileNavigation();
        nav.init();

        nav.elements.nav.querySelector('[data-tab-id="history"] i').click();
        expect(URLValidator.safeAssign).toHaveBeenCalledWith(
            window.location,
            'href',
            '/history/',
        );
        expect(nav.state.activeTab).toBe('history');
        expect(JSON.parse(localStorage.getItem('mobileNavState')))
            .toEqual({ activeTab: 'history' });

        nav.elements.nav.querySelector('[data-tab-id="more"] i').click();
        expect(nav.state.sheetOpen).toBe(true);
        nav.elements.sheet.querySelector('[data-item-id="collections"] i')
            .click();
        expect(URLValidator.safeAssign).toHaveBeenLastCalledWith(
            window.location,
            'href',
            '/library/collections',
        );
        expect(nav.state.sheetOpen).toBe(false);

        nav.elements.sheet.querySelector('[data-item-id="logout"] i').click();
        expect(submit).toHaveBeenCalledOnce();
    });

    it('dismisses only a downward swipe beyond the sheet threshold', () => {
        setInnerWidth(500);
        nav = new MobileNavigation({ persistState: false });
        nav.init();
        Object.defineProperty(nav.elements.sheet, 'offsetHeight', {
            value: 500,
            configurable: true,
        });
        nav.openSheet();
        const handle = nav.elements.sheet.querySelector(
            '.ldr-mobile-sheet-handle',
        );

        handle.dispatchEvent(touchEvent('touchstart', 100));
        handle.dispatchEvent(touchEvent('touchmove', 140));
        expect(nav.elements.sheet.style.transform).toBe('translateY(40px)');
        handle.dispatchEvent(touchEvent('touchend', 140));
        expect(nav.state.sheetOpen).toBe(true);
        expect(nav.elements.sheet.style.transform).toBe('');

        handle.dispatchEvent(touchEvent('touchstart', 100));
        handle.dispatchEvent(touchEvent('touchmove', 230));
        handle.dispatchEvent(touchEvent('touchend', 230));
        expect(nav.state.sheetOpen).toBe(false);
        expect(nav.elements.sheet.getAttribute('aria-hidden')).toBe('true');
    });
});