8e98651db6
Keep the Material-only chat shell path while preserving public Cupertino parameters for callers. Narrow matching tests to the supported Material host.
180 lines
5.3 KiB
Dart
180 lines
5.3 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 'package:xterm/xterm.dart';
|
|
|
|
import '../conversation/chat_thread_fixtures.dart';
|
|
|
|
/// A gateway for one open conversation (session `s`) that answers the shell
|
|
/// command requests from recorded state and lets a test push their frames.
|
|
class ShellGateway implements ApiGateway {
|
|
final calls = <({String type, JsonMap fields})>[];
|
|
final _frames = StreamController<JsonMap>.broadcast();
|
|
List<JsonMap> runs = const [];
|
|
|
|
/// The chat history `chat_messages` answers with.
|
|
List<JsonMap> messages = const [];
|
|
String? refusal;
|
|
|
|
/// Dismissed ids the `chat_shell_runs` readback reports, and the dismissed
|
|
/// runs it carries (a server that keeps them).
|
|
List<String> dismissed = const [];
|
|
List<JsonMap> dismissedRuns = const [];
|
|
|
|
/// The refusal of `chat_shell_dismiss` / `chat_shell_restore`, when set.
|
|
String? dismissRefusal;
|
|
String? restoreRefusal;
|
|
|
|
/// The reply to a `chat_shell_run_output` that resumes from an offset.
|
|
JsonMap? resumeReply;
|
|
int _serial = 0;
|
|
|
|
@override
|
|
Stream<JsonMap> get frames => _frames.stream;
|
|
|
|
List<JsonMap> bodies(String type) => [
|
|
for (final call in calls)
|
|
if (call.type == type)
|
|
(call.fields['chat_shell_run_request'] ??
|
|
call.fields['chat_prompt'] ??
|
|
call.fields['chat_query'])
|
|
as JsonMap,
|
|
];
|
|
|
|
@override
|
|
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
|
calls.add((type: type, fields: fields));
|
|
final body = fields['chat_shell_run_request'] as JsonMap? ?? const {};
|
|
switch (type) {
|
|
case 'chat_messages':
|
|
return chatPage(messages);
|
|
case 'chat_shell_runs':
|
|
return {
|
|
'chat_shell_runs': runs,
|
|
'chat_shell_dismissed': dismissed,
|
|
'chat_shell_dismissed_runs': dismissedRuns,
|
|
};
|
|
case 'chat_shell_dismiss':
|
|
if (dismissRefusal != null) throw StateError(dismissRefusal!);
|
|
return {'chat_shell_dismissed': body['run_ids']};
|
|
case 'chat_shell_restore':
|
|
if (restoreRefusal != null) throw StateError(restoreRefusal!);
|
|
final ids = (body['run_ids'] as List).toSet();
|
|
return {
|
|
'chat_shell_runs': [
|
|
for (final run in [...runs, ...dismissedRuns])
|
|
if (ids.contains(run['id'])) run,
|
|
],
|
|
};
|
|
case 'chat_shell_run':
|
|
if (refusal != null) throw StateError(refusal!);
|
|
return {
|
|
'chat_shell_run': shellRecord(
|
|
'run_${++_serial}',
|
|
command: '${body['command']}',
|
|
),
|
|
};
|
|
case 'chat_shell_run_output':
|
|
if (body['after'] != null && resumeReply != null) {
|
|
return {'chat_shell_run': resumeReply};
|
|
}
|
|
return {
|
|
'chat_shell_run': [
|
|
...runs,
|
|
...dismissedRuns,
|
|
].firstWhere((r) => r['id'] == body['run_id']),
|
|
};
|
|
}
|
|
return <String, dynamic>{};
|
|
}
|
|
|
|
void emitRun(JsonMap run) => _frames.add({
|
|
'type': 'chat_shell_run_event',
|
|
'chat_shell_run_event': {
|
|
'session_id': 's',
|
|
'run_id': run['id'],
|
|
'run': run,
|
|
},
|
|
});
|
|
|
|
void emitOutput(String id, String text, int offset) => _frames.add({
|
|
'type': 'chat_shell_run_event',
|
|
'chat_shell_run_event': {
|
|
'session_id': 's',
|
|
'run_id': id,
|
|
'output': text,
|
|
'offset': offset,
|
|
'end': offset + text.length,
|
|
},
|
|
});
|
|
|
|
void emitDismissed(String id) => _frames.add({
|
|
'type': 'chat_shell_run_event',
|
|
'chat_shell_run_event': {
|
|
'session_id': 's',
|
|
'run_id': id,
|
|
'dismissed': true,
|
|
},
|
|
});
|
|
|
|
void close() => _frames.close();
|
|
}
|
|
|
|
JsonMap shellRecord(
|
|
String id, {
|
|
String command = 'make test',
|
|
String status = 'running',
|
|
int? exitCode,
|
|
String output = '',
|
|
int durationMs = 0,
|
|
bool terminal = false,
|
|
String text = '',
|
|
int outputStart = 0,
|
|
int startedAt = 1000,
|
|
}) => {
|
|
'id': id,
|
|
'session_id': 's',
|
|
'command': command,
|
|
'directory': '/home/op/project',
|
|
'status': status,
|
|
'exit_code': ?exitCode,
|
|
'started_at': startedAt,
|
|
'ended_at': ?(status == 'running' ? null : startedAt + durationMs),
|
|
'duration_ms': durationMs,
|
|
'output': output,
|
|
'output_start': outputStart,
|
|
'output_bytes': outputStart + output.length,
|
|
if (terminal) ...{'terminal': true, 'cols': 100, 'rows': 30},
|
|
if (text.isNotEmpty) 'text': text,
|
|
};
|
|
|
|
/// The text the run card's terminal draws, one row per line, trailing blank
|
|
/// rows dropped.
|
|
String terminalText(WidgetTester tester, {int index = 0}) {
|
|
final view = tester.widgetList<TerminalView>(find.byType(TerminalView));
|
|
final buffer = view.elementAt(index).terminal.buffer;
|
|
final lines = [
|
|
for (var y = 0; y < buffer.height; y++)
|
|
buffer.lines[y].getText().trimRight(),
|
|
];
|
|
while (lines.isNotEmpty && lines.last.isEmpty) {
|
|
lines.removeLast();
|
|
}
|
|
return lines.join('\n');
|
|
}
|
|
|
|
Widget shellThread(ShellGateway gateway) {
|
|
final thread = ChatThreadView(
|
|
gateway: gateway,
|
|
sessionId: 's',
|
|
syncToken: 0,
|
|
onBack: () {},
|
|
drafts: MemoryChatDraftStore(),
|
|
);
|
|
return MaterialApp(home: Scaffold(body: thread));
|
|
}
|