import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:minstrel/player/audio_handler.dart';
import 'package:minstrel/player/player_provider.dart';

void main() {
  // MinstrelAudioHandler() instantiates just_audio's AudioPlayer, which calls
  // setMethodCallHandler on a platform channel during construction. Without an
  // initialized binding the call asserts. We're not exercising real audio in
  // the test — this just gives us a working binary messenger.
  TestWidgetsFlutterBinding.ensureInitialized();

  test('audioHandlerProvider must be overridden — bare read throws', () {
    final container = ProviderContainer();
    addTearDown(container.dispose);
    // Riverpod 3 wraps the underlying error in a ProviderException. The
    // intent of the test is "bare read fails," so accept the wrapper as
    // long as the inner error is the UnimplementedError we threw.
    expect(
      () => container.read(audioHandlerProvider),
      throwsA(predicate((e) {
        if (e is UnimplementedError) return true;
        // ProviderException is private to riverpod; match by name + by
        // walking its toString for the inner UnimplementedError text.
        return e.runtimeType.toString().contains('ProviderException') &&
            e.toString().contains('UnimplementedError');
      })),
    );
  });

  test('overridden audioHandlerProvider exposes playbackState stream', () {
    final handler = MinstrelAudioHandler();
    final container = ProviderContainer(overrides: [
      audioHandlerProvider.overrideWithValue(handler),
    ]);
    addTearDown(container.dispose);
    expect(container.read(audioHandlerProvider), same(handler));
    final sub = container.listen(playbackStateProvider, (_, __) {});
    sub.close();
  });
}
