import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:userfront/core/services/auth_token_store.dart';
import 'package:userfront/features/dashboard/presentation/dashboard_screen.dart';

void main() {
  setUp(() {
    AuthTokenStore.clear();
  });

  tearDown(() {
    AuthTokenStore.clear();
  });

  testWidgets('대시보드는 로그인 토큰이 있으면 크래시 없이 기본 프레임을 렌더링한다', (tester) async {
    final recordedErrors = <FlutterErrorDetails>[];
    final previousOnError = FlutterError.onError;
    FlutterError.onError = (details) {
      final text = details.exceptionAsString();
      if (text.contains('A RenderFlex overflowed')) {
        return;
      }
      recordedErrors.add(details);
    };
    addTearDown(() {
      FlutterError.onError = previousOnError;
    });

    tester.view.devicePixelRatio = 1.0;
    tester.view.physicalSize = const Size(1920, 1080);
    addTearDown(tester.view.resetPhysicalSize);
    addTearDown(tester.view.resetDevicePixelRatio);

    AuthTokenStore.setToken('smoke-token', provider: 'ory');

    await tester.pumpWidget(
      const ProviderScope(child: MaterialApp(home: DashboardScreen())),
    );
    await tester.pump();

    expect(find.byType(Scaffold), findsOneWidget);
    final hasNullCheckCrash = recordedErrors.any(
      (error) => error.exceptionAsString().contains(
        'Null check operator used on a null value',
      ),
    );
    expect(hasNullCheckCrash, isFalse);
  });
}
