feat(chat): show sent images as thumbnails with a full-screen viewer

Operator-attached images render as rounded thumbnails above the user
bubble and open a zoomable Material/Cupertino viewer. Inline data: URIs
decode locally; file:// attachment-store URIs load through the new
chat_attachment_image websocket request, which serves only regular image
files directly inside the private attachment store (os.Root, <= 4 MiB,
sniffed raster types). Missing files show an 'Image no longer available'
placeholder; other files keep the file row.
This commit is contained in:
2026-10-30 20:53:03 +00:00
parent 44b2cff347
commit 8c421952db
18 changed files with 1397 additions and 29 deletions
@@ -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);
});
}