import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:social_app/core/storage/token_storage.dart';

class MockTokenStorage extends Mock implements TokenStorage {}

void main() {
  late TokenStorage storage;

  setUp(() {
    storage = MockTokenStorage();
  });

  group('TokenStorage', () {
    test('saves and retrieves access token', () async {
      when(
        () => storage.getAccessToken(),
      ).thenAnswer((_) async => 'test_access');
      when(
        () =>
            storage.saveTokens(access: 'test_access', refresh: 'test_refresh'),
      ).thenAnswer((_) async {});

      await storage.saveTokens(access: 'test_access', refresh: 'test_refresh');
      final token = await storage.getAccessToken();

      expect(token, 'test_access');
      verify(
        () =>
            storage.saveTokens(access: 'test_access', refresh: 'test_refresh'),
      ).called(1);
    });

    test('clear removes all tokens', () async {
      when(() => storage.clear()).thenAnswer((_) async {});
      when(() => storage.getAccessToken()).thenAnswer((_) async => null);

      await storage.clear();
      final token = await storage.getAccessToken();

      expect(token, isNull);
    });
  });
}
