import 'package:flutter_test/flutter_test.dart'; import 'package:yx_tracking_flutter/src/model/device_info.dart'; import 'package:yx_tracking_flutter/src/model/event.dart'; import 'package:yx_tracking_flutter/src/storage/isolate_event_storage.dart'; Event _buildEvent(String type, DateTime createTime) { final ts = createTime.millisecondsSinceEpoch; return Event( systemCode: 'SYS', eventType: type, userInfo: null, clientType: 3, clientTimestamp: ts, timestamp: createTime.toUtc().toIso8601String(), deviceInfo: const DeviceInfo( os: 'os', model: 'model', screenResolution: '1x1', ), eventParams: null, customTags: null, createTime: createTime, ); } void main() { TestWidgetsFlutterBinding.ensureInitialized(); group('IsolateEventStorage (memory backend)', () { late IsolateEventStorage storage; setUp(() async { storage = IsolateEventStorage(backend: IsolateStorageBackend.memory); await storage.init(); }); tearDown(() async { await storage.dispose(); }); test('insert/count/fetchBatch 基础流程', () async { final now = DateTime.now(); await storage.insert( _buildEvent('A', now.subtract(const Duration(seconds: 2))), ); await storage.insert( _buildEvent('B', now.subtract(const Duration(seconds: 1))), ); expect(await storage.count(), 2); final batch = await storage.fetchBatch(10); expect(batch.map((e) => e.event.eventType), ['A', 'B']); }); test('fetchRecent 按时间降序返回', () async { final now = DateTime.now(); await storage.insert( _buildEvent('OLD', now.subtract(const Duration(seconds: 2))), ); await storage.insert(_buildEvent('NEW', now)); final recent = await storage.fetchRecent(1); expect(recent.single.event.eventType, 'NEW'); }); test('deleteByIds 删除指定事件', () async { final now = DateTime.now(); final id1 = await storage.insert(_buildEvent('A', now)); final id2 = await storage.insert( _buildEvent('B', now.add(const Duration(seconds: 1))), ); await storage.deleteByIds([id1]); final left = await storage.fetchBatch(10); expect(left.map((e) => e.id), [id2]); }); test('trimToMaxSize 会删除最旧事件', () async { final now = DateTime.now(); await storage.insert( _buildEvent('A', now.subtract(const Duration(seconds: 3))), ); await storage.insert( _buildEvent('B', now.subtract(const Duration(seconds: 2))), ); await storage.insert( _buildEvent('C', now.subtract(const Duration(seconds: 1))), ); final trimmed = await storage.trimToMaxSize(2); expect(trimmed, 1); final left = await storage.fetchBatch(10); expect(left.map((e) => e.event.eventType), ['B', 'C']); }); test('deleteExpired 会清理过期事件', () async { final now = DateTime.now(); await storage.insert( _buildEvent('OLD', now.subtract(const Duration(days: 8))), ); await storage.insert(_buildEvent('KEEP', now)); final removed = await storage.deleteExpired( now.subtract(const Duration(days: 7)), ); expect(removed, 1); final left = await storage.fetchBatch(10); expect(left.single.event.eventType, 'KEEP'); }); test('updateRetryCount 会更新 retryCount', () async { final now = DateTime.now(); final id = await storage.insert(_buildEvent('RETRY', now)); await storage.updateRetryCount(id, 2); final stored = await storage.fetchBatch(1); expect(stored.single.retryCount, 2); expect(stored.single.event.retryCount, 2); }); }); }