Files
Flutter_Shell/lib/config/app_config.dart
Booker 15d767100a Refactor widget tests for H5 line generation and validation
- Simplified tests for H5 line creation, focusing on single line generation with a numeric 16-digit label.
- Updated assertions to validate the generated H5 line's label and URI structure.
- Removed redundant tests related to default H5 line URLs and local access, consolidating functionality into fewer tests.
- Added new tests to ensure correct URL generation from configured fallback order and query parameter handling.
- Enhanced test coverage for restoring cached H5 line URLs and validating their structure.
2026-06-06 03:48:20 +07:00

100 lines
2.6 KiB
Dart

import 'dart:math';
class H5Line {
const H5Line({
required this.label,
required this.url,
});
final String label;
final String url;
Uri get uri => Uri.parse(url);
}
class AppConfig {
static const String appName = '本地打包';
static const String appLogo = '';
static const String flutterShellMarkerQueryKey = 'flutter_shell';
static const String flutterShellUrlQueryKey = 'flutter_shell_url';
static const List<String> wildcardH5DomainTemplates = [
'*.albzyuxq.vip',
'*.zdyswvnt.vip',
'*.gxifpxcr.vip',
];
static final Random _random = Random.secure();
static H5Line get h5Line => createRandomH5Line();
static H5Line createRandomH5Line({
int attempt = 0,
String Function()? wildcardFactory,
}) {
final template = wildcardH5DomainTemplates[
attempt.abs() % wildcardH5DomainTemplates.length];
return h5LineFromUrl(
_buildFlutterShellHomeUrl(
_replaceWildcardHost(template, wildcardFactory: wildcardFactory),
),
);
}
static H5Line h5LineFromUrl(String value) {
return H5Line(label: '线路1', url: _normalizeHomeUrl(value));
}
static String originForUrl(String value) {
final uri = Uri.parse(value);
return '${uri.scheme}://${uri.authority}';
}
static String _buildFlutterShellHomeUrl(String host) {
final normalized = _normalizeHomeUrl(host);
final uri = Uri.parse(normalized);
final origin = originForUrl(normalized);
final queryParameters = Map<String, String>.from(uri.queryParameters)
..[flutterShellMarkerQueryKey] = '1'
..[flutterShellUrlQueryKey] = origin;
return uri.replace(queryParameters: queryParameters).toString();
}
static String _normalizeHomeUrl(String host) {
final value = host.trim();
if (value.isEmpty) {
throw const FormatException('Empty H5 line URL');
}
final normalized =
value.startsWith('http://') || value.startsWith('https://')
? value
: 'https://$value';
final uri = Uri.parse(normalized);
final path = uri.path.isEmpty ? '/' : uri.path;
return uri.replace(path: path).toString();
}
static String _replaceWildcardHost(
String value, {
String Function()? wildcardFactory,
}) {
if (!value.contains('*.')) {
return value;
}
return value.replaceAllMapped(
'*.',
(_) => '${(wildcardFactory ?? _randomWildcardLabel)()}.',
);
}
static String _randomWildcardLabel() {
final digits = StringBuffer(_random.nextInt(9) + 1);
for (var index = 1; index < 16; index += 1) {
digits.write(_random.nextInt(10));
}
return digits.toString();
}
}