Files
pipelines/app/test/features/chat/chat_screen_mode_test.dart
T
alan ad4233f7b8 Chat: /new continues the same chat on a fresh session
The composer's /new used to create a separate chat. It now sends
chat_new_session. The server starts a fresh native session on the chat's
own harness (OpenCode, Codex or Claude Code) and re-keys the
operator_sessions row in one transaction. The chat keeps its title,
directory, backend, colour, star, model and mode. The old row is kept
unlisted with replaced_by, and its native session stays resumable.

The reply and the chat_session_changed push carry previous_id, so every
client swaps the row in place and an open conversation follows it to the
fresh, empty transcript with a notice.
2026-11-03 14:06:44 +00:00

289 lines
8.7 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/conversation/chat_mode_control.dart';
import 'package:pipelines_app/features/chat/screen.dart';
/// A chat destination gateway whose listing can change between reads, as it
/// does when the server learns a row's mode after the row was first opened.
class _ModeScreenGateway implements ApiGateway {
final calls = <({String type, JsonMap fields})>[];
List<JsonMap> sessions = [
_claude('s', 'Wire chat', mode: 'default', name: 'Ask for permission'),
];
/// The `chat_session` a `/new` replies with, for the chat `s`.
JsonMap created = _claude('n1', 'New chat');
/// Holds list reads while set, so a test can look before the re-read lands.
Completer<void>? listGate;
@override
Stream<JsonMap> get frames => const Stream.empty();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
calls.add((type: type, fields: fields));
switch (type) {
case 'chat_sessions':
final rows = [...sessions];
await listGate?.future;
return {'chat_sessions': rows};
case 'chat_new_session':
return {
'chat_session': {...created, 'previous_id': 's'},
};
case 'chat_messages':
return {
'chat_history': {
'messages': const [],
'next_cursor': '',
'has_more': false,
},
};
}
return <String, dynamic>{};
}
}
JsonMap _claude(
String id,
String title, {
String mode = '',
String name = '',
}) => {
'id': id,
'title': title,
'backend': 'claude',
'directory': '/w',
if (mode.isNotEmpty) 'mode': mode,
if (name.isNotEmpty) 'mode_name': name,
};
/// `/new` is typed on a desktop keyboard, where Enter sends.
final _desktop = TargetPlatformVariant.only(TargetPlatform.linux);
final _modeControl = find.byKey(const Key('chat-mode'));
Widget _app(
_ModeScreenGateway gateway, {
required TargetPlatform platform,
String? initialSessionId,
int syncToken = 0,
}) => MaterialApp(
theme: ThemeData(platform: platform),
home: Scaffold(
body: ChatScreen(
gateway: gateway,
initialSessionId: initialSessionId,
syncToken: syncToken,
),
),
);
Future<void> _pump(
WidgetTester tester,
_ModeScreenGateway gateway, {
required TargetPlatform platform,
Size size = const Size(390, 844),
String? initialSessionId,
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(
_app(gateway, platform: platform, initialSessionId: initialSessionId),
);
await tester.pumpAndSettle();
}
/// The mode the drawn control names, or null when the bar has none. A
/// Cupertino phone bar draws only the shield, so read the control itself.
String? _shown(WidgetTester tester) {
if (_modeControl.evaluate().isEmpty) return null;
final control = find.byType(ChatModeControl);
expect(control, findsOneWidget);
return tester.widget<ChatModeControl>(control).modeName;
}
void main() {
for (final platform in [TargetPlatform.android, TargetPlatform.iOS]) {
final style = platform == TargetPlatform.iOS ? 'Cupertino' : 'Material';
testWidgets(
'$style: an existing chat opened from the list shows its mode',
(tester) async {
final gateway = _ModeScreenGateway();
await _pump(tester, gateway, platform: platform);
await tester.tap(find.byKey(const Key('chat-row-s')));
await tester.pumpAndSettle();
expect(_shown(tester), 'Ask for permission');
},
);
testWidgets(
'$style: a mode the list learns after the chat opened appears in place',
(tester) async {
final gateway = _ModeScreenGateway()
..sessions = [_claude('s', 'Wire chat')];
await _pump(tester, gateway, platform: platform);
await tester.tap(find.byKey(const Key('chat-row-s')));
await tester.pumpAndSettle();
expect(_shown(tester), isNull);
// A reconnect re-reads the list; the open chat follows that row
// without being closed and opened again.
gateway.sessions = [
_claude(
's',
'Wire chat',
mode: 'default',
name: 'Ask for permission',
),
];
await tester.pumpWidget(
_app(gateway, platform: platform, syncToken: 1),
);
await tester.pumpAndSettle();
expect(_shown(tester), 'Ask for permission');
// Another device chose Auto; the next re-read keeps the bar in sync.
gateway.sessions = [
_claude('s', 'Wire chat', mode: 'auto', name: 'Auto'),
];
await tester.pumpWidget(
_app(gateway, platform: platform, syncToken: 2),
);
await tester.pumpAndSettle();
expect(_shown(tester), 'Auto');
},
);
testWidgets('$style: a routed chat shows its mode once the list loads', (
tester,
) async {
final gateway = _ModeScreenGateway();
await _pump(tester, gateway, platform: platform, initialSessionId: 's');
expect(_shown(tester), 'Ask for permission');
});
testWidgets('$style: switching chats shows each chat its own mode', (
tester,
) async {
final gateway = _ModeScreenGateway()
..sessions = [
_claude(
's',
'Wire chat',
mode: 'default',
name: 'Ask for permission',
),
_claude('t', 'Other chat', mode: 'auto', name: 'Auto'),
{'id': 'u', 'title': 'Plain chat', 'backend': 'opencode'},
];
// Wide: the list stays beside the open chat, so a row swaps it.
await _pump(
tester,
gateway,
platform: platform,
size: const Size(1400, 900),
);
await tester.tap(find.byKey(const Key('chat-row-s')));
await tester.pumpAndSettle();
expect(_shown(tester), 'Ask for permission');
await tester.tap(find.byKey(const Key('chat-row-t')));
await tester.pumpAndSettle();
expect(_shown(tester), 'Auto');
await tester.tap(find.byKey(const Key('chat-row-u')));
await tester.pumpAndSettle();
expect(_shown(tester), isNull);
await tester.tap(find.byKey(const Key('chat-row-s')));
await tester.pumpAndSettle();
expect(_shown(tester), 'Ask for permission');
});
}
Future<void> runNew(WidgetTester tester) async {
await tester.enterText(
find.byKey(const Key('chat-composer-field')),
'/new',
);
await tester.pump();
// The first Enter may complete the palette row; the second runs it.
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump();
final field = tester.widget<EditableText>(
find.descendant(
of: find.byKey(const Key('chat-composer-field')),
matching: find.byType(EditableText),
),
);
if (field.controller.text.isNotEmpty) {
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
}
await tester.pumpAndSettle();
}
testWidgets(
'a new session whose reply names its mode shows it before the list re-reads',
(tester) async {
final gateway = _ModeScreenGateway()
..created = _claude('n1', 'New chat', mode: 'plan', name: 'Plan');
await _pump(
tester,
gateway,
platform: TargetPlatform.linux,
size: const Size(1400, 900),
);
await tester.tap(find.byKey(const Key('chat-row-s')));
await tester.pumpAndSettle();
gateway.listGate = Completer<void>();
await runNew(tester);
expect(find.text('New chat'), findsWidgets);
expect(_shown(tester), 'Plan');
gateway.listGate!.complete();
await tester.pumpAndSettle();
},
variant: _desktop,
);
testWidgets(
'a new session whose reply lacks its mode shows it when the list re-reads',
(tester) async {
final gateway = _ModeScreenGateway();
await _pump(
tester,
gateway,
platform: TargetPlatform.linux,
size: const Size(1400, 900),
);
await tester.tap(find.byKey(const Key('chat-row-s')));
await tester.pumpAndSettle();
gateway
..listGate = Completer<void>()
..sessions = [_claude('n1', 'New chat', mode: 'plan', name: 'Plan')];
await runNew(tester);
// The row is open on the /new reply, which names no mode.
expect(find.text('New chat'), findsWidgets);
expect(_shown(tester), isNull);
gateway.listGate!.complete();
await tester.pumpAndSettle();
expect(_shown(tester), 'Plan');
},
variant: _desktop,
);
}