// @vitest-environment happy-dom
import { describe, expect, it } from 'vitest';
import { act, renderHook } from '@testing-library/react';
import { parseRouteQuery, useAdminRouter } from '../../router/index.js';
// The admin router must carry the query string in `currentPath` so deep links
// like `/pages/new?parent=<id>` survive hard reloads and back/forward — not
// just in-app `navigate()` calls (which always carried it).
describe('useAdminRouter query strings', () => {
    it('includes the query string when syncing from the browser URL on mount', () => {
        window.history.pushState({}, '', '/admin/pages/new?parent=abc');
        const { result } = renderHook(() => useAdminRouter('/admin', '/pages/new'));
        expect(result.current.currentPath).toBe('/pages/new?parent=abc');
        expect(result.current.search).toBe('?parent=abc');
        // matchRoute strips the query, so route patterns still match.
        expect(result.current.matchRoute('/pages/new')).toEqual({});
        expect(parseRouteQuery(result.current.currentPath).get('parent')).toBe('abc');
    });
    it('carries the query string through popstate (back/forward)', () => {
        window.history.pushState({}, '', '/admin/settings?tab=users');
        const { result } = renderHook(() => useAdminRouter('/admin', '/'));
        act(() => {
            window.history.pushState({}, '', '/admin/seo?tab=audit&issueId=i1');
            window.dispatchEvent(new PopStateEvent('popstate'));
        });
        expect(result.current.currentPath).toBe('/seo?tab=audit&issueId=i1');
        expect(result.current.search).toBe('?tab=audit&issueId=i1');
        expect(result.current.matchRoute('/seo')).toEqual({});
        expect(result.current.matchRoute('/seo/:id')).toBeNull();
    });
    it('handles a query on the admin root path', () => {
        window.history.pushState({}, '', '/admin?foo=1');
        const { result } = renderHook(() => useAdminRouter('/admin', '/'));
        expect(result.current.currentPath).toBe('/?foo=1');
        expect(result.current.matchRoute('/')).toEqual({});
    });
    it('parseRouteQuery accepts full paths, raw search strings, and hashes', () => {
        expect(parseRouteQuery('/pages/new?parent=abc').get('parent')).toBe('abc');
        expect(parseRouteQuery('?tab=users&selected=u1').get('selected')).toBe('u1');
        expect(parseRouteQuery('/pages').get('parent')).toBeNull();
        expect(parseRouteQuery('').get('parent')).toBeNull();
        expect(parseRouteQuery('/seo?tab=audit#section').get('tab')).toBe('audit');
    });
});
//# sourceMappingURL=query-string.test.js.map