Merge branch 'worktree-agent-ae487572986d7a387' into integrate/chat-ntfy-attachments
This commit is contained in:
@@ -215,7 +215,13 @@ void main() {
|
||||
await tester.tap(find.byKey(const Key('chat-composer-attach')));
|
||||
await tester.pumpAndSettle();
|
||||
await typeAndSend(tester, 'hello');
|
||||
expect(find.text('screen.png'), findsOneWidget);
|
||||
// The sent image draws from its own inline bytes as a thumbnail named
|
||||
// for the file; the text file stays a named row.
|
||||
expect(
|
||||
find.byKey(const Key('chat-attachment-thumbnail')),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.bySemanticsLabel('screen.png'), findsOneWidget);
|
||||
expect(find.text('notes.txt'), findsOneWidget);
|
||||
expect(find.textContaining('data'), findsNothing);
|
||||
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_attachment_image.dart';
|
||||
|
||||
void main() {
|
||||
test('image type comes from MIME, data: URI or extension', () {
|
||||
expect(
|
||||
chatPartImageMime({'type': 'file', 'mime': 'image/PNG', 'uri': 'x'}),
|
||||
'image/png',
|
||||
);
|
||||
expect(
|
||||
chatPartImageMime({'type': 'file', 'uri': 'data:image/jpeg;base64,AA'}),
|
||||
'image/jpeg',
|
||||
);
|
||||
expect(
|
||||
chatPartImageMime({'type': 'file', 'name': 'a.WEBP', 'uri': 'file:///a'}),
|
||||
'image/webp',
|
||||
);
|
||||
expect(
|
||||
chatPartImageMime({'type': 'file', 'mime': 'image/svg+xml', 'uri': 'a'}),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
chatPartImageMime({
|
||||
'type': 'file',
|
||||
'mime': 'text/plain',
|
||||
'name': 'a.png',
|
||||
}),
|
||||
isNull,
|
||||
);
|
||||
expect(chatPartImageMime({'type': 'text', 'mime': 'image/png'}), isNull);
|
||||
});
|
||||
|
||||
test('only inline, stored and uploaded URIs are loaded', () {
|
||||
expect(
|
||||
chatImageSourceOf('data:image/png;base64,AA'),
|
||||
ChatImageSource.inline,
|
||||
);
|
||||
expect(chatImageSourceOf('file:///s/up-1-a.png'), ChatImageSource.stored);
|
||||
expect(chatImageSourceOf('upload:up-1-a.png'), ChatImageSource.upload);
|
||||
expect(chatImageSourceOf('https://x.test/a.png'), isNull);
|
||||
expect(chatImageSourceOf('a.png'), isNull);
|
||||
});
|
||||
|
||||
test('optimistic URIs and stored names line up with the recorded turn', () {
|
||||
expect(
|
||||
chatOptimisticAttachmentUri({
|
||||
'name': 'a b.png',
|
||||
'upload': 'up-1-a b.png',
|
||||
}),
|
||||
'upload:up-1-a b.png',
|
||||
);
|
||||
expect(
|
||||
chatOptimisticAttachmentUri({
|
||||
'name': 'a.png',
|
||||
'mime': 'image/png',
|
||||
'data': 'AA',
|
||||
}),
|
||||
'data:image/png;base64,AA',
|
||||
);
|
||||
expect(chatOptimisticAttachmentUri({'name': 'a.png'}), 'a.png');
|
||||
expect(chatStoredFileName('upload:up-1-a b.png'), 'up-1-a b.png');
|
||||
expect(chatStoredFileName('file:///s/up-1-a%20b.png'), 'up-1-a b.png');
|
||||
});
|
||||
|
||||
test('data: URIs decode, malformed ones do not', () {
|
||||
expect(chatDataUriBytes('data:image/png;base64,AAEC'), [0, 1, 2]);
|
||||
expect(chatDataUriBytes('data:image/png,plain'), isNull);
|
||||
expect(chatDataUriBytes('data:image/png;base64,%%%'), isNull);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
import 'dart:convert';
|
||||
|
||||
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/core/transport/connection_state.dart';
|
||||
import 'package:pipelines_app/features/chat/data/chat_attachment_images.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
|
||||
|
||||
/// A 1x1 PNG.
|
||||
const String _png =
|
||||
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==';
|
||||
const String _stored =
|
||||
'file:///home/op/.pipelines-chat-attachments/up-1-shot.png';
|
||||
|
||||
class _ImageGateway implements ApiGateway {
|
||||
final calls = <JsonMap>[];
|
||||
Object? failure;
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
expect(type, 'chat_attachment_image');
|
||||
calls.add(fields);
|
||||
final failure = this.failure;
|
||||
if (failure != null) throw failure;
|
||||
final uri = (fields['chat_attachment_image_request'] as Map)['uri'];
|
||||
return {
|
||||
'type': 'chat_attachment_image',
|
||||
'chat_attachment_image': {'uri': uri, 'mime': 'image/png', 'data': _png},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
JsonMap _user(List<JsonMap> parts) => transcriptRows([
|
||||
{'id': 'u1', 'role': 'user', 'parts': parts},
|
||||
]).single;
|
||||
|
||||
Widget _host(
|
||||
JsonMap row,
|
||||
ChatAttachmentImages? images, {
|
||||
bool cupertino = false,
|
||||
}) {
|
||||
final body = ListView(
|
||||
children: [ChatMessageView(row: row, images: images)],
|
||||
);
|
||||
if (cupertino) {
|
||||
return CupertinoApp(home: CupertinoPageScaffold(child: body));
|
||||
}
|
||||
return MaterialApp(
|
||||
theme: ThemeData(platform: TargetPlatform.android),
|
||||
home: Scaffold(body: body),
|
||||
);
|
||||
}
|
||||
|
||||
final _thumbnail = find.byKey(const Key('chat-attachment-thumbnail'));
|
||||
final _viewerImage = find.byKey(const Key('chat-image-viewer-image'));
|
||||
|
||||
void main() {
|
||||
for (final cupertino in [false, true]) {
|
||||
final style = cupertino ? 'Cupertino' : 'Material';
|
||||
|
||||
testWidgets(
|
||||
'$style: an inline data: image draws a thumbnail above the text '
|
||||
'and opens the full-screen viewer',
|
||||
(tester) async {
|
||||
final handle = tester.ensureSemantics();
|
||||
await tester.pumpWidget(
|
||||
_host(
|
||||
_user([
|
||||
{'type': 'text', 'text': 'See this'},
|
||||
{
|
||||
'type': 'file',
|
||||
'name': 'image',
|
||||
'mime': 'image/png',
|
||||
'uri': 'data:image/png;base64,$_png',
|
||||
},
|
||||
]),
|
||||
null,
|
||||
cupertino: cupertino,
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(_thumbnail, findsOneWidget);
|
||||
expect(
|
||||
find.byKey(const Key('chat-attachment-thumbnail-image')),
|
||||
findsOneWidget,
|
||||
);
|
||||
// No paperclip row and no base64 shown as a file name.
|
||||
expect(find.byIcon(Icons.attach_file), findsNothing);
|
||||
expect(find.byIcon(CupertinoIcons.paperclip), findsNothing);
|
||||
expect(find.textContaining('base64'), findsNothing);
|
||||
expect(
|
||||
tester.getTopLeft(_thumbnail).dy,
|
||||
lessThan(tester.getTopLeft(find.text('See this')).dy),
|
||||
);
|
||||
expect(
|
||||
tester.getSemantics(_thumbnail.first),
|
||||
matchesSemantics(
|
||||
label: 'image',
|
||||
isButton: true,
|
||||
isImage: true,
|
||||
hasTapAction: true,
|
||||
),
|
||||
);
|
||||
handle.dispose();
|
||||
|
||||
await tester.tap(_thumbnail);
|
||||
await tester.pumpAndSettle();
|
||||
expect(_viewerImage, findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-image-viewer-zoom')), findsOneWidget);
|
||||
await tester.tap(find.byKey(const Key('chat-image-viewer-close')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(_viewerImage, findsNothing);
|
||||
expect(_thumbnail, findsOneWidget);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('a stored file:// image is fetched once through the gateway and '
|
||||
'reused when the row is rebuilt', (tester) async {
|
||||
final gateway = _ImageGateway();
|
||||
final images = ChatAttachmentImages(gateway);
|
||||
// Codex records no MIME: the name's extension makes it an image.
|
||||
final row = _user([
|
||||
{'type': 'file', 'name': 'shot.png', 'uri': _stored},
|
||||
]);
|
||||
await tester.pumpWidget(_host(row, images));
|
||||
expect(
|
||||
find.byKey(const Key('chat-attachment-thumbnail-loading')),
|
||||
findsOneWidget,
|
||||
);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(_thumbnail, findsOneWidget);
|
||||
expect(gateway.calls, [
|
||||
{
|
||||
'chat_attachment_image_request': {'uri': _stored},
|
||||
},
|
||||
]);
|
||||
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
await tester.pumpWidget(_host(row, images));
|
||||
expect(_thumbnail, findsOneWidget);
|
||||
expect(gateway.calls, hasLength(1));
|
||||
|
||||
await tester.tap(_thumbnail);
|
||||
await tester.pumpAndSettle();
|
||||
expect(_viewerImage, findsOneWidget);
|
||||
expect(find.text('shot.png'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('an expired stored image becomes a labelled placeholder', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = _ImageGateway()
|
||||
..failure = const ProtocolError('image is no longer available');
|
||||
final images = ChatAttachmentImages(gateway);
|
||||
final row = _user([
|
||||
{'type': 'text', 'text': 'Old screenshot'},
|
||||
{'type': 'file', 'name': 'shot.png', 'mime': 'image/png', 'uri': _stored},
|
||||
]);
|
||||
await tester.pumpWidget(_host(row, images));
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(find.text(chatImageGoneMessage), findsOneWidget);
|
||||
expect(_thumbnail, findsNothing);
|
||||
expect(find.text('Old screenshot'), findsOneWidget);
|
||||
|
||||
// The refusal is settled: rebuilding does not ask again.
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
await tester.pumpWidget(_host(row, images));
|
||||
expect(find.text(chatImageGoneMessage), findsOneWidget);
|
||||
expect(gateway.calls, hasLength(1));
|
||||
});
|
||||
|
||||
testWidgets('non-image files and remote image URLs keep the file row', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = _ImageGateway();
|
||||
await tester.pumpWidget(
|
||||
_host(
|
||||
_user([
|
||||
{'type': 'text', 'text': 'Files'},
|
||||
{
|
||||
'type': 'file',
|
||||
'name': 'report.pdf',
|
||||
'mime': 'application/pdf',
|
||||
'uri':
|
||||
'file:///home/op/.pipelines-chat-attachments/up-2-report.pdf',
|
||||
},
|
||||
{'type': 'file', 'name': 'remote.png', 'uri': 'https://x.test/a.png'},
|
||||
{
|
||||
'type': 'file',
|
||||
'name': 'logo.svg',
|
||||
'mime': 'image/svg+xml',
|
||||
'uri': 'file:///home/op/.pipelines-chat-attachments/up-3-logo.svg',
|
||||
},
|
||||
]),
|
||||
ChatAttachmentImages(gateway),
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(_thumbnail, findsNothing);
|
||||
expect(find.text('report.pdf'), findsOneWidget);
|
||||
expect(find.text('remote.png'), findsOneWidget);
|
||||
expect(find.text('logo.svg'), findsOneWidget);
|
||||
expect(find.byIcon(Icons.attach_file), findsNWidgets(3));
|
||||
expect(gateway.calls, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('a just-uploaded image draws from the composer bytes before and '
|
||||
'after the server records the turn', (tester) async {
|
||||
final gateway = _ImageGateway();
|
||||
final images = ChatAttachmentImages(gateway)
|
||||
..rememberUpload('up-1-shot.png', base64Decode(_png));
|
||||
await tester.pumpWidget(
|
||||
_host(
|
||||
_user([
|
||||
{
|
||||
'type': 'file',
|
||||
'name': 'shot.png',
|
||||
'mime': 'image/png',
|
||||
'uri': 'upload:up-1-shot.png',
|
||||
},
|
||||
]),
|
||||
images,
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(_thumbnail, findsOneWidget);
|
||||
await tester.pumpWidget(
|
||||
_host(
|
||||
_user([
|
||||
{
|
||||
'type': 'file',
|
||||
'name': 'shot.png',
|
||||
'mime': 'image/png',
|
||||
'uri': _stored,
|
||||
},
|
||||
]),
|
||||
images,
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(_thumbnail, findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user