import 'dart:typed_data';

import 'package:flutter_test/flutter_test.dart';
import 'package:hashlib/random.dart';
import 'package:twonly/src/utils/misc.dart';
import 'package:twonly/src/utils/pow.dart';
import 'package:twonly/src/visual/components/animate_icon.comp.dart';

void main() {
  group('testing utils', () {
    test('test isEmoji function', () {
      expect(isOneEmoji('Hallo'), false);
      expect(isOneEmoji('😂'), true);
      expect(isOneEmoji('😂😂'), false);
      expect(isOneEmoji('Hallo 😂'), false);
      for (final icon in EmojiAnimationComp.animatedIcons.keys) {
        expect(isOneEmoji(icon), true);
      }
    });

    test('test proof-of-work simple', () async {
      // ignore: prefer_single_quotes
      expect(await calculatePoW("testing", 10), 783);
    });

    test('encode hex', () async {
      final list1 = Uint8List.fromList([41, 41, 41, 41, 41, 41, 41]);
      expect(list1, hexToUint8List(uint8ListToHex(list1)));
    });

    test('Zero inputs produce all-zero UUID', () {
      expect(
        getUUIDforDirectChat(0, 0),
        '00000000-0000-0000-0000-000000000000',
      );
      expect(getUUIDforDirectChat(0, 0).length, uuid.v1().length);
    });

    test('Max int values (0x7fffffff)', () {
      const max32 = 0x7fffffff; // 2147483647
      expect(
        getUUIDforDirectChat(max32, max32),
        '00000000-7fff-ffff-0000-00007fffffff',
      );
    });

    test('Bigger goes front', () {
      expect(
        getUUIDforDirectChat(1, 0),
        '00000000-0000-0001-0000-000000000000',
      );
      expect(
        getUUIDforDirectChat(0, 1),
        '00000000-0000-0001-0000-000000000000',
      );
    });

    test('Arbitrary within 32-bit range', () {
      expect(
        getUUIDforDirectChat(0x12345678, 0x0abcdef0),
        '00000000-1234-5678-0000-00000abcdef0',
      );
    });
    test('Arbitrary within 32-bit range', () {
      expect(
        // ignore: avoid_js_rounded_ints
        getUUIDforDirectChat(0x7dc8a4f20a75de46, 0x7ffffffffffffff8),
        '7fffffff-ffff-fff8-7dc8-a4f20a75de46',
      );
    });

    // test('Reject values > 0x7fffffff', () {
    //   expect(() => getUUIDforDirectChat(0x80000000, 0), throwsArgumentError);
    // });
  });
}
