Files

306 lines
9.5 KiB
Dart
Raw Permalink Normal View History

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:openchamber/api/models.dart';
import 'package:openchamber/api/shells.dart';
import 'package:openchamber/background_jobs.dart';
import 'package:openchamber/widgets/shell_jobs.dart';
ContentPart shellCall({
String name = 'bash',
String status = 'completed',
String command = 'pnpm dev',
Map<String, dynamic>? metadata,
}) {
return ContentPart(
type: 'tool',
name: name,
state: ToolState(
status: status,
input: {'command': command},
metadata: metadata,
),
);
}
void main() {
group('backgroundShellId', () {
test('a detached command is completed with a running shell id', () {
expect(
backgroundShellId(shellCall(metadata: {
'status': 'running',
'shellID': 'sh_abc',
})),
'sh_abc',
);
});
test('a foreground command has the id but no detached marker', () {
expect(backgroundShellId(shellCall(metadata: {'shellID': 'sh_abc'})),
isNull);
expect(
backgroundShellId(shellCall(
status: 'running',
metadata: {'status': 'running', 'shellID': 'sh_abc'},
)),
isNull,
);
});
test('other tools and broken metadata say nothing', () {
expect(
backgroundShellId(
shellCall(name: 'read', metadata: {'status': 'running', 'shellID': 'sh_a'})),
isNull,
);
expect(backgroundShellId(shellCall(metadata: null)), isNull);
expect(
backgroundShellId(shellCall(metadata: {'status': 'running', 'shellID': ''})),
isNull,
);
});
});
test('the transcript gives command and start time for the strip', () {
final messages = [
Message(id: 'm1', type: 'assistant', created: 1000, content: [
shellCall(command: 'sleep 600', metadata: {
'status': 'running',
'shellID': 'sh_one',
}),
shellCall(metadata: {'shellID': 'sh_foreground'}),
]),
];
final tasks = backgroundShellsFromMessages(messages, 'ses_x');
expect(tasks, hasLength(1));
expect(tasks.single.id, 'sh_one');
expect(tasks.single.command, 'sleep 600');
expect(tasks.single.startedAt, 1000);
expect(tasks.single.sessionId, 'ses_x');
});
test('the job list gives shell jobs with their shell id and owner', () {
final tasks = shellTasksFromJobs({
'data': [
{
'id': 'job_a',
'kind': 'shell',
'status': 'running',
'sessionID': 'ses_a',
'shellID': 'sh_a',
'command': 'npm run dev',
'time': {'started': 1795000000000},
},
{
'id': 'job_b',
'kind': 'image',
'status': 'running',
'sessionID': 'ses_a',
'path': '/tmp/a.png',
'model': 'm',
'time': {'started': 1795000000000},
},
{
'id': 'job_c',
'kind': 'shell',
'status': 'completed',
'sessionID': 'ses_a',
'shellID': 'sh_c',
'command': 'npm test',
'time': {'started': 1794000000000},
},
],
});
expect(tasks, hasLength(1));
expect(tasks.single.id, 'sh_a');
expect(tasks.single.sessionId, 'ses_a');
expect(tasks.single.command, 'npm run dev');
expect(tasks.single.startedAt, 1795000000000);
});
test('a completion note gives the shell id, state and output', () {
final note = shellCompletionNote(Message(
id: 's',
type: 'synthetic',
text: '<background-result kind="shell" id="sh_a" state="completed" '
'command="echo hi">\nhi\nthere\n</background-result>',
));
expect(note, isNotNull);
expect(note!.shellId, 'sh_a');
expect(note.state, 'completed');
expect(note.output, 'hi\nthere');
expect(note.failed, isFalse);
final failed = shellCompletionNote(Message(
id: 's2',
type: 'synthetic',
text: '<background-result kind="shell" id="sh_b" state="error">\n'
'Shell.NotFoundError\n</background-result>',
));
expect(failed!.failed, isTrue);
expect(
shellCompletionNote(
Message(id: 's3', type: 'synthetic', text: 'plain note')),
isNull,
);
expect(
shellCompletionNote(Message(
id: 's4',
type: 'synthetic',
text: '<background-result kind="subagent" id="x" state="completed">'
'\ndone\n</background-result>',
)),
isNull,
);
});
test('the newest completion for a shell wins', () {
Message note(String id, String output) => Message(
id: id,
type: 'synthetic',
text: '<background-result kind="shell" id="sh_a" '
'state="completed">\n$output\n</background-result>',
);
final found = shellCompletionFor(
[note('n1', 'first'), note('n2', 'second')], 'sh_a');
expect(found!.output, 'second');
expect(shellCompletionFor([note('n1', 'x')], 'sh_other'), isNull);
});
test('a stop note marks its shells as stopped by the user', () {
final messages = [
Message(
id: 's',
type: 'synthetic',
text: 'The user stopped the background command `sleep 1`.',
metadata: {
'openchamber': {
'shellStop': {
'shellIDs': ['sh_a', 'sh_b'],
},
},
},
),
Message(id: 'a', type: 'assistant', text: 'ok'),
];
expect(shellWasStopped(messages, 'sh_a'), isTrue);
expect(shellWasStopped(messages, 'sh_b'), isTrue);
expect(shellWasStopped(messages, 'sh_c'), isFalse);
expect(shellWasStopped(const [], 'sh_a'), isFalse);
});
test('the shell list keeps owner, clock and running state', () {
final tasks = shellTasksFromList({
'location': {'directory': '/repo'},
'data': [
{
'id': 'sh_a',
'status': 'running',
'command': 'npm run dev',
'metadata': {'sessionID': 'ses_a'},
'time': {'started': 1795000000000},
},
{
'id': 'sh_b',
'status': 'exited',
'command': 'npm test',
'metadata': {},
'time': {'started': 1794000000000},
},
],
});
expect(tasks.map((t) => t.id), ['sh_a', 'sh_b']);
expect(tasks.first.running, isTrue);
expect(tasks.first.sessionId, 'ses_a');
expect(tasks.first.startedAt, 1795000000000);
expect(tasks.last.running, isFalse);
});
test('an output page reads text, cursor and size', () {
final page = ShellOutputPage.fromJson({
'data': {'output': 'one\ntwo\n', 'cursor': 8, 'size': 8, 'truncated': false},
});
expect(page.output, 'one\ntwo\n');
expect(page.cursor, 8);
expect(page.size, 8);
expect(page.truncated, isFalse);
expect(ShellOutputPage.fromJson({}).output, '');
});
test('elapsed reads in seconds, minutes and hours', () {
expect(formatElapsed(0), '0秒');
expect(formatElapsed(12 * 1000), '12秒');
expect(formatElapsed(185 * 1000), '3分05秒');
expect(formatElapsed(3723 * 1000), '1:02:03');
});
test('the stop note names the command and says it was the user', () {
final note = stopNote(' pnpm dev ');
expect(note, contains('pnpm dev'));
expect(note, contains('user stopped'));
expect(note, contains('Shell.NotFoundError'));
});
group('ShellTaskBar', () {
ShellTask task(String id, {String command = 'pnpm dev', String session = 'ses_a'}) =>
ShellTask(
id: id,
sessionId: session,
command: command,
startedAt: DateTime.now().millisecondsSinceEpoch - 65000,
);
Future<void> pump(WidgetTester tester, List<ShellTask> tasks,
{void Function(ShellTask)? onOpen}) async {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: Column(
children: [
ShellTaskBar(
tasks: tasks,
sessionId: 'ses_a',
onOpen: onOpen ?? (_) {},
),
],
),
),
));
}
testWidgets('one command is its own row, with its clock', (tester) async {
ShellTask? opened;
await pump(tester, [task('sh_1', command: 'pnpm dev')],
onOpen: (t) => opened = t);
expect(find.text('pnpm dev'), findsOneWidget);
expect(find.text('1分05秒'), findsOneWidget);
expect(find.textContaining('正在执行'), findsNothing);
// Stopping is in the sheet the row opens, not a button on the bar.
expect(find.text('停止'), findsNothing);
await tester.tap(find.text('pnpm dev'));
expect(opened?.id, 'sh_1');
});
testWidgets('two commands share a count header and both show',
(tester) async {
await pump(tester, [
task('sh_1', command: 'pnpm dev'),
task('sh_2', command: 'pnpm test', session: 'ses_child'),
]);
expect(find.text('正在执行 2 条命令'), findsOneWidget);
expect(find.text('pnpm dev'), findsOneWidget);
expect(find.text('pnpm test'), findsOneWidget);
expect(find.text('子代理'), findsOneWidget);
expect(find.text('停止'), findsNothing);
});
testWidgets('a long list folds until asked', (tester) async {
await pump(tester, [for (var i = 0; i < 4; i++) task('sh_$i', command: 'cmd $i')]);
expect(find.text('正在执行 4 条命令'), findsOneWidget);
expect(find.text('cmd 0'), findsNothing);
await tester.tap(find.text('正在执行 4 条命令'));
await tester.pump();
expect(find.text('cmd 0'), findsOneWidget);
expect(find.text('cmd 3'), findsOneWidget);
});
});
}