Merge branch 'main' into flutter/f4-chat

This commit is contained in:
2026-10-21 13:37:09 +01:00
8 changed files with 685 additions and 5 deletions
@@ -0,0 +1,144 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import 'pipelines_client_test.dart' show FakeChannel;
Future<void> flush() => Future<void>.delayed(Duration.zero);
Map<String, dynamic> sent(FakeChannel channel, [int index = 0]) =>
jsonDecode(channel.outgoing[index] as String) as Map<String, dynamic>;
void reply(FakeChannel channel, {int index = 0, String type = 'pong'}) =>
channel.incoming.add(
jsonEncode({'id': sent(channel, index)['id'], 'type': type}),
);
void main() {
test(
'requests serialize through replies, errors do not poison queue',
() async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
await client.connect(Uri.parse('ws://example.test'));
final first = client.call('ping');
final firstError = expectLater(first, throwsA(isA<ProtocolError>()));
final second = client.call('list_runs');
await flush();
expect(channel.outgoing, hasLength(1));
channel.incoming.add(
jsonEncode({
'id': sent(channel)['id'],
'type': 'error',
'error': 'refused',
}),
);
await firstError;
await flush();
expect(channel.outgoing, hasLength(2));
reply(channel, index: 1, type: 'runs');
expect((await second)['type'], 'runs');
await client.dispose();
},
);
test(
'timeout releases queue and late response is an unsolicited frame',
() async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(
channelFactory: (_) => channel,
requestTimeout: const Duration(milliseconds: 10),
);
await client.connect(Uri.parse('ws://example.test'));
await expectLater(client.call('ping'), throwsA(isA<TimeoutException>()));
final late = client.frames.first;
reply(channel);
expect((await late)['type'], 'pong');
await client.dispose();
},
);
test(
'replacement fails queued calls rather than sending on new socket',
() async {
final channels = [
FakeChannel()..readyCompleter.complete(),
FakeChannel()..readyCompleter.complete(),
];
var index = 0;
final client = PipelinesClient(channelFactory: (_) => channels[index++]);
await client.connect(Uri.parse('ws://old.test'));
final firstError = expectLater(client.call('ping'), throwsStateError);
final queuedError = expectLater(
client.call('list_runs'),
throwsStateError,
);
await flush();
await client.connect(Uri.parse('ws://new.test'));
await Future.wait([firstError, queuedError]);
expect(channels.last.outgoing, isEmpty);
expect(client.state.kind, ConnectionKind.connected);
await client.dispose();
},
);
test('sendOnly never blocks correlated requests', () async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
await client.connect(Uri.parse('ws://example.test'));
client.sendOnly('subscribe', {'workspace': ''});
final ping = client.call('ping');
await flush();
expect(sent(channel)['type'], 'subscribe');
reply(channel, index: 1);
expect((await ping)['type'], 'pong');
await client.dispose();
});
test('overlapping connect cannot create a superseded socket', () async {
var factories = 0;
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(
channelFactory: (_) {
factories++;
return channel;
},
);
await Future.wait([
client.connect(Uri.parse('ws://old.test')),
client.connect(Uri.parse('ws://new.test')),
]);
expect(factories, 1);
expect(client.state.kind, ConnectionKind.connected);
await client.dispose();
await expectLater(
client.connect(Uri.parse('ws://new.test')),
throwsStateError,
);
});
test('socket failure reconnects, disconnect cancels retry', () async {
final first = FakeChannel()..readyCompleter.complete();
final next = FakeChannel()..readyCompleter.complete();
var factories = 0;
final client = PipelinesClient(
channelFactory: (_) => factories++ == 0 ? first : next,
backoffBase: const Duration(milliseconds: 5),
);
await client.connect(Uri.parse('ws://example.test'));
final connectedAgain = client.states.firstWhere(
(s) => s.kind == ConnectionKind.connected,
);
first.incoming.addError(StateError('network drop'));
await connectedAgain.timeout(const Duration(seconds: 2));
expect(factories, 2);
await client.disconnect();
expect(client.state.retrying, isFalse);
expect(
() => client.sendOnly('subscribe'),
throwsA(isA<NotConnectedError>()),
);
await client.dispose();
});
}
@@ -0,0 +1,87 @@
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,51 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/transport/server_url.dart';
void main() {
test('IPv6 authorities and encoded artifact queries stay intact', () {
expect(
ServerUrl.validate('ws://[::1]:8081').wsUrl,
'ws://[::1]:8081/connect',
);
expect(
ServerUrl.resolveArtifactUrl(
'ws://[::1]:8081/connect',
'/artifacts/a%20b?download=1',
).toString(),
'http://[::1]:8081/artifacts/a%20b?download=1',
);
expect(() => ServerUrl.validate('ws://host:70000'), throwsFormatException);
});
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',
);
});
}