c316bc9bc8
A chat subscriber whose queue fills is detached and its writer ends the stream with a resync frame (the app re-attaches and re-reads history); a run subscriber gets a final runs_resync and the app subscribes again and re-reads the Runs list and open detail. Consecutive text/reasoning deltas of one part are joined in a chat subscriber's queue; joined numbered frames carry seq_from and delta_lens so the app trims exactly the prefix a history page already holds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
206 lines
6.7 KiB
Dart
206 lines
6.7 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/application/app_session.dart';
|
|
import 'package:pipelines_app/core/transport/pipelines_client.dart';
|
|
|
|
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
|
|
|
|
Future<void> flush() => Future<void>.delayed(Duration.zero);
|
|
|
|
Future<void> waitFor(bool Function() condition, String reason) async {
|
|
for (var attempt = 0; attempt < 200; attempt++) {
|
|
if (condition()) return;
|
|
await Future<void>.delayed(const Duration(milliseconds: 5));
|
|
}
|
|
fail('Timed out: $reason');
|
|
}
|
|
|
|
List<String> types(FakeChannel channel) => [
|
|
for (final frame in channel.outgoing)
|
|
(jsonDecode(frame as String) as Map<String, dynamic>)['type'] as String,
|
|
];
|
|
|
|
void main() {
|
|
test('Retry preserves an endpoint changed through shared Settings', () async {
|
|
final endpoints = <Uri>[];
|
|
final client = PipelinesClient(
|
|
channelFactory: (uri) {
|
|
endpoints.add(uri);
|
|
return FakeChannel()..readyCompleter.complete();
|
|
},
|
|
);
|
|
final session = AppSession(
|
|
endpoint: Uri.parse('ws://original.test/connect'),
|
|
client: client,
|
|
);
|
|
addTearDown(session.dispose);
|
|
await session.start();
|
|
final changed = Uri.parse('ws://selected.test/connect');
|
|
await session.client.connect(changed);
|
|
await session.stop();
|
|
await session.retry();
|
|
expect(endpoints, [
|
|
Uri.parse('ws://original.test/connect'),
|
|
changed,
|
|
changed,
|
|
]);
|
|
expect(session.endpoint, changed);
|
|
});
|
|
|
|
test(
|
|
'start subscribes once, stop unsubscribes, and the app owns the socket',
|
|
() async {
|
|
final channel = FakeChannel()..readyCompleter.complete();
|
|
final client = PipelinesClient(channelFactory: (_) => channel);
|
|
final session = AppSession(
|
|
endpoint: Uri.parse('ws://example.test/connect'),
|
|
client: client,
|
|
);
|
|
|
|
await session.start();
|
|
await flush();
|
|
expect(session.connected, isTrue);
|
|
expect(types(channel), ['subscribe']);
|
|
final frame = jsonDecode(channel.outgoing.single as String) as Map;
|
|
expect(frame['workspace'], '');
|
|
|
|
await session.stop();
|
|
await flush();
|
|
expect(types(channel), ['subscribe', 'unsubscribe']);
|
|
expect(session.connected, isFalse);
|
|
session.dispose();
|
|
},
|
|
);
|
|
|
|
test('reconnect subscribes the new socket again', () async {
|
|
final first = FakeChannel()..readyCompleter.complete();
|
|
final second = FakeChannel()..readyCompleter.complete();
|
|
var sockets = 0;
|
|
final client = PipelinesClient(
|
|
channelFactory: (_) => sockets++ == 0 ? first : second,
|
|
backoffBase: const Duration(milliseconds: 5),
|
|
);
|
|
final session = AppSession(
|
|
endpoint: Uri.parse('ws://example.test/connect'),
|
|
client: client,
|
|
);
|
|
addTearDown(session.dispose);
|
|
|
|
await session.start();
|
|
await flush();
|
|
expect(types(first), ['subscribe']);
|
|
expect(session.connectionEpoch, 1);
|
|
|
|
first.incoming.addError(StateError('network drop'));
|
|
await waitFor(
|
|
() => sockets == 2 && session.connected && second.outgoing.isNotEmpty,
|
|
'reconnected socket did not subscribe',
|
|
);
|
|
expect(types(second), ['subscribe']);
|
|
expect(session.connectionEpoch, 2);
|
|
});
|
|
|
|
test('a subscribe ack marks the watch live, a refusal reports it', () async {
|
|
final channel = FakeChannel()..readyCompleter.complete();
|
|
final client = PipelinesClient(channelFactory: (_) => channel);
|
|
final session = AppSession(
|
|
endpoint: Uri.parse('ws://example.test/connect'),
|
|
client: client,
|
|
);
|
|
addTearDown(session.dispose);
|
|
|
|
await session.start();
|
|
await flush();
|
|
final subscribe =
|
|
jsonDecode(channel.outgoing.single as String) as Map<String, dynamic>;
|
|
expect(session.subscription, RunSubscriptionState.pending);
|
|
|
|
channel.incoming.add(
|
|
jsonEncode({'id': subscribe['id'], 'type': 'subscribe'}),
|
|
);
|
|
await flush();
|
|
expect(session.subscription, RunSubscriptionState.live);
|
|
expect(session.subscriptionError, isEmpty);
|
|
|
|
session.resubscribe();
|
|
await flush();
|
|
final repeated =
|
|
jsonDecode(channel.outgoing.last as String) as Map<String, dynamic>;
|
|
expect(repeated['type'], 'subscribe');
|
|
channel.incoming.add(
|
|
jsonEncode({
|
|
'id': repeated['id'],
|
|
'type': 'error',
|
|
'error': 'too many run subscriptions on this connection',
|
|
}),
|
|
);
|
|
await flush();
|
|
expect(session.subscription, RunSubscriptionState.refused);
|
|
expect(session.subscriptionError, contains('too many run subscriptions'));
|
|
});
|
|
|
|
test('runs_resync watches again and advances the runs epoch', () async {
|
|
// The server detached this connection's run watch because it fell behind
|
|
// and discarded the frames it held; without a new subscribe the Runs
|
|
// list and detail froze until the next reconnect.
|
|
final channel = FakeChannel()..readyCompleter.complete();
|
|
final client = PipelinesClient(channelFactory: (_) => channel);
|
|
final session = AppSession(
|
|
endpoint: Uri.parse('ws://example.test/connect'),
|
|
client: client,
|
|
);
|
|
addTearDown(session.dispose);
|
|
|
|
await session.start();
|
|
await flush();
|
|
final subscribe =
|
|
jsonDecode(channel.outgoing.single as String) as Map<String, dynamic>;
|
|
channel.incoming.add(
|
|
jsonEncode({'id': subscribe['id'], 'type': 'subscribe'}),
|
|
);
|
|
await flush();
|
|
final epoch = session.runsEpoch;
|
|
final connection = session.connectionEpoch;
|
|
|
|
channel.incoming.add(jsonEncode({'type': 'runs_resync'}));
|
|
await flush();
|
|
expect(types(channel), ['subscribe', 'subscribe']);
|
|
expect(session.runsEpoch, greaterThan(epoch));
|
|
expect(session.connectionEpoch, connection);
|
|
expect(session.subscription, RunSubscriptionState.pending);
|
|
});
|
|
|
|
test('dispose drops the watch and releases the client', () async {
|
|
final channel = FakeChannel()..readyCompleter.complete();
|
|
final client = PipelinesClient(channelFactory: (_) => channel);
|
|
final session = AppSession(
|
|
endpoint: Uri.parse('ws://example.test/connect'),
|
|
client: client,
|
|
);
|
|
|
|
await session.start();
|
|
await flush();
|
|
session.dispose();
|
|
await flush();
|
|
|
|
expect(types(channel), ['subscribe', 'unsubscribe']);
|
|
await expectLater(
|
|
client.connect(Uri.parse('ws://example.test/connect')),
|
|
throwsStateError,
|
|
);
|
|
});
|
|
|
|
test('a missing endpoint is not replaced by a guessed connection', () async {
|
|
final channel = FakeChannel();
|
|
final client = PipelinesClient(channelFactory: (_) => channel);
|
|
final session = AppSession(endpoint: null, client: client);
|
|
addTearDown(session.dispose);
|
|
|
|
await session.start();
|
|
await flush();
|
|
expect(channel.outgoing, isEmpty);
|
|
expect(session.connected, isFalse);
|
|
});
|
|
}
|