import 'package:flutter_test/flutter_test.dart'; import 'package:yx_tracking_flutter/src/config/analytics_config.dart'; AnalyticsConfig _base({ String? systemCode, String? endpointBaseUrl, int? batchSize, int? flushInterval, int? maxCacheSize, int? maxRetryCount, Duration? connectTimeout, Duration? readTimeout, Duration? maxEventAge, bool? useIsolateStorage, bool? allowInsecureHttp, bool? enableMetrics, Duration? metricsReportInterval, }) { return AnalyticsConfig( systemCode: systemCode ?? 'SYS', endpointBaseUrl: endpointBaseUrl ?? 'https://example.com', batchSize: batchSize ?? 30, flushInterval: flushInterval ?? 30, maxCacheSize: maxCacheSize ?? 100, maxRetryCount: maxRetryCount ?? 3, connectTimeout: connectTimeout ?? const Duration(seconds: 10), readTimeout: readTimeout ?? const Duration(seconds: 10), maxEventAge: maxEventAge ?? const Duration(days: 7), useIsolateStorage: useIsolateStorage ?? false, allowInsecureHttp: allowInsecureHttp ?? 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('包含路径的 baseUrl 会抛错', () { expect( () => _base(endpointBaseUrl: 'https://example.com/api').validate(), throwsArgumentError, ); }); test('非 https 会抛错', () { expect( () => _base(endpointBaseUrl: 'http://example.com').validate(), throwsArgumentError, ); }); test('允许 http 时不抛错', () { expect( () => _base( endpointBaseUrl: 'http://example.com', allowInsecureHttp: true) .validate(), returnsNormally, ); }); 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('基础 host 会拼接 api/ExternalEventlogs 路径', () { final config = _base(endpointBaseUrl: 'https://example.com'); expect( config.addEventListLogUri.path, '/api/ExternalEventlogs/AddEventListLog', ); expect( config.addEventLogUri.path, '/api/ExternalEventlogs/AddEventLog', ); expect( config.getSystemAllDimInfoUri.path, '/api/ExternalEventlogs/GetSystemAllDimInfo', ); }); test('末尾带斜杠的 host 也可正常拼接', () { final config = _base(endpointBaseUrl: 'https://example.com/'); expect( config.addEventListLogUri.path, '/api/ExternalEventlogs/AddEventListLog', ); expect( config.addEventLogUri.path, '/api/ExternalEventlogs/AddEventLog', ); expect( config.getSystemAllDimInfoUri.path, '/api/ExternalEventlogs/GetSystemAllDimInfo', ); }); }); }