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

import 'package:smplayer/features/player/session/relink_scheduler.dart';

void main() {
  group('RelinkScheduler', () {
    test('schedule cancels pending sustain timer (attempts must accumulate)',
        () {
      fakeAsync((async) {
        final s = RelinkScheduler();
        var gaveUp = false;

        void failOnce() {
          s.schedule(onRelink: (_) {}, onGiveUp: () => gaveUp = true);

          async.flushTimers(flushPeriodicTimers: false);
          s.armSustain(sustain: const Duration(seconds: 10));

          async.elapse(const Duration(seconds: 5));
        }


        failOnce();
        failOnce();
        failOnce();
        expect(gaveUp, isFalse);
        s.schedule(onRelink: (_) {}, onGiveUp: () => gaveUp = true);
        expect(gaveUp, isTrue);
        s.dispose();
      });
    });

    test('sustained playback resets attempts', () {
      fakeAsync((async) {
        final s = RelinkScheduler();
        s.schedule(onRelink: (_) {}, onGiveUp: () {});
        async.flushTimers(flushPeriodicTimers: false);
        expect(s.attempts, 1);
        s.armSustain(sustain: const Duration(seconds: 10));
        async.elapse(const Duration(seconds: 11));
        expect(s.attempts, 0);
        s.dispose();
      });
    });
  });
}
