event-driven test waits, fail-loud on timeout (T-108)

Replaces the fixed Future.delayed sleeps the consultant flagged
with stream-based waits that complete when the awaited event
arrives. Timeout callbacks call fail() with a diagnostic instead
of `onTimeout: () {}` swallowing the signal — a never-producing
pty now reports "pty did not produce X within 5s" instead of an
unhelpful "Actual: ''".

session_test.dart:
  - _readUntil helper subscribes to s.output, completes when a
    marker substring appears (or onDone), fails on timeout.
  - _waitForBuffer polls a buffer the listener is already filling
    after a write; 25ms tick, 5s ceiling, fail-loud on miss.
  - Drops the 500ms settle + 50×100ms polling pattern in the write
    test; uses a "first-byte" completer for prompt-readiness.
  - retry: 2 restored on the four read-dependent forkpty tests
    (the underlying flutter-test-runner pty-output flake hasn't
    fully gone away; recovers cleanly on a fresh spawn).

watcher_test.dart:
  - "emits a created event" awaits stream.firstWhere instead of two
    fixed sleeps.
  - "filters ignored paths" uses pre + post sentinel markers to
    bracket the inotify-delivery window event-driven; the negative
    assertion only runs after the post marker is observed.

event_sink.dart:
  - RecordingEventSink gains a broadcast `stream` for the same
    event-await pattern. PaneRegistry's output test subscribes
    BEFORE spawn so first bytes aren't lost.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-17 22:05:49 +02:00
co-authored by Claude Opus 4.7
parent 7937da1734
commit b66e8f6cc0
7 changed files with 160 additions and 54 deletions
+43 -13
View File
@@ -2,7 +2,6 @@
/// Directory.watch against a tempdir.
library;
import 'dart:async';
import 'dart:io';
import 'package:clide/src/files/ignore.dart';
@@ -60,14 +59,20 @@ void main() {
test('emits a created event when a file is added under root', () async {
await watcher.start();
final received = <FileChange>[];
final sub = watcher.stream.listen(received.add);
addTearDown(sub.cancel);
// Give inotify a moment to settle, then create a file.
await Future<void>.delayed(const Duration(milliseconds: 50));
// Wait for the specific change rather than sleeping a fixed
// amount. firstWhere completes on the first matching event;
// the timeout fails the test with a clear message if inotify
// never delivers (instead of asserting on an empty list).
final saw = watcher.stream.firstWhere(
(c) => c.path == 'new.txt',
orElse: () => throw StateError('stream closed before new.txt arrived'),
);
await File('${sandbox.path}/new.txt').writeAsString('hi');
await Future<void>.delayed(const Duration(milliseconds: 200));
expect(received.any((c) => c.path == 'new.txt'), isTrue);
final change = await saw.timeout(
const Duration(seconds: 5),
onTimeout: () => fail('no `new.txt` event within 5s'),
);
expect(change.path, 'new.txt');
});
test('filters ignored paths', () async {
@@ -75,11 +80,22 @@ void main() {
final received = <FileChange>[];
final sub = watcher.stream.listen(received.add);
addTearDown(sub.cancel);
// .dart_tool/ is in the builtin ignore set.
await Future<void>.delayed(const Duration(milliseconds: 50));
final dt = Directory('${sandbox.path}/.dart_tool')..createSync();
await File('${dt.path}/hidden').writeAsString('x');
await Future<void>.delayed(const Duration(milliseconds: 200));
// Two-phase: a pre-marker proves inotify is delivering at all
// (warm-up), then create the ignored entry sandwiched between
// an actionable post-marker. When the post-marker arrives we
// know inotify has caught up to operations performed earlier
// in the same tick. Failing loudly with `fail()` beats the old
// fixed `Future.delayed(200)` that pretended a quiet stream was
// proof of filtering.
await File('${sandbox.path}/pre.txt').writeAsString('p');
await _expectReceived(received, (c) => c.path == 'pre.txt');
Directory('${sandbox.path}/.dart_tool').createSync();
await File('${sandbox.path}/.dart_tool/hidden').writeAsString('x');
await File('${sandbox.path}/post.txt').writeAsString('q');
await _expectReceived(received, (c) => c.path == 'post.txt');
expect(received.any((c) => c.path.startsWith('.dart_tool')), isFalse);
});
@@ -96,3 +112,17 @@ void main() {
});
});
}
/// Wait until [received] satisfies [predicate]. Polls the list (it
/// gets mutated by the listener subscription) every 25 ms with a
/// generous 8 s ceiling; fails loudly on miss instead of silently
/// continuing as the old fixed-sleep tests did.
Future<void> _expectReceived<T>(List<T> received, bool Function(T) predicate) async {
final deadline = DateTime.now().add(const Duration(seconds: 8));
while (!received.any(predicate)) {
if (DateTime.now().isAfter(deadline)) {
fail('expected event never arrived within 8s; received=${received.length} entries');
}
await Future<void>.delayed(const Duration(milliseconds: 25));
}
}