Chat perf: path-free running border, keystroke- and delta-quiet composer, spinners that do not relayout their list
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
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);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/composer_running_outline.dart';
|
||||
|
||||
/// Records the strokes a [RunningRRectOutline] draws.
|
||||
class _Recorder implements Canvas {
|
||||
final lines = <(Offset, Offset)>[];
|
||||
final arcs = <(Rect, double, double)>[];
|
||||
|
||||
@override
|
||||
void drawLine(Offset p1, Offset p2, Paint paint) => lines.add((p1, p2));
|
||||
|
||||
@override
|
||||
void drawArc(Rect rect, double start, double sweep, bool c, Paint paint) =>
|
||||
arcs.add((rect, start, sweep));
|
||||
|
||||
@override
|
||||
dynamic noSuchMethod(Invocation invocation) =>
|
||||
throw UnsupportedError('${invocation.memberName}');
|
||||
}
|
||||
|
||||
void main() {
|
||||
const shape = RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(24)),
|
||||
);
|
||||
|
||||
for (final size in const [Size(320, 48), Size(300, 100), Size(40, 30)]) {
|
||||
test('follows the path outline of a $size pill by arc length', () {
|
||||
final rect = Offset.zero & size;
|
||||
final outline = RunningRRectOutline.of(shape, rect)!;
|
||||
final metric = shape.getOuterPath(rect).computeMetrics().single;
|
||||
// Path metrics approximate curves to about a pixel over the lap.
|
||||
expect(outline.length, closeTo(metric.length, 1.5));
|
||||
for (var k = 0; k < 64; k++) {
|
||||
final d = metric.length * k / 64;
|
||||
final expected = metric.getTangentForOffset(d)!.position;
|
||||
expect(
|
||||
(outline.pointAt(d) - expected).distance,
|
||||
lessThan(1.5),
|
||||
reason: 'at $d of ${metric.length}',
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
test('draws a range as lines along edges and arcs round corners', () {
|
||||
final outline = RunningRRectOutline.of(
|
||||
shape,
|
||||
const Rect.fromLTWH(0, 0, 320, 48),
|
||||
)!;
|
||||
// A 320x48 pill with radius 24: no left straight, then the top-left
|
||||
// corner, then the top edge.
|
||||
final canvas = _Recorder();
|
||||
outline.draw(canvas, 10, 60, Paint());
|
||||
expect(canvas.arcs, hasLength(1));
|
||||
expect(
|
||||
canvas.arcs.single.$1,
|
||||
Rect.fromCircle(center: const Offset(24, 24), radius: 24),
|
||||
);
|
||||
expect(canvas.arcs.single.$2, closeTo(math.pi + 10 / 24, 1e-9));
|
||||
expect(canvas.lines, hasLength(1));
|
||||
final (from, to) = canvas.lines.single;
|
||||
expect(from, const Offset(24, 0));
|
||||
expect(to.dx, closeTo(24 + 60 - 12 * math.pi, 1e-9));
|
||||
});
|
||||
|
||||
test('other shapes fall back to the measured path', () {
|
||||
expect(
|
||||
RunningRRectOutline.of(
|
||||
const StadiumBorder(),
|
||||
const Rect.fromLTWH(0, 0, 9, 9),
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
RunningRRectOutline.of(
|
||||
const RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.only(topLeft: Radius.circular(4)),
|
||||
),
|
||||
const Rect.fromLTWH(0, 0, 9, 9),
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user