Files
buzz/mobile/test/shared/widgets/avatar_image_test.dart
Tom Brow 90772a43e9 fix(mobile): keep avatars and images loading on slow links (#8879)
On a slow link, avatars in a channel could fail to appear entirely. Two
causes:

- **Duplicate downloads.** Each decode size is its own image-cache
entry, so an avatar shown at two sizes (a message row and its reply
summary) started two downloads of the same bytes, which then competed
for bandwidth. The in-flight download is now shared per URL and auth
service.
- **Full-size avatars.** Avatars a few dozen points wide downloaded the
original upload, which can be megabytes. The relay generates a JPEG
thumbnail of at most 320px for every uploaded image, and desktop already
loads it for small avatars. Mobile now does the same for any image
format when an avatar is at most 240 physical pixels on each side. About
88% of relay-hosted avatars on the internal relay are PNG, with a median
size of 337 KB. As on desktop, the thumbnail is a still JPEG:
transparent areas render black and animation is lost. Rendered side by
side, mobile and desktop look the same. If the relay returns 404 for a
thumbnail (older or externally populated media), the avatar loads the
original instead, as the notification extension's avatar loader already
does.

### Before merge

- [x] Kenny Lopez reviews the visible changes ([approved in Buzz
#buzz-mobile](buzz://message?channel=9f18f3c6-b5e5-453d-b36d-65487913be0b&id=c8b01eaa3760e44c3b6583f2e02bec576d78679adfb364e5fe718cc0926bdc2d)).

### Validation

On an iPhone SE (iOS 27.0.1) running a profile build, relay traffic went
through a Mac-side proxy shaped to 200ms one-way latency, 240 kbps down
and 200 kbps up. I opened a busy channel and logged each relay image
fetch. Every fetch is aborted 30s after it starts, which
[#8881](https://github.com/block/buzz/pull/8881) changes separately;
with thumbnails the avatars below no longer come close to that limit.

| Avatar | Before | After |
| --- | --- | --- |
| A | 1.45 MB, fetched twice; both aborted at 30s, never shown | 14.5 KB
thumbnail, 3.6s |
| B | aborted at 30s, never shown | 14.5 KB thumbnail, 4.1s |
| C | 125 KB, fetched twice, 26s each | 21.5 KB thumbnail, 4.3s |
| D | 46 KB, 11.1s | 24.7 KB thumbnail, 4.6s |

Added tests (each fails without its fix):

- One URL resolved at two decode sizes downloads once.
- Small relay avatars request the thumbnail and large ones the original.
- A thumbnail 404 falls back to the original, and other thumbnail
failures show the avatar fallback.
- The thumbnail URL is built only for relay-hosted blobs
(`/media/<sha256>` with an optional extension), the same pattern
desktop's `mediaUrl` uses.

The full mobile suite passes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 07:37:11 -08:00

306 lines
10 KiB
Dart

import 'dart:convert';
import 'package:buzz/shared/relay/media_auth.dart';
import 'package:buzz/shared/relay/media_image.dart';
import 'package:buzz/shared/widgets/avatar_shape.dart';
import 'package:buzz/shared/widgets/avatar_image.dart';
import 'package:buzz/shared/emoji/native_emoji_glyph.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart' as http_testing;
void main() {
const svg =
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">'
'<text x="16" y="24" text-anchor="middle">🦝</text></svg>';
Widget subject(
String? imageUrl, {
Color? backgroundColor,
bool isAgent = false,
}) => ProviderScope(
child: MaterialApp(
home: AvatarImage(
imageUrl: imageUrl,
radius: 16,
backgroundColor: backgroundColor,
fallback: const Text('R'),
isAgent: isAgent,
),
),
);
testWidgets('only animated agent artwork shrinks inside its unchanged frame', (
tester,
) async {
const poster = 'https://relay.example/poster.png';
final animated =
'$poster#buzz-anim=${Uri.encodeComponent('https://relay.example/animation.png')}';
const preset =
'https://dwwgwmfqqjotj.cloudfront.net/avatars/20260925T154625166Z/poster/pollies/pollies-10.png';
for (final url in [poster, animated, preset]) {
await tester.pumpWidget(subject(url, isAgent: true));
final clip = find.byType(ClipPath);
final scale = find.descendant(of: clip, matching: find.byType(Transform));
if (url != poster) {
expect(scale, findsOneWidget);
expect(tester.widget<Transform>(scale).transform.entry(0, 0), 0.85);
final content = find.byType(AvatarImageContent);
expect(tester.getSize(content), tester.getSize(clip));
} else {
expect(scale, findsNothing);
}
}
});
testWidgets('small relay avatars load the relay thumbnail', (tester) async {
const relay = 'https://relay.example';
final hash = 'ab' * 32;
final original = '$relay/media/$hash.png';
final requested = <String>[];
final client = http_testing.MockClient((request) async {
requested.add(request.url.toString());
return http.Response('', 404);
});
tester.view.devicePixelRatio = 3;
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
ProviderScope(
overrides: [
mediaGetAuthServiceProvider.overrideWithValue(
MediaGetAuthService(baseUrl: relay, nsec: null),
),
mediaHttpClientProvider.overrideWithValue(client),
],
child: MaterialApp(
home: Column(
mainAxisSize: MainAxisSize.min,
children: [
// 40pt = 120px, within the thumbnail.
AvatarImage(
imageUrl: original,
radius: 20,
fallback: const Text('R'),
),
// 120pt = 360px, larger than the thumbnail.
AvatarImage(
imageUrl: original,
radius: 60,
fallback: const Text('R'),
),
],
),
),
),
);
final shown = tester.widgetList<Image>(find.byType(Image)).map((image) {
final provider = (image.image as ResizeImage).imageProvider;
return (provider as MediaImageProvider).url;
});
expect(shown, ['$relay/media/$hash.thumb.jpg', original]);
});
testWidgets('a missing thumbnail falls back to the original', (tester) async {
const relay = 'https://relay.example';
final missing = '$relay/media/${'cd' * 32}.jpg';
final broken = '$relay/media/${'ef' * 32}.jpg';
final requested = <String>[];
final client = http_testing.MockClient((request) async {
final url = request.url.toString();
requested.add(url);
if (url.startsWith(broken.replaceFirst('.jpg', ''))) {
return http.Response('', 500);
}
return http.Response('', 404);
});
tester.view.devicePixelRatio = 3;
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
ProviderScope(
overrides: [
mediaGetAuthServiceProvider.overrideWithValue(
MediaGetAuthService(baseUrl: relay, nsec: null),
),
mediaHttpClientProvider.overrideWithValue(client),
],
child: MaterialApp(
home: Column(
mainAxisSize: MainAxisSize.min,
children: [
for (final url in [missing, broken])
AvatarImage(
imageUrl: url,
radius: 20,
fallback: Text(url == missing ? 'M' : 'B'),
),
],
),
),
),
);
for (var i = 0; i < 4; i++) {
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 20)),
);
await tester.pump();
}
String thumb(String url) => url.replaceFirst('.jpg', '.thumb.jpg');
// A 404 retries the original; any other failure shows the fallback.
expect(requested, containsAll([thumb(missing), missing, thumb(broken)]));
expect(requested, isNot(contains(broken)));
expect(find.text('B'), findsOneWidget);
});
testWidgets('missing thumbnails stay bounded and out of the cooldown', (
tester,
) async {
const relay = 'https://relay.example';
MediaImageProvider.debugResetCooldowns();
MediaImage.debugResetMissingThumbnails();
addTearDown(MediaImageProvider.debugResetCooldowns);
addTearDown(MediaImage.debugResetMissingThumbnails);
final client = http_testing.MockClient(
(request) async => http.Response('', 404),
);
tester.view.devicePixelRatio = 3;
addTearDown(tester.view.resetDevicePixelRatio);
String avatar(int i) =>
'$relay/media/${i.toRadixString(16).padLeft(64, '0')}.png';
for (var batch = 0; batch < 6; batch++) {
await tester.pumpWidget(
ProviderScope(
overrides: [
mediaGetAuthServiceProvider.overrideWithValue(
MediaGetAuthService(baseUrl: relay, nsec: null),
),
mediaHttpClientProvider.overrideWithValue(client),
],
child: MaterialApp(
home: Wrap(
children: [
for (var i = batch * 50; i < (batch + 1) * 50; i++)
AvatarImage(
imageUrl: avatar(i),
radius: 20,
fallback: const Text('?'),
),
],
),
),
),
);
for (var i = 0; i < 4; i++) {
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 20)),
);
await tester.pump();
}
}
expect(MediaImage.debugMissingThumbnailCount, 256);
expect(
MediaImageProvider.debugCooldownUrls.where(
(url) => url.endsWith('.thumb.jpg'),
),
isEmpty,
);
});
testWidgets('clips agents to the desktop squircle', (tester) async {
await tester.pumpWidget(subject(null, isAgent: true));
expect(find.byType(CircleAvatar), findsNothing);
final clip = tester.widget<ClipPath>(find.byType(ClipPath));
expect(
(clip.clipper! as ShapeBorderClipper).shape,
avatarShape(isAgent: true),
);
});
testWidgets('renders raccoon percent-encoded SVG data avatar', (
tester,
) async {
const raccoonAvatar =
'data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22512%22%20height%3D%22512%22%20viewBox%3D%220%200%20512%20512%22%3E%3Crect%20width%3D%22512%22%20height%3D%22512%22%20rx%3D%22256%22%20fill%3D%22%233399FF%22%2F%3E%3Ctext%20x%3D%2250%25%22%20y%3D%2256%25%22%20dominant-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20font-size%3D%22258%22%3E%F0%9F%A6%9D%3C%2Ftext%3E%3C%2Fsvg%3E';
await tester.pumpWidget(subject(raccoonAvatar));
await tester.pumpAndSettle();
final emoji = tester.widget<Text>(find.text('🦝'));
expect(emoji.style?.height, 1);
final glyph = tester.widget<NativeEmojiGlyph>(
find.byType(NativeEmojiGlyph),
);
expect(glyph.size, closeTo(32 * 258 / 512, 0.001));
expect(glyph.opticalBoxSize, glyph.size);
expect(find.byType(SvgPicture), findsNothing);
expect(find.text('R'), findsNothing);
expect(tester.takeException(), isNull);
});
testWidgets('renders base64 SVG data avatar', (tester) async {
await tester.pumpWidget(
subject('data:image/svg+xml;base64,${base64Encode(utf8.encode(svg))}'),
);
expect(find.byType(SvgPicture), findsOneWidget);
});
testWidgets('centers fallback when no avatar is configured', (tester) async {
await tester.pumpWidget(subject(null));
final avatarCenter = tester.getCenter(find.byType(CircleAvatar));
final fallbackCenter = tester.getCenter(find.text('R'));
expect(fallbackCenter, avatarCenter);
});
testWidgets('uses fallback for malformed image data', (tester) async {
await tester.pumpWidget(subject('data:image/svg+xml;base64,%%%'));
expect(find.text('R'), findsOneWidget);
});
testWidgets('renders animated-avatar posters without an opaque background', (
tester,
) async {
const posterUrl = 'https://relay.example/media/poster.png';
const animationUrl = 'https://relay.example/media/animation.png';
final animatedUrl =
'$posterUrl#buzz-anim=${Uri.encodeComponent(animationUrl)}';
await tester.pumpWidget(subject(animatedUrl, backgroundColor: Colors.red));
expect(
tester.widget<CircleAvatar>(find.byType(CircleAvatar)).backgroundColor,
Colors.transparent,
);
expect(
tester
.widget<AvatarImageContent>(find.byType(AvatarImageContent))
.imageUrl,
posterUrl,
);
});
testWidgets('reuses parsed raster bytes across parent rebuilds', (
tester,
) async {
const png =
'data:image/png;base64,'
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=';
await tester.pumpWidget(subject(png));
final firstBytes = tester.widget<Image>(find.byType(Image)).image;
await tester.pumpWidget(subject(png));
final rebuiltBytes = tester.widget<Image>(find.byType(Image)).image;
final firstMemory = firstBytes as MemoryImage;
final rebuiltMemory = rebuiltBytes as MemoryImage;
expect(rebuiltMemory.bytes, same(firstMemory.bytes));
});
}