Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed May 23, 2024
1 parent d2ea408 commit 7e47f7e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
5 changes: 5 additions & 0 deletions lib/src/model/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:meta/meta.dart';

import 'pubspec.yaml.g.dart';
import 'transport_interface.dart';

/// Token used for authentication
///
Expand Down Expand Up @@ -170,6 +171,7 @@ final class SpinifyConfig {
this.serverPingDelay = const Duration(seconds: 8),
Map<String, String>? headers,
this.logger,
this.transportBuilder,
}) : headers = Map<String, String>.unmodifiable(
headers ?? const <String, String>{}),
client = client ??
Expand Down Expand Up @@ -244,6 +246,9 @@ final class SpinifyConfig {
/// - [context] - the log context data
final SpinifyLogger? logger;

/// Callback to build Spinify transport.
final SpinifyTransportBuilder? transportBuilder;

@override
String toString() => 'SpinifyConfig{}';
}
4 changes: 2 additions & 2 deletions lib/src/model/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,10 @@ final class SpinifyState$Closed extends SpinifyState {
bool get isConnecting => false;

@override
bool get isConnected => true;
bool get isConnected => false;

@override
bool get isClosed => false;
bool get isClosed => true;

@override
R map<R>({
Expand Down
6 changes: 3 additions & 3 deletions lib/src/spinify_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ abstract base class SpinifyBase implements ISpinify {
/// Client initialization (from constructor).
@mustCallSuper
void _init() {
_createTransport = $create$WS$PB$Transport;
_createTransport = config.transportBuilder ?? $create$WS$PB$Transport;
config.logger?.call(
const SpinifyLogLevel.info(),
'init',
Expand Down Expand Up @@ -122,7 +122,7 @@ base mixin SpinifyStateMixin on SpinifyBase {

@nonVirtual
void _setState(SpinifyState state) {
final previous = metrics.state;
final previous = _metrics.state;
_statesController.add(_metrics.state = state);
config.logger?.call(
const SpinifyLogLevel.config(),
Expand Down Expand Up @@ -248,7 +248,7 @@ base mixin SpinifyCommandMixin on SpinifyBase {
@override
Future<void> _onReply(SpinifyReply reply) async {
assert(
reply.id >= 0 && reply.id <= metrics.commandId,
reply.id >= 0 && reply.id <= _metrics.commandId,
'Reply ID should be greater or equal to 0 '
'and less or equal than command ID');
if (reply.id case int id when id > 0) {
Expand Down
24 changes: 17 additions & 7 deletions test/unit/spinify_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ import 'package:test/test.dart';

void main() {
group('Spinify', () {
Spinify createFakeClient() =>
Spinify(/* createTransport: $createFakeSpinifyTransport */);
Spinify createFakeClient() => Spinify(
config: SpinifyConfig(
transportBuilder: $createFakeSpinifyTransport,
),
);

test('Create_and_close_client', () async {
final client = createFakeClient();
expect(client.isClosed, isFalse);
expect(client.state, isA<SpinifyState$Disconnected>());
await client.close();
expect(client.isClosed, isTrue);
expect(client.state, isA<SpinifyState$Closed>());
expect(client.isClosed, isTrue);
});

test('Create_and_close_multiple_clients', () async {
Expand All @@ -34,14 +37,21 @@ void main() {
});

test('Change_client_states', () {
final client = createFakeClient()
..connect('ws://localhost:8000/connection/websocket')
..disconnect()
..close();
final client = createFakeClient();
Stream.fromIterable([
() => client.connect('ws://localhost:8000/connection/websocket'),
client.disconnect,
() => client.connect('ws://localhost:8000/connection/websocket'),
client.disconnect,
client.close,
]).asyncMap(Future.new).drain<void>();
expect(client.state, isA<SpinifyState$Disconnected>());
expectLater(
client.states,
emitsInOrder([
isA<SpinifyState$Connecting>(),
isA<SpinifyState$Connected>(),
isA<SpinifyState$Disconnected>(),
isA<SpinifyState$Connecting>(),
isA<SpinifyState$Connected>(),
isA<SpinifyState$Disconnected>(),
Expand Down

0 comments on commit 7e47f7e

Please sign in to comment.