`forkpty` calls `fork()` underneath. `fork()` in a multithreaded process is unsafe: only the calling thread survives in the child, but libc locks held by other threads remain "locked forever." With the multi-threaded Dart VM as parent, ~5% of spawns deadlocked in the child before `execve` (forensic probe: child stuck in S state with comm=`DartWorker`, master fd never sees POLLIN). `posix_spawn` uses `vfork` on glibc/musl/macOS, keeping the parent suspended until execve completes — no Dart code runs in the child. Pty pair built via the POSIX-standard `posix_openpt` / `grantpt` / `unlockpt` / `ptsname` sequence. Probed: zero hangs in 300 sequential spawns vs ~5% before. Behavior change: missing executable / missing workingDirectory now surface as a `PtyException` thrown by `NativePty.start` rather than a diagnostic written from the child to the slave PTY. Cleaner error path for callers. Side benefit: drops the `libutil.so.1` dynamic-library dependency. PTY now resolves entirely against libc via `DynamicLibrary.process()`. Splits the library-level `@Tags(['forkpty'])` on session_test.dart into a per-test tag, so the now-runnable-under-flutter-test cases contribute to coverage. `dart_test.yaml` declares the tag so the exclude-tags filters honor it. Drops the `retry: 2` workaround from the formerly-flaky registry test. D-5 amended. Trims session-introduced CHANGELOG entries that were over-verbose for the Keep-a-Changelog format. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
119 lines
3.9 KiB
Dart
119 lines
3.9 KiB
Dart
/// Unit tests for [PaneRegistry].
|
|
///
|
|
/// Exercises spawn / list / write / resize / close against the real
|
|
/// NativePty (forkpty via FFI). Events are captured via
|
|
/// [RecordingEventSink].
|
|
library;
|
|
|
|
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:clide/clide.dart';
|
|
import 'package:clide/src/panes/registry.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
void main() {
|
|
if (!Platform.isLinux && !Platform.isMacOS) return;
|
|
|
|
group('PaneRegistry', () {
|
|
late RecordingEventSink sink;
|
|
late PaneRegistry registry;
|
|
|
|
setUp(() {
|
|
sink = RecordingEventSink();
|
|
registry = PaneRegistry(events: sink);
|
|
});
|
|
|
|
tearDown(() => registry.shutdown());
|
|
|
|
test('spawn → emits pane.spawned and lists the pane', () async {
|
|
final pane = await registry.spawn(
|
|
kind: PaneKind.terminal,
|
|
argv: const ['/bin/echo', 'hi'],
|
|
);
|
|
|
|
expect(pane.id, startsWith('p_'));
|
|
expect(pane.kind, PaneKind.terminal);
|
|
expect(registry.panes, contains(pane));
|
|
expect(sink.ofKind('pane.spawned'), hasLength(1));
|
|
final evt = sink.ofKind('pane.spawned').first;
|
|
expect(evt.data['id'], pane.id);
|
|
});
|
|
|
|
test('output events base64-encode the child bytes', tags: ['forkpty'], () async {
|
|
await registry.spawn(
|
|
kind: PaneKind.terminal,
|
|
// Child writes then lingers so the reader's poll has a wide
|
|
// window to see POLLIN before HUP.
|
|
argv: const ['/bin/sh', '-c', 'printf hello-panes; sleep 0.25'],
|
|
);
|
|
|
|
final deadline = DateTime.now().add(const Duration(seconds: 2));
|
|
String decoded() => sink.ofKind('pane.output').map((e) => utf8.decode(base64Decode(e.data['bytes_b64']! as String))).join();
|
|
while (!decoded().contains('hello-panes') && DateTime.now().isBefore(deadline)) {
|
|
await Future<void>.delayed(const Duration(milliseconds: 25));
|
|
}
|
|
|
|
expect(sink.ofKind('pane.output'), isNotEmpty);
|
|
expect(decoded(), contains('hello-panes'));
|
|
});
|
|
|
|
test('write + resize emit no spurious events, update state', () async {
|
|
final pane = await registry.spawn(
|
|
kind: PaneKind.terminal,
|
|
argv: const ['/bin/cat'],
|
|
);
|
|
|
|
final writeCount = registry.write(pane.id, utf8.encode('abc'));
|
|
expect(writeCount, greaterThan(0));
|
|
|
|
registry.resize(pane.id, cols: 120, rows: 40);
|
|
final resized = sink.ofKind('pane.resized').toList();
|
|
expect(resized, hasLength(1));
|
|
expect(resized.single.data['cols'], 120);
|
|
expect(resized.single.data['rows'], 40);
|
|
});
|
|
|
|
test('close is idempotent + emits pane.closed once', () async {
|
|
final pane = await registry.spawn(
|
|
kind: PaneKind.terminal,
|
|
argv: const ['/bin/cat'],
|
|
);
|
|
|
|
await registry.close(pane.id);
|
|
await registry.close(pane.id); // second call: no-op
|
|
|
|
expect(registry.get(pane.id), isNull);
|
|
expect(sink.ofKind('pane.closed'), hasLength(1));
|
|
});
|
|
|
|
test('close on unknown id does nothing', () async {
|
|
await registry.close('p_nonexistent');
|
|
expect(sink.ofKind('pane.closed'), isEmpty);
|
|
});
|
|
|
|
test('claude kind round-trips on the wire', () async {
|
|
final pane = await registry.spawn(
|
|
kind: PaneKind.claude,
|
|
argv: const ['/bin/sh', '-c', 'exit 0'],
|
|
);
|
|
expect(pane.kind, PaneKind.claude);
|
|
expect(pane.toJson()['kind'], 'claude');
|
|
});
|
|
});
|
|
|
|
group('RecordingEventSink filters', () {
|
|
test('ofSubsystem narrows events to a single subsystem', () {
|
|
final s = RecordingEventSink();
|
|
final ts = DateTime.now().toUtc();
|
|
s.emit(IpcEvent(subsystem: 'pane', kind: 'spawned', timestamp: ts, data: const {}));
|
|
s.emit(IpcEvent(subsystem: 'git', kind: 'changed', timestamp: ts, data: const {}));
|
|
s.emit(IpcEvent(subsystem: 'pane', kind: 'closed', timestamp: ts, data: const {}));
|
|
expect(s.ofSubsystem('pane'), hasLength(2));
|
|
expect(s.ofSubsystem('git'), hasLength(1));
|
|
expect(s.ofSubsystem('files'), isEmpty);
|
|
});
|
|
});
|
|
}
|