Skip to content

Commit

Permalink
increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
frnandu committed Dec 26, 2024
1 parent d2df8cd commit 3456832
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// ignore_for_file: constant_identifier_names

/// Nwc Kinds
enum NwcKind {
INFO(13194),
REQUEST(23194),
RESPONSE(23195),
LEGACY_NOTIFICATION(23196),
NOTIFICATION(23197);

/// int value
final int value;

const NwcKind(this.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,6 +46,7 @@ class NwcRequest {
});
}

///
factory NwcRequest.fromMap(Map<String, dynamic> map) {
final method = NwcMethod.fromPlaintext(map['method'] as String);

Expand Down
77 changes: 77 additions & 0 deletions packages/ndk/test/usecases/nwc/nwc_request_test.dart
Original file line number Diff line number Diff line change
@@ -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<GetInfoRequest>());
// });

test('fromMap should return correct request type for GET_INFO', () {
final map = {
'method': 'get_info',
};

final request = NwcRequest.fromMap(map);

expect(request, isA<GetInfoRequest>());
});

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<MakeInvoiceRequest>());
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
});
}

0 comments on commit 3456832

Please sign in to comment.