Skip to content

Commit

Permalink
test: SshnpdChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierChanth committed Nov 23, 2023
1 parent 8934c40 commit 7934bf2
Show file tree
Hide file tree
Showing 3 changed files with 491 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ abstract class SshnpdChannel with AsyncInitialization, AtClientBindings {
subscribe(
regex: regex,
shouldDecrypt: true,
).listen(_handleSshnpdResponses);
).listen(handleSshnpdResponses);
}

/// Main reponse handler for the daemon's notifications.
Future<void> _handleSshnpdResponses(AtNotification notification) async {
@visibleForTesting
Future<void> handleSshnpdResponses(AtNotification notification) async {
String notificationKey = notification.key
.replaceAll('${notification.to}:', '')
.replaceAll('.$namespace@${notification.from}', '')
Expand Down Expand Up @@ -183,7 +184,7 @@ abstract class SshnpdChannel with AsyncInitialization, AtClientBindings {
'device_info\\.$sshnpDeviceNameRegex\\.${DefaultArgs.namespace}';

var atKeys =
await _getAtKeysRemote(regex: scanRegex, sharedBy: params.sshnpdAtSign);
await getAtKeysRemote(regex: scanRegex, sharedBy: params.sshnpdAtSign);

SshnpDeviceList deviceList = SshnpDeviceList();

Expand Down Expand Up @@ -234,7 +235,8 @@ abstract class SshnpdChannel with AsyncInitialization, AtClientBindings {
}

/// A custom implementation of AtClient.getAtKeys which bypasses the cache
Future<List<AtKey>> _getAtKeysRemote(
@visibleForTesting
Future<List<AtKey>> getAtKeysRemote(
{String? regex,
String? sharedBy,
String? sharedWith,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:at_client/at_client.dart';
import 'package:mocktail/mocktail.dart';
import 'package:noports_core/sshnp_foundation.dart';

abstract class HandleSshnpdPayloadCaller {
Future<SshnpdAck> call(AtNotification notification);
}

class HandleSshnpdPayloadStub extends Mock
implements HandleSshnpdPayloadCaller {}

class StubbedSshnpdChannel extends SshnpdChannel {
final Future<void> Function(AtKey, String)? _notify;
final Stream<AtNotification> Function({String? regex, bool shouldDecrypt})?
_subscribe;
final Future<SshnpdAck> Function(AtNotification notification)?
_handleSshnpdPayload;

StubbedSshnpdChannel({
required super.atClient,
required super.params,
required super.sessionId,
required super.namespace,
Future<void> Function(AtKey, String)? notify,
Stream<AtNotification> Function({String? regex, bool shouldDecrypt})?
subscribe,
Future<SshnpdAck> Function(AtNotification notification)?
handleSshnpdPayload,
}) : _notify = notify,
_subscribe = subscribe,
_handleSshnpdPayload = handleSshnpdPayload;

@override
Future<SshnpdAck> handleSshnpdPayload(AtNotification notification) async {
return await _handleSshnpdPayload?.call(notification) ??
SshnpdAck.notAcknowledged;
}

@override
Future<void> notify(
AtKey atKey,
String value,
) async {
return _notify?.call(atKey, value);
}

@override
Stream<AtNotification> subscribe({
String? regex,
bool shouldDecrypt = false,
}) {
return _subscribe?.call(regex: regex, shouldDecrypt: shouldDecrypt) ??
Stream.empty();
}
}

class MockRemoteSecondary extends Mock implements RemoteSecondary {}
Loading

0 comments on commit 7934bf2

Please sign in to comment.