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.

10.3 KB
Raw Blame
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness_mobile/core/models.dart';
import 'package:harness_mobile/phone/agent_home.dart';
import 'package:harness_mobile/phone/phone_shell.dart';
import 'package:harness_mobile/phone/phone_shell_scope.dart';
import 'package:harness_mobile/phone/settings_page.dart';
import 'package:harness_mobile/settings/experimental_features.dart';
import 'package:harness_mobile/swarm/swarm_computers.dart';
import 'package:harness_mobile/swarm/swarm_page.dart';
import 'package:harness_mobile/swarm/swarm_session.dart';
import 'package:harness_mobile/swarm/swarm_you_page.dart';

import 'devices_fixture.dart';

/// With Swarm on, the app opens on Swarm Home, full screen, with Settings in
/// You — opened from the avatar on Home since 2026-11-03, no longer a tab. Harness is the root
/// while an agent is open, with a back chevron in its header to Swarm Home (and, on iOS, an edge
/// swipe), as Android's system back always went; Settings ▸ Swarm off turns Swarm Home off. With
/// Swarm off, the app opens on the terminal, as it always has.
void main() {
  late DevicesApp app;

  setUp(() {
    app = DevicesApp()
      ..currentUser = const CurrentUserProfile(
        id: 'person-me',
        email: '[email protected]',
      );
    // In memory only: the value, not the store's write, so nothing reaches the state file.
    experimentalFeaturesStore.value = {PhoneExperiment.swarm};
  });
  tearDown(() {
    experimentalFeaturesStore.value = const {};
    app.dispose();
  });

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

  /// Frames enough for a page to slide in or out, and for the shell to hear
  /// that the stack moved. Not pumpAndSettle: Swarm keeps a poll running.
  Future<void> frames(WidgetTester tester) async {
    for (var i = 0; i < 8; i++) {
      await tester.pump(const Duration(milliseconds: 150));
    }
  }

  Future<void> open(WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(home: PhoneShell(notifier: app)));
    await frames(tester);
  }

  /// You, from the avatar at the top of Home.
  Future<void> openYou(WidgetTester tester) async {
    expect(key('swarm-tab-you'), findsNothing, reason: 'no You tab');
    await tester.tap(key('swarm-you-button'));
    await frames(tester);
  }

  /// Row [row] of You, tapped.
  Future<void> youTo(WidgetTester tester, String row) async {
    await openYou(tester);
    // You scrolls: on the test's short screen its last rows sit below.
    await tester.ensureVisible(key(row));
    await tester.pump();
    await tester.tap(key(row));
    await frames(tester);
  }

  /// An agent explicitly opened through the shell: the terminal at the root, over Swarm
  /// Home.
  Future<void> toHarness(WidgetTester tester) async {
    PhoneShellScope.maybeOf(tester.element(find.byType(SwarmPage)))!
        .onOpenAgent('mac-1', 'agent-1');
    await frames(tester);
  }

  /// The Swarm switch in Settings ▸ Experimental, scrolled to and tapped.
  Future<void> tapSwarmSwitch(WidgetTester tester) async {
    final row = key('settings-experiment-swarm');
    await tester.scrollUntilVisible(
      row,
      200,
      scrollable: find
          .descendant(
            of: find.byType(SettingsPage),
            matching: find.byType(Scrollable),
          )
          .first,
    );
    await tester.tap(row);
    await frames(tester);
  }

  testWidgets('with Swarm on, it opens on Swarm Home, and builds no terminal '
      'until one is asked for', (tester) async {
    await open(tester);

    expect(find.byType(SwarmPage), findsOneWidget);
    expect(key('swarm-title-button'), findsOneWidget);
    expect(key('tty-back'), findsNothing, reason: 'Home has nothing under it');
    expect(find.byType(AgentHome, skipOffstage: false), findsNothing);
  });

  testWidgets("the shell hands the app's computers to Swarm, for the start "
      'screens, and lets go of them with itself', (tester) async {
    await open(tester);

    final computers = tester
        .widget<SwarmScope>(find.byType(SwarmScope))
        .notifier!
        .computers;
    expect(computers, isA<AppSwarmComputers>());

    await tester.pumpWidget(const SizedBox());
    expect(
      () => computers!.addListener(() {}),
      throwsFlutterError,
      reason: 'disposed with the shell',
    );
  });

  testWidgets('with Swarm off, it opens on the terminal, as it always has', (
    tester,
  ) async {
    experimentalFeaturesStore.value = const {};
    await open(tester);

    expect(find.byType(AgentHome), findsOneWidget);
    expect(find.byType(SwarmPage, skipOffstage: false), findsNothing);
  });

  testWidgets('with Swarm on, You has no Harness: its models, then '
      'Settings at its foot', (tester) async {
    await open(tester);
    await openYou(tester);

    final you = find.byType(SwarmYouPage);
    expect(key('swarm-you-models'), findsOneWidget);
    expect(key('swarm-you-settings'), findsOneWidget);
    expect(
      find.descendant(of: you, matching: find.textContaining('Harness')),
      findsNothing,
      reason: 'Settings ▸ Swarm off is the way back to Harness',
    );
  });

  testWidgets("You's Notifications asks the phone's own notice centre, and "
      "Settings under it has none of its own", (tester) async {
    await open(tester);
    await openYou(tester);

    expect(
      tester
          .widget<NotificationsSettingsRow>(
            find.descendant(
              of: find.byType(SwarmYouPage),
              matching: find.byType(NotificationsSettingsRow),
            ),
          )
          .notices,
      same(app.agentNotices.system),
    );

    await tester.ensureVisible(key('swarm-you-settings'));
    await tester.pump();
    await tester.tap(key('swarm-you-settings'));
    await frames(tester);
    expect(find.byType(SettingsPage), findsOneWidget);
    expect(
      find.descendant(
        of: find.byType(SettingsPage),
        matching: find.byType(NotificationsSettingsRow),
      ),
      findsNothing,
    );
  });

  testWidgets('an agent explicitly opened puts the terminal at the root; '
      'back goes to Swarm Home; each keeps the other out of sight', (
    tester,
  ) async {
    await open(tester);

    await toHarness(tester);
    expect(find.byType(AgentHome), findsOneWidget);
    expect(find.byType(SwarmPage), findsNothing);
    expect(
      find.byType(SwarmPage, skipOffstage: false),
      findsOneWidget,
      reason: 'Home waits out of sight, as it was left',
    );

    await tester.binding.handlePopRoute();
    await frames(tester);
    expect(find.byType(SwarmPage), findsOneWidget);
    expect(find.byType(AgentHome), findsNothing);
    expect(
      find.byType(AgentHome, skipOffstage: false),
      findsOneWidget,
      reason: 'the terminal waits out of sight, as it was left',
    );
  });

  testWidgets('Settings in You opens over the tabs; back is You', (
    tester,
  ) async {
    await open(tester);

    await youTo(tester, 'swarm-you-settings');
    expect(find.byType(SettingsPage), findsOneWidget);
    expect(
      tester.widget<SettingsPage>(find.byType(SettingsPage)).swarm,
      isTrue,
      reason: "drawn as Swarm's pages are",
    );

    await tester.binding.handlePopRoute();
    await frames(tester);
    expect(find.byType(SettingsPage), findsNothing);
    expect(find.byType(SwarmYouPage), findsOneWidget);
  });

  testWidgets('Swarm turned off puts the terminal at the root; turned on '
      'again, Swarm Home', (tester) async {
    await open(tester);
    await toHarness(tester);

    experimentalFeaturesStore.value = const {};
    await tester.pump();
    expect(find.byType(AgentHome), findsOneWidget);
    expect(find.byType(SwarmPage, skipOffstage: false), findsNothing);

    experimentalFeaturesStore.value = {PhoneExperiment.swarm};
    await frames(tester);
    expect(find.byType(SwarmPage), findsOneWidget);
    expect(find.byType(AgentHome), findsNothing);
  });

  testWidgets('Swarm asked for from the terminal (its mark, Find) closes '
      'every page and puts Swarm Home at the root', (tester) async {
    await open(tester);
    await toHarness(tester);
    final terminal = tester.element(find.byType(AgentHome));
    unawaited(
      Navigator.of(terminal).push(
        MaterialPageRoute<void>(builder: (_) => const Text('a page over it')),
      ),
    );
    await frames(tester);
    expect(find.text('a page over it'), findsOneWidget);

    PhoneShellScope.maybeOf(terminal)!.onShowSwarm!();
    await frames(tester);

    expect(find.text('a page over it'), findsNothing);
    expect(find.byType(SwarmPage), findsOneWidget);
    expect(
      key('swarm-title-button'),
      findsOneWidget,
      reason: 'the root, not pushed',
    );
    expect(find.byType(AgentHome), findsNothing);
    expect(find.byType(AgentHome, skipOffstage: false), findsOneWidget);
  });

  testWidgets('Settings ▸ Swarm off, from You, is the way to Harness: '
      'back from Settings is the terminal; Swarm on again from its Settings is '
      'Swarm Home', (tester) async {
    await open(tester);

    await youTo(tester, 'swarm-you-settings');
    await tapSwarmSwitch(tester);
    expect(experimentalFeaturesStore.isOn(PhoneExperiment.swarm), isFalse);
    expect(find.byType(SettingsPage), findsOneWidget, reason: 'still open');

    await tester.binding.handlePopRoute();
    await frames(tester);
    expect(find.byType(SettingsPage), findsNothing);
    expect(find.byType(AgentHome), findsOneWidget);
    expect(find.byType(SwarmPage, skipOffstage: false), findsNothing);

    // The terminal's Settings (its menu, Find), as the person reaches it.
    openPhoneSettings(tester.element(find.byType(AgentHome)), app);
    await frames(tester);
    expect(
      tester.widget<SettingsPage>(find.byType(SettingsPage)).swarm,
      isFalse,
      reason: "the terminal's Settings keeps the phone's own look",
    );
    await tapSwarmSwitch(tester);
    await tester.tap(find.text('Turn on Swarm'));
    await frames(tester);
    expect(experimentalFeaturesStore.isOn(PhoneExperiment.swarm), isTrue);
    expect(find.byType(SettingsPage), findsOneWidget, reason: 'back in it');

    await tester.binding.handlePopRoute();
    await frames(tester);
    expect(find.byType(SwarmPage), findsOneWidget);
    expect(
      key('swarm-title-button'),
      findsOneWidget,
      reason: 'the root, not pushed',
    );
    expect(find.byType(AgentHome), findsNothing);
  });
}