Files
pipelines/app/test/features/chat/shell/chat_shell_fixtures.dart
T
alan 89882d90f3 Draw chat shell runs in an xterm terminal that takes typed input
The card's output area is now an xterm.dart emulator kept per run by the
controller: it is fed the raw stream in order, takes typing and paste while
the command runs (with a key toolbar and a soft-keyboard field on touch
platforms), resizes the process to its layout, opens printed links, shrinks
finished runs to their output and opens full screen. A reconnect resumes from
the byte the card holds; Copy output copies the server's plain text.
2026-11-09 20:08:33 +00:00

142 lines
4.1 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 '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 [];
String? refusal;
/// 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(const []);
case 'chat_shell_runs':
return {'chat_shell_runs': runs};
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.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 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,
}) => {
'id': id,
'session_id': 's',
'command': command,
'directory': '/home/op/project',
'status': status,
'exit_code': ?exitCode,
'started_at': 1000,
'ended_at': ?(status == 'running' ? null : 1000 + 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, {bool cupertino = false}) {
final thread = ChatThreadView(
gateway: gateway,
sessionId: 's',
syncToken: 0,
onBack: () {},
drafts: MemoryChatDraftStore(),
);
return cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: thread))
: MaterialApp(home: Scaffold(body: thread));
}