import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
const push = vi.fn()
const refresh = vi.fn()
vi.mock('next/navigation', () => ({
usePathname: () => '/hse/capa',
useRouter: () => ({ push, refresh }),
}))
vi.mock('@/components/notifications/bell', () => ({ NotificationBell: () => }))
import { AppShell } from '@/components/layout/app-shell'
beforeEach(() => {
push.mockClear()
refresh.mockClear()
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true }))
})
describe('AppShell', () => {
it('has a 56px dark top bar, a grey sidebar and the page content', () => {
const { container } = render(page body
)
expect(container.querySelector('header')?.className).toContain('h-14')
expect(container.querySelector('header')?.className).toContain('bg-topbar')
expect(container.querySelector('aside')?.className).toContain('bg-rule')
expect(container.querySelector('aside')?.className).toContain('w-60')
expect(screen.getByText('page body')).toBeTruthy()
expect(screen.getByText('Setia HSE')).toBeTruthy()
})
it('marks the active sidebar item with aria-current on a white ringed item', () => {
render()
const active = screen.getAllByRole('link', { name: /CAPA/ }).find(a => a.getAttribute('aria-current') === 'page')!
expect(active.className).toContain('bg-surface')
expect(active.className).toContain('shadow-chip')
})
it('shows a GET search form that targets the incident list for HSE roles', () => {
const { container } = render()
const form = container.querySelector('header form')!
expect(form.getAttribute('action')).toBe('/hse/incidents')
expect(form.getAttribute('method')).toBe('get')
expect((form.querySelector('input') as HTMLInputElement).name).toBe('q')
})
it('focuses the search box on Cmd-K', () => {
render()
fireEvent.keyDown(document, { key: 'k', metaKey: true })
expect(document.activeElement?.getAttribute('name')).toBe('q')
})
it('targets the site incident list for the SSA role', () => {
const { container } = render()
expect(container.querySelector('header form')!.getAttribute('action')).toBe('/ssa/incidents')
})
it('hides the search box for roles without an incident list', () => {
const { container } = render()
expect(container.querySelector('header form')).toBeNull()
})
it('shows the user chip, version label, and logs out', async () => {
render()
expect(screen.getAllByText('AL').length).toBeGreaterThan(0)
expect(screen.getByText('v1.2.2')).toBeTruthy()
fireEvent.click(screen.getAllByRole('button', { name: /Log ?out/i })[0])
await Promise.resolve()
expect(fetch).toHaveBeenCalledWith('/api/auth/logout', { method: 'POST' })
})
it('renders a mobile tab bar that is hidden from md up', () => {
const { container } = render()
const nav = container.querySelector('nav[aria-label="Mobile"]')!
expect(nav.className).toContain('md:hidden')
expect(nav.className).toContain('border-t')
})
it('has no 2px rules', () => {
const { container } = render()
expect(container.innerHTML).not.toMatch(/border-[a-z]?-?2\b/)
})
})