247 lines
7.2 KiB
Dart
247 lines
7.2 KiB
Dart
import 'dart:async';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/core/api_gateway.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_composer_bar.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_composer_controller.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
|
|
import 'package:pipelines_app/features/chat/domain/chat_attachment_draft.dart';
|
|
import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart';
|
|
|
|
import 'chat_composer_bar_fixtures.dart';
|
|
|
|
/// A picker whose selections are supplied by the test, one call at a time.
|
|
class _FakePicker {
|
|
_FakePicker(this.results);
|
|
|
|
final List<List<ChatPickedFile>?> results;
|
|
int calls = 0;
|
|
|
|
ChatAttachmentPicker get picker => ChatAttachmentPicker(
|
|
select: () async {
|
|
calls += 1;
|
|
return results.isEmpty ? null : results.removeAt(0);
|
|
},
|
|
);
|
|
}
|
|
|
|
ChatPickedFile _file(String name, {int size = 3, String mime = 'text/plain'}) =>
|
|
ChatPickedFile(name: name, mime: mime, bytes: Uint8List(size));
|
|
|
|
Widget _host(Widget bar, {required bool cupertino}) => cupertino
|
|
? CupertinoApp(home: CupertinoPageScaffold(child: bar))
|
|
: materialHost(bar);
|
|
|
|
void main() {
|
|
late ComposerBarGateway gateway;
|
|
late ChatComposerController composer;
|
|
late List<ChatComposerController> controllers;
|
|
|
|
setUp(() {
|
|
gateway = ComposerBarGateway();
|
|
controllers = [];
|
|
});
|
|
|
|
/// Created inside the test body so the controller's futures belong to the
|
|
/// widget tester's clock rather than to `setUp`'s.
|
|
ChatComposerController newComposer() {
|
|
final controller = ChatComposerController(
|
|
gateway,
|
|
's',
|
|
drafts: MemoryChatDraftStore(),
|
|
);
|
|
controllers.add(controller);
|
|
composer = controller;
|
|
return controller;
|
|
}
|
|
|
|
tearDown(() {
|
|
for (final controller in controllers) {
|
|
controller.dispose();
|
|
}
|
|
gateway.close();
|
|
});
|
|
|
|
Future<void> pumpBar(
|
|
WidgetTester tester, {
|
|
ChatAttachmentPicker? picker,
|
|
bool cupertino = false,
|
|
}) async {
|
|
newComposer();
|
|
await tester.pumpWidget(
|
|
_host(
|
|
ChatComposerBar(composer: composer, running: false, picker: picker),
|
|
cupertino: cupertino,
|
|
),
|
|
);
|
|
await tester.pump();
|
|
}
|
|
|
|
for (final cupertino in [false, true]) {
|
|
testWidgets('picking a file shows its recorded name and removes it '
|
|
'(${cupertino ? 'cupertino' : 'material'})', (tester) async {
|
|
final fake = _FakePicker([
|
|
[_file('notes.txt')],
|
|
]);
|
|
await pumpBar(tester, picker: fake.picker, cupertino: cupertino);
|
|
|
|
await tester.tap(find.byKey(const Key('chat-composer-attach')));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(fake.calls, 1);
|
|
expect(
|
|
find.byKey(const Key('chat-composer-attachments')),
|
|
findsOneWidget,
|
|
);
|
|
expect(find.text('notes.txt'), findsOneWidget);
|
|
|
|
if (cupertino) {
|
|
await tester.tap(
|
|
find.byKey(const Key('chat-composer-attachment-remove-0')),
|
|
);
|
|
} else {
|
|
await tester.tap(find.byTooltip('Remove notes.txt'));
|
|
}
|
|
await tester.pump();
|
|
|
|
expect(find.text('notes.txt'), findsNothing);
|
|
expect(find.byKey(const Key('chat-composer-attachments')), findsNothing);
|
|
});
|
|
}
|
|
|
|
testWidgets('a file over the draft budget shows the draft refusal line', (
|
|
tester,
|
|
) async {
|
|
final fake = _FakePicker([
|
|
[
|
|
_file(
|
|
'huge.bin',
|
|
size: ChatAttachmentDraft.maxTotalBytes + 1,
|
|
mime: 'application/octet-stream',
|
|
),
|
|
],
|
|
]);
|
|
await pumpBar(tester, picker: fake.picker);
|
|
|
|
await tester.tap(find.byKey(const Key('chat-composer-attach')));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(
|
|
find.text(ChatAttachmentRefusal.totalTooLarge.message),
|
|
findsOneWidget,
|
|
);
|
|
expect(find.byKey(const Key('chat-composer-attachments')), findsNothing);
|
|
expect(find.text('huge.bin'), findsNothing);
|
|
});
|
|
|
|
testWidgets('attachments survive a normal parent rebuild', (tester) async {
|
|
newComposer();
|
|
final fake = _FakePicker([
|
|
[_file('keep me.pdf', mime: 'application/pdf')],
|
|
]);
|
|
var rebuilt = false;
|
|
late StateSetter rebuild;
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: Scaffold(
|
|
body: StatefulBuilder(
|
|
builder: (context, setState) {
|
|
rebuild = setState;
|
|
return ChatComposerBar(
|
|
composer: composer,
|
|
running: rebuilt,
|
|
picker: fake.picker,
|
|
);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
);
|
|
await tester.pump();
|
|
|
|
await tester.tap(find.byKey(const Key('chat-composer-attach')));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('keep me.pdf'), findsOneWidget);
|
|
|
|
rebuild(() => rebuilt = true);
|
|
await tester.pump();
|
|
|
|
expect(find.text('keep me.pdf'), findsOneWidget);
|
|
expect(find.byKey(const Key('chat-composer-attachments')), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('the attach control is disabled while a prompt is in flight', (
|
|
tester,
|
|
) async {
|
|
final reply = Completer<JsonMap>();
|
|
gateway.prompt = (_, _) => reply.future;
|
|
final fake = _FakePicker([
|
|
[_file('notes.txt')],
|
|
]);
|
|
await pumpBar(tester, picker: fake.picker);
|
|
|
|
IconButton attach() => tester.widget<IconButton>(
|
|
find.byKey(const Key('chat-composer-attach')),
|
|
);
|
|
expect(attach().onPressed, isNotNull);
|
|
|
|
await tester.enterText(
|
|
find.byKey(const Key('chat-composer-field')),
|
|
'hello',
|
|
);
|
|
await tester.pumpAndSettle();
|
|
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(attach().onPressed, isNull);
|
|
expect(gateway.promptBodies, hasLength(1));
|
|
|
|
reply.complete(const {});
|
|
await tester.pumpAndSettle();
|
|
expect(attach().onPressed, isNotNull);
|
|
});
|
|
|
|
testWidgets('the attach control is disabled without a host picker', (
|
|
tester,
|
|
) async {
|
|
await pumpBar(tester);
|
|
|
|
final attach = tester.widget<IconButton>(
|
|
find.byKey(const Key('chat-composer-attach')),
|
|
);
|
|
expect(attach.onPressed, isNull);
|
|
});
|
|
|
|
for (final cupertino in [false, true]) {
|
|
testWidgets('a long attachment name fits a narrow 2x composer '
|
|
'(${cupertino ? 'cupertino' : 'material'})', (tester) async {
|
|
tester.view.physicalSize = const Size(640, 1200);
|
|
tester.view.devicePixelRatio = 2;
|
|
tester.platformDispatcher.textScaleFactorTestValue = 2;
|
|
addTearDown(tester.view.resetPhysicalSize);
|
|
addTearDown(tester.view.resetDevicePixelRatio);
|
|
addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);
|
|
|
|
final name =
|
|
'a-very-long-recorded-attachment-name-that-must-not-overflow.pdf';
|
|
final fake = _FakePicker([
|
|
[_file(name)],
|
|
]);
|
|
await pumpBar(tester, picker: fake.picker, cupertino: cupertino);
|
|
|
|
await tester.tap(find.byKey(const Key('chat-composer-attach')));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(tester.takeException(), isNull);
|
|
expect(find.text(name), findsOneWidget);
|
|
expect(
|
|
find.byKey(const Key('chat-composer-attachments')),
|
|
findsOneWidget,
|
|
);
|
|
});
|
|
}
|
|
}
|