Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

test(mobile): unread marks, the announcer's lifecycle, stats and the config store

dee62429b22d244
diff --git a/mobile/test/notify/agent_unread_test.dart b/mobile/test/notify/agent_unread_test.dart
new file mode 100644
index 000000000..90ca68834
--- /dev/null
+++ b/mobile/test/notify/agent_unread_test.dart
@@ -0,0 +1,89 @@
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/notify/agent_notice.dart';
+import 'package:harness_mobile/notify/agent_unread.dart';
+
+/// The unread marks: which agents have news the person has not gone to yet.
+///
+/// Every row, tab pill and count on the phone reads these, and each redraws on
+/// a notification — so a mark that did not change must not notify, or one
+/// agent's news would redraw every row for nothing.
+void main() {
+ const a = (machineId: 'm', agentId: 'a');
+ const b = (machineId: 'm', agentId: 'b');
+ const c = (machineId: 'other', agentId: 'a');
+
+ late AgentUnread unread;
+ late int notified;
+
+ setUp(() {
+ unread = AgentUnread();
+ notified = 0;
+ unread.addListener(() => notified++);
+ });
+
+ tearDown(() => unread.dispose());
+
+ test('an agent is told apart by its machine too', () {
+ unread.mark(a, NoticeKind.done);
+ expect(unread.contains(a), isTrue);
+ expect(unread.contains(c), isFalse);
+ });
+
+ test('the same news twice is one notification', () {
+ unread
+ ..mark(a, NoticeKind.done)
+ ..mark(a, NoticeKind.done);
+ expect(notified, 1);
+ unread.mark(a, NoticeKind.question);
+ expect(notified, 2);
+ expect(unread.anyQuestion, isTrue);
+ });
+
+ group('the most urgent news among a tab\'s agents', () {
+ test('a question over a finished turn, wherever it sits', () {
+ unread
+ ..mark(a, NoticeKind.done)
+ ..mark(b, NoticeKind.question);
+ expect(unread.mostUrgentOf([a, b]), NoticeKind.question);
+ expect(unread.mostUrgentOf([b, a]), NoticeKind.question);
+ });
+
+ test('a finished turn when that is all there is', () {
+ unread.mark(b, NoticeKind.done);
+ expect(unread.mostUrgentOf([a, b, c]), NoticeKind.done);
+ });
+
+ test('nothing when none of them carries news', () {
+ unread.mark(c, NoticeKind.question);
+ expect(unread.mostUrgentOf([a, b]), isNull);
+ expect(unread.mostUrgentOf(const []), isNull);
+ });
+ });
+
+ group('clearing', () {
+ test('a clear of one kind leaves the other kind standing', () {
+ unread.mark(a, NoticeKind.done);
+ unread.clear(a, kind: NoticeKind.question);
+ expect(unread.kindFor(a), NoticeKind.done);
+ expect(notified, 1);
+ });
+
+ test('nothing to clear is silent', () {
+ unread
+ ..clear(a)
+ ..clearAll();
+ expect(notified, 0);
+ });
+
+ test('clearing everything takes every mark down at once', () {
+ unread
+ ..mark(a, NoticeKind.done)
+ ..mark(b, NoticeKind.question);
+ notified = 0;
+ unread.clearAll();
+ expect(unread.count, 0);
+ expect(unread.anyQuestion, isFalse);
+ expect(notified, 1);
+ });
+ });
+}
diff --git a/mobile/test/notify/announcer_lifecycle_test.dart b/mobile/test/notify/announcer_lifecycle_test.dart
new file mode 100644
index 000000000..ac813f9fa
--- /dev/null
+++ b/mobile/test/notify/announcer_lifecycle_test.dart
@@ -0,0 +1,73 @@
+import 'package:flutter/widgets.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/notify/agent_announcer.dart';
+import 'package:harness_mobile/notify/agent_notice.dart';
+import 'package:harness_mobile/notify/system_notices.dart';
+
+/// The announcer as the app builds it — with its own answer to "is the app in
+/// front", read off the binding, rather than the one the other tests inject.
+///
+/// That answer decides between a mark and a lock-screen notice, so it is
+/// pinned against the real lifecycle states a phone passes through.
+void main() {
+ const ref = (machineId: 'm', agentId: 'a');
+ const agent = (ref: ref, name: 'Fix login', machine: 'MacBook');
+ const news = (
+ aborted: false,
+ replay: false,
+ subagent: false,
+ reply: 'Fixed the login screen.',
+ );
+
+ // First, and deliberately before any `testWidgets`: with no binding at all
+ // `WidgetsBinding.instance` throws rather than answering null, and plain
+ // `test()`s drive the notifier exactly like that.
+ test('with no binding at all, unknown counts as in front', () {
+ final announcer = AgentAnnouncer(system: SilentSystemNotices());
+ addTearDown(announcer.dispose);
+ expect(announcer.inFront, isTrue);
+ });
+
+ testWidgets('in front while resumed; a pocketed phone gets the notice', (
+ tester,
+ ) async {
+ final system = _Recording();
+ final announcer = AgentAnnouncer(system: system);
+ addTearDown(announcer.dispose);
+
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
+ expect(announcer.inFront, isTrue);
+ // In front, elsewhere: the haptic tap and a mark — the default chime must
+ // never throw, even where no platform answers it.
+ expect(
+ announcer.turnEnded(agent, news, watching: () => false),
+ AgentNotice.mark,
+ );
+ expect(system.shown, isEmpty);
+
+ for (final state in [
+ AppLifecycleState.inactive,
+ AppLifecycleState.hidden,
+ AppLifecycleState.paused,
+ ]) {
+ tester.binding.handleAppLifecycleStateChanged(state);
+ }
+ expect(announcer.inFront, isFalse);
+ expect(
+ announcer.turnEnded(agent, news, watching: () => true),
+ AgentNotice.alert,
+ );
+ expect(system.shown.single.body, 'Fixed the login screen.');
+
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
+ });
+}
+
+class _Recording extends SilentSystemNotices {
+ final shown = <AgentNoticeMessage>[];
+
+ @override
+ Future<void> show(AgentNoticeMessage message) async => shown.add(message);
+}
diff --git a/mobile/test/settings/config_store_test.dart b/mobile/test/settings/config_store_test.dart
new file mode 100644
index 000000000..8e7ee858b
--- /dev/null
+++ b/mobile/test/settings/config_store_test.dart
@@ -0,0 +1,83 @@
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/core/local_key_value_store.dart';
+import 'package:harness_mobile/settings/config_store.dart';
+
+/// The connection settings the phone reads once, at the top of every launch
+/// (`AppNotifier.bootstrap`), before it knows which backend to sign in to.
+///
+/// Only the read is the phone's: the writes behind it (a base URL, the staging
+/// switch, a skipped desktop update) have no screen on a phone, and are left to
+/// the desktop's own suite.
+void main() {
+ test('before anything is read it points at production', () {
+ // Touches the shared file store's constructor only — no read, no IO.
+ final config = ConfigStore().config;
+ expect(config.apiBaseUrl, ConfigStore.defaultBaseUrl);
+ expect(config.autonomousEnv, 'prod');
+ });
+
+ test('nothing saved: production, and no skipped update', () async {
+ final store = ConfigStore(storage: _Store({}));
+ final config = await store.load();
+ expect(config.apiBaseUrl, ConfigStore.defaultBaseUrl);
+ expect(config.autonomousEnv, 'prod');
+ expect(store.skippedDesktopUpdateVersion, isNull);
+ });
+
+ test('what was saved comes back, in one batched read', () async {
+ final storage = _Store({
+ 'app_api_base_url': 'https://staging.example',
+ 'app_autonomous_environment': 'stag',
+ 'skipped_desktop_update_version': ' 1.4.0 ',
+ });
+ final store = ConfigStore(storage: storage);
+ final config = await store.load();
+ expect(config.apiBaseUrl, 'https://staging.example');
+ expect(config.autonomousEnv, 'stag');
+ expect(store.skippedDesktopUpdateVersion, '1.4.0');
+ expect(store.config.apiBaseUrl, 'https://staging.example');
+ // `readMany`, never three reads: each read on the real store takes the
+ // file lock and re-parses `state.json` on the launch path.
+ expect(storage.batches, 1);
+ expect(storage.reads, 0);
+ });
+
+ test('an environment that is not staging is production', () async {
+ final store = ConfigStore(
+ storage: _Store({
+ 'app_autonomous_environment': 'Stag ',
+ 'skipped_desktop_update_version': ' ',
+ }),
+ );
+ final config = await store.load();
+ expect(config.autonomousEnv, 'prod');
+ expect(store.skippedDesktopUpdateVersion, isNull);
+ });
+}
+
+/// An in-memory `state.json` that counts how it was asked.
+class _Store implements BatchLocalKeyValueStore {
+ _Store(this.values);
+
+ final Map<String, String> values;
+ int batches = 0;
+ int reads = 0;
+
+ @override
+ Future<Map<String, String?>> readMany(Iterable<String> keys) async {
+ batches++;
+ return {for (final key in keys) key: values[key]};
+ }
+
+ @override
+ Future<String?> read(String key) async {
+ reads++;
+ return values[key];
+ }
+
+ @override
+ Future<void> write(String key, String value) async => values[key] = value;
+
+ @override
+ Future<void> delete(String key) async => values.remove(key);
+}
diff --git a/mobile/test/stats/harness_stats_test.dart b/mobile/test/stats/harness_stats_test.dart
new file mode 100644
index 000000000..9d7180dd1
--- /dev/null
+++ b/mobile/test/stats/harness_stats_test.dart
@@ -0,0 +1,187 @@
+import 'dart:convert';
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/core/snapshot_store.dart';
+import 'package:harness_mobile/stats/harness_stats.dart';
+
+/// The app's own counters — agents started, turns taken, time worked.
+///
+/// Nothing on the phone draws them today, but they are kept and written every
+/// launch, so what lands on disk has to be something a page brought back later
+/// could trust: no turn counted twice for a re-announce, no clock time invented
+/// for a turn this app never saw start, and a snapshot it cannot read treated
+/// as no snapshot rather than a failed launch.
+///
+/// The debounced write never runs under `flutter test` (a pending timer is a
+/// `pumpAndSettle` that never settles), so what reaches the store is read here
+/// through [HarnessStats.flush] — the one other door to the same write.
+void main() {
+ Map<String, Object?> written(MemorySnapshotStore store) =>
+ jsonDecode(store.contents!) as Map<String, Object?>;
+
+ test(
+ 'counts agents and turns, and the time between a start and its end',
+ () async {
+ final store = MemorySnapshotStore();
+ final stats = HarnessStats(store: store);
+ addTearDown(stats.dispose);
+ final t0 = DateTime(2026, 9, 27, 9);
+ var notified = 0;
+ stats.addListener(() => notified++);
+
+ stats.onAgentSpawned(at: t0);
+ stats.onTurnStarted('m/a', at: t0);
+ stats.onTurnEnded('m/a', at: t0.add(const Duration(seconds: 90)));
+ await stats.flush();
+
+ expect(written(store), {
+ 'version': 1,
+ 'agentsSpawned': 1,
+ 'turns': 1,
+ 'workedMs': 90000,
+ 'firstEventAt': t0.toIso8601String(),
+ });
+ expect(notified, 3);
+ },
+ );
+
+ test(
+ 'a turn re-announced while under way is one turn, timed from its start',
+ () async {
+ final store = MemorySnapshotStore();
+ final stats = HarnessStats(store: store);
+ addTearDown(stats.dispose);
+ final t0 = DateTime(2026, 9, 27, 9);
+ stats.onTurnStarted('m/a', at: t0);
+ // A heartbeat, or the re-announce after a reconnect.
+ stats.onTurnStarted('m/a', at: t0.add(const Duration(minutes: 1)));
+ stats.onTurnEnded('m/a', at: t0.add(const Duration(minutes: 2)));
+ await stats.flush();
+ expect(written(store)['turns'], 1);
+ expect(written(store)['workedMs'], 120000);
+ },
+ );
+
+ test('an end for a turn this app never saw start adds no time', () async {
+ final store = MemorySnapshotStore();
+ final stats = HarnessStats(store: store);
+ addTearDown(stats.dispose);
+ stats.onTurnEnded('m/unknown');
+ // And a clock that went backwards contributes nothing either.
+ final t0 = DateTime(2026, 9, 27, 9);
+ stats.onTurnStarted('m/a', at: t0);
+ stats.onTurnEnded('m/a', at: t0.subtract(const Duration(seconds: 5)));
+ await stats.flush();
+ expect(written(store)['workedMs'], 0);
+ expect(written(store)['turns'], 1);
+ });
+
+ test('"tracking since" is the first event and never moves', () async {
+ final store = MemorySnapshotStore();
+ final stats = HarnessStats(store: store);
+ addTearDown(stats.dispose);
+ final first = DateTime(2026, 9, 1);
+ stats.onAgentSpawned(at: first);
+ stats.onAgentSpawned(at: DateTime(2026, 9, 20));
+ stats.onTurnStarted('x');
+ await stats.flush();
+ expect(written(store)['firstEventAt'], first.toIso8601String());
+ });
+
+ test(
+ 'a flush closes out the turns still running, counting their time',
+ () async {
+ final store = MemorySnapshotStore();
+ final stats = HarnessStats(store: store);
+ addTearDown(stats.dispose);
+ stats.onTurnStarted(
+ 'm/a',
+ at: DateTime.now().subtract(const Duration(minutes: 5)),
+ );
+ await stats.flush();
+ expect(written(store)['workedMs'] as int, greaterThanOrEqualTo(300000));
+ // Closed, not still live: a second flush adds nothing more.
+ final before = written(store)['workedMs'];
+ await stats.flush();
+ expect(written(store)['workedMs'], before);
+ },
+ );
+
+ group('load', () {
+ test(
+ 'reads the counters back, and they keep counting from there',
+ () async {
+ final store = MemorySnapshotStore(
+ jsonEncode({
+ 'version': 1,
+ 'agentsSpawned': 4,
+ 'turns': 9,
+ 'workedMs': 1000,
+ 'firstEventAt': '2026-08-01T10:00:00.000',
+ }),
+ );
+ final stats = HarnessStats(store: store);
+ addTearDown(stats.dispose);
+ var notified = 0;
+ stats.addListener(() => notified++);
+ await stats.load();
+ expect(notified, 1);
+ stats.onAgentSpawned(at: DateTime(2026, 9, 27));
+ await stats.flush();
+ expect(written(store), {
+ 'version': 1,
+ 'agentsSpawned': 5,
+ 'turns': 9,
+ 'workedMs': 1000,
+ // The stored date stands: a later event does not move it.
+ 'firstEventAt': '2026-08-01T10:00:00.000',
+ });
+ },
+ );
+
+ test('nothing stored is nothing to load', () async {
+ final stats = HarnessStats(store: MemorySnapshotStore());
+ addTearDown(stats.dispose);
+ var notified = 0;
+ stats.addListener(() => notified++);
+ await stats.load();
+ expect(notified, 0);
+ });
+
+ test('a snapshot it cannot read is one it does not have', () async {
+ for (final contents in [
+ 'not json',
+ '[1, 2]',
+ jsonEncode({'version': 0, 'turns': 50}),
+ jsonEncode({
+ 'version': 1,
+ 'agentsSpawned': -3,
+ 'turns': 'many',
+ 'workedMs': 2.5e3,
+ 'firstEventAt': 17,
+ }),
+ ]) {
+ final store = MemorySnapshotStore(contents);
+ final stats = HarnessStats(store: store);
+ await stats.load();
+ await stats.flush();
+ final back = written(store);
+ expect(back['turns'], 0, reason: contents);
+ expect(back['agentsSpawned'], 0, reason: contents);
+ expect(back['firstEventAt'], isNull, reason: contents);
+ // A fractional or odd number is read as far as it goes, not dropped.
+ if (contents.contains('2500')) expect(back['workedMs'], 2500);
+ stats.dispose();
+ }
+ });
+ });
+
+ test('after dispose it counts quietly, notifying nobody', () {
+ final stats = HarnessStats(store: MemorySnapshotStore());
+ stats.dispose();
+ // A disposed ChangeNotifier throws when notified; this must not.
+ stats.onAgentSpawned();
+ stats.onTurnStarted('k');
+ stats.onTurnEnded('k');
+ });
+}