import 'package:flutter_test/flutter_test.dart';
import 'package:marianum_mobile/api/marianumcloud/webdav/queries/list_files/cacheable_file.dart';
import 'package:marianum_mobile/utils/file_clipboard.dart';

CacheableFile _file(String name) =>
    CacheableFile(path: '/$name', isDirectory: false, name: name);

void main() {
  // FileClipboard is a singleton — clear between tests so state doesn't leak.
  setUp(FileClipboard.instance.clear);

  group('FileClipboard.cut', () {
    test('switches to cut state and notifies listeners', () {
      final cb = FileClipboard.instance;
      var notifyCount = 0;
      void listener() => notifyCount++;
      cb.addListener(listener);
      addTearDown(() => cb.removeListener(listener));

      cb.cut([_file('a.txt')]);

      expect(cb.operation, FileClipboardOperation.cut);
      expect(cb.files.map((f) => f.name), ['a.txt']);
      expect(cb.isEmpty, isFalse);
      expect(notifyCount, 1);
    });

    test('empty input is a no-op', () {
      final cb = FileClipboard.instance;
      var notifyCount = 0;
      void listener() => notifyCount++;
      cb.addListener(listener);
      addTearDown(() => cb.removeListener(listener));

      cb.cut(const []);

      expect(cb.operation, isNull);
      expect(cb.isEmpty, isTrue);
      expect(notifyCount, 0, reason: 'no state change → no notifyListeners');
    });

    test('files getter returns an unmodifiable view', () {
      final cb = FileClipboard.instance;
      cb.cut([_file('a.txt')]);
      expect(() => cb.files.add(_file('b.txt')), throwsUnsupportedError);
    });
  });

  group('FileClipboard.copy', () {
    test('switches to copy state and notifies listeners', () {
      final cb = FileClipboard.instance;
      cb.copy([_file('a.txt'), _file('b.txt')]);

      expect(cb.operation, FileClipboardOperation.copy);
      expect(cb.files, hasLength(2));
    });

    test('overwrites a previous cut state', () {
      final cb = FileClipboard.instance;
      cb.cut([_file('cut.txt')]);
      cb.copy([_file('copy.txt')]);

      expect(cb.operation, FileClipboardOperation.copy);
      expect(cb.files.single.name, 'copy.txt');
    });
  });

  group('FileClipboard.clear', () {
    test('resets state and notifies', () {
      final cb = FileClipboard.instance;
      cb.copy([_file('a.txt')]);
      var notifyCount = 0;
      void listener() => notifyCount++;
      cb.addListener(listener);
      addTearDown(() => cb.removeListener(listener));

      cb.clear();

      expect(cb.operation, isNull);
      expect(cb.isEmpty, isTrue);
      expect(notifyCount, 1);
    });

    test('clearing an already-empty clipboard is a no-op', () {
      final cb = FileClipboard.instance;
      var notifyCount = 0;
      void listener() => notifyCount++;
      cb.addListener(listener);
      addTearDown(() => cb.removeListener(listener));

      cb.clear();
      cb.clear();

      expect(notifyCount, 0);
    });
  });
}
