Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: SshnpdChannel #587

Merged
merged 6 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mixin class AsyncInitialization {
// * Public members

/// Used to check if initialization has started
bool get initalizeStarted => _initializeStarted;
bool get initializeStarted => _initializeStarted;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed a typo

/// Used to check if initialization has completed
Future<void> get initialized => _initializedCompleter.future;
Expand Down
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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scope: Private -> Visible for Testing

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(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scope: Private -> Visible for Testing

{String? regex,
String? sharedBy,
String? sharedWith,
Expand Down
6 changes: 6 additions & 0 deletions packages/noports_core/test/sshnp/sshnp_mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ abstract class FunctionCaller<T> {

class FunctionStub<T> extends Mock implements FunctionCaller<T> {}

abstract class NotifyCaller {
Future<void> call(AtKey key, String value);
}

class NotifyStub extends Mock implements NotifyCaller {}

abstract class SubscribeCaller {
Stream<AtNotification> call({String? regex, bool shouldDecrypt});
}
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
Loading