import 'dart:async'; import 'package:halkyon_learn_agent/src/agent_sync.dart'; import 'package:test/test.dart'; /// The agent's sync (Halkyon's ADR 0044, D7), the `SyncRequests` its /// gateways ask: a burst of requests syncs once after a pause, a tool's /// sync runs at once and says whether it went, and what a sync merged is /// heard. void main() { late List runs; late Exception? fails; late AgentSync sync; setUp(() { runs = []; fails = null; sync = AgentSync(({required onMerge}) async { runs.add('sync'); if (fails case final error?) { throw error; } onMerge(); }, debounce: const Duration(milliseconds: 50)); addTearDown(sync.dispose); }); test('requests in a burst sync once, after the pause', () async { sync ..requestSync() ..requestSync() ..requestSync(); expect(runs, isEmpty); await Future.delayed(const Duration(milliseconds: 150)); expect(runs, ['sync']); }); test('a sync now runs at once, and what it merged is heard', () async { final merged = sync.merges.first; final synced = sync.synced.first; await sync.syncNow(); expect(runs, ['sync']); await merged; await synced; }); test('a sync now that fails says so; the next that goes through ' "doesn't", () async { fails = Exception('offline'); await expectLater(sync.syncNow(), throwsA(isA())); fails = null; await sync.syncNow(); expect(runs, ['sync', 'sync']); }); }