From 3456832050adfb8064fae832a6fa96f7fa13f810 Mon Sep 17 00:00:00 2001 From: Fmar Date: Thu, 26 Dec 2024 01:15:55 +0100 Subject: [PATCH] increase coverage --- .../usecases/nwc/consts/nwc_kind.dart | 4 + .../usecases/nwc/requests/make_invoice.dart | 2 +- .../usecases/nwc/requests/nwc_request.dart | 3 + .../test/usecases/nwc/nwc_request_test.dart | 77 +++++++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 packages/ndk/test/usecases/nwc/nwc_request_test.dart diff --git a/packages/ndk/lib/domain_layer/usecases/nwc/consts/nwc_kind.dart b/packages/ndk/lib/domain_layer/usecases/nwc/consts/nwc_kind.dart index 22418e69a..75b11f8d6 100644 --- a/packages/ndk/lib/domain_layer/usecases/nwc/consts/nwc_kind.dart +++ b/packages/ndk/lib/domain_layer/usecases/nwc/consts/nwc_kind.dart @@ -1,3 +1,6 @@ +// ignore_for_file: constant_identifier_names + +/// Nwc Kinds enum NwcKind { INFO(13194), REQUEST(23194), @@ -5,6 +8,7 @@ enum NwcKind { LEGACY_NOTIFICATION(23196), NOTIFICATION(23197); + /// int value final int value; const NwcKind(this.value); diff --git a/packages/ndk/lib/domain_layer/usecases/nwc/requests/make_invoice.dart b/packages/ndk/lib/domain_layer/usecases/nwc/requests/make_invoice.dart index c2c11fd73..9f7573507 100644 --- a/packages/ndk/lib/domain_layer/usecases/nwc/requests/make_invoice.dart +++ b/packages/ndk/lib/domain_layer/usecases/nwc/requests/make_invoice.dart @@ -2,7 +2,7 @@ import 'package:ndk/domain_layer/usecases/nwc/consts/nwc_method.dart'; import 'nwc_request.dart'; -// Subclass for requests to make a bolt11 invoice +/// Subclass for requests to make a bolt11 invoice class MakeInvoiceRequest extends NwcRequest { final int amountSat; final String? description; diff --git a/packages/ndk/lib/domain_layer/usecases/nwc/requests/nwc_request.dart b/packages/ndk/lib/domain_layer/usecases/nwc/requests/nwc_request.dart index 92c9d88ac..fa9cc5510 100644 --- a/packages/ndk/lib/domain_layer/usecases/nwc/requests/nwc_request.dart +++ b/packages/ndk/lib/domain_layer/usecases/nwc/requests/nwc_request.dart @@ -20,10 +20,12 @@ import '../consts/transaction_type.dart'; class NwcRequest { final NwcMethod method; + /// const NwcRequest({ required this.method, }); + /// factory NwcRequest.fromEvent( Nip01Event event, String contentDecryptionPrivateKey) { final connectionPubkey = event.pubKey; @@ -44,6 +46,7 @@ class NwcRequest { }); } + /// factory NwcRequest.fromMap(Map map) { final method = NwcMethod.fromPlaintext(map['method'] as String); diff --git a/packages/ndk/test/usecases/nwc/nwc_request_test.dart b/packages/ndk/test/usecases/nwc/nwc_request_test.dart new file mode 100644 index 000000000..ebf6d50fe --- /dev/null +++ b/packages/ndk/test/usecases/nwc/nwc_request_test.dart @@ -0,0 +1,77 @@ +import 'package:ndk/domain_layer/usecases/nwc/consts/nwc_method.dart'; +import 'package:ndk/domain_layer/usecases/nwc/requests/get_info.dart'; +import 'package:ndk/domain_layer/usecases/nwc/requests/make_invoice.dart'; +import 'package:ndk/domain_layer/usecases/nwc/requests/nwc_request.dart'; +import 'package:test/test.dart'; + +void main() { + group('NwcRequest', () { + + // KeyPair app = Bip340.generatePrivateKey(); + // KeyPair wallet = Bip340.generatePrivateKey(); + // + // test('fromEvent should correctly parse a valid event', () { + // GetInfoRequest getInfoRequest = GetInfoRequest(); + // var json = getInfoRequest.toMap(); + // var content = jsonEncode(json); + // + // var encrypted = Nip04.encrypt(app.privateKey!, app.publicKey, content); + // // Mock data + // final event = Nip01Event( + // pubKey: wallet.publicKey, + // content: encrypted, + // createdAt: DateTime.now().millisecondsSinceEpoch, + // kind: NwcKind.REQUEST.value, + // tags: [], + // ); + // + // final request = NwcRequest.fromEvent(event, app.privateKey!); + // + // expect(request, isA()); + // }); + + test('fromMap should return correct request type for GET_INFO', () { + final map = { + 'method': 'get_info', + }; + + final request = NwcRequest.fromMap(map); + + expect(request, isA()); + }); + + test('fromMap should return correct request type for MAKE_INVOICE', () { + final map = { + 'method': 'make_invoice', + 'amount': 1000, + 'description': 'Test invoice', + 'description_hash': null, + 'expiry': 3600, + }; + + final request = NwcRequest.fromMap(map); + + expect(request, isA()); + expect((request as MakeInvoiceRequest).amountSat, 1); + expect(request.description, 'Test invoice'); + }); + + test('fromMap should throw exception for unknown method', () { + final map = { + 'method': 'UNKNOWN_METHOD', + }; + + expect(() => NwcRequest.fromMap(map), throwsException); + }); + + test('toMap should correctly convert NwcRequest to map', () { + final request = NwcRequest(method: NwcMethod.GET_INFO); + + final map = request.toMap(); + + expect(map['method'], 'get_info'); + }); + + // Add more tests for other methods and edge cases + }); +}