Skip to content

Commit

Permalink
Fixed issues with relayUrl not being used and fixed false positive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sterling Long committed Aug 8, 2023
1 parent 5ffe1ea commit f6c90f1
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 51 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.1.1

- Fixed issue with relayUrl not being used correctly
- Updated false positive tests for relayUrl and added additional ones to ensure relayUrl is used correctly

## 2.1.0

- Added IWebSocketHandler input to core for testing purposes
Expand Down
20 changes: 10 additions & 10 deletions lib/apis/core/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Core implements ICore {
fromJson: (dynamic value) => value as String,
),
);

_relayUrl = relayUrl;
relayClient = RelayClient(
core: this,
messageTracker: MessageTracker(
Expand Down Expand Up @@ -153,20 +153,20 @@ class Core implements ICore {
await storage.init();
await crypto.init();

// If the relay URL is the default, try both it and the backup (.org)
if (relayUrl == WalletConnectConstants.DEFAULT_RELAY_URL) {
_relayUrl = relayUrl;
try {
await relayClient.init();
} catch (e) {
try {
await relayClient.init();
} catch (e) {
// If the relay URL is the default, try both it and the backup (.org)
if (_relayUrl == WalletConnectConstants.DEFAULT_RELAY_URL) {
_relayUrl = WalletConnectConstants.FALLBACK_RELAY_URL;
await relayClient.init();
} else {
// Otherwise, just rethrow the error
rethrow;
}
} else {
await relayClient.init();
}

await expirer.init();
// await history.init();
await pairing.init();
}
}
2 changes: 1 addition & 1 deletion lib/apis/utils/constants.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class WalletConnectConstants {
static const SDK_VERSION = '2.1.0';
static const SDK_VERSION = '2.1.1';

static const CORE_PROTOCOL = 'wc';
static const CORE_VERSION = 2;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: walletconnect_flutter_v2
description: WalletConnect v2 client made in dart for flutter.
version: 2.1.0
version: 2.1.1
repository: https://github.com/WalletConnect/WalletConnectFlutterV2

environment:
Expand Down
91 changes: 91 additions & 0 deletions test/core_api/core_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:walletconnect_flutter_v2/apis/core/relay_client/relay_client.dart';
import 'package:walletconnect_flutter_v2/walletconnect_flutter_v2.dart';

import '../shared/shared_test_utils.dart';
import '../shared/shared_test_utils.mocks.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group('Core throws errors', () {
test('on start if there is no internet connection', () async {
final MockWebSocketHandler mockWebSocketHandler = MockWebSocketHandler();
when(mockWebSocketHandler.connect()).thenThrow(const WalletConnectError(
code: -1,
message: 'No internet connection: test',
));

ICore core = Core(
projectId: 'abc',
memoryStore: true,
);
core.relayClient = RelayClient(
core: core,
messageTracker: getMessageTracker(core: core),
topicMap: getTopicMap(core: core),
socketHandler: mockWebSocketHandler,
);

try {
await core.start();
expect(true, false);
} on WalletConnectError catch (e) {
expect(e.message, 'No internet connection: test');
}

verifyInOrder([
mockWebSocketHandler.setup(
url: argThat(
contains(
WalletConnectConstants.DEFAULT_RELAY_URL,
),
named: 'url',
),
),
mockWebSocketHandler.connect(),
mockWebSocketHandler.setup(
url: argThat(
contains(
WalletConnectConstants.FALLBACK_RELAY_URL,
),
named: 'url',
),
),
mockWebSocketHandler.connect(),
]);

const testRelayUrl = 'wss://relay.test.com';
core = Core(
projectId: 'abc',
memoryStore: true,
relayUrl: testRelayUrl,
);
core.relayClient = RelayClient(
core: core,
messageTracker: getMessageTracker(core: core),
topicMap: getTopicMap(core: core),
socketHandler: mockWebSocketHandler,
);

try {
await core.start();
expect(true, false);
} on WalletConnectError catch (e) {
expect(e.message, 'No internet connection: test');
}

// Check that setup was called once for custom URL
verify(
mockWebSocketHandler.setup(
url: argThat(
contains(testRelayUrl),
named: 'url',
),
),
).called(1);
verify(mockWebSocketHandler.connect()).called(1);
});
});
}
80 changes: 41 additions & 39 deletions test/core_api/relay_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,63 +30,65 @@ void main() {
group('Relay throws errors', () {
test('on init if there is no internet connection', () async {
final MockWebSocketHandler mockWebSocketHandler = MockWebSocketHandler();
when(mockWebSocketHandler.setup(url: anyNamed('url'))).thenAnswer(
(_) async => Future.value(),
);
// when(mockWebSocketHandler.setup(url: anyNamed('url'))).thenAnswer(
// (_) async => Future.value(),
// );
when(mockWebSocketHandler.connect()).thenThrow(const WalletConnectError(
code: -1,
message: 'No internet connection: test',
));

const testRelayUrl = 'wss://relay.test.com';
ICore core = Core(
projectId: 'abc',
memoryStore: true,
relayUrl: testRelayUrl,
);
core.relayClient = RelayClient(
core: core,
messageTracker: getMessageTracker(core: core),
topicMap: getTopicMap(core: core),
socketHandler: mockWebSocketHandler,
);

expect(
() async => await core.start(),
throwsA(
isA<WalletConnectError>().having(
(e) => e.message,
'No internet connection',
'No internet connection: test',
),
await core.storage.init();
await core.crypto.init();
try {
await core.relayClient.init();
expect(true, false);
} on WalletConnectError catch (e) {
expect(e.message, 'No internet connection: test');
}

verify(mockWebSocketHandler.setup(
url: argThat(
contains(testRelayUrl),
named: 'url',
),
);

// Check that setup was called twice (It attempts the fallback .org URL if the default URL is provided)
// TODO: Figure out why the mocked class isn't counting the number of times it's called
// verify(mockWebSocketHandler.setup(url: anyNamed('url'))).called(2);
// verify(mockWebSocketHandler.connect()).called(2);
)).called(1);
verify(mockWebSocketHandler.connect()).called(1);

core = Core(
projectId: 'abc',
memoryStore: true,
relayUrl: 'wss://relay.test.com',
);
core.relayClient = RelayClient(
core: core,
messageTracker: getMessageTracker(core: core),
topicMap: getTopicMap(core: core),
socketHandler: mockWebSocketHandler,
);
// core = Core(
// projectId: 'abc',
// memoryStore: true,
// relayUrl: 'wss://relay.test.com',
// );
// core.relayClient = RelayClient(
// core: core,
// messageTracker: getMessageTracker(core: core),
// topicMap: getTopicMap(core: core),
// socketHandler: mockWebSocketHandler,
// );

expect(
() async => await core.start(),
throwsA(
isA<WalletConnectError>().having(
(e) => e.message,
'No internet connection',
'No internet connection: test',
),
),
);
// expect(
// () async => await core.start(),
// throwsA(
// isA<WalletConnectError>().having(
// (e) => e.message,
// 'No internet connection',
// 'No internet connection: test',
// ),
// ),
// );

// // Check that setup was called once for custom URL
// TODO: Figure out why the mocked class isn't counting the number of times it's called
Expand Down

0 comments on commit f6c90f1

Please sign in to comment.