44b2cff347
On Android and iOS the keyboard's Return now inserts a newline and the Send button sends; desktop keeps Enter to send. The Material activity line drops TextButton's inset so it starts at the answer's edge now that replies have no bubble.
191 lines
5.9 KiB
Dart
191 lines
5.9 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' create, close and rename requests.
|
|
class _SessionGateway extends ComposerBarGateway {
|
|
String? refuse;
|
|
|
|
@override
|
|
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
|
if (type == 'chat_create' ||
|
|
type == 'chat_close' ||
|
|
type == 'chat_rename') {
|
|
calls.add((type: type, fields: fields));
|
|
if (refuse != null) throw ProtocolError(refuse!);
|
|
if (type == 'chat_create') {
|
|
return {
|
|
'chat_session': {'id': 'n1', 'title': 'New chat', 'directory': '/w'},
|
|
};
|
|
}
|
|
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? renamed;
|
|
var closed = 0;
|
|
|
|
setUp(() {
|
|
gateway = _SessionGateway();
|
|
created = null;
|
|
createdNotice = 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',
|
|
onCreatedSession: (session, notice) {
|
|
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 creates in this directory and hands the row over', (
|
|
tester,
|
|
) async {
|
|
await pump(tester);
|
|
await run(tester, '/new');
|
|
|
|
final body = gateway.sent('chat_create').single['chat_create'] as JsonMap;
|
|
expect(body['title'], 'New chat');
|
|
expect(body['directory'], '/w');
|
|
expect(body['operation_id'], 'op-1');
|
|
expect(created?['id'], 'n1');
|
|
expect(createdNotice, 'Started a new session “New chat” in /w.');
|
|
expect(gateway.promptBodies, isEmpty);
|
|
}, variant: _desktop);
|
|
|
|
testWidgets('/new without a directory sends nothing and reports it', (
|
|
tester,
|
|
) async {
|
|
await pump(tester, directory: '');
|
|
await run(tester, '/new');
|
|
|
|
expect(gateway.sent('chat_create'), isEmpty);
|
|
expect(
|
|
find.text(
|
|
"Can't start a new session here: the server reported no directory "
|
|
'for 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);
|
|
}
|