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.

31.3 KB
Raw Blame
import 'dart:async';

import 'package:clock/clock.dart';
import 'package:fake_async/fake_async.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/shared/theme/app_theme.dart' as grid;
import 'package:harness/super_tabs/activity.dart';
import 'package:harness/super_tabs/attention.dart';
import 'package:harness/super_tabs/buddy_rows.dart';
import 'package:harness/super_tabs/controller.dart';
import 'package:harness/super_tabs/model.dart';
import 'package:harness/super_tabs/parts.dart';
import 'package:harness/super_tabs/project_sidebar.dart';
import 'package:harness/super_tabs/view.dart';
import 'package:harness/widgets/desktop_chrome.dart';

import 'super_tab_attention_test.dart'
    show
        close,
        conversation,
        fromLead,
        inRow,
        openHere,
        projectServer,
        rowText,
        unreadThread;
import 'support/buddy_fixture.dart';
import 'super_tab_activity_lists_test.dart' show SuperTabActivityTotals;
import 'support/super_tab_fixture.dart';

Future<SuperTabController> open(ConversationServer server) async {
  final c = server.controller();
  addTearDown(() async {
    await c.flush();
    c.dispose();
  });
  await c.refresh();
  return c;
}

Future<void> settle() async {
  for (var i = 0; i < 6; i++) {
    await pumpEventQueue();
  }
}

Future<void> pumpSidebar(
  WidgetTester tester,
  SuperTabController c, {
  Brightness brightness = Brightness.dark,
}) async {
  await tester.binding.setSurfaceSize(const Size(320, 900));
  addTearDown(() => tester.binding.setSurfaceSize(null));
  await tester.pumpWidget(
    MaterialApp(
      theme: grid.buildAppTheme(brightness: brightness),
      home: Scaffold(
        body: ListenableBuilder(
          listenable: c,
          builder: (context, _) => ProjectSidebar(
            controller: c,
            onSelect: c.select,
            attention: SuperTabAttention.of(c),
            activity: SuperTabActivity.of(c),
            onPage: (_) {},
          ),
        ),
      ),
    ),
  );
  await tester.pumpAndSettle();
}

void main() {
  group('the read model keeps the reader\'s chat settings', () {
    Map<String, dynamic> load(Map<String, dynamic> derived) => {
      'format': superTabReadFormat,
      'tabId': 'tab',
      'cursor': 'c1',
      'epoch': 1,
      'lastChangedAt': 1,
      'head': {
        'key': 'head',
        'version': 1,
        'changedAt': 1,
        'data': {
          'id': 'tab',
          'name': 'Swarm',
          'ownerId': 'owner',
          'projects': <dynamic>[],
          'invitations': <dynamic>[],
        },
      },
      'groups': [
        {
          'key': 'lead',
          'version': 1,
          'changedAt': 1,
          'data': conversationFixture('lead', 'Lead'),
        },
      ],
      'derived': derived,
    };

    test('a change read without them keeps them; a moved one replaces it; '
        'the conversation gone takes it', () {
      final parts = SuperTabParts.firstLoad(
        load({
          'activityDigest': 'd1',
          'chatSettings': {'lead': 'muted', 'gone': 'loud'},
        }),
      );
      expect(parts.state['chatSettings'], {'lead': 'muted'});
      expect(parts.state['activityDigest'], 'd1');
      var change = parts.apply({
        'cursor': 'c2',
        'derived': {'activityDigest': 'd2'},
      });
      expect(parts.state['chatSettings'], {'lead': 'muted'});
      expect(parts.state['activityDigest'], 'd2');
      change = parts.apply({
        'cursor': 'c3',
        'derived': {
          'activityDigest': 'd2',
          'chatSettings': {'lead': 'all'},
        },
      });
      expect(change.changed, isTrue);
      expect(parts.state['chatSettings'], {'lead': 'all'});
      // Kept on this device and read back.
      final back = SuperTabParts.fromJson(parts.toJson());
      expect(back.state['chatSettings'], {'lead': 'all'});
      parts.apply({
        'cursor': 'c4',
        'groups': [
          {'key': 'lead', 'version': 2, 'changedAt': 4, 'deleted': true},
        ],
      });
      expect(parts.state['chatSettings'], isNull);
      // A setting the app does not know reads as the default.
      expect(SuperTabNotify.parse('muted'), SuperTabNotify.muted);
      expect(SuperTabNotify.parse('all'), SuperTabNotify.all);
      expect(SuperTabNotify.parse('loud'), SuperTabNotify.mentions);
    });
  });

  group('per-chat notification setting', () {
    test('shows at once, is kept on the server, and a failure puts the old '
        'setting back', () async {
      final server = projectServer();
      final c = await open(server);
      expect(c.notifyOf('offline'), SuperTabNotify.mentions);
      server.holdNotify = Completer();
      final kept = c.setNotify('offline', SuperTabNotify.muted);
      expect(c.notifyOf('offline'), SuperTabNotify.muted);
      server.holdNotify!.complete();
      expect(await kept, isTrue);
      expect(server.notifyPosts.last, {
        'conversationId': 'offline',
        'notify': 'muted',
      });
      // Still muted until a read carries it, and after.
      expect(c.notifyOf('offline'), SuperTabNotify.muted);
      await c.refresh();
      expect(c.snapshot!.chatSettings['offline'], SuperTabNotify.muted);
      expect(c.notifyOf('offline'), SuperTabNotify.muted);
      server.failNotify = true;
      expect(await c.setNotify('offline', SuperTabNotify.all), isFalse);
      expect(c.notifyOf('offline'), SuperTabNotify.muted);
    });

    test('another device\'s change wins once a read carries it', () async {
      final server = projectServer();
      final c = await open(server);
      await c.setNotify('offline', SuperTabNotify.muted);
      await c.refresh();
      (server.state['chatSettings'] as Map)['owner']['offline'] = 'all';
      server.revision++;
      await c.refresh();
      expect(c.notifyOf('offline'), SuperTabNotify.all);
    });

    test(
      'access lost while a setting is on its way: nothing of it stays',
      () async {
        final server = projectServer();
        final c = await open(server);
        server.holdNotify = Completer();
        final kept = c.setNotify('offline', SuperTabNotify.muted);
        expect(c.notifyOf('offline'), SuperTabNotify.muted);
        c.revokeAccess('Removed from this Swarm.');
        // At once, not when the answer comes.
        expect(c.notifyOf('offline'), SuperTabNotify.mentions);
        server.holdNotify!.complete();
        await kept;
        expect(c.notifyOf('offline'), SuperTabNotify.mentions);
      },
    );

    test('a muted chat is never unread in the sidebar and its tags never '
        'count', () async {
      final server = projectServer();
      fromLead(server, 'News', conversationId: 'offline');
      server.tag('message-1', conversationId: 'offline');
      final c = await open(server);
      final attention = SuperTabAttention.of(c);
      await attention.mentions.load();
      final group = c.snapshot!.conversation('offline')!;
      expect(c.unreadShown(group), isTrue);
      expect(attention.mentionsIn('offline'), 1);
      await c.setNotify('offline', SuperTabNotify.muted);
      expect(c.unread(group), isTrue);
      expect(c.unreadShown(group), isFalse);
      expect(attention.mentionsIn('offline'), 0);
    });

    test('a muted chat\'s thread tags never count on Threads either; '
        'unmuted, they count again', () async {
      final server = projectServer();
      server.followedThreads = {
        'unread': [
          unreadThread('message-1', 2),
          unreadThread('message-1', 2, conversationId: 'offline'),
        ],
        'read': [],
        'more': false,
      };
      fromLead(server, 'Root', conversationId: 'offline');
      server.tag(
        'message-1',
        conversationId: 'offline',
        threadId: 'message-1',
        messageSequence: 2,
      );
      server.tag('message-1', threadId: 'message-1', messageSequence: 2);
      final c = await open(server);
      final attention = SuperTabAttention.of(c);
      await attention.mentions.load();
      await attention.load();
      expect(attention.threadMentions, 2);
      await c.setNotify('offline', SuperTabNotify.muted);
      // Only the tag in the chat that is not muted.
      expect(attention.threadMentions, 1);
      await c.setNotify('offline', SuperTabNotify.all);
      expect(attention.threadMentions, 2);
    });
  });

  group('Mark all read (Shift+Esc) holds its write for Undo', () {
    test('marks every unread chat here at once, sends nothing until the hold '
        'ends, then sends each', () {
      fakeAsync((async) {
        final server = projectServer();
        fromLead(server, 'One');
        fromLead(server, 'Two', conversationId: 'offline');
        late SuperTabController c;
        c = server.controller();
        unawaited(c.refresh());
        async.flushMicrotasks();
        final groups = c.snapshot!.conversations;
        expect(groups.where(c.unreadShown).length, 2);
        final held = c.markAllRead()!;
        expect(held.count, 2);
        expect(groups.where(c.unreadShown), isEmpty);
        async.elapse(const Duration(seconds: 5));
        expect(server.readMarkPosts, isEmpty);
        async.elapse(const Duration(seconds: 2));
        async.flushMicrotasks();
        expect(server.readMarkPosts.map((p) => p['conversationId']).toSet(), {
          'lead',
          'offline',
        });
        unawaited(c.flush());
        async.flushMicrotasks();
        c.dispose();
      });
    });

    test('Undo puts each chat back as it was and sends nothing', () {
      fakeAsync((async) {
        final server = projectServer();
        fromLead(server, 'One', conversationId: 'offline');
        final c = server.controller();
        unawaited(c.refresh());
        async.flushMicrotasks();
        final group = c.snapshot!.conversation('offline')!;
        final held = c.markAllRead()!;
        expect(c.unreadShown(group), isFalse);
        held.undo();
        expect(c.unreadShown(group), isTrue);
        async.elapse(const Duration(seconds: 10));
        async.flushMicrotasks();
        expect(server.readMarkPosts, isEmpty);
        expect(c.markAllRead(), isNotNull);
        unawaited(c.flush());
        async.flushMicrotasks();
        c.dispose();
      });
    });

    test('access lost during the hold sends nothing; closing the Swarm sends '
        'the marks', () {
      fakeAsync((async) {
        final server = projectServer();
        fromLead(server, 'One', conversationId: 'offline');
        final c = server.controller();
        unawaited(c.refresh());
        async.flushMicrotasks();
        c.markAllRead();
        c.revokeAccess('Removed from this Swarm.');
        async.elapse(const Duration(seconds: 10));
        async.flushMicrotasks();
        expect(server.readMarkPosts, isEmpty);
        c.dispose();

        final again = projectServer();
        fromLead(again, 'One', conversationId: 'offline');
        final d = again.controller();
        unawaited(d.refresh());
        async.flushMicrotasks();
        d.markAllRead();
        d.dispose();
        async.flushMicrotasks();
        expect(again.readMarkPosts.map((p) => p['conversationId']), [
          'offline',
        ]);
      });
    });

    test(
      'a muted chat is left as it is; nothing unread gives nothing',
      () async {
        final server = projectServer();
        fromLead(server, 'One', conversationId: 'offline');
        final c = await open(server);
        await c.setNotify('offline', SuperTabNotify.muted);
        expect(c.markAllRead(), isNull);
      },
    );
  });

  group('next unread chat follows the sidebar\'s order', () {
    test('projects\' chats A to Z; their tasks are in the Tasks view, not '
        'here', () async {
      final server = projectServer();
      (server.state['projects'] as List).add(
        projectFixture('alpha', 'alpha', 'alpha-chat'),
      );
      (server.state['conversations'] as List)
        ..add({
          ...conversationFixture('alpha-chat', 'alpha'),
          'projectId': 'alpha',
        })
        ..add({
          ...conversationFixture('newer', 'Newer'),
          'projectId': 'lead',
          'number': 20,
        });
      final c = await open(server);
      expect(superTabSidebarOrder(c), ['alpha-chat', 'lead']);
    });
  });

  group('All new messages tells the system about each new message', () {
    test('only for a chat set to all, not on the first look, not the open '
        'chat', () async {
      final server = projectServer();
      final c = await open(server);
      final told = <(String, int)>[];
      SuperTabChatNotices.of(c).onMessage = (g, s) => told.add((g.id, s));
      fromLead(server, 'Before', conversationId: 'offline');
      await c.refresh();
      await settle();
      expect(told, isEmpty);
      await c.setNotify('offline', SuperTabNotify.all);
      fromLead(server, 'After', conversationId: 'offline');
      await c.refresh();
      await settle();
      expect(told.map((t) => t.$1), ['offline']);
      fromLead(server, 'In the project chat');
      await c.refresh();
      await settle();
      expect(told.map((t) => t.$1), ['offline']);
    });

    Future<(SuperTabController, ConversationServer, List<int>)>
    openChatSetToAll() async {
      final binding = TestWidgetsFlutterBinding.ensureInitialized();
      binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
      final server = projectServer();
      final c = await open(server);
      final told = <int>[];
      SuperTabChatNotices.of(c).onMessage = (_, s) => told.add(s);
      await c.setNotify(c.selectedId, SuperTabNotify.all);
      return (c, server, told);
    }

    test('the open chat is told while its Swarm is away', () async {
      final (c, server, told) = await openChatSetToAll();
      c.away = true;
      fromLead(server, 'While on another Swarm');
      await c.refresh();
      await settle();
      expect(told, hasLength(1));
    });

    test('the open chat is told while another app is in front', () async {
      final (c, server, told) = await openChatSetToAll();
      final binding = TestWidgetsFlutterBinding.instance;
      addTearDown(
        () => binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed),
      );
      binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
      fromLead(server, 'While in another app');
      await c.refresh();
      await settle();
      expect(told, hasLength(1));
    });

    // Task #2224: what a person sent from their phone, the web or another Mac tells them nothing.
    Future<(SuperTabController, ConversationServer, List<int>)>
    chatSetToAll() async {
      final server = projectServer();
      final c = await open(server);
      final told = <int>[];
      SuperTabChatNotices.of(c).onMessage = (_, s) => told.add(s);
      await c.setNotify('offline', SuperTabNotify.all);
      return (c, server, told);
    }

    test('their own message from another device tells them nothing', () async {
      final (c, server, told) = await chatSetToAll();
      server.append('From my phone', conversationId: 'offline');
      await c.refresh();
      await settle();
      expect(told, isEmpty);
    });

    // Task #2265: the server moves its author's mark in the commit that wrote it.
    test('their own message, with the mark the server moved, is no news: no '
        'notice, not bold, and no read to find out whose it was', () async {
      final (c, server, told) = await chatSetToAll();
      server.append('From my phone', conversationId: 'offline');
      ((server.state['readMarks'] ??= <String, dynamic>{}) as Map).update(
        'owner',
        (marks) => {...marks as Map, 'offline': server.revision},
        ifAbsent: () => {'offline': server.revision},
      );
      final reads = <String>[];
      server.onRead = reads.add;
      await c.refresh();
      await settle();
      expect(told, isEmpty);
      expect(c.unread(c.snapshot!.conversation('offline')!), isFalse);
      expect(reads.where((p) => p.contains('offline/messages')), isEmpty);
    });

    test(
      'a message whose author can\'t be read is told about, never lost',
      () async {
        final server = projectServer();
        final c = SuperTabController(
          tabId: 'tab',
          accountId: 'owner',
          save: server.save,
          request: (method, path, {body}) async {
            if (path.contains('offline/messages?after=')) {
              throw StateError('Offline');
            }
            return server.request(method, path, body: body);
          },
        );
        addTearDown(c.dispose);
        await c.refresh();
        final told = <int>[];
        SuperTabChatNotices.of(c).onMessage = (_, s) => told.add(s);
        await c.setNotify('offline', SuperTabNotify.all);
        fromLead(server, 'From the Lead', conversationId: 'offline');
        await c.refresh();
        await settle();
        expect(told, hasLength(1));
      },
    );

    // Review of #2646: a chat's top is its main stream, and agents answer in threads.
    test(
      'a teammate\'s thread reply before their own message tells nothing',
      () async {
        final (c, server, told) = await chatSetToAll();
        fromLead(server, 'In a thread', conversationId: 'offline');
        server.messages.last['thread'] = 'a-root';
        server.append('From my phone', conversationId: 'offline');
        await c.refresh();
        await settle();
        expect(told, isEmpty);
      },
    );

    test('a page with more behind it is unsure, so it tells', () async {
      final server = projectServer();
      final c = SuperTabController(
        tabId: 'tab',
        accountId: 'owner',
        save: server.save,
        request: (method, path, {body}) async {
          final answer = await server.request(method, path, body: body);
          return path.contains('offline/messages?after=')
              ? {
                  ...answer,
                  'next': {'sequence': 1, 'id': 'more'},
                }
              : answer;
        },
      );
      addTearDown(c.dispose);
      await c.refresh();
      final told = <int>[];
      SuperTabChatNotices.of(c).onMessage = (_, s) => told.add(s);
      await c.setNotify('offline', SuperTabNotify.all);
      server.append('From my phone', conversationId: 'offline');
      await c.refresh();
      await settle();
      expect(told, hasLength(1));
    });

    test('a teammate\'s message still does', () async {
      final (c, server, told) = await chatSetToAll();
      fromLead(server, 'From the Lead', conversationId: 'offline');
      await c.refresh();
      await settle();
      expect(told, hasLength(1));
    });

    test('a teammate\'s message among their own still does', () async {
      final (c, server, told) = await chatSetToAll();
      fromLead(server, 'From the Lead', conversationId: 'offline');
      server.append('From my phone', conversationId: 'offline');
      await c.refresh();
      await settle();
      expect(told, hasLength(1));
    });

    test('the open chat on screen in a focused app is not told', () async {
      final (c, server, told) = await openChatSetToAll();
      fromLead(server, 'While reading it');
      await c.refresh();
      expect(told, isEmpty);
    });
  });

  group('sidebar', () {
    for (final brightness in Brightness.values) {
      testWidgets('Mentions is bold with the tag; a '
          'muted chat is greyed with its mark and no number ($brightness)', (
        tester,
      ) async {
        final server = projectServer();
        fromLead(server, 'News');
        server.tag('message-1');
        final c = await openHere(server);
        await SuperTabAttention.of(c).mentions.load();
        await c.setNotify('lead', SuperTabNotify.muted);
        await pumpSidebar(tester, c, brightness: brightness);
        expect(find.text('Notifications'), findsNothing);
        expect(inRow('super-mentions', find.text('Mentions')), findsOneWidget);
        // Mentions is bold for the unread tag (#2887) and carries no number
        // (task #3128); the muted chat's own row carries none either.
        expect(inRow('super-mentions', find.text('1')), findsNothing);
        expect(
          rowText(tester, 'super-mentions', 'Mentions').style?.fontWeight,
          grid.AppFont.semibold,
        );
        expect(find.byKey(const ValueKey('super-muted-lead')), findsOneWidget);
        expect(
          inRow('super-group-lead', find.byType(SuperTabCountBadge)),
          findsNothing,
        );
        // The project's name, drawn in the row's style: never bold, though
        // its chat has news.
        final name = rowText(tester, 'super-group-lead', 'web-project');
        expect(name.style?.fontWeight, isNot(grid.AppFont.semibold));
        await close(tester, c);
      });
    }

    testWidgets('the unread filter lists only unread chats and the open one, '
        'and hides muted ones', (tester) async {
      final server = projectServer();
      // Projects of their own: the sidebar lists project chats, not tasks.
      for (final id in ['quiet', 'hushed', 'news']) {
        (server.state['projects'] as List).add(
          projectFixture(id, '$id-project', id),
        );
        (server.state['conversations'] as List).add({
          ...conversationFixture(id, id),
          'projectId': id,
        });
      }
      fromLead(server, 'Read', conversationId: 'quiet');
      server.markReadElsewhere('quiet', server.revision);
      fromLead(server, 'News', conversationId: 'news');
      fromLead(server, 'Hushed news', conversationId: 'hushed');
      final c = await openHere(server);
      await c.setNotify('hushed', SuperTabNotify.muted);
      await pumpSidebar(tester, c);
      expect(find.byKey(const ValueKey('super-group-quiet')), findsOneWidget);
      await tester.tap(find.byKey(const ValueKey('super-unread-only')));
      await tester.pumpAndSettle();
      expect(c.unreadOnly, isTrue);
      expect(find.byKey(const ValueKey('super-group-news')), findsOneWidget);
      // The open chat stays listed.
      expect(
        find.byKey(ValueKey('super-group-${c.selectedId}')),
        findsOneWidget,
      );
      expect(find.byKey(const ValueKey('super-group-quiet')), findsNothing);
      expect(find.byKey(const ValueKey('super-group-hushed')), findsNothing);
      await close(tester, c);
    });
  });

  group('the one Buddy row (#2447) follows the same rules', () {
    Future<SuperTabController> show(
      WidgetTester tester,
      ConversationServer server,
    ) async {
      await tester.binding.setSurfaceSize(const Size(1280, 800));
      addTearDown(() => tester.binding.setSurfaceSize(null));
      final c = server.controller(account: 'peer');
      await c.refresh();
      await tester.pumpWidget(
        MaterialApp(
          theme: grid.buildAppTheme(brightness: Brightness.dark),
          home: Scaffold(
            body: SuperTabView(
              controller: c,
              onInvite: () {},
              onAddAgent: (_) {},
              onOpenTerminal: (_) {},
              machineName: (_) => 'Kens-MacBook',
            ),
          ),
        ),
      );
      await tester.pumpAndSettle();
      return c;
    }

    final buddyRow = find.byKey(const ValueKey('super-swarm-buddy'));
    Text buddyName(WidgetTester tester) => tester.widget<Text>(
      find.descendant(of: buddyRow, matching: find.text('Your agent')),
    );

    testWidgets('muted: greyed with its mark, never bold; first in the next '
        'unread order', (tester) async {
      final server = oneBuddySwarm();
      giveSwarmBuddy(server);
      server.append('A word from Wren', conversationId: 'swarm-buddy-peer');
      final c = await show(tester, server);
      expect(superTabSidebarOrder(c).first, 'swarm-buddy-peer');
      expect(buddyName(tester).style?.fontWeight, grid.AppFont.semibold);
      await c.setNotify('swarm-buddy-peer', SuperTabNotify.muted);
      await tester.pumpAndSettle();
      expect(buddyName(tester).style?.color, DesktopChrome.muted);
      expect(buddyName(tester).style?.fontWeight, isNot(grid.AppFont.semibold));
      expect(
        find.byKey(const ValueKey('super-muted-swarm-buddy-peer')),
        findsOneWidget,
      );
      expect(
        c.unreadShown(c.snapshot!.conversation('swarm-buddy-peer')!),
        isFalse,
      );
      await tester.pumpWidget(const SizedBox());
      await c.flush();
      c.dispose();
    });

    testWidgets('the unread filter keeps it shown, bold only while it has '
        'something unread', (tester) async {
      final server = oneBuddySwarm();
      giveSwarmBuddy(server);
      final c = await show(tester, server);
      expect(buddyRow, findsOneWidget);
      c.toggleUnreadOnly();
      await tester.pumpAndSettle();
      expect(c.unreadOnly, isTrue);
      expect(buddyRow, findsOneWidget);
      expect(buddyName(tester).style?.fontWeight, isNot(grid.AppFont.semibold));
      server.append('A word from Wren', conversationId: 'swarm-buddy-peer');
      await c.refresh();
      await tester.pumpAndSettle();
      expect(buddyRow, findsOneWidget);
      expect(buddyName(tester).style?.fontWeight, grid.AppFont.semibold);
      await tester.pumpWidget(const SizedBox());
      await c.flush();
      c.dispose();
    });
  });

  group('Needs you counts what was asked of the reader', () {
    ConversationServer withAgent() {
      final server = projectServer();
      server.state['agents'] = [
        {
          'id': 'kestrel',
          'projectId': 'lead',
          'name': 'Builder',
          'displayName': 'Kestrel',
          'machineId': 'host',
          'harnessId': 'session',
          'contributorId': 'owner',
          'configuration': {'engine': 'codex'},
        },
      ];
      conversation(server, 'offline')['agentIds'] = ['kestrel'];
      return server;
    }

    void assign(
      ConversationServer server,
      String status, {
      String? askedOf,
      String? requesterId,
    }) {
      final group = conversation(server, 'offline');
      if (requesterId != null) group['requesterId'] = requesterId;
      group['assignment'] = {
        'id': 'a1',
        'agentId': 'kestrel',
        'status': status,
        'askedOf': ?askedOf,
      };
    }

    Future<SuperTabActivity> activityOf(ConversationServer server) async {
      final c = await open(server);
      return SuperTabActivity.of(c);
    }

    test('an unread tag counts once in Activity and in Mentions; Mark read '
        'clears both', () async {
      final server = withAgent();
      server.tag('message-1', messageSequence: 1);
      final activity = await activityOf(server);
      await activity.mentions.load();
      await activity.decisions.load();
      expect(activity.count, 1);
      expect(activity.mentionsUnread, 1);
      expect(activity.needsCount, 0);
      expect(activity.unread, isTrue);
      activity.markMentionsRead(activity.mentions.waiting);
      await settle();
      expect(activity.count, 0);
      expect(activity.mentionsUnread, 0);
    });

    test('a tag answered but not read past still counts; read past, it does '
        'not', () async {
      final server = withAgent();
      server.tag('message-1', messageSequence: 5);
      conversation(server, 'lead')['lastMessageSequence'] = 20;
      server.mentions.last
        ..['resolvedAt'] = '2026-10-24T06:00:00Z'
        ..['resolvedBy'] = 'answer';
      server.state['mentionsWaiting'] = 0;
      final activity = await activityOf(server);
      await activity.mentions.load();
      await activity.decisions.load();
      expect(activity.count, 1);
      expect(activity.mentionsUnread, 1);
      activity.markMentionsRead(activity.mentions.earlier);
      expect(activity.controller.readMark('lead'), 5);
      expect(activity.count, 0);
      expect(activity.mentionsUnread, 0);
    });

    test('a tag answered in a chat this Swarm does not hold is read: it '
        'could never be read past or marked read here', () async {
      final server = withAgent();
      // An archived task chat: the server lists its tags, the snapshot
      // does not hold it.
      server.tag('message-1', conversationId: 'archived', messageSequence: 5);
      server.mentions.last
        ..['resolvedAt'] = '2026-10-24T06:00:00Z'
        ..['resolvedBy'] = 'answer';
      server.state['mentionsWaiting'] = 0;
      final activity = await activityOf(server);
      await activity.mentions.load();
      await activity.decisions.load();
      expect(activity.controller.snapshot?.conversation('archived'), isNull);
      expect(activity.mentions.earlier, hasLength(1));
      expect(activity.count, 0);
      expect(activity.mentionsUnread, 0);
      expect(activity.mentionRows, isEmpty);
    });

    test('each folded tag counts once', () async {
      final server = withAgent();
      for (var i = 1; i <= 3; i++) {
        server.tag('message-$i', messageSequence: i, leadId: 'dee');
      }
      final activity = await activityOf(server);
      await activity.mentions.load();
      await activity.decisions.load();
      expect(activity.mentionRows, hasLength(1));
      expect(activity.count, 3);
      expect(activity.mentionsUnread, 3);
    });

    test('a stuck task nobody was asked about does not count', () async {
      final server = withAgent();
      assign(server, 'blocked', requesterId: 'owner');
      final activity = await activityOf(server);
      await activity.decisions.load();
      expect(activity.count, 0);
      expect(activity.needsYou, isEmpty);
    });

    test('an agent that asked the reader by name counts once, before and '
        'after the decisions load', () async {
      final server = withAgent();
      server.ask(['Ship it?'], conversationId: 'offline');
      assign(server, 'needs_human', askedOf: 'owner');
      final hold = server.holdDecisions = Completer<void>();
      final activity = await activityOf(server);
      // Not loaded yet: the server's number stands in and the task waits for
      // the list that tells it is the same ask.
      expect(activity.count, 1);
      hold.complete();
      await settle();
      await activity.decisions.load();
      expect(activity.count, 1);
    });

    testWidgets('before the tags and decisions are read, the sidebar shows '
        'the server\'s waiting ones bold', (tester) async {
      final server = withAgent();
      server.state['mentionsWaiting'] = 4;
      server.state['decisionsWaiting'] = 2;
      server.failDecisions = true;
      server.failMentions = true;
      final c = await openHere(server);
      await pumpSidebar(tester, c);
      for (final (row, label) in [
        ('super-decisions', 'Decisions'),
        ('super-mentions', 'Mentions'),
      ]) {
        expect(
          rowText(tester, row, label).style?.fontWeight,
          grid.AppFont.semibold,
          reason: row,
        );
      }
      expect(inRow('super-decisions', find.text('2')), findsNothing);
      expect(inRow('super-mentions', find.text('4')), findsNothing);
      await close(tester, c);
    });

    test('handled today lists the PM\'s answers of today', () async {
      final now = DateTime(2026, 11, 4, 12);
      final server = withAgent();
      server.ask(['Today?', 'Yesterday?']);
      String at(DateTime t) => t.toUtc().toIso8601String();
      server.decisions[0]
        ..['answeredAt'] = at(DateTime(2026, 11, 4, 9))
        ..['choice'] = 0
        ..['delegated'] = true;
      server.decisions[1]
        ..['answeredAt'] = at(DateTime(2026, 11, 3, 9))
        ..['choice'] = 0
        ..['delegated'] = true;
      await withClock(Clock.fixed(now), () async {
        final activity = await activityOf(server);
        await activity.decisions.load();
        expect([for (final d in activity.handledToday) d.question], ['Today?']);
      });
    });
  });
}