stack_wallet/test/cached_electrumx_test.dart
2024-05-27 18:01:41 -06:00

178 lines
5.3 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:hive_test/hive_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:stackwallet/electrumx_rpc/cached_electrumx_client.dart';
import 'package:stackwallet/electrumx_rpc/electrumx_client.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart';
import 'cached_electrumx_test.mocks.dart';
// import 'sample_data/get_anonymity_set_sample_data.dart';
@GenerateMocks([ElectrumXClient, Prefs])
void main() {
group("tests using mock hive", () {
setUp(() async {
await setUpTestHive();
// await Hive.openBox<dynamic>(
// DB.instance.boxNameUsedSerialsCache(coin: Coin.firo));
// await Hive.openBox<dynamic>(DB.instance.boxNameSetCache(coin: Coin.firo));
// await Hive.openBox<dynamic>(DB.instance.boxNameTxCache(coin: Coin.firo));
});
group("getAnonymitySet", () {
// test("empty set cache call", () async {
// final client = MockElectrumXClient();
// when(
// client.getAnonymitySet(
// groupId: "1",
// blockhash: "",
// ),
// ).thenAnswer(
// (_) async => GetAnonymitySetSampleData.data,
// );
//
// final cachedClient = CachedElectrumX(
// electrumXClient: client,
// port: 0,
// failovers: [],
// server: '',
// useSSL: true,
// prefs: Prefs.instance);
//
// final result = await cachedClient.getAnonymitySet(
// groupId: "1",
// coin: Coin.firo,
// );
//
// final expected =
// Map<String, dynamic>.from(GetAnonymitySetSampleData.data);
// expected["setId"] = "1";
//
// expect(result, expected);
// });
//
// test("use and update set cache call", () async {
// final storedData = Map.from(GetAnonymitySetSampleData.initialData);
// storedData["setId"] = "1";
// final box = await Hive.openBox('Some coinName_anonymitySetCache');
// await box.put("1", storedData);
//
// final client = MockElectrumXClient();
// when(
// client.getAnonymitySet(
// groupId: "1",
// blockhash: GetAnonymitySetSampleData.initialData["blockHash"],
// ),
// ).thenAnswer(
// (_) async => GetAnonymitySetSampleData.followUpData,
// );
//
// final cachedClient = CCachedElectrumX(
// electrumXClient: client,
// port: 0,
// failovers: [],
// server: '',
// useSSL: true,
// prefs: Prefs.instance);
//
// final result = await cachedClient.getAnonymitySet(
// groupId: "1",
// coinName: "Some coinName",
// callOutSideMainIsolate: true,
// );
//
// final expected = Map.from(GetAnonymitySetSampleData.finalData as Map);
// expected["setId"] = "1";
//
// expect(result, expected);
// fail("This test needs updating");
// });
// test("getAnonymitySet throws", () async {
// final client = MockElectrumXClient();
// when(
// client.getAnonymitySet(
// groupId: "1",
// blockhash: "",
// ),
// ).thenThrow(Exception());
//
// final cachedClient = CachedElectrumX(
// electrumXClient: client,
// port: 0,
// failovers: [],
// server: '',
// useSSL: true,
// prefs: Prefs.instance);
//
// expect(
// () async => await cachedClient.getAnonymitySet(
// groupId: "1",
// coin: Coin.firo,
// ),
// throwsA(isA<Exception>()));
// });
});
test("getTransaction throws", () async {
final client = MockElectrumXClient();
when(
client.getTransaction(
txHash: "some hash",
),
).thenThrow(Exception());
final cachedClient = CachedElectrumXClient(
electrumXClient: client,
);
expect(
() async => await cachedClient.getTransaction(
txHash: "some hash",
cryptoCurrency: Firo(
CryptoCurrencyNetwork.main,
),
),
throwsA(isA<Exception>()));
});
test("clearSharedTransactionCache", () async {
final cachedClient = CachedElectrumXClient(
electrumXClient: MockElectrumXClient(),
);
bool didThrow = false;
try {
await cachedClient.clearSharedTransactionCache(
cryptoCurrency: Firo(
CryptoCurrencyNetwork.main,
),
);
} catch (_) {
didThrow = true;
}
expect(didThrow, false);
});
tearDown(() async {
await tearDownTestHive();
});
});
test(".from factory", () {
final node = ElectrumXNode(
address: "some address",
port: 1,
name: "some name",
id: "some ID",
useSSL: true,
);
final client =
CachedElectrumXClient.from(electrumXClient: MockElectrumXClient());
expect(client, isA<CachedElectrumXClient>());
});
}