import 'package:commons_core/commons_core.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/services/social_service.dart';

/// Wiring test: the app derives its social identity from the SAME root seed the
/// recovery QR backs up, deterministically and without any network.
void main() {
  // A fixed 32-byte seed (64 hex chars).
  const seedHex =
      '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f';

  test('derives a stable Nostr identity from the root-seed hex', () async {
    final service = await SocialService.fromRootSeedHex(seedHex);
    expect(service.npub, startsWith('npub1'));
    expect(service.publicKeyHex, matches(RegExp(r'^[0-9a-f]{64}$')));
  });

  test('is deterministic: same seed → same public identity', () async {
    final a = await SocialService.fromRootSeedHex(seedHex);
    final b = await SocialService.fromRootSeedHex(seedHex);
    expect(a.publicKeyHex, b.publicKeyHex);
    expect(a.npub, b.npub);
  });

  test('matches commons_core derivation on the same bytes', () async {
    final service = await SocialService.fromRootSeedHex(seedHex);
    final direct = await NostrKeyDerivation.deriveFromSeed([
      for (var i = 0; i < seedHex.length; i += 2)
        int.parse(seedHex.substring(i, i + 2), radix: 16),
    ]);
    expect(service.publicKeyHex, direct.publicKeyHex);
  });

  test('a different seed yields a different identity', () async {
    final a = await SocialService.fromRootSeedHex(seedHex);
    final b = await SocialService.fromRootSeedHex(
      'ff${seedHex.substring(2)}',
    );
    expect(a.publicKeyHex, isNot(b.publicKeyHex));
  });

  test('rejects a malformed (odd-length) seed hex', () async {
    await expectLater(
      SocialService.fromRootSeedHex('abc'),
      throwsA(isA<ArgumentError>()),
    );
  });
}
