import 'package:flutter_test/flutter_test.dart';
import 'package:tane/services/trust_referents.dart';

import '../support/test_support.dart';

void main() {
  const npub =
      'npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6';
  const hex =
      '3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d';

  // No bundled asset in the unit test bundle → _bundled() degrades to empty, so
  // these exercise the user-added set (the cold-start bootstrap path).
  late TrustReferents referents;
  setUp(() => referents = TrustReferents(InMemorySecretStore()));

  test('starts empty', () async {
    expect(await referents.all(), isEmpty);
    expect(await referents.userAdded(), isEmpty);
  });

  test('adds a referent from an npub, storing hex', () async {
    final stored = await referents.add(npub);
    expect(stored, hex);
    expect(await referents.userAdded(), {hex});
    expect(await referents.all(), contains(hex));
  });

  test('accepts a hex key directly and is idempotent', () async {
    await referents.add(hex);
    await referents.add(npub); // same identity, npub form
    expect(await referents.userAdded(), {hex});
  });

  test('rejects an invalid identity code', () async {
    expect(() => referents.add('not-a-key'), throwsFormatException);
    expect(await referents.userAdded(), isEmpty);
  });

  test('removes a referent', () async {
    await referents.add(hex);
    await referents.remove(hex);
    expect(await referents.userAdded(), isEmpty);
  });
}
