import { renderToStaticMarkup } from 'react-dom/server';
import { describe, expect, it } from 'vitest';
import { AuthGate, initialAuthenticationStatus } from './AuthGate';

describe('AuthGate', () => {
  it('shows a neutral session check without rendering the signed-out view', () => {
    const html = renderToStaticMarkup(
      <AuthGate
        status="checking"
        authenticated={<div>工作台内容</div>}
        unauthenticated={<div>登录后进入工作台</div>}
      />,
    );

    expect(html).toContain('正在确认登录状态');
    expect(html).not.toContain('登录后进入工作台');
    expect(html).not.toContain('工作台内容');
    expect(html).toContain('aria-busy="true"');
  });

  it('renders the matching view after authentication is resolved', () => {
    const authenticated = renderToStaticMarkup(
      <AuthGate
        status="authenticated"
        authenticated={<div>工作台内容</div>}
        unauthenticated={<div>登录后进入工作台</div>}
      />,
    );
    const unauthenticated = renderToStaticMarkup(
      <AuthGate
        status="unauthenticated"
        authenticated={<div>工作台内容</div>}
        unauthenticated={<div>登录后进入工作台</div>}
      />,
    );

    expect(authenticated).toContain('工作台内容');
    expect(authenticated).not.toContain('登录后进入工作台');
    expect(unauthenticated).toContain('登录后进入工作台');
    expect(unauthenticated).not.toContain('工作台内容');
  });

  it('checks for a cookie session only when no stored access token exists', () => {
    expect(initialAuthenticationStatus('')).toBe('checking');
    expect(initialAuthenticationStatus('local-access-token')).toBe('authenticated');
  });
});
