yumi-flutter/test/room_rocket_pag_resource_test.dart
2026-05-15 22:49:21 +08:00

83 lines
2.9 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:yumi/app/config/app_config.dart';
import 'package:yumi/app/constants/sc_global_config.dart';
import 'package:yumi/shared/data_sources/models/message/room_rocket_launch_broadcast_message.dart';
import 'package:yumi/ui_kit/widgets/room/rocket/room_rocket_pag_effect_overlay.dart';
void main() {
setUpAll(() {
AppConfig.initialize();
});
test('normalizes unescaped pag network urls', () {
final uri = debugRoomRocketPagNetworkUri(
'https://example.com/assets/火箭 输出/火箭1级.pag?name=火箭 1',
);
expect(uri, isNotNull);
expect(uri!.scheme, 'https');
expect(uri.host, 'example.com');
expect(uri.toString(), contains('%E7%81%AB%E7%AE%AD'));
expect(uri.toString(), contains('%20'));
});
test('normalizes protocol-relative and server-relative pag urls', () {
final apiUri = Uri.parse(SCGlobalConfig.apiHost);
final apiScheme = apiUri.scheme == 'http' ? 'http' : 'https';
expect(
debugNormalizeRoomRocketPagResource('//cdn.example.com/rocket.pag'),
'$apiScheme://cdn.example.com/rocket.pag',
);
expect(
debugNormalizeRoomRocketPagResource('/external/oss/local/火箭.pag'),
apiUri.resolve('/external/oss/local/火箭.pag').toString(),
);
});
test('detects local file sources before falling back to assets', () {
expect(
debugRoomRocketPagFilePath('file:///tmp/%E7%81%AB%E7%AE%AD.pag'),
'/tmp/火箭.pag',
);
expect(debugRoomRocketPagSourceKind('/tmp/rocket.pag'), 'file');
expect(debugRoomRocketPagSourceKind('assets/rocket/rocket.pag'), 'asset');
});
test('keeps pag bytes in memory for five minutes by default', () {
expect(debugRoomRocketPagMemoryTtl(), const Duration(minutes: 5));
});
test('releases pag bytes when the configured ttl expires', () async {
var released = false;
final timer = debugScheduleRoomRocketPagMemoryRelease(
memoryTtl: const Duration(milliseconds: 10),
onRelease: () => released = true,
);
expect(released, isFalse);
await Future<void>.delayed(const Duration(milliseconds: 30));
expect(released, isTrue);
timer?.cancel();
});
test('extracts launch pag urls from nested resource payloads', () {
final launch = RoomRocketLaunchBroadcastMessage.fromJson({
'roomId': 'room-1',
'rocketAnimationResource': {
'sourceUrl': 'https://cdn.example.com/rocket/launch.pag',
},
'rocketIcon': {'url': 'https://cdn.example.com/rocket/icon.png'},
'room': {
'cover': {'url': 'https://cdn.example.com/room/cover.png'},
},
});
expect(
launch.rocketAnimationUrl,
'https://cdn.example.com/rocket/launch.pag',
);
expect(launch.rocketIconUrl, 'https://cdn.example.com/rocket/icon.png');
expect(launch.roomCoverUrl, 'https://cdn.example.com/room/cover.png');
});
}