Files
pipelines/app/test/features/settings/fake_channel.dart
T
alan abd696b0d9 feat(app): add native Settings connection and workspace screen
Add the Settings destination under lib/features/settings with an editable
websocket endpoint, truthful connection states, one ping per connect/mount,
workspace validation and persistence under new feature-owned keys. Material
and Cupertino controls, wrapping copy at large text, pending ping state, and
isolated probe tools plus widget/unit coverage.
2026-10-21 17:39:18 +01:00

116 lines
2.8 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:web_socket_channel/web_socket_channel.dart';
/// A deterministic websocket channel for the Settings tests. It records every
/// outgoing request and lets the test complete readiness and deliver replies.
class FakeChannel implements WebSocketChannel {
FakeChannel(this.uri);
final Uri uri;
final StreamController<dynamic> incoming = StreamController<dynamic>();
final List<String> outgoing = <String>[];
final Completer<void> readyCompleter = Completer<void>();
bool _closed = false;
void markReady() {
if (!readyCompleter.isCompleted) readyCompleter.complete();
}
void reply(Map<String, dynamic> message) {
if (!incoming.isClosed) incoming.add(jsonEncode(message));
}
void fail(String message) {
if (!incoming.isClosed) incoming.addError(StateError(message));
}
void closeStream() {
if (!incoming.isClosed) {
_closed = true;
incoming.close();
}
}
List<Map<String, dynamic>> get decodedOutgoing => outgoing
.map((item) => jsonDecode(item) as Map<String, dynamic>)
.toList(growable: false);
@override
Stream<dynamic> get stream => incoming.stream;
@override
WebSocketSink get sink => _FakeSink(this);
@override
Future<void> get ready => readyCompleter.future;
@override
String? get protocol => null;
@override
int? get closeCode => _closed ? 1000 : null;
@override
String? get closeReason => null;
@override
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
class _FakeSink implements WebSocketSink {
_FakeSink(this.channel);
final FakeChannel channel;
@override
void add(dynamic event) {
if (event is String) channel.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 {
channel.closeStream();
}
@override
Future<void> get done async {}
@override
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
/// Records every channel the client asks for, so tests can prove that a rebuild
/// or an invalid URL opened no socket at all.
class FakeChannelFactory {
final List<FakeChannel> channels = <FakeChannel>[];
final List<Uri> uris = <Uri>[];
FakeChannel get last => channels.last;
WebSocketChannel call(Uri uri) {
uris.add(uri);
final channel = FakeChannel(uri);
channels.add(channel);
return channel;
}
}
/// Let queued microtasks (state events, request sends, replies) run.
Future<void> settle() async {
for (var i = 0; i < 4; i++) {
await Future<void>.delayed(Duration.zero);
}
}