import 'package:flutter_test/flutter_test.dart';
import 'package:tane/security/secret_store.dart';
import 'package:tane/security/secure_key_store.dart';

class InMemorySecretStore implements SecretStore {
  final Map<String, String> _values = {};
  int writeCount = 0;

  @override
  Future<String?> read(String key) async => _values[key];

  @override
  Future<void> write(String key, String value) async {
    _values[key] = value;
    writeCount++;
  }
}

void main() {
  group('SecureKeyStore', () {
    test('creates a 256-bit (64 hex char) db key on first run', () async {
      final keys = SecureKeyStore(store: InMemorySecretStore());
      final key = await keys.databaseKeyHex();
      expect(key, matches(RegExp(r'^[0-9a-f]{64}$')));
    });

    test('reuses the same db key across calls (created once)', () async {
      final store = InMemorySecretStore();
      final keys = SecureKeyStore(store: store);
      final first = await keys.databaseKeyHex();
      final second = await keys.databaseKeyHex();
      expect(second, first);
      expect(store.writeCount, 1);
    });

    test('db key and root seed are independent secrets', () async {
      final keys = SecureKeyStore(store: InMemorySecretStore());
      expect(await keys.databaseKeyHex(), isNot(await keys.rootSeedHex()));
    });
  });
}
