import { jsx as _jsx } from "react/jsx-runtime";
// @vitest-environment happy-dom
import { afterEach, describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react';
// The sidebar fetches forms unread counts on mount; stub it so these brand
// tests stay offline and deterministic.
vi.mock('../../lib/forms-service.js', () => ({
    fetchFormsSidebarCounts: vi.fn(async () => []),
}));
const DEFAULT_THEME = {
    adminBrand: { primaryUrl: null, markUrl: null, darkUrl: null, initials: 'AC' },
    resolvedTheme: 'light',
    density: 'default',
};
let themeState = { ...DEFAULT_THEME };
vi.mock('../../components/ThemeProvider.js', () => ({
    useTheme: () => themeState,
}));
const { Sidebar } = await import('../../layout/Sidebar.js');
function setTheme(brand, resolvedTheme = 'light') {
    themeState = {
        ...DEFAULT_THEME,
        resolvedTheme,
        adminBrand: { ...DEFAULT_THEME.adminBrand, ...brand },
    };
}
function renderSidebar(collapsed, config) {
    return render(_jsx(Sidebar, { collapsed: collapsed, onToggleCollapse: () => { }, currentPath: "/", onNavigate: () => { }, config: config }));
}
afterEach(() => {
    themeState = { ...DEFAULT_THEME };
});
/**
 * Brand lockup fallback behaviour. When an install is branded (a site name is
 * set, so initials exist) but no logo has been uploaded, the sidebar must show
 * the neutral logo placeholder — NOT a filled initials badge, which reads as a
 * user avatar (the real account avatar already lives top-right).
 */
describe('Sidebar brand placeholder', () => {
    it('renders the logo placeholder (not an initials avatar) when branded with no logo — expanded', () => {
        renderSidebar(false, { admin: { branding: { name: 'Acme Co' } } });
        // Placeholder slot is present and the brand name shows beside it.
        expect(screen.getByText('Acme Co')).toBeTruthy();
        // The old avatar-style initials badge must not render.
        expect(screen.queryByText('AC')).toBeNull();
    });
    it('renders the logo placeholder slot when branded with no logo — collapsed', () => {
        renderSidebar(true);
        expect(screen.getByRole('img', { name: 'Logo placeholder' })).toBeTruthy();
        expect(screen.queryByText('AC')).toBeNull();
    });
});
describe('Sidebar brand logo rendering', () => {
    it('shows the primary logo (scaled to fit) when collapsed — never the favicon', () => {
        setTheme({ primaryUrl: 'https://cdn.example.com/logo.svg' });
        renderSidebar(true);
        const img = screen.getByRole('img', { name: 'Admin' });
        expect(img.src).toContain('logo.svg');
        // Wide logos must be width-capped so they scale down instead of overflowing.
        expect(img.className).toContain('max-w-full');
    });
    it('caps the expanded logo width so wide logos stay responsive', () => {
        setTheme({ primaryUrl: 'https://cdn.example.com/logo.svg' });
        renderSidebar(false);
        const img = screen.getByRole('img', { name: 'Admin' });
        expect(img.className).toContain('max-w-full');
        expect(img.className).toContain('object-contain');
    });
    it('prefers the dark logo in dark mode and falls back to primary on error', () => {
        setTheme({
            primaryUrl: 'https://cdn.example.com/logo.svg',
            darkUrl: 'https://cdn.example.com/dark.svg',
        }, 'dark');
        renderSidebar(false);
        const img = screen.getByRole('img', { name: 'Admin' });
        expect(img.src).toContain('dark.svg');
    });
    it('falls through to the placeholder when the logo URL is broken (onError)', () => {
        setTheme({ primaryUrl: 'https://cdn.example.com/broken.svg' });
        renderSidebar(false, { admin: { branding: { name: 'Acme Co' } } });
        // With a brand name configured, the logo alt is the brand name.
        const img = screen.getByRole('img', { name: 'Acme Co' });
        expect(img.src).toContain('broken.svg');
        // Simulate the asset URL failing to load.
        fireEvent.error(img);
        // The broken logo is gone; the neutral placeholder + brand name take over
        // (no browser broken-image glyph left behind).
        expect(screen.queryByRole('img', { name: 'Acme Co' })).toBeNull();
        expect(screen.getByText('Acme Co')).toBeTruthy();
    });
});
//# sourceMappingURL=sidebar-brand.render.test.js.map