Files
pipelines/app/test/features/chat/conversation/chat_command_session_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

204 lines
6.3 KiB
Dart

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/core/transport/connection_state.dart';
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'package:pipelines_app/features/chat/conversation/chat_transcript_cache.dart';
import 'chat_composer_bar_fixtures.dart';
/// These tests drive a desktop keyboard, where Enter sends; on a phone the
/// on-screen keyboard's Return breaks the line instead.
final _desktop = TargetPlatformVariant.only(TargetPlatform.linux);
/// Answers the session commands' new-session, close and rename requests.
class _SessionGateway extends ComposerBarGateway {
String? refuse;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
if (type == 'chat_new_session' ||
type == 'chat_close' ||
type == 'chat_rename') {
calls.add((type: type, fields: fields));
if (refuse != null) throw ProtocolError(refuse!);
if (type == 'chat_new_session') {
return {
'chat_session': {
'id': 'n1',
'title': 'Old',
'directory': '/w',
'previous_id': 's',
},
};
}
return <String, dynamic>{};
}
return super.call(type, fields);
}
List<JsonMap> sent(String type) => [
for (final call in calls)
if (call.type == type) call.fields,
];
}
void main() {
late _SessionGateway gateway;
JsonMap? created;
String? createdNotice;
String? replacedId;
String? renamed;
var closed = 0;
setUp(() {
gateway = _SessionGateway();
created = null;
createdNotice = null;
replacedId = null;
renamed = null;
closed = 0;
});
Future<void> pump(
WidgetTester tester, {
String directory = '/w',
ChatTranscriptCache? transcripts,
}) async {
await tester.pumpWidget(
materialHost(
ChatThreadView(
gateway: gateway,
sessionId: 's',
title: 'Old',
session: {'id': 's', 'title': 'Old', 'directory': directory},
onBack: () {},
drafts: MemoryChatDraftStore(),
transcripts: transcripts,
newOperationId: () => 'op-1',
onNewSession: (previousId, session, notice) {
replacedId = previousId;
created = session;
createdNotice = notice;
},
onSessionRenamed: (title) => renamed = title,
onSessionClosed: () => closed++,
),
),
);
await tester.pumpAndSettle();
}
Future<void> run(WidgetTester tester, String draft) async {
await tester.enterText(find.byKey(const Key('chat-composer-field')), draft);
await tester.pump();
// The first Enter may complete the palette row; the second runs it.
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump();
if (fieldText(tester).isNotEmpty) {
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
}
await tester.pumpAndSettle();
}
testWidgets('/new keeps this chat on a fresh session and hands it over', (
tester,
) async {
await pump(tester);
await run(tester, '/new');
expect(gateway.sent('chat_create'), isEmpty);
final body =
gateway.sent('chat_new_session').single['chat_new_session'] as JsonMap;
expect(body['session_id'], 's');
expect(body['instructions'], isNotEmpty);
expect(replacedId, 's');
expect(created?['id'], 'n1');
expect(created?['title'], 'Old');
expect(created?.containsKey('previous_id'), isFalse);
expect(createdNotice, startsWith('New session started.'));
expect(gateway.promptBodies, isEmpty);
}, variant: _desktop);
testWidgets('/new refused keeps the reason here and hands nothing over', (
tester,
) async {
gateway.refuse = 'a turn is still running in this chat';
await pump(tester);
await run(tester, '/new');
expect(gateway.sent('chat_new_session'), hasLength(1));
expect(created, isNull);
expect(
find.text(
"Couldn't start a new session: a turn is still running in this chat",
),
findsOneWidget,
);
}, variant: _desktop);
testWidgets('/rename sends the trimmed title and reports it', (tester) async {
await pump(tester);
await run(tester, '/rename Fresh title ');
final body = gateway.sent('chat_rename').single['chat_rename'] as JsonMap;
expect(body, {'session_id': 's', 'title': 'Fresh title'});
expect(renamed, 'Fresh title');
expect(find.text('Renamed this session to “Fresh title”.'), findsOneWidget);
}, variant: _desktop);
testWidgets('/rename without a title reports usage and sends nothing', (
tester,
) async {
await pump(tester);
await run(tester, '/rename');
expect(gateway.sent('chat_rename'), isEmpty);
expect(
find.text('Usage: /rename <title> — renames this session.'),
findsOneWidget,
);
}, variant: _desktop);
testWidgets('/close confirms, closes and a refusal keeps the reason', (
tester,
) async {
await pump(tester);
await run(tester, '/close');
await tester.tap(find.text('Close chat'));
await tester.pumpAndSettle();
expect(gateway.sent('chat_close').single['chat_query'], {
'session_id': 's',
});
expect(closed, 1);
gateway.refuse = 'chat is locked';
await run(tester, '/close');
await tester.tap(find.text('Close chat'));
await tester.pumpAndSettle();
expect(closed, 1);
expect(find.text('chat is locked'), findsOneWidget);
}, variant: _desktop);
testWidgets('/close forgets the cached transcript, also after leaving', (
tester,
) async {
final transcripts = ChatTranscriptCache()
..write('s', ChatTranscriptSnapshot(messages: const []))
..write('other', ChatTranscriptSnapshot(messages: const []));
await pump(tester, transcripts: transcripts);
await run(tester, '/close');
await tester.tap(find.text('Close chat'));
await tester.pumpAndSettle();
expect(closed, 1);
expect(transcripts.sessionIds, ['other']);
// Leaving the closed conversation does not write it back.
await tester.pumpWidget(const SizedBox.shrink());
expect(transcripts.sessionIds, ['other']);
}, variant: _desktop);
}