194 lines
6.1 KiB
Dart
194 lines
6.1 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/features/terminal/terminal_hub.dart';
|
|
import 'package:pipelines_app/features/terminal/terminal_tab.dart';
|
|
import 'package:xterm/core.dart';
|
|
|
|
import 'terminal_fixtures.dart';
|
|
|
|
String screen(TerminalTab tab) {
|
|
final buffer = tab.session.terminal.buffer;
|
|
return [
|
|
for (var y = 0; y < buffer.height; y++)
|
|
buffer.lines[y].getText().trimRight(),
|
|
].where((line) => line.isNotEmpty).join('\n');
|
|
}
|
|
|
|
void main() {
|
|
late TerminalGateway mac;
|
|
late TestMachines machines;
|
|
late TerminalHub hub;
|
|
|
|
setUp(() {
|
|
mac = TerminalGateway();
|
|
machines = TestMachines([machine('mac', mac, name: 'Mac')]);
|
|
hub = TerminalHub(machines);
|
|
});
|
|
tearDown(() async {
|
|
hub.dispose();
|
|
await mac.close();
|
|
});
|
|
|
|
Future<void> settle() => Future<void>.delayed(Duration.zero);
|
|
|
|
test('nothing is requested until the terminal is first shown', () async {
|
|
mac.add('term_a', text: 'hello\r\n');
|
|
await settle();
|
|
expect(mac.requests, isEmpty);
|
|
hub.activate();
|
|
await settle();
|
|
await settle();
|
|
expect(mac.requests.map((r) => r['type']), [
|
|
'terminal_list',
|
|
'terminal_output',
|
|
]);
|
|
expect(hub.tabs.single.title, '~');
|
|
expect(screen(hub.tabs.single), 'hello');
|
|
});
|
|
|
|
test('live output appends in order; a gap reads the tail again', () async {
|
|
mac.add('term_a', text: r'$ ');
|
|
hub.activate();
|
|
await settle();
|
|
await settle();
|
|
final tab = hub.tabs.single;
|
|
mac.write('term_a', 'ls\r\nREADME\r\n');
|
|
await settle();
|
|
expect(screen(tab), '\$ ls\nREADME');
|
|
// A frame the client missed: the next one starts after its end.
|
|
mac.write('term_a', 'missed\r\n', push: false);
|
|
mac.write('term_a', 'after\r\n');
|
|
await settle();
|
|
await settle();
|
|
expect(screen(tab), '\$ ls\nREADME\nmissed\nafter');
|
|
final reads = mac.bodies('terminal_output');
|
|
expect(reads.last['after'], greaterThan(0));
|
|
});
|
|
|
|
test('a reconnect resumes after the held end; a restarted server '
|
|
'(new stream) is read again from its start', () async {
|
|
mac.add('term_a', text: 'one\r\n');
|
|
hub.activate();
|
|
await settle();
|
|
await settle();
|
|
final tab = hub.tabs.single;
|
|
machines.value = [];
|
|
await settle();
|
|
expect(tab.connected, isFalse);
|
|
mac.write('term_a', 'two\r\n', push: false);
|
|
final again = TerminalGateway()
|
|
..terminals.addAll(mac.terminals)
|
|
..output.addAll(mac.output);
|
|
machines.value = [machine('mac', again, name: 'Mac')];
|
|
await settle();
|
|
await settle();
|
|
expect(identical(hub.tabs.single, tab), isTrue);
|
|
expect(tab.connected, isTrue);
|
|
expect(again.bodies('terminal_output').single['after'], 5);
|
|
expect(screen(tab), 'one\ntwo');
|
|
// The server restarted: the tmux client redrew the screen into a new
|
|
// stream starting at zero.
|
|
machines.value = [];
|
|
await settle();
|
|
final restarted = TerminalGateway(stream: 's2')
|
|
..terminals.addAll(mac.terminals)
|
|
..output['term_a'] = 'one\r\ntwo\r\n\$ ';
|
|
machines.value = [machine('mac', restarted, name: 'Mac')];
|
|
await settle();
|
|
await settle();
|
|
await settle();
|
|
expect(
|
|
restarted.bodies('terminal_output').last.containsKey('after'),
|
|
isFalse,
|
|
);
|
|
expect(screen(tab), 'one\ntwo\n\$');
|
|
await again.close();
|
|
await restarted.close();
|
|
});
|
|
|
|
test('open selects a new tab; close removes it; another client\'s '
|
|
'terminal appears and its close removes it', () async {
|
|
expect(await hub.open(cwd: '/Users/op/src'), isNull);
|
|
await settle();
|
|
expect(mac.bodies('terminal_open').single['cwd'], '/Users/op/src');
|
|
final first = hub.selected!;
|
|
expect(first.title, 'src');
|
|
expect(await hub.open(), isNull);
|
|
await settle();
|
|
expect(hub.tabs, hasLength(2));
|
|
expect(hub.selected, isNot(first));
|
|
hub.select(first);
|
|
expect(hub.selected, first);
|
|
expect(await hub.close(first), isNull);
|
|
expect(hub.tabs, hasLength(1));
|
|
mac.add('term_other');
|
|
mac.lifecycle('term_other', {});
|
|
await settle();
|
|
await settle();
|
|
expect(hub.tabs, hasLength(2));
|
|
mac.lifecycle('term_other', {'status': 'closed'});
|
|
await settle();
|
|
expect(hub.tabs, hasLength(1));
|
|
});
|
|
|
|
test(
|
|
'the shell title wins over the folder; an ended terminal says why',
|
|
() async {
|
|
mac.add('term_a', text: '\x1b]0;vim notes.md\x07');
|
|
hub.activate();
|
|
await settle();
|
|
await settle();
|
|
final tab = hub.tabs.single;
|
|
expect(tab.title, 'vim notes.md');
|
|
expect(tab.session.live, isTrue);
|
|
mac.lifecycle('term_a', {
|
|
'status': 'exited',
|
|
'reason':
|
|
'The Pipelines server restarted and this shell ended with it.',
|
|
});
|
|
await settle();
|
|
expect(tab.session.live, isFalse);
|
|
expect(tab.info.endedText, contains('ended with it'));
|
|
},
|
|
);
|
|
|
|
test('typed input and the laid-out size go to the server', () async {
|
|
mac.add('term_a');
|
|
hub.activate();
|
|
await settle();
|
|
await settle();
|
|
final tab = hub.tabs.single;
|
|
tab.session.terminal.textInput('ls');
|
|
tab.session.terminal.keyInput(TerminalKey.enter);
|
|
await settle();
|
|
expect(
|
|
mac.bodies('terminal_input').map((body) => body['data']).join(),
|
|
'ls\r',
|
|
);
|
|
});
|
|
|
|
test('several machines: New terminal opens on the chosen one, tabs name '
|
|
'their machine', () async {
|
|
final linux = TerminalGateway();
|
|
machines.value = [
|
|
machine('mac', mac, name: 'Mac'),
|
|
machine('linux', linux, name: 'Linux'),
|
|
];
|
|
hub.activate();
|
|
await settle();
|
|
expect(hub.multiMachine, isTrue);
|
|
expect(await hub.open(machineId: 'linux'), isNull);
|
|
expect(linux.bodies('terminal_open'), hasLength(1));
|
|
expect(mac.bodies('terminal_open'), isEmpty);
|
|
expect(hub.selected!.machineName, 'Linux');
|
|
expect(await hub.open(machineId: 'gone'), contains('No machine'));
|
|
await linux.close();
|
|
});
|
|
|
|
test('a refused open is kept for the surface to show', () async {
|
|
mac.refuse['terminal_open'] = 'this machine already has 8 open terminals';
|
|
expect(await hub.open(), contains('8 open terminals'));
|
|
expect(hub.error, contains('8 open terminals'));
|
|
expect(hub.tabs, isEmpty);
|
|
});
|
|
}
|