import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/conversation/chat_escape_stop.dart'; void main() { group('ChatEscapeStopTracker', () { test('a second press inside the window completes a pair', () { final tracker = ChatEscapeStopTracker(); final start = DateTime(2026, 11, 12, 10); expect(tracker.press(start), isFalse); expect( tracker.press(start.add(const Duration(milliseconds: 900))), isTrue, ); }); test('a press after the window starts a new pair', () { final tracker = ChatEscapeStopTracker(); final start = DateTime(2026, 11, 12, 10); expect(tracker.press(start), isFalse); expect(tracker.press(start.add(const Duration(seconds: 2))), isFalse); expect( tracker.press(start.add(const Duration(seconds: 2, milliseconds: 500))), isTrue, ); }); test('reset forgets a pending first press', () { final tracker = ChatEscapeStopTracker(); final start = DateTime(2026, 11, 12, 10); tracker.press(start); tracker.reset(); expect( tracker.press(start.add(const Duration(milliseconds: 100))), isFalse, ); }); }); group('ChatEscapeStop', () { late DateTime now; late int stops; setUp(() { now = DateTime(2026, 11, 12, 10); stops = 0; }); Widget host({required bool running, Widget? child}) => MaterialApp( home: Scaffold( body: ChatEscapeStop( running: running, onStop: () => stops++, clock: () => now, child: child ?? const TextField(key: Key('field')), ), ), ); testWidgets('a single Escape shows the hint and does not stop', ( tester, ) async { await tester.pumpWidget(host(running: true)); await tester.tap(find.byKey(const Key('field'))); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); expect(stops, 0); expect(find.text(chatEscapeStopHint), findsOneWidget); }); testWidgets('two Escapes within a second stop the turn once', ( tester, ) async { await tester.pumpWidget(host(running: true)); await tester.tap(find.byKey(const Key('field'))); await tester.sendKeyEvent(LogicalKeyboardKey.escape); now = now.add(const Duration(milliseconds: 400)); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); expect(stops, 1); expect(find.text(chatEscapeStopHint), findsNothing); }); testWidgets('two Escapes more than a second apart do not stop', ( tester, ) async { await tester.pumpWidget(host(running: true)); await tester.tap(find.byKey(const Key('field'))); await tester.sendKeyEvent(LogicalKeyboardKey.escape); now = now.add(const Duration(milliseconds: 1500)); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); expect(stops, 0); }); testWidgets('an idle chat ignores Escape', (tester) async { await tester.pumpWidget(host(running: false)); await tester.tap(find.byKey(const Key('field'))); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); expect(stops, 0); expect(find.text(chatEscapeStopHint), findsNothing); }); testWidgets('focus on the transcript counts the same as the composer', ( tester, ) async { await tester.pumpWidget( host( running: true, child: const Focus( autofocus: true, child: SizedBox(width: 10, height: 10), ), ), ); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); expect(stops, 1); }); testWidgets('an Escape a descendant already handled never counts', ( tester, ) async { await tester.pumpWidget( host( running: true, child: Focus( autofocus: true, onKeyEvent: (_, event) => event.logicalKey == LogicalKeyboardKey.escape ? KeyEventResult.handled : KeyEventResult.ignored, child: const SizedBox(width: 10, height: 10), ), ), ); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); expect(stops, 0); expect(find.text(chatEscapeStopHint), findsNothing); }); testWidgets('a held Escape is one press, not a pair', (tester) async { await tester.pumpWidget( host( running: true, child: const Focus( autofocus: true, child: SizedBox(width: 10, height: 10), ), ), ); await tester.pump(); await tester.sendKeyDownEvent(LogicalKeyboardKey.escape); await tester.sendKeyRepeatEvent(LogicalKeyboardKey.escape); await tester.sendKeyRepeatEvent(LogicalKeyboardKey.escape); await tester.sendKeyUpEvent(LogicalKeyboardKey.escape); await tester.pump(); expect(stops, 0); }); }); }