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.

21.7 KB
Raw Blame
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness_mobile/notify/system_notices.dart';
import 'package:harness_mobile/phone/out_of_sight.dart';
import 'package:harness_mobile/settings/experimental_features.dart';
import 'package:harness_mobile/swarm/swarm_activity_page.dart';
import 'package:harness_mobile/swarm/swarm_conversation_page.dart';
import 'package:harness_mobile/swarm/swarm_request.dart';
import 'package:harness_mobile/swarm/swarm_session.dart';
import 'package:harness_mobile/swarm/swarm_thread_page.dart';
import 'package:harness_mobile/swarm/swarm_threads_page.dart';

import '../voice_fakes.dart';
import 'swarm_activity_fixture.dart';
import 'swarm_fixture.dart';

/// The Activity screen (Tony's prototype C3): four filters, each with its
/// count; Approve, which asks first, and Reply on the row; rows the person
/// handled kept, dimmed, below the others; and nothing of a Swarm left on
/// screen once the person can no longer read it.
void main() {
  const work = 'work:$tab:g1:attempt-g1:needs_human';

  /// [home] under a signed-in shell's Swarm, Swarm on, on a 390-point phone.
  Future<SwarmHost> pump(
    WidgetTester tester,
    FakeSwarm fake, {
    Widget home = const SwarmActivityPage(),
    MemoryKeyValueStore? store,
  }) async {
    await tester.binding.setSurfaceSize(const Size(390, 844));
    addTearDown(() => tester.binding.setSurfaceSize(null));
    final features = ExperimentalFeaturesStore(storage: MemoryKeyValueStore())
      ..value = {PhoneExperiment.swarm};
    final account = ValueNotifier<String?>(me);
    final host = SwarmHost(
      accountId: () => account.value,
      account: account,
      request: fake.request,
      notices: SilentSystemNotices(),
      features: features,
      readMarks: store ?? MemoryKeyValueStore(),
    )..foreground = false;
    addTearDown(host.dispose);
    await tester.pumpWidget(
      MaterialApp(
        builder: (context, child) => SwarmScope(notifier: host, child: child!),
        home: home,
      ),
    );
    await tester.pumpAndSettle();
    return host;
  }

  Finder key(String value) => find.byKey(ValueKey(value));

  SwarmHost host(WidgetTester tester) =>
      tester.widget<SwarmScope>(find.byType(SwarmScope)).notifier!;

  int chatReads(FakeSwarm fake) =>
      fake.paths.where((p) => p.startsWith(activityChatPath)).length;

  /// Opens filter [name], scrolled into view first: the pills scroll
  /// sideways, as the prototype's do.
  Future<void> filter(WidgetTester tester, String name) async {
    await tester.ensureVisible(key('swarm-activity-filter-$name'));
    await tester.pumpAndSettle();
    await tester.tap(key('swarm-activity-filter-$name'));
    await tester.pumpAndSettle();
  }

  testWidgets('five filters, All first, each with its count, each a thumb '
      'tall', (tester) async {
    await pump(tester, activityBackend(page: activityPage));

    expect(find.text('Activity'), findsOneWidget);
    for (final (name, label, count) in [
      ('all', 'All', '4'),
      ('needsYou', 'Needs you', '2'),
      // A word a screen reader says, on the phone (2026-11-03).
      ('mentions', 'Mentions', '1'),
      ('threads', 'Threads', '1'),
      ('reactions', 'Reactions', '0'),
    ]) {
      final pill = key('swarm-activity-filter-$name');
      expect(
        find.descendant(of: pill, matching: find.text(label)),
        findsOneWidget,
      );
      expect(
        find.descendant(of: pill, matching: find.text(count)),
        findsOneWidget,
        reason: '$label carries its count',
      );
      expect(tester.getSize(pill).height, greaterThanOrEqualTo(44));
    }
  });

  testWidgets("the tab's top bar has Threads before You, where Home's Threads "
      'tile was (anh Dee, 2026-11-04); a tap opens the followed threads', (
    tester,
  ) async {
    await pump(
      tester,
      activityBackend(page: activityPage),
      home: const Scaffold(body: SwarmActivityView()),
    );

    final threads = tester.getRect(key('swarm-activity-threads'));
    final you = tester.getRect(key('swarm-you-button'));
    expect(threads.right, lessThanOrEqualTo(you.left));
    expect(threads.center.dy, closeTo(you.center.dy, 0.5));
    expect(threads.height, greaterThanOrEqualTo(44));
    expect(find.bySemanticsLabel('Threads'), findsWidgets);

    await tester.tap(key('swarm-activity-threads'));
    await tester.pumpAndSettle();
    expect(find.byType(SwarmThreadsPage), findsOneWidget);
  });

  testWidgets('Needs you: where and who over the ask, Approve on a decision, '
      'Reply on work', (tester) async {
    await pump(tester, activityBackend());

    final decision = key('swarm-activity-row-decision:d1');
    expect(
      find.descendant(of: decision, matching: find.text('#Mobile · River')),
      findsOneWidget,
    );
    expect(
      find.descendant(
        of: decision,
        matching: find.text('Approve merge: PR #2154?'),
      ),
      findsOneWidget,
    );
    final approve = key('swarm-activity-approve-decision:d1');
    final reply = key('swarm-activity-reply-$work');
    expect(approve, findsOneWidget);
    expect(reply, findsOneWidget);
    expect(key('swarm-activity-reply-decision:d1'), findsNothing);
    for (final button in [approve, reply]) {
      expect(tester.getSize(button).height, greaterThanOrEqualTo(44));
      expect(tester.getSize(button).width, greaterThanOrEqualTo(44));
    }
  });

  // Tony's phone list, task 26, as Desktop's decisions_view.dart.
  group('Answered', () {
    Map<String, dynamic> settled(
      String id,
      int minute, {
      bool delegated = false,
    }) => {
      'id': id,
      'projectId': 'lead',
      'projectName': 'Mobile',
      'conversationId': leadChat,
      'conversationTitle': 'Lead',
      'askerName': 'River',
      'question': 'Question $id?',
      'options': ['Merge', 'Wait'],
      'recommended': 0,
      'choice': 0,
      if (delegated) 'delegated': true,
      'createdAt': DateTime.utc(2026, 10, 29, 1, minute).toIso8601String(),
    };

    testWidgets('lists the newest 5, the coordinator\'s answer never as the '
        'person\'s own', (tester) async {
      final fake = activityBackend();
      fake.routes['/api/super-tabs/$tab/decisions?state=settled'] = (_) => {
        'decisions': [
          settled('a1', 1),
          settled('a2', 2, delegated: true),
          settled('a3', 3),
          settled('a4', 4),
          settled('a5', 5),
          settled('a6', 6),
        ],
      };
      await pump(tester, fake);
      await tester.scrollUntilVisible(
        key('swarm-answered'),
        200,
        scrollable: find
            .descendant(
              of: find.byType(ListView).last,
              matching: find.byType(Scrollable),
            )
            .first,
      );
      expect(find.text('Answered'), findsOneWidget);
      expect(
        find.text('Question a1?'),
        findsNothing,
        reason: 'the oldest of 6',
      );
      expect(find.text('Question a6?'), findsOneWidget);
      expect(find.text('You chose: Merge'), findsNWidgets(4));
      expect(find.text('Product Manager decided: Merge'), findsOneWidget);
    });

    testWidgets('says when it could not be read, and reads again on asking', (
      tester,
    ) async {
      final fake = activityBackend();
      var down = true;
      fake.routes['/api/super-tabs/$tab/decisions?state=settled'] = (_) {
        if (down) throw StateError('Offline');
        return {
          'decisions': [settled('a1', 1)],
        };
      };
      await pump(tester, fake);
      expect(
        find.text('Could not load answered decisions. Try again.'),
        findsOneWidget,
      );
      down = false;
      await tester.tap(key('swarm-answered-retry'));
      await tester.pumpAndSettle();
      expect(find.text('You chose: Merge'), findsOneWidget);
    });

    testWidgets('shows nothing with none answered', (tester) async {
      final fake = activityBackend();
      fake.routes['/api/super-tabs/$tab/decisions?state=settled'] = (_) => {
        'decisions': <Object>[],
      };
      await pump(tester, fake);
      expect(find.text('Answered'), findsNothing);
    });
  });

  testWidgets('Approve asks first, naming the option; Cancel sends nothing', (
    tester,
  ) async {
    final fake = activityBackend();
    await pump(tester, fake);

    await tester.tap(key('swarm-activity-approve-decision:d1'));
    await tester.pumpAndSettle();
    expect(find.text('Approve?'), findsOneWidget);
    expect(find.textContaining('“Merge”'), findsOneWidget);

    await tester.tap(key('swarm-activity-approve-cancel'));
    await tester.pump(const Duration(seconds: 5));
    await tester.pumpAndSettle();

    expect(fake.commands, isEmpty);
    expect(key('swarm-activity-approve-decision:d1'), findsOneWidget);
  });

  testWidgets('Approve confirmed is held for Undo, then sent, and the row '
      'stays as handled', (tester) async {
    final fake = activityBackend();
    final host = await pump(tester, fake);

    await tester.tap(key('swarm-activity-approve-decision:d1'));
    await tester.pumpAndSettle();
    await tester.tap(key('swarm-activity-approve-confirm'));
    await tester.pump();
    expect(find.text('Sending “Merge”'), findsOneWidget);
    expect(key('swarm-undo-d1'), findsOneWidget);

    fake.routes['/api/super-tabs/$tab/decisions?state=open'] = (_) => {
      'decisions': <Object>[],
    };
    await tester.pump(const Duration(seconds: 4));
    await tester.pumpAndSettle();
    expect(fake.commands.single, {
      'type': 'decision.answer',
      'decisionId': 'd1',
      'choice': 1,
    });
    await host.session!.inbox.refresh();
    await tester.pumpAndSettle();

    expect(key('swarm-activity-handled-decision:d1'), findsOneWidget);
    expect(find.text('Approved'), findsOneWidget);
    expect(
      find.descendant(
        of: key('swarm-activity-filter-needsYou'),
        matching: find.text('1'),
      ),
      findsOneWidget,
    );
  });

  testWidgets('Reply opens where it was asked; back, the row is handled', (
    tester,
  ) async {
    await pump(tester, activityBackend());

    await tester.tap(key('swarm-activity-reply-$work'));
    await tester.pumpAndSettle();
    expect(find.byType(SwarmConversationPage), findsOneWidget);
    expect(
      tester
          .widget<SwarmConversationPage>(find.byType(SwarmConversationPage))
          .conversationId,
      'g1',
    );

    await tester.tap(find.byKey(const ValueKey('tty-back')).first);
    await tester.pumpAndSettle();
    expect(key('swarm-activity-handled-$work'), findsOneWidget);
    expect(find.text('Opened'), findsOneWidget);
    expect(
      find.descendant(
        of: key('swarm-activity-filter-needsYou'),
        matching: find.text('1'),
      ),
      findsOneWidget,
    );
  });

  testWidgets('a row tapped opens in full, with every option', (tester) async {
    await pump(tester, activityBackend());

    await tester.tap(key('swarm-activity-row-decision:d1'));
    await tester.pumpAndSettle();

    expect(key('swarm-option-d1-0'), findsOneWidget);
    expect(key('swarm-option-d1-1'), findsOneWidget);
  });

  testWidgets('@: the tags, Reply opening the chat at the tag', (tester) async {
    await pump(tester, activityBackend());
    await filter(tester, 'mentions');

    expect(find.text('Can you check the copy?'), findsOneWidget);
    await tester.tap(key('swarm-activity-reply-mention:m1'));
    await tester.pumpAndSettle();

    final page = tester.widget<SwarmConversationPage>(
      find.byType(SwarmConversationPage),
    );
    expect((page.conversationId, page.messageId), (leadChat, 'msg-tag'));
    expectHomeCProject('Mobile');
  });

  testWidgets('Threads: the person\'s threads, the one with news first', (
    tester,
  ) async {
    await pump(tester, activityBackend(page: activityPage));
    await filter(tester, 'threads');

    final open = key('swarm-activity-row-thread:$tab:root-1');
    expect(
      find.descendant(of: open, matching: find.text('Ship the build tonight?')),
      findsOneWidget,
    );
    expect(
      find.descendant(of: open, matching: find.text('2 replies')),
      findsOneWidget,
    );
    expect(key('swarm-activity-handled-thread:$tab:root-old'), findsOneWidget);
    expect(
      tester.getTopLeft(open).dy,
      lessThan(
        tester.getTopLeft(key('swarm-activity-row-thread:$tab:root-old')).dy,
      ),
    );

    await tester.tap(key('swarm-activity-reply-thread:$tab:root-1'));
    await tester.pumpAndSettle();
    // The thread is open over its chat, which is at the root behind it.
    expect(find.byType(SwarmThreadPage), findsOneWidget);
    final page = tester.widget<SwarmConversationPage>(
      find.byType(SwarmConversationPage, skipOffstage: false),
    );
    expect((page.conversationId, page.messageId), (leadChat, 'root-1'));
    expect(page.threadId, 'root-1');
  });

  testWidgets("Reactions: who reacted how to the person's message", (
    tester,
  ) async {
    final store = MemoryKeyValueStore()
      ..values['swarm_activity.$me.$tab'] = '{"marks":{}}';
    await pump(tester, activityBackend(page: activityPage), store: store);
    await filter(tester, 'reactions');

    final row = key('swarm-activity-row-reaction:$tab:mine-2');
    expect(
      find.descendant(of: row, matching: find.text('Copy is fixed.')),
      findsOneWidget,
    );
    expect(
      find.descendant(of: row, matching: find.textContaining('River')),
      findsWidgets,
    );
    expect(
      find.descendant(of: row, matching: find.textContaining('✅ 1')),
      findsOneWidget,
    );
    expect(key('swarm-activity-row-reaction:$tab:own-only'), findsNothing);
  });

  testWidgets('the same agent id in two Swarms: each row is named in its '
      'own Swarm', (tester) async {
    final fake = activityBackend(page: activityPage);
    activitySecondSwarm(fake, page: activityPage);
    await pump(tester, fake);
    await filter(tester, 'threads');

    Finder caption(String row, String text) => find.descendant(
      of: key('swarm-activity-row-$row'),
      matching: find.textContaining(text),
    );
    await tester.ensureVisible(key('swarm-activity-row-thread:tab-2:root-1'));
    expect(caption('thread:$tab:root-1', 'Nova'), findsOneWidget);
    expect(caption('thread:$tab:root-1', 'Pip'), findsNothing);
    expect(caption('thread:tab-2:root-1', 'Pip'), findsOneWidget);
    expect(caption('thread:tab-2:root-1', 'Nova'), findsNothing);
  });

  testWidgets('a filter with nothing in it says so', (tester) async {
    await pump(tester, activityBackend());
    await filter(tester, 'reactions');

    expect(key('swarm-activity-none'), findsOneWidget);
    expect(find.text('No reactions to you yet.'), findsOneWidget);
  });

  testWidgets('a Swarm the person can no longer read leaves the screen at '
      'once, with nothing of it left', (tester) async {
    final fake = activityBackend(page: activityPage);
    final host = await pump(tester, fake);
    expect(key('swarm-activity-row-decision:d1'), findsOneWidget);

    fake.routes['/api/super-tabs/list'] = (_) => {
      'tabs': <Object>[],
      'next': null,
    };
    fake.routes['/api/super-tabs/invitations'] = (_) => <Object>[];
    await host.session!.inbox.refresh();
    await tester.pumpAndSettle();

    expect(find.textContaining('Mobile'), findsNothing);
    expect(find.textContaining('River'), findsNothing);
    expect(key('swarm-activity-none'), findsOneWidget);
    await filter(tester, 'threads');
    expect(find.text('Ship the build tonight?'), findsNothing);
    expect(key('swarm-activity-loading'), findsNothing);
  });

  testWidgets('a project chat that cannot be read says so, and Try again '
      'reads it again', (tester) async {
    final fake = activityBackend();
    fake.routes[activityChatPath] = (_) =>
        throw const SuperTabFailure('MACHINE_UNAVAILABLE', 'No computer.');
    await pump(tester, fake);
    await filter(tester, 'threads');

    expect(find.text('Some chats could not be read.'), findsOneWidget);
    expect(key('swarm-activity-loading'), findsNothing);

    fake.routes[activityChatPath] = (_) => activityPage();
    await tester.tap(key('swarm-activity-retry'));
    await tester.pumpAndSettle();

    expect(find.text('Some chats could not be read.'), findsNothing);
    expect(find.text('Ship the build tonight?'), findsOneWidget);
  });

  testWidgets('out of sight, it reads no chat; in sight again, it does', (
    tester,
  ) async {
    final fake = activityBackend(page: activityPage);
    final hidden = ValueNotifier(true);
    addTearDown(hidden.dispose);
    await pump(
      tester,
      fake,
      home: Scaffold(
        body: ValueListenableBuilder(
          valueListenable: hidden,
          builder: (_, out, _) =>
              OutOfSight(hidden: out, child: const SwarmActivityView()),
        ),
      ),
    );
    await host(tester).session!.inbox.refresh();
    await tester.pumpAndSettle();
    expect(chatReads(fake), 0);

    hidden.value = false;
    await tester.pumpAndSettle();
    expect(chatReads(fake), 1);
  });

  group('opened at a decision (a notice or link)', () {
    Future<SwarmHost> at(
      WidgetTester tester,
      FakeSwarm fake, {
      String focus = 'decision:d1',
      String swarmId = tab,
    }) => pump(
      tester,
      fake,
      home: SwarmActivityPage(focus: focus, swarmId: swarmId),
    );

    testWidgets('it opens in full, every option and Done there', (
      tester,
    ) async {
      await at(tester, activityBackend());

      expect(key('swarm-activity-open-decision:d1'), findsOneWidget);
      expect(key('swarm-option-d1-0'), findsOneWidget);
      expect(key('swarm-option-d1-1'), findsOneWidget);
      expect(key('swarm-activity-focus-gone'), findsNothing);
    });

    testWidgets('one no longer open, another Swarm\'s, or never the '
        'person\'s says so alike, naming nothing of it', (tester) async {
      for (final (focus, swarmId) in [
        ('decision:gone', tab),
        ('decision:d1', 'tab-other'),
        ('decision:theirs', 'tab-other'),
      ]) {
        await tester.pumpWidget(const SizedBox());
        await at(tester, activityBackend(), focus: focus, swarmId: swarmId);

        expect(
          find.text('This decision is no longer open.'),
          findsOneWidget,
          reason: '$focus in $swarmId',
        );
        expect(key('swarm-activity-open-decision:d1'), findsNothing);
        expect(key('swarm-activity-loading'), findsNothing);
        if (swarmId != tab) {
          expect(find.textContaining('Approve merge'), findsNothing);
          expect(find.textContaining('Mobile'), findsNothing);
        }
      }
    });

    testWidgets('until the decisions are read it says nothing of it; a read '
        'that fails says so, and Try again brings the decision', (
      tester,
    ) async {
      final fake = activityBackend();
      final held = Completer<Object?>();
      fake.routes['/api/super-tabs/$tab/decisions?state=open'] = (_) =>
          held.future;
      await pump(
        tester,
        fake,
        home: const SwarmActivityPage(focus: 'decision:d1', swarmId: tab),
      );
      expect(key('swarm-activity-focus-gone'), findsNothing);
      expect(key('swarm-activity-focus-failed'), findsNothing);

      held.completeError(const SuperTabFailure('UNAVAILABLE', 'Down.'));
      await tester.pumpAndSettle();
      expect(key('swarm-activity-focus-failed'), findsOneWidget);
      expect(key('swarm-activity-focus-gone'), findsNothing);

      final first = activityBackend();
      fake.routes['/api/super-tabs/$tab/decisions?state=open'] =
          first.routes['/api/super-tabs/$tab/decisions?state=open']!;
      await tester.tap(key('swarm-activity-focus-retry'));
      await tester.pumpAndSettle();
      expect(key('swarm-activity-focus-failed'), findsNothing);
      expect(key('swarm-activity-open-decision:d1'), findsOneWidget);
    });

    testWidgets('a read that answers after access was lost shows nothing of '
        'it', (tester) async {
      final fake = activityBackend();
      final held = Completer<Object?>();
      fake.routes['/api/super-tabs/$tab/decisions?state=open'] = (_) =>
          held.future;
      final host = await at(tester, fake);
      host.session!.inbox.reader(tab)!.revoke();
      await tester.pump();
      held.complete(
        activityBackend().routes['/api/super-tabs/$tab/decisions?state=open']!(
          null,
        ),
      );
      await tester.pumpAndSettle();

      expect(key('swarm-activity-open-decision:d1'), findsNothing);
      expect(find.textContaining('Approve merge'), findsNothing);
      expect(find.text('This decision is no longer open.'), findsOneWidget);
    });

    testWidgets('access lost while it is answered: the row goes and the '
        'answer held for Undo is never sent', (tester) async {
      final fake = activityBackend();
      final host = await at(tester, fake);
      await tester.tap(key('swarm-option-d1-1'));
      await tester.pump();
      expect(find.text('Sending “Merge”'), findsOneWidget);

      host.session!.inbox.reader(tab)!.revoke();
      await tester.pump(const Duration(seconds: 10));
      await tester.pumpAndSettle();

      expect(fake.commands, isEmpty);
      expect(key('swarm-activity-open-decision:d1'), findsNothing);
      expect(find.textContaining('Approve merge'), findsNothing);
      expect(find.textContaining('Sending'), findsNothing);
    });

    testWidgets('access lost while Approve asks: the question closes and '
        'nothing is sent', (tester) async {
      final fake = activityBackend();
      final host = await pump(tester, fake);
      await tester.tap(key('swarm-activity-approve-decision:d1'));
      await tester.pumpAndSettle();
      expect(find.text('Approve?'), findsOneWidget);

      host.session!.inbox.reader(tab)!.revoke();
      await tester.pumpAndSettle();
      expect(find.text('Approve?'), findsNothing);
      expect(find.textContaining('Approve merge'), findsNothing);
      expect(fake.commands, isEmpty);
    });
  });
}