import 'package:app_upgrade_plugin/app_upgrade_plugin_enhanced.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); group('版本比较测试', () { late VersionComparator comparator; setUp(() { comparator = VersionComparator(strategy: VersionCompareStrategy.semantic); }); test('语义化版本比较', () { expect(comparator.compare('1.0.0', '2.0.0'), equals(-1)); expect(comparator.compare('2.0.0', '1.0.0'), equals(1)); expect(comparator.compare('1.0.0', '1.0.0'), equals(0)); expect(comparator.compare('1.2.3', '1.2.4'), equals(-1)); expect(comparator.compare('1.2.3', '1.3.0'), equals(-1)); expect(comparator.compare('2.0.0', '1.9.9'), equals(1)); }); test('预发布版本比较', () { expect(comparator.compare('1.0.0-alpha', '1.0.0-beta'), equals(-1)); expect(comparator.compare('1.0.0-beta', '1.0.0'), equals(-1)); expect(comparator.compare('1.0.0', '1.0.0-beta'), equals(1)); }); test('版本更新检测', () { expect(comparator.isUpdateAvailable('1.0.0', '2.0.0'), isTrue); expect(comparator.isUpdateAvailable('2.0.0', '1.0.0'), isFalse); expect(comparator.isMajorUpdate('1.0.0', '2.0.0'), isTrue); expect(comparator.isMinorUpdate('1.0.0', '1.1.0'), isTrue); expect(comparator.isPatchUpdate('1.0.0', '1.0.1'), isTrue); }); test('批量版本操作', () { final versions = ['1.0.0', '2.0.0', '1.5.0', '1.2.0']; expect(comparator.getLatestVersion(versions), equals('2.0.0')); final sorted = comparator.sortVersions(versions); expect(sorted, equals(['1.0.0', '1.2.0', '1.5.0', '2.0.0'])); final sortedDesc = comparator.sortVersions(versions, descending: true); expect(sortedDesc, equals(['2.0.0', '1.5.0', '1.2.0', '1.0.0'])); }); }); group('升级信息模型测试', () { test('从JSON创建', () { final json = { 'hasUpdate': true, 'isForceUpdate': false, 'versionCode': '2', 'versionName': '1.2.0', 'updateContent': 'Bug fixes', 'downloadUrl': 'https://example.com/app.apk', 'appStoreUrl': 'https://apps.apple.com/app/id123', 'apkSize': 1024 * 1024 * 10, 'apkMd5': 'abc123', }; final info = UpgradeInfo.fromJson(json); expect(info.hasUpdate, isTrue); expect(info.isForceUpdate, isFalse); expect(info.versionCode, equals('2')); expect(info.versionName, equals('1.2.0')); expect(info.updateContent, equals('Bug fixes')); expect(info.downloadUrl, equals('https://example.com/app.apk')); expect(info.appStoreUrl, equals('https://apps.apple.com/app/id123')); expect(info.apkSize, equals(1024 * 1024 * 10)); expect(info.apkMd5, equals('abc123')); }); test('转换为JSON', () { final info = UpgradeInfo( hasUpdate: true, isForceUpdate: true, versionCode: '3', versionName: '2.0.0', updateContent: 'Major update', ); final json = info.toJson(); expect(json['hasUpdate'], isTrue); expect(json['isForceUpdate'], isTrue); expect(json['versionCode'], equals('3')); expect(json['versionName'], equals('2.0.0')); expect(json['updateContent'], equals('Major update')); }); }); group('下载进度测试', () { test('进度计算', () { final progress = DownloadProgress(received: 500, total: 1000); expect(progress.progress, equals(0.5)); expect(progress.percentage, equals(50)); }); test('处理总大小为0', () { final progress = DownloadProgress(received: 100, total: 0); expect(progress.progress, equals(0.0)); expect(progress.percentage, equals(0)); }); }); group('配置管理测试', () { test('配置更新', () { final config = UpgradeConfig.instance; config.updateConfig(debugMode: false, wifiOnly: false, maxRetryCount: 5); expect(config.debugMode, isFalse); expect(config.wifiOnly, isFalse); expect(config.maxRetryCount, equals(5)); }); test('配置重置', () { final config = UpgradeConfig.instance; config.updateConfig(maxRetryCount: 10); config.reset(); expect(config.maxRetryCount, equals(3)); // 默认值 }); test('配置导出导入', () { final config = UpgradeConfig.instance; config.updateConfig(debugMode: false, wifiOnly: false, maxRetryCount: 5); final exportedMap = config.toMap(); expect(exportedMap['debugMode'], isFalse); expect(exportedMap['wifiOnly'], isFalse); expect(exportedMap['maxRetryCount'], equals(5)); config.reset(); config.fromMap(exportedMap); expect(config.debugMode, isFalse); expect(config.wifiOnly, isFalse); expect(config.maxRetryCount, equals(5)); }); }); // 插件基础功能测试需要mock平台通道,暂时跳过 // group('插件基础功能测试', () { // test('插件单例', () { // final plugin1 = AppUpgradePluginEnhanced.instance; // final plugin2 = AppUpgradePluginEnhanced.instance; // expect(identical(plugin1, plugin2), isTrue); // }); // test('回调管理', () { // final plugin = AppUpgradePluginEnhanced.instance; // int upgradeCallCount = 0; // int downloadCallCount = 0; // int errorCallCount = 0; // void upgradeCallback(UpgradeInfo info) { // upgradeCallCount++; // } // void downloadCallback(DownloadTask task) { // downloadCallCount++; // } // void errorCallback(String error) { // errorCallCount++; // } // plugin.addUpgradeCallback(upgradeCallback); // plugin.addDownloadCallback(downloadCallback); // plugin.addErrorCallback(errorCallback); // // 移除回调 // plugin.removeUpgradeCallback(upgradeCallback); // plugin.removeDownloadCallback(downloadCallback); // plugin.removeErrorCallback(errorCallback); // // 验证回调已移除 // expect(upgradeCallCount, equals(0)); // expect(downloadCallCount, equals(0)); // expect(errorCallCount, equals(0)); // }); // }); group('版本解析测试', () { test('解析语义化版本', () { final version = Version.parse('1.2.3-beta.1+build.123'); expect(version.major, equals(1)); expect(version.minor, equals(2)); expect(version.patch, equals(3)); expect(version.preRelease, equals('beta.1')); expect(version.buildMetadata, equals('build.123')); expect(version.isPreRelease, isTrue); }); test('解析简单版本', () { final version = Version.parse('1.2.3'); expect(version.major, equals(1)); expect(version.minor, equals(2)); expect(version.patch, equals(3)); expect(version.preRelease, isNull); expect(version.isPreRelease, isFalse); }); test('解析构建号', () { final version = Version.parse('123'); expect(version.buildNumber, equals(123)); expect(version.major, isNull); }); test('版本数组', () { final version = Version.parse('1.2.3'); expect(version.versionArray, equals([1, 2, 3])); }); }); }