// REQ-USR-003: 路由表接线（替换 / 占位，含守卫与重定向，BR1/BR2/D7）
import { describe, it, expect } from 'vitest';
import { screen } from '@testing-library/react';
import { renderShell } from './renderShell';
import AppRouter from '../../src/router';
import type { AuthUser } from '../../src/api/types';

const ADMIN: AuthUser = { id: 1, sUserName: '朱子纯', sUserType: '超级管理员', sLanguage: '中文' };

function renderRouter(initialEntries: string[], preloadedAuth?: Parameters<typeof renderShell>[1]['preloadedAuth']) {
  return renderShell(<AppRouter />, { initialEntries, preloadedAuth });
}

describe('AppRouter', () => {
  it('unauthenticated / redirects to /login', () => {
    renderRouter(['/'], { token: null, user: null });
    // 登录页含「用户登录」标题
    expect(screen.getByText('用户登录')).toBeInTheDocument();
  });

  it('authenticated / renders HomePage shell', () => {
    renderRouter(['/'], { token: 't', user: ADMIN });
    expect(screen.getByText('KPI监控')).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '全部导航' })).toBeInTheDocument();
  });

  it('authenticated /usr/users renders under AppLayout', () => {
    renderRouter(['/usr/users'], { token: 't', user: ADMIN });
    // 外壳顶栏在；用户列表标签激活（占位内容由 FE-03 落地）
    expect(screen.getByRole('button', { name: '全部导航' })).toBeInTheDocument();
    expect(screen.getByTestId('tab-userlist').getAttribute('aria-pressed')).toBe('true');
  });

  it('authenticated /login redirects to /', () => {
    renderRouter(['/login'], { token: 't', user: ADMIN });
    expect(screen.getByText('KPI监控')).toBeInTheDocument();
  });

  it('unknown protected path redirects to /', () => {
    renderRouter(['/no/such/path'], { token: 't', user: ADMIN });
    expect(screen.getByText('KPI监控')).toBeInTheDocument();
  });
});
