Files
pipelines/app/test/features/terminal/terminal_fixtures.dart
T
alan a792b30513 Open one login terminal on first show; toggling only hides it
The first show of an app run starts one terminal (the machine's login
prompt) unless one is running; later toggles never open another. Drop
the tmux field and restart note, and update the Settings copy and the
terminal notes.
2026-11-10 00:42:59 +00:00

151 lines
4.5 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/terminal/terminal_layout.dart';
import 'package:pipelines_app/features/terminal/terminal_machines.dart';
/// One machine's terminals as a server holds them, answering the terminal
/// requests and pushing frames like the real one.
class TerminalGateway implements ApiGateway {
TerminalGateway({this.stream = 's1'});
final _frames = StreamController<JsonMap>.broadcast();
final List<JsonMap> requests = [];
final Map<String, JsonMap> terminals = {};
final Map<String, String> output = {};
String stream;
int _next = 0;
/// When set, requests of this type fail with this message.
final Map<String, String> refuse = {};
@override
Stream<JsonMap> get frames => _frames.stream;
List<JsonMap> bodies(String type) => [
for (final request in requests)
if (request['type'] == type)
Map<String, dynamic>.from(request['terminal_request'] as Map? ?? {}),
];
JsonMap add(String id, {String cwd = '/Users/op', String text = ''}) {
final record = <String, dynamic>{
'id': id,
'cwd': cwd,
'shell': '/bin/zsh',
'status': 'running',
'cols': 80,
'rows': 24,
'created_at': 1,
};
terminals[id] = record;
output[id] = text;
return record;
}
JsonMap _record(String id, {bool withOutput = false, int after = 0}) {
final text = output[id] ?? '';
final bytes = utf8.encode(text);
final start = after.clamp(0, bytes.length);
return {
...terminals[id]!,
'stream': stream,
'output_end': bytes.length,
if (withOutput) 'output': utf8.decode(bytes.sublist(start)),
if (withOutput) 'output_start': start,
};
}
/// The shell writes [text]: it is retained and pushed.
void write(String id, String text, {bool push = true}) {
final offset = utf8.encode(output[id] ?? '').length;
output[id] = (output[id] ?? '') + text;
if (push) {
pushOutput(id, text, offset);
}
}
void pushOutput(String id, String text, int offset) => _frames.add({
'type': 'terminal_event',
'terminal_event': {
'terminal_id': id,
'output': text,
'offset': offset,
'end': offset + utf8.encode(text).length,
},
});
void lifecycle(String id, Map<String, dynamic> changes) {
terminals[id] = {...terminals[id]!, ...changes};
_frames.add({
'type': 'terminal_event',
'terminal_event': {'terminal_id': id, 'terminal': _record(id)},
});
}
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
requests.add({'type': type, ...fields});
final refusal = refuse[type];
if (refusal != null) throw StateError(refusal);
final body = (fields['terminal_request'] as Map?) ?? const {};
final id = '${body['terminal_id'] ?? ''}';
switch (type) {
case 'terminal_list':
return {
'type': type,
'terminals': [for (final id in terminals.keys) _record(id)],
};
case 'terminal_open':
final next = 'term_${++_next}';
final cwd = '${body['cwd'] ?? ''}';
add(next, cwd: cwd.isEmpty ? '/Users/op' : cwd, text: r'$ ');
return {'type': type, 'terminal': _record(next)};
case 'terminal_output':
return {
'type': type,
'terminal': _record(
id,
withOutput: true,
after: (body['after'] as num?)?.toInt() ?? 0,
),
};
case 'terminal_close':
final record = {..._record(id), 'status': 'closed'};
terminals.remove(id);
return {'type': type, 'terminal': record};
case 'terminal_input':
return {'type': type};
case 'terminal_resize':
return {'type': type, 'terminal': _record(id)};
}
throw StateError('unexpected $type');
}
Future<void> close() => _frames.close();
}
/// A machine list tests change by hand.
class TestMachines extends ValueNotifier<List<TerminalMachine>> {
TestMachines(super.value);
}
TerminalMachine machine(String id, TerminalGateway gateway, {String? name}) =>
TerminalMachine(id: id, name: name ?? id, gateway: gateway);
/// A layout store kept in memory.
class MemoryLayoutStore implements TerminalLayoutStore {
String? saved;
int saves = 0;
@override
Future<String?> load() async => saved;
@override
Future<bool> save(String value) async {
saved = value;
saves++;
return true;
}
}