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.
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
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/domain/chat_commands.dart';
|
||||
import 'package:pipelines_app/features/chat/screen.dart';
|
||||
|
||||
/// A chat destination server whose `/new` re-keys chat `s` to `n1`, as the
|
||||
/// registry does, and which can push another device's move.
|
||||
class _NewSessionGateway implements ApiGateway {
|
||||
final calls = <({String type, JsonMap fields})>[];
|
||||
final pushed = StreamController<JsonMap>.broadcast();
|
||||
var current = _row('s');
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => pushed.stream;
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
calls.add((type: type, fields: fields));
|
||||
switch (type) {
|
||||
case 'chat_sessions':
|
||||
return {
|
||||
'chat_sessions': [current, _other],
|
||||
};
|
||||
case 'chat_new_session':
|
||||
current = _row('n1');
|
||||
return {
|
||||
'chat_session': {...current, 'previous_id': 's'},
|
||||
};
|
||||
case 'chat_messages':
|
||||
final query = fields['chat_query'] as JsonMap;
|
||||
final old = query['session_id'] == 's';
|
||||
return {
|
||||
'chat_history': {
|
||||
'messages': [
|
||||
if (old)
|
||||
{
|
||||
'id': 'm1',
|
||||
'role': 'user',
|
||||
'parts': [
|
||||
{'type': 'text', 'text': 'Earlier question'},
|
||||
],
|
||||
},
|
||||
],
|
||||
'next_cursor': '',
|
||||
'has_more': false,
|
||||
},
|
||||
};
|
||||
}
|
||||
return <String, dynamic>{};
|
||||
}
|
||||
|
||||
Iterable<String> subscribed() => [
|
||||
for (final call in calls)
|
||||
if (call.type == 'chat_subscribe')
|
||||
'${(call.fields['chat_subscribe'] as Map?)?['session_id']}',
|
||||
];
|
||||
}
|
||||
|
||||
JsonMap _row(String id) => {
|
||||
'id': id,
|
||||
'title': 'Wire chat',
|
||||
'backend': 'claude',
|
||||
'directory': '/w',
|
||||
'color': 'teal',
|
||||
'starred': true,
|
||||
'resume_command': 'cd /w && claude --resume $id',
|
||||
};
|
||||
|
||||
const JsonMap _other = {'id': 't', 'title': 'Other chat', 'directory': '/w'};
|
||||
|
||||
final _desktop = TargetPlatformVariant.only(TargetPlatform.linux);
|
||||
|
||||
Future<void> _pump(WidgetTester tester, _NewSessionGateway gateway) async {
|
||||
tester.view.physicalSize = const Size(1400, 900);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: ThemeData(platform: TargetPlatform.linux),
|
||||
home: Scaffold(body: ChatScreen(gateway: gateway)),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(const Key('chat-row-s')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Earlier question'), findsOneWidget);
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'/new keeps the chat in its row and opens the fresh, empty session',
|
||||
(tester) async {
|
||||
final gateway = _NewSessionGateway();
|
||||
await _pump(tester, gateway);
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('chat-composer-field')),
|
||||
'/new',
|
||||
);
|
||||
await tester.pump();
|
||||
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();
|
||||
|
||||
expect(gateway.calls.where((c) => c.type == 'chat_create'), isEmpty);
|
||||
expect(find.byKey(const Key('chat-row-s')), findsNothing);
|
||||
expect(find.byKey(const Key('chat-row-n1')), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-row-t')), findsOneWidget);
|
||||
expect(find.text(chatNewSessionNotice), findsOneWidget);
|
||||
expect(find.text('Earlier question'), findsNothing);
|
||||
expect(gateway.subscribed().last, 'n1');
|
||||
},
|
||||
variant: _desktop,
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'an open chat follows a new session started on another device',
|
||||
(tester) async {
|
||||
final gateway = _NewSessionGateway();
|
||||
await _pump(tester, gateway);
|
||||
gateway.current = _row('n1');
|
||||
gateway.pushed.add({
|
||||
'type': 'chat_session_changed',
|
||||
'chat_session': {...gateway.current, 'previous_id': 's'},
|
||||
});
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byKey(const Key('chat-row-s')), findsNothing);
|
||||
expect(find.byKey(const Key('chat-row-n1')), findsOneWidget);
|
||||
expect(find.text(chatNewSessionElsewhereNotice), findsOneWidget);
|
||||
expect(find.text('Earlier question'), findsNothing);
|
||||
expect(gateway.subscribed().last, 'n1');
|
||||
},
|
||||
variant: _desktop,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user