import 'package:flutter_test/flutter_test.dart';
import 'package:marianum_mobile/widget/avatar_disk_cache.dart';

void main() {
  group('AvatarDiskCache.fileName', () {
    test('user files carry the size, group files do not', () {
      expect(
        AvatarDiskCache.fileName(id: 'alice', isGroup: false, size: 256),
        'u_616c696365_256',
      );
      expect(
        AvatarDiskCache.fileName(id: 'alice', isGroup: false, size: 64),
        'u_616c696365_64',
      );
      // Groups are served at one fixed size — the size argument is ignored.
      expect(
        AvatarDiskCache.fileName(id: 'room1', isGroup: true, size: 512),
        AvatarDiskCache.fileName(id: 'room1', isGroup: true, size: 64),
      );
      expect(
        AvatarDiskCache.fileName(id: 'room1', isGroup: true, size: 0),
        startsWith('g_'),
      );
    });

    test('names are file-safe even for exotic ids', () {
      final name = AvatarDiskCache.fileName(
        id: 'a/b c@d',
        isGroup: false,
        size: 128,
      );
      expect(name, matches(RegExp(r'^u_[0-9a-f]+_128$')));
    });

    test(
      'a short id evict prefix never matches a longer id file (hex + _ '
      'separator keeps the boundary unambiguous)',
      () {
        // Eviction deletes files starting with `u_<hex(id)>_`.
        final shortPrefix =
            'u_${AvatarDiskCache.fileName(id: 'a', isGroup: false, size: 1).split('_')[1]}_';
        final longFile = AvatarDiskCache.fileName(
          id: 'a_b',
          isGroup: false,
          size: 256,
        );
        expect(longFile.startsWith(shortPrefix), isFalse);
      },
    );
  });
}
