121 lines
3.4 KiB
Dart
121 lines
3.4 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
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;
|
|
|
|
@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!();
|
|
}
|
|
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();
|
|
|
|
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 cupertinoHost(Widget child) => CupertinoApp(
|
|
theme: const CupertinoThemeData(brightness: Brightness.dark),
|
|
home: CupertinoPageScaffold(child: child),
|
|
);
|
|
|
|
Widget composerThread(
|
|
ComposerBarGateway gateway, {
|
|
bool cupertino = false,
|
|
int syncToken = 0,
|
|
ChatDraftStore? drafts,
|
|
String Function()? newOperationId,
|
|
VoidCallback? onBack,
|
|
}) {
|
|
final thread = ChatThreadView(
|
|
gateway: gateway,
|
|
sessionId: 's',
|
|
syncToken: syncToken,
|
|
onBack: onBack ?? () {},
|
|
drafts: drafts ?? MemoryChatDraftStore(),
|
|
newOperationId: newOperationId,
|
|
);
|
|
return cupertino ? cupertinoHost(thread) : 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;
|