import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'chat_composer_bar_fixtures.dart'; /// Counts rebuilds of the composer's own controls (its attach button stands /// for the whole bar, Send for the usage ring around it): typing and streamed /// frames must leave them alone. int _barBuilds = 0; const _controls = [Key('chat-composer-attach'), Key('chat-composer-primary')]; void _countBar(Element element, bool builtOnce) { if (_controls.contains(element.widget.key)) _barBuilds++; } void main() { late ComposerBarGateway gateway; setUp(() { gateway = ComposerBarGateway()..page = fixtureTurn(); _barBuilds = 0; }); tearDown(() { debugOnRebuildDirtyWidget = null; gateway.close(); }); testWidgets('typing rebuilds the bar only when its controls change', ( tester, ) async { await tester.pumpWidget(composerThread(gateway)); await tester.pumpAndSettle(); final field = find.byKey(const Key('chat-composer-field')); debugOnRebuildDirtyWidget = _countBar; // The first character enables Send: one rebuild of each control. await tester.enterText(field, 'h'); await tester.pump(); expect(_barBuilds, 2); // Further plain typing changes only the field's own text. for (final draft in ['he', 'hel', 'hell', 'hello', 'hello there']) { await tester.enterText(field, draft); await tester.pump(); } expect(_barBuilds, 2); // A `/` command opens the palette, which the bar draws. await tester.enterText(field, '/'); await tester.pump(); expect(_barBuilds, 4); await tester.enterText(field, ''); await tester.pump(); }); testWidgets('a streamed frame does not rebuild the bar', (tester) async { await tester.pumpWidget(composerThread(gateway)); await tester.pumpAndSettle(); debugOnRebuildDirtyWidget = _countBar; for (var i = 0; i < 6; i++) { // The first deltas start the turn, which the bar shows (its running // edge); later ones only grow the reply. if (i == 2) _barBuilds = 0; gateway.emitEvent({ 'session_id': 's', 'message_id': 'live', 'kind': 'text', 'part': {'id': 'live#text#0'}, 'delta': 'Streaming $i ', }); await tester.pump(); } expect(find.textContaining('Streaming'), findsOneWidget); expect(_barBuilds, 0); }); }