import 'package:flutter_test/flutter_test.dart';
import 'package:userfront/core/services/auth_proxy_service.dart';
import 'package:userfront/features/auth/domain/consent_error_routing.dart';

void main() {
  test('tenant_not_allowed consent error routes to dedicated error screen', () {
    const error = AuthProxyException(
      errorCode: 'tenant_not_allowed',
      message: '허용되지 않은 테넌트입니다.',
    );

    expect(shouldRouteConsentErrorToErrorScreen(error), isTrue);
  });

  test('generic consent error stays on consent screen', () {
    const error = AuthProxyException(
      errorCode: 'forbidden',
      message: '동의 정보를 가져오지 못했습니다.',
    );

    expect(shouldRouteConsentErrorToErrorScreen(error), isFalse);
  });

  test('tenant_not_allowed auth error also routes to error screen', () {
    const error = AuthProxyException(
      errorCode: 'tenant_not_allowed',
      message: '허용되지 않은 테넌트입니다.',
    );

    expect(shouldRouteTenantAccessErrorToErrorScreen(error), isTrue);
  });

  test('buildTenantAccessErrorPath builds userfront error route', () {
    const error = AuthProxyException(
      errorCode: 'tenant_not_allowed',
      message: '허용되지 않은 테넌트입니다.',
      details: {
        'allowed_tenants': ['tenant-a'],
      },
    );

    final target = buildTenantAccessErrorPath(
      error,
      Uri.parse('https://sso-test.hmac.kr/ko?login_challenge=abc'),
    );

    expect(target, contains('/error?'));
    expect(target, contains('error=tenant_not_allowed'));
    expect(target, contains('error_description='));
    expect(target, contains('details='));
  });
}
