45f5323c75
Treat server-synced open workspace snapshots as pane membership only so they do not show or retain a phone chat page. Add regression coverage for repeated system, button, and predictive Back cycles with tiled and floating entries.
343 lines
12 KiB
Dart
343 lines
12 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/application/pipelines_app.dart';
|
|
import 'package:pipelines_app/core/transport/pipelines_client.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
|
|
import 'chat_registration_test.dart' show webServer;
|
|
|
|
/// Answers the requests the app sends with plausible replies, holding the
|
|
/// server's open-chat set in [_entries] like the real `chat_open_workspace`.
|
|
class _Server {
|
|
_Server(this.channel, {required List<Object?> entries})
|
|
: _entries = entries,
|
|
_revision = entries.isEmpty ? 0 : 5;
|
|
|
|
final FakeChannel channel;
|
|
final _answered = <Object?>{};
|
|
final types = <String>{};
|
|
int _revision;
|
|
List<Object?> _entries;
|
|
|
|
/// Another device (or the server) changed the open-chat set: pushes it.
|
|
void change(List<Object?> entries) {
|
|
_entries = entries;
|
|
_revision++;
|
|
channel.incoming.add(
|
|
jsonEncode({
|
|
'type': 'chat_open_workspace_changed',
|
|
'chat_open_workspace': {'revision': _revision, 'entries': _entries},
|
|
}),
|
|
);
|
|
}
|
|
|
|
void answer() {
|
|
for (final raw in channel.outgoing.toList()) {
|
|
final m = jsonDecode(raw as String) as Map<String, dynamic>;
|
|
final id = m['id'];
|
|
if (id == null || !_answered.add(id)) continue;
|
|
final type = '${m['type']}';
|
|
types.add(type);
|
|
channel.incoming.add(jsonEncode(_reply(type, id, m)));
|
|
}
|
|
}
|
|
|
|
Map<String, Object?> _reply(String type, Object? id, Map<String, dynamic> m) {
|
|
switch (type) {
|
|
case 'chat_sessions':
|
|
return {
|
|
'id': id,
|
|
'type': type,
|
|
'chat_sessions': [
|
|
for (final s in const ['a', 'b', 'c'])
|
|
{
|
|
'id': s,
|
|
'title': 'Chat $s',
|
|
'directory': '/workspace/demo',
|
|
'updated_at': '2026-11-12T10:00:00Z',
|
|
},
|
|
],
|
|
};
|
|
case 'chat_shells':
|
|
return {'id': id, 'type': type, 'chat_shells': const []};
|
|
case 'chat_shell_runs':
|
|
return {'id': id, 'type': type, 'chat_shell_runs': const []};
|
|
case 'chat_automations':
|
|
return {
|
|
'id': id,
|
|
'type': type,
|
|
'chat_automations': {
|
|
'session_id': m['chat_query']?['session_id'],
|
|
'provider': 'fixture',
|
|
'status': 'unavailable',
|
|
},
|
|
};
|
|
case 'chat_messages':
|
|
return {
|
|
'id': id,
|
|
'type': 'chat_history',
|
|
'chat_history': {
|
|
'messages': [
|
|
for (var i = 0; i < 4; i++)
|
|
{
|
|
'id': 'm$i',
|
|
'role': i.isEven ? 'user' : 'assistant',
|
|
'parts': [
|
|
{'type': 'text', 'text': 'Message $i'},
|
|
],
|
|
},
|
|
],
|
|
'next_cursor': '',
|
|
'has_more': false,
|
|
},
|
|
};
|
|
case 'chat_open_workspace':
|
|
final write = m['chat_open_workspace_request'] as Map?;
|
|
if (write != null) {
|
|
_revision++;
|
|
_entries = write['entries'] as List<Object?>;
|
|
}
|
|
return {
|
|
'id': id,
|
|
'type': type,
|
|
'chat_open_workspace': {'revision': _revision, 'entries': _entries},
|
|
};
|
|
default:
|
|
return {'id': id, 'type': type};
|
|
}
|
|
}
|
|
}
|
|
|
|
enum _BackVia { system, button, predictive }
|
|
|
|
/// Pumps [d] in 16 ms steps while answering the app's requests.
|
|
Future<void> _run(WidgetTester tester, _Server server, Duration d) async {
|
|
for (var t = 0; t < d.inMilliseconds; t += 16) {
|
|
server.answer();
|
|
await tester.pump(const Duration(milliseconds: 16));
|
|
}
|
|
server.answer();
|
|
}
|
|
|
|
Future<void> _gesture(WidgetTester tester, String method, [double? progress]) =>
|
|
tester.binding.defaultBinaryMessenger.handlePlatformMessage(
|
|
'flutter/backgesture',
|
|
const StandardMethodCodec().encodeMethodCall(
|
|
MethodCall(
|
|
method,
|
|
progress == null
|
|
? null
|
|
: {
|
|
'touchOffset': <double>[5 + 300 * progress, 400],
|
|
'progress': progress,
|
|
'swipeEdge': 0,
|
|
},
|
|
),
|
|
),
|
|
(_) {},
|
|
);
|
|
|
|
/// Presses Back from the open chat through [via] and waits for the list.
|
|
Future<void> _back(WidgetTester tester, _Server server, _BackVia via) async {
|
|
switch (via) {
|
|
case _BackVia.system:
|
|
await tester.binding.handlePopRoute();
|
|
case _BackVia.button:
|
|
expect(find.byType(BackButton), findsWidgets);
|
|
await tester.tap(find.byType(BackButton).last);
|
|
case _BackVia.predictive:
|
|
await _gesture(tester, 'startBackGesture', 0);
|
|
for (var p = 1; p <= 6; p++) {
|
|
await _gesture(tester, 'updateBackGestureProgress', p / 10);
|
|
await tester.pump(const Duration(milliseconds: 16));
|
|
}
|
|
await _gesture(tester, 'commitBackGesture');
|
|
}
|
|
await _run(tester, server, const Duration(milliseconds: 900));
|
|
}
|
|
|
|
void main() {
|
|
// The server holds two tiled chats, or one tiled and one floating chat.
|
|
// Phone Back from an open chat must return to the list on every press.
|
|
final layouts = <String, List<Object?>>{
|
|
'two tiled': [
|
|
{'session_id': 'a'},
|
|
{'session_id': 'b'},
|
|
],
|
|
'one tiled and one floating': [
|
|
{'session_id': 'a'},
|
|
{'session_id': 'c', 'floating': true},
|
|
],
|
|
};
|
|
for (final layout in layouts.entries) {
|
|
for (final via in _BackVia.values) {
|
|
testWidgets('phone Back from a chat returns to the list 20 times '
|
|
'via ${via.name}, server holding ${layout.key}', (tester) async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
tester.view.physicalSize = const Size(412, 915);
|
|
tester.view.devicePixelRatio = 1;
|
|
addTearDown(tester.view.reset);
|
|
|
|
bool? handlesBack;
|
|
final exits = <String>[];
|
|
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
|
SystemChannels.platform,
|
|
(call) async {
|
|
if (call.method == 'SystemNavigator.setFrameworkHandlesBack') {
|
|
handlesBack = call.arguments as bool;
|
|
}
|
|
if (call.method == 'SystemNavigator.pop') exits.add(call.method);
|
|
return null;
|
|
},
|
|
);
|
|
addTearDown(
|
|
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
|
SystemChannels.platform,
|
|
null,
|
|
),
|
|
);
|
|
|
|
final channel = FakeChannel()..readyCompleter.complete();
|
|
final client = PipelinesClient(channelFactory: (_) => channel);
|
|
final server = _Server(channel, entries: layout.value);
|
|
final provider = PlatformRouteInformationProvider(
|
|
initialRouteInformation: RouteInformation(uri: Uri.parse('/chat')),
|
|
);
|
|
addTearDown(provider.dispose);
|
|
await tester.pumpWidget(
|
|
PipelinesApp(
|
|
androidSupported: false,
|
|
server: webServer(),
|
|
client: client,
|
|
routeInformationProvider: provider,
|
|
),
|
|
);
|
|
tester.binding.handleAppLifecycleStateChanged(
|
|
AppLifecycleState.resumed,
|
|
);
|
|
await _run(tester, server, const Duration(seconds: 2));
|
|
|
|
// The list alone: no chat is open, so it does not take Back.
|
|
expect(provider.value.uri.path, '/chat');
|
|
expect(handlesBack, isFalse);
|
|
expect(find.text('Chat a'), findsWidgets);
|
|
expect(find.text('Chat b'), findsWidgets);
|
|
expect(find.text('Chat c'), findsWidgets);
|
|
|
|
// Tiled chats are opened from their rows. A floating chat's row
|
|
// raises its window instead of opening a page, so it is only checked
|
|
// to stay listed.
|
|
const tiled = ['a', 'b'];
|
|
for (var i = 0; i < 20; i++) {
|
|
final chat = tiled[i % tiled.length];
|
|
await tester.tap(find.text('Chat $chat').first);
|
|
await _run(tester, server, const Duration(milliseconds: 900));
|
|
expect(provider.value.uri.path, '/chat/$chat', reason: 'cycle $i');
|
|
expect(
|
|
find.byType(ChatThreadView),
|
|
findsWidgets,
|
|
reason: 'cycle $i open',
|
|
);
|
|
|
|
await _back(tester, server, via);
|
|
expect(provider.value.uri.path, '/chat', reason: 'back $i');
|
|
expect(handlesBack, isFalse, reason: 'back $i');
|
|
expect(find.byType(ChatThreadView), findsNothing, reason: 'back $i');
|
|
expect(find.text('Chat a'), findsWidgets, reason: 'back $i');
|
|
expect(find.text('Chat b'), findsWidgets, reason: 'back $i');
|
|
expect(find.text('Chat c'), findsWidgets, reason: 'back $i');
|
|
expect(tester.takeException(), isNull, reason: 'back $i');
|
|
}
|
|
expect(exits, isEmpty);
|
|
expect(server.types, contains('chat_open_workspace'));
|
|
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
await tester.pump();
|
|
});
|
|
}
|
|
}
|
|
|
|
testWidgets('a server snapshot that removes the open chat leaves the phone '
|
|
'view alone, and Back still returns to the list on the first press', (
|
|
tester,
|
|
) async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
tester.view.physicalSize = const Size(412, 915);
|
|
tester.view.devicePixelRatio = 1;
|
|
addTearDown(tester.view.reset);
|
|
|
|
bool? handlesBack;
|
|
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
|
SystemChannels.platform,
|
|
(call) async {
|
|
if (call.method == 'SystemNavigator.setFrameworkHandlesBack') {
|
|
handlesBack = call.arguments as bool;
|
|
}
|
|
return null;
|
|
},
|
|
);
|
|
addTearDown(
|
|
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
|
SystemChannels.platform,
|
|
null,
|
|
),
|
|
);
|
|
|
|
final channel = FakeChannel()..readyCompleter.complete();
|
|
final client = PipelinesClient(channelFactory: (_) => channel);
|
|
final server = _Server(
|
|
channel,
|
|
entries: const [
|
|
{'session_id': 'a'},
|
|
],
|
|
);
|
|
final provider = PlatformRouteInformationProvider(
|
|
initialRouteInformation: RouteInformation(uri: Uri.parse('/chat')),
|
|
);
|
|
addTearDown(provider.dispose);
|
|
await tester.pumpWidget(
|
|
PipelinesApp(
|
|
androidSupported: false,
|
|
server: webServer(),
|
|
client: client,
|
|
routeInformationProvider: provider,
|
|
),
|
|
);
|
|
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
|
|
await _run(tester, server, const Duration(seconds: 2));
|
|
|
|
// The server's chat alone does not open a page or take Back.
|
|
expect(provider.value.uri.path, '/chat');
|
|
expect(handlesBack, isFalse);
|
|
expect(find.byType(ChatThreadView), findsNothing);
|
|
|
|
await tester.tap(find.text('Chat a').first);
|
|
await _run(tester, server, const Duration(milliseconds: 900));
|
|
expect(provider.value.uri.path, '/chat/a');
|
|
|
|
// The server drops the chat that is showing. The snapshot must not change
|
|
// the view, so the route and page stay as they were.
|
|
server.change(const []);
|
|
await _run(tester, server, const Duration(milliseconds: 900));
|
|
expect(provider.value.uri.path, '/chat/a');
|
|
expect(tester.takeException(), isNull);
|
|
|
|
// The first Back still returns to the list, with its rows present.
|
|
await tester.binding.handlePopRoute();
|
|
await _run(tester, server, const Duration(milliseconds: 900));
|
|
expect(provider.value.uri.path, '/chat');
|
|
expect(handlesBack, isFalse);
|
|
expect(find.text('Chat a'), findsWidgets);
|
|
expect(find.text('Chat b'), findsWidgets);
|
|
expect(tester.takeException(), isNull);
|
|
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
await tester.pump();
|
|
});
|
|
}
|