import 'package:flutter_test/flutter_test.dart';
import 'package:marianum_mobile/api/marianumcloud/files_sharing/share_permissions.dart';

void main() {
  group('permissionsFor', () {
    test('readOnly is just the read bit', () {
      expect(permissionsFor(SharePreset.readOnly), kPermissionRead);
    });

    test('edit on a file is only read+update (no create/delete)', () {
      expect(
        permissionsFor(SharePreset.edit),
        kPermissionRead | kPermissionUpdate,
      );
    });

    test('edit on a folder adds create+delete', () {
      expect(
        permissionsFor(SharePreset.edit, isFolder: true),
        kPermissionRead |
            kPermissionUpdate |
            kPermissionCreate |
            kPermissionDelete,
      );
    });

    test('edit adds the share bit when reshare is allowed', () {
      final mask = permissionsFor(SharePreset.edit, allowReshare: true);
      expect(hasPermission(mask, kPermissionShare), isTrue);
      expect(hasPermission(mask, kPermissionUpdate), isTrue);
    });

    test('fileDrop is create-only (upload)', () {
      expect(permissionsFor(SharePreset.fileDrop), kPermissionCreate);
      expect(
        hasPermission(permissionsFor(SharePreset.fileDrop), kPermissionRead),
        isFalse,
      );
    });
  });

  group('presetFromBitmask', () {
    test('round-trips readOnly and fileDrop', () {
      expect(
        presetFromBitmask(permissionsFor(SharePreset.readOnly)),
        SharePreset.readOnly,
      );
      expect(
        presetFromBitmask(permissionsFor(SharePreset.fileDrop)),
        SharePreset.fileDrop,
      );
    });

    test('edit is recognised regardless of the reshare bit', () {
      expect(
        presetFromBitmask(permissionsFor(SharePreset.edit)),
        SharePreset.edit,
      );
      expect(
        presetFromBitmask(permissionsFor(SharePreset.edit, allowReshare: true)),
        SharePreset.edit,
      );
    });

    test('returns null for an unmatched mask', () {
      // share-only (16) with no read bit matches nothing.
      expect(presetFromBitmask(kPermissionShare), isNull);
      expect(presetFromBitmask(0), isNull);
    });
  });

  group('hasPermission', () {
    test('detects individual bits', () {
      const mask = kPermissionRead | kPermissionUpdate;
      expect(hasPermission(mask, kPermissionRead), isTrue);
      expect(hasPermission(mask, kPermissionUpdate), isTrue);
      expect(hasPermission(mask, kPermissionDelete), isFalse);
    });
  });
}
