import 'package:flutter_test/flutter_test.dart'; import 'package:yx_tracking_flutter/src/config/analytics_config.dart'; AnalyticsConfig _base({ String? systemCode, String? endpointBaseUrl, int? clientType, int? batchSize, int? flushInterval, int? maxCacheSize, int? maxRetryCount, Duration? connectTimeout, Duration? readTimeout, Duration? maxEventAge, bool? useIsolateStorage, bool? enableMetrics, Duration? metricsReportInterval, }) { return AnalyticsConfig( systemCode: systemCode ?? 'SYS', endpointBaseUrl: endpointBaseUrl ?? 'https://example.com/api', clientType: clientType ?? 3, batchSize: batchSize ?? 20, flushInterval: flushInterval ?? 15, maxCacheSize: maxCacheSize ?? 100, maxRetryCount: maxRetryCount ?? 3, connectTimeout: connectTimeout ?? const Duration(seconds: 1), readTimeout: readTimeout ?? const Duration(seconds: 1), maxEventAge: maxEventAge ?? const Duration(days: 7), useIsolateStorage: useIsolateStorage ?? false, enableMetrics: enableMetrics ?? true, metricsReportInterval: metricsReportInterval ?? const Duration(minutes: 1), ); } void main() { group('AnalyticsConfig.validate 全分支', () { test('非法 systemCode 会抛错', () { expect(() => _base(systemCode: ' ').validate(), throwsArgumentError); }); test('非法 URL 会抛错', () { expect( () => _base(endpointBaseUrl: 'not-a-url').validate(), throwsArgumentError, ); }); test('非 https 会抛错', () { expect( () => _base(endpointBaseUrl: 'http://example.com').validate(), throwsArgumentError, ); }); test('clientType 必须为正数', () { expect(() => _base(clientType: 0).validate(), throwsArgumentError); }); test('batchSize 必须为正数', () { expect(() => _base(batchSize: 0).validate(), throwsArgumentError); }); test('flushInterval 必须为正数', () { expect(() => _base(flushInterval: 0).validate(), throwsArgumentError); }); test('maxCacheSize 必须为正数', () { expect(() => _base(maxCacheSize: 0).validate(), throwsArgumentError); }); test('maxRetryCount 不能为负数', () { expect(() => _base(maxRetryCount: -1).validate(), throwsArgumentError); }); test('connectTimeout 必须大于 0', () { expect( () => _base(connectTimeout: Duration.zero).validate(), throwsArgumentError, ); }); test('readTimeout 必须大于 0', () { expect( () => _base(readTimeout: Duration.zero).validate(), throwsArgumentError, ); }); test('maxEventAge 不能为负数', () { expect( () => _base(maxEventAge: const Duration(seconds: -1)).validate(), throwsArgumentError, ); }); test('enableMetrics 时 metricsReportInterval 必须大于 0', () { expect( () => _base(metricsReportInterval: Duration.zero).validate(), throwsArgumentError, ); }); test('关闭 metrics 时允许 0 间隔', () { expect( () => _base(enableMetrics: false, metricsReportInterval: Duration.zero) .validate(), returnsNormally, ); }); }); group('AnalyticsConfig.uri 组装', () { test('basePath 为空时会补 /leaf', () { final config = _base(endpointBaseUrl: 'https://example.com'); expect(config.addEventListLogUri.path, '/AddEventListLog'); expect(config.addEventLogUri.path, '/AddEventLog'); expect(config.getSystemAllDimInfoUri.path, '/GetSystemAllDimInfo'); }); test('basePath 有值且带斜杠时会规范化', () { final config = _base(endpointBaseUrl: 'https://example.com/api/'); expect(config.addEventListLogUri.path, '/api/AddEventListLog'); expect(config.addEventLogUri.path, '/api/AddEventLog'); expect(config.getSystemAllDimInfoUri.path, '/api/GetSystemAllDimInfo'); }); }); }