feat(app): add Flutter websocket transport
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/transport/pipelines_client.dart';
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
|
||||
class FakeChannel implements WebSocketChannel {
|
||||
final incoming = StreamController<dynamic>();
|
||||
final outgoing = <dynamic>[];
|
||||
final readyCompleter = Completer<void>();
|
||||
@override
|
||||
Stream<dynamic> get stream => incoming.stream;
|
||||
@override
|
||||
WebSocketSink get sink => FakeSink(outgoing, incoming);
|
||||
@override
|
||||
Future<void> get ready => readyCompleter.future;
|
||||
@override
|
||||
String? get protocol => null;
|
||||
@override
|
||||
int? get closeCode => null;
|
||||
@override
|
||||
String? get closeReason => null;
|
||||
@override
|
||||
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
}
|
||||
|
||||
class FakeSink implements WebSocketSink {
|
||||
FakeSink(this.outgoing, this.incoming);
|
||||
final List<dynamic> outgoing;
|
||||
final StreamController<dynamic> incoming;
|
||||
@override
|
||||
void add(dynamic event) => outgoing.add(event);
|
||||
@override
|
||||
void addError(Object error, [StackTrace? stackTrace]) => throw error;
|
||||
@override
|
||||
Future<void> addStream(Stream<dynamic> stream) async { await for (final item in stream) { add(item); } }
|
||||
@override
|
||||
Future<void> close([int? closeCode, String? closeReason]) async { await incoming.close(); }
|
||||
@override
|
||||
Future<void> get done async {}
|
||||
@override
|
||||
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('correlates replies and broadcasts unclaimed frames', () async {
|
||||
final channel = FakeChannel();
|
||||
final client = PipelinesClient(channelFactory: (_) => channel);
|
||||
final frames = <Map<String, dynamic>>[];
|
||||
client.frames.listen(frames.add);
|
||||
final connecting = client.connect(Uri.parse('ws://example.test'));
|
||||
channel.readyCompleter.complete();
|
||||
await connecting;
|
||||
final request = client.call('ping', {'value': 1});
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
final sent = jsonDecode(channel.outgoing.single as String) as Map<String, dynamic>;
|
||||
channel.incoming.add(jsonEncode({'id': sent['id'], 'type': 'pong'}));
|
||||
expect((await request)['type'], 'pong');
|
||||
channel.incoming.add(jsonEncode({'type': 'run_event'}));
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(frames.single['type'], 'run_event');
|
||||
await client.dispose();
|
||||
});
|
||||
|
||||
test('disconnect fails pending requests', () async {
|
||||
final channel = FakeChannel();
|
||||
final client = PipelinesClient(channelFactory: (_) => channel);
|
||||
final connecting = client.connect(Uri.parse('ws://example.test'));
|
||||
channel.readyCompleter.complete();
|
||||
await connecting;
|
||||
final pending = client.call('ping');
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
await client.disconnect();
|
||||
await expectLater(pending, throwsA(isA<StateError>()));
|
||||
await client.dispose();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/transport/server_url.dart';
|
||||
|
||||
void main() {
|
||||
test('validates and normalizes endpoint with path and query', () {
|
||||
final value = ServerUrl.validate(' wss://example.test:8443/?token=x ');
|
||||
expect(value.wsUrl, 'wss://example.test:8443/connect?token=x');
|
||||
expect(value.httpUrl, 'https://example.test:8443/connect?token=x');
|
||||
expect(value.secure, isTrue);
|
||||
});
|
||||
|
||||
test('rejects unsupported schemes and credentials', () {
|
||||
expect(() => ServerUrl.validate('https://example.test'), throwsFormatException);
|
||||
expect(() => ServerUrl.validate('ws://user:pass@example.test'), throwsFormatException);
|
||||
});
|
||||
|
||||
test('resolves relative artifacts at the server origin', () {
|
||||
expect(ServerUrl.resolveArtifactUrl('ws://example.test:8081/connect', '/artifacts/a')!.toString(),
|
||||
'http://example.test:8081/artifacts/a');
|
||||
expect(ServerUrl.resolveArtifactUrl('', '/artifacts/a'), isNull);
|
||||
expect(ServerUrl.resolveArtifactUrl('', 'https://cdn.test/a')!.toString(), 'https://cdn.test/a');
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user