2a182d7f17
Keep Esc and /stop scoped to the foreground turn while exposing an explicit stop-tasks operation for supported chat backends. Add the protocol, server routing, app command, and coverage for task stopping and unsupported harnesses.
124 lines
3.5 KiB
Dart
124 lines
3.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/core/api_gateway.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
|
|
|
import 'chat_thread_fixtures.dart';
|
|
|
|
/// A gateway for one open fixture conversation with a composer. History pages
|
|
/// are tests' own; prompt/interrupt replies are controlled per test.
|
|
class ComposerBarGateway implements ApiGateway {
|
|
final calls = <({String type, JsonMap fields})>[];
|
|
final _frames = StreamController<JsonMap>.broadcast();
|
|
JsonMap page = chatPage(const []);
|
|
Future<JsonMap> Function(String text, String operationId)? prompt;
|
|
Future<JsonMap> Function()? interrupt;
|
|
Future<JsonMap> Function()? stopTasks;
|
|
|
|
@override
|
|
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
|
calls.add((type: type, fields: fields));
|
|
switch (type) {
|
|
case 'chat_subscribe':
|
|
return <String, dynamic>{};
|
|
case 'chat_messages':
|
|
return page;
|
|
case 'chat_prompt':
|
|
final body = fields['chat_prompt'] as JsonMap? ?? const {};
|
|
return prompt == null
|
|
? <String, dynamic>{}
|
|
: prompt!('${body['text'] ?? ''}', '${body['operation_id'] ?? ''}');
|
|
case 'chat_interrupt':
|
|
return interrupt == null ? <String, dynamic>{} : interrupt!();
|
|
case 'chat_stop_tasks':
|
|
return stopTasks == null ? <String, dynamic>{} : stopTasks!();
|
|
}
|
|
return <String, dynamic>{};
|
|
}
|
|
|
|
@override
|
|
Stream<JsonMap> get frames => _frames.stream;
|
|
|
|
List<JsonMap> get promptBodies => calls
|
|
.where((call) => call.type == 'chat_prompt')
|
|
.map((call) => call.fields['chat_prompt'] as JsonMap)
|
|
.toList();
|
|
|
|
List<JsonMap> get interruptBodies => calls
|
|
.where((call) => call.type == 'chat_interrupt')
|
|
.map((call) => call.fields['chat_query'] as JsonMap)
|
|
.toList();
|
|
|
|
List<JsonMap> get stopTasksBodies => calls
|
|
.where((call) => call.type == 'chat_stop_tasks')
|
|
.map((call) => call.fields['chat_query'] as JsonMap)
|
|
.toList();
|
|
|
|
void emitEvent(JsonMap body) =>
|
|
_frames.add({'type': 'chat_event', 'chat_event': body});
|
|
|
|
void close() => _frames.close();
|
|
}
|
|
|
|
Widget materialHost(Widget child) => MaterialApp(
|
|
theme: ThemeData(
|
|
platform: TargetPlatform.android,
|
|
brightness: Brightness.light,
|
|
),
|
|
home: Scaffold(body: child),
|
|
);
|
|
|
|
Widget composerThread(
|
|
ComposerBarGateway gateway, {
|
|
int syncToken = 0,
|
|
ChatDraftStore? drafts,
|
|
String Function()? newOperationId,
|
|
VoidCallback? onBack,
|
|
JsonMap? session,
|
|
}) {
|
|
final thread = ChatThreadView(
|
|
gateway: gateway,
|
|
sessionId: 's',
|
|
session: session,
|
|
syncToken: syncToken,
|
|
onBack: onBack ?? () {},
|
|
drafts: drafts ?? MemoryChatDraftStore(),
|
|
newOperationId: newOperationId,
|
|
);
|
|
return materialHost(thread);
|
|
}
|
|
|
|
/// The common fixture turn, so a rebuild has real transcript content to move.
|
|
JsonMap fixtureTurn({
|
|
String user = 'Stored question',
|
|
String answer = 'Stored answer',
|
|
}) => chatPage([
|
|
chatMessage(
|
|
'u1',
|
|
'user',
|
|
parts: [
|
|
{'type': 'text', 'text': user},
|
|
],
|
|
),
|
|
chatMessage(
|
|
'a1',
|
|
'assistant',
|
|
parts: [
|
|
{'type': 'text', 'text': answer},
|
|
],
|
|
),
|
|
]);
|
|
|
|
String fieldText(WidgetTester tester) => tester
|
|
.widget<EditableText>(
|
|
find.descendant(
|
|
of: find.byKey(const Key('chat-composer-field')),
|
|
matching: find.byType(EditableText),
|
|
),
|
|
)
|
|
.controller
|
|
.text;
|