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.

47.6 KB
Raw Blame
import 'support/workspace_tools.dart';
import 'support/resource_picker.dart';

import 'dart:async';
import 'dart:io';
import 'dart:ui' as ui;

import 'package:harness/shared/theme/app_icons.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/auth/auth_session.dart';
import 'package:harness/auth/cli_link.dart';
import 'package:harness/core/config.dart';
import 'package:harness/core/machine_resources.dart';
import 'package:harness/core/models.dart';
import 'package:harness/screens/swarm_screen.dart';
import 'package:harness/state/new_harness.dart';
import 'package:harness/shared/theme/app_theme.dart' as grid;
import 'package:harness/shortcuts/app_keymap.dart';
import 'package:harness/shortcuts/keymap_host.dart';
import 'package:harness/state/app_state.dart';
import 'package:harness/widgets/browser_menu_guard.dart';
import 'package:harness/widgets/machines_panel.dart';
import 'package:harness/widgets/sign_in_step.dart';

import 'keymap_host_test.dart' show MemoryKeymap, key;
import 'support/password_cli.dart';
import 'support/real_fonts.dart';

class _Cli extends PasswordCli {
  final connections = <(String, String)>[];
  Completer<CliLinkConnectResult>? connectReply;
  String? connectError;
  @override
  Future<CliLinkConnectResult> connect(
    String machineId,
    String password, {
    void Function(String stage)? onProgress,
    String? displayName,
  }) async {
    connections.add((machineId, password));
    onProgress?.call('exchanging');
    return connectReply?.future ??
        CliLinkConnectResult(error: connectError, linkedMachineId: machineId);
  }
}

class _App extends AppNotifier {
  _App(_Cli cli)
    : super(
        config: AppConfig.dev,
        authSession: AuthSession(),
        configStore: null,
        cliLink: cli,
        peerLinks: cli,
      );
  String? inventoryError;
  int refreshes = 0;
  final deletes = <String>[];
  @override
  Future<String?> deleteMachine(String id) async {
    deletes.add(id);
    machineStates.remove(id);
    machines = machineStates.values.map((s) => s.machine).toList();
    notifyListeners();
    return null;
  }

  final resourceReadIds = <String>[];
  final resourceValues = <String, MachineResources?>{};
  final resourceReplies = <String, Completer<MachineResources?>>{};
  @override
  Future<MachineResources?> readMachineResources(String id) async {
    resourceReadIds.add(id);
    return resourceReplies[id]?.future ?? resourceValues[id];
  }

  @override
  String? get machineListError => inventoryError;
  @override
  Future<void> retryMachines() async {
    refreshes++;
  }

  void add(
    String id,
    String name, {
    bool local = false,
    bool linked = false,
    bool online = true,
  }) {
    final machine = Machine(
      machineId: id,
      name: name,
      authMode: MachineAuthMode.remote,
    );
    machineStates[id] = MachineState(machine)
      ..localOnly = local
      ..nodeOnline = online
      ..needsLink = !linked && !local
      ..agentLoadStatus = linked || local
          ? AgentLoadStatus.loaded
          : AgentLoadStatus.needsLink
      ..connectionStatus = linked || local
          ? ConnectionStatus.connected
          : ConnectionStatus.disconnected;
    machines = machineStates.values.map((s) => s.machine).toList();
    notifyListeners();
  }

  void connected(String id) {
    stateOf(id)!.connectionStatus = ConnectionStatus.connected;
    stateOf(id)!.agentLoadStatus = AgentLoadStatus.loaded;
    notifyListeners();
  }
}

void main() {
  late _Cli cli;
  late _App app;
  final captureKey = GlobalKey();
  String? opened;
  setUp(() {
    cli = _Cli();
    app = _App(cli)..add('local', 'M2', local: true);
    opened = null;
  });
  tearDown(() => app.dispose());

  Future<void> mount(
    WidgetTester tester, {
    Size size = const Size(1000, 900),
    double scale = 1,
    Brightness brightness = Brightness.dark,
    AppKeymap? keymap,
    bool workspace = false,
  }) async {
    tester.view.devicePixelRatio = 1;
    tester.view.physicalSize = size;
    addTearDown(tester.view.reset);
    if (Platform.environment['HARNESS_MACHINES_CAPTURE_DIR'] != null) {
      await tester.runAsync(() async {
        if (Platform.isMacOS) {
          final bytes = ByteData.sublistView(
            await File('/System/Library/Fonts/Menlo.ttc').readAsBytes(),
          );
          for (final family in [
            '.AppleSystemUIFontMonospaced',
            'SF Mono',
            'Menlo',
          ]) {
            await (FontLoader(family)..addFont(Future.value(bytes))).load();
          }
          final sans = ByteData.sublistView(
            await File('/System/Library/Fonts/Supplemental/Arial.ttf')
                .readAsBytes(),
          );
          for (final family in [
            '.AppleSystemUIFont',
            'SF Pro Text',
            'Roboto',
          ]) {
            await (FontLoader(family)..addFont(Future.value(sans))).load();
          }
        } else {
          await loadRealFonts();
        }
        final font = FontLoader('MaterialIcons');
        font.addFont(rootBundle.load('fonts/MaterialIcons-Regular.otf'));
        await font.load();
        await (FontLoader('packages/lucide_icons_flutter/Lucide')..addFont(
              rootBundle.load(
                'packages/lucide_icons_flutter/assets/lucide.ttf',
              ),
            ))
            .load();
      });
    }
    final home = workspace
        ? SwarmScreen(notifier: app, nativeTabs: false)
        : Scaffold(
            body: Builder(
              builder: (context) => TextButton(
                onPressed: () async {
                  opened = await showMachinesPanel(context, app);
                },
                child: const Text('open machines'),
              ),
            ),
          );
    await tester.pumpWidget(
      RepaintBoundary(
        key: captureKey,
        child: MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: grid.buildAppTheme(brightness: brightness),
          builder: (context, child) => MediaQuery(
            data: MediaQuery.of(context)
                .copyWith(textScaler: TextScaler.linear(scale)),
            child: child!,
          ),
          home: keymap == null
              ? home
              : KeymapProvider(
                  keymap: keymap,
                  child: KeymapHost(
                    keymap: keymap,
                    actions: const {},
                    enabled: () => false,
                    child: home,
                  ),
                ),
        ),
      ),
    );
    if (workspace &&
        Platform.environment['HARNESS_MACHINES_CAPTURE_DIR'] != null) {
      await tester.runAsync(
        () => precacheImage(
          const AssetImage('assets/store/polymath.png'),
          tester.element(find.byType(SwarmScreen)),
        ),
      );
      await tester.pumpAndSettle();
    }
    if (workspace) {
      await openWorkspaceManagement(tester, 'machines');
      await tester.pumpAndSettle();
    } else {
      await tester.tap(find.text('open machines'));
    }
    await tester.pumpAndSettle();
  }

  Future<void> tap(WidgetTester tester, Finder target) async {
    await tester.ensureVisible(target);
    await tester.tap(target);
    await tester.pumpAndSettle();
  }

  Future<void> options(WidgetTester tester, String id) async {
    final row = find.byKey(ValueKey('machine-$id'));
    await tester.ensureVisible(row);
    await tester.tap(row, buttons: kSecondaryMouseButton);
    await tester.pumpAndSettle();
  }

  Future<void> changePassword(WidgetTester tester) async {
    await options(tester, 'local');
    await tap(tester, find.text('Change password'));
  }

  Future<void> capture(
    WidgetTester tester,
    String name, {
    bool panelOnly = false,
  }) async {
    final dir = Platform.environment['HARNESS_MACHINES_CAPTURE_DIR'];
    if (dir == null) return;
    await tester.runAsync(() async {
      final boundary =
          captureKey.currentContext!.findRenderObject()
              as RenderRepaintBoundary;
      final image = panelOnly
          ? await (boundary.debugLayer! as OffsetLayer).toImage(
              tester.getRect(find.byKey(const ValueKey('machines-panel'))),
              pixelRatio: 2,
            )
          : await boundary.toImage(pixelRatio: 2);
      final bytes = await image.toByteData(format: ui.ImageByteFormat.png);
      Directory(dir).createSync(recursive: true);
      File('$dir/$name.png').writeAsBytesSync(bytes!.buffer.asUint8List());
      image.dispose();
    });
  }

  final localInput = find.byKey(const ValueKey('make-available-password'));
  final remoteInput = find.byKey(
    const ValueKey('connect-password-remote-input'),
  );

  testWidgets(
    'in a browser, pointing at a machine with a menu switches the browser menu off',
    (tester) async {
      final told = <bool>[];
      BrowserMenuGuard.debugWeb = true;
      BrowserMenuGuard.debugSink = (off) async => told.add(off);
      addTearDown(BrowserMenuGuard.debugReset);
      await mount(tester);
      final row = find.byKey(const ValueKey('machine-local'));
      await tester.ensureVisible(row);
      final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse);
      addTearDown(mouse.removePointer);
      await mouse.addPointer(location: const Offset(1, 1));
      await mouse.moveTo(tester.getCenter(row));
      await tester.pump();
      expect(told, [true]);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'in a browser, the browser menu stays off while a machine\'s menu is open',
    (tester) async {
      BrowserMenuGuard.debugWeb = true;
      BrowserMenuGuard.debugSink = (_) async {};
      addTearDown(BrowserMenuGuard.debugReset);
      await mount(tester);
      await options(tester, 'local');
      expect(BrowserMenuGuard.isOff, isTrue);
      await tester.pumpWidget(const SizedBox());
      expect(BrowserMenuGuard.isOff, isFalse);
    },
  );

  testWidgets('a full machine list stays quiet and reveals all setup steps', (
    tester,
  ) async {
    cli.status = const RemotePasswordStatus(hasPassword: true);
    app.add('rig', '2x 4090 Rig');
    app.add('empty', '1x 4090 Rig', linked: true);
    app.add('home', 'iMac – Home', linked: true);
    app.add('office', 'iMac – Office', linked: true);
    app.add('laptop', 'MacBook Pro', online: false);
    app.add('server', 'Build server', online: false);
    for (final (id, count, cpu, memory) in [
      ('local', 159, 18.0, 57.0),
      ('home', 68, 9.0, 54.0),
      ('office', 67, 11.0, 56.0),
    ]) {
      app.stateOf(id)!.agents = [
        for (var i = 0; i < count; i++) Agent(id: '$id-$i', name: 'Work $i'),
      ];
      app.resourceValues[id] = MachineResources(
        cpuPercent: cpu,
        memoryUsedBytes: memory * 1024 * 1024 * 1024,
        memoryTotalBytes: 64 * 1024 * 1024 * 1024,
      );
    }
    await mount(tester, size: const Size(1000, 760));
    expect(find.text('Change password'), findsNothing);
    expect(find.text('New harness'), findsNothing);
    expect(find.textContaining('Available'), findsNothing);
    expect(find.text('Offline'), findsNothing);
    expect(find.textContaining('—'), findsNothing);
    expect(find.byIcon(AppIcons.ellipsis), findsNothing);
    expect(find.byIcon(AppIcons.monitorOff), findsNWidgets(2));
    expect(find.text('Connect'), findsOneWidget);
    expect(find.byKey(const ValueKey('machine-stats-rig')), findsNothing);
    expect(find.text('0 harnesses'), findsOneWidget);
    await capture(tester, 'machines-clean-list', panelOnly: true);

    await tap(tester, find.byKey(const ValueKey('add-machine')));
    final panel = find.byKey(const ValueKey('machines-panel'));
    void visible(Finder target) {
      expect(target.hitTestable(), findsOneWidget);
      final bounds = tester.getRect(target);
      expect(bounds.top, greaterThan(tester.getRect(panel).top + 50));
      expect(bounds.bottom, lessThan(tester.getRect(panel).bottom));
    }

    visible(find.text('1. Open Harness on your other computer.'));
    visible(find.text('Download Harness'));
    visible(find.text('2. Open Machines and choose Set password.'));
    // flutter_test runs as Android: a phone's word (audit finding S8).
    visible(find.text('Back here, tap Connect and enter that password.'));
    visible(find.text('Set up a server…'));
    await capture(tester, 'machines-clean-setup', panelOnly: true);

    await tap(tester, find.text('Set up a server…'));
    visible(find.byTooltip('Copy commands'));
    expect(tester.takeException(), isNull);
    await tester.pumpWidget(const SizedBox());
  });

  testWidgets(
    'keyboard opens the context menu and the local row opens its harnesses',
    (tester) async {
      cli.status = const RemotePasswordStatus(hasPassword: true);
      await mount(tester);
      Focus.of(tester.element(find.text('M2'))).requestFocus();
      await tester.pump();
      await key(tester, LogicalKeyboardKey.f10, shift: true);
      await tester.pumpAndSettle();
      expect(find.text('Change password'), findsOneWidget);
      expect(opened, isNull);
      await tap(tester, find.text('Change password'));
      expect(tester.widget<TextField>(localInput).focusNode!.hasFocus, isTrue);
      await tap(tester, find.text('Cancel'));
      await tap(tester, find.text('M2'));
      expect(opened, 'local');
      expect(find.byKey(const ValueKey('machines-panel')), findsNothing);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'workspace connection commands use the shared machine picker and inline forms',
    (tester) async {
      final previous = newHarnessOpensInBox;
      newHarnessOpensInBox = true;
      addTearDown(() => newHarnessOpensInBox = previous);
      app.add('remote', 'Mac mini');
      app.add('office', 'iMac · Office', linked: true);
      app.stateOf('office')!.agents = List.generate(
        8,
        (i) => Agent(id: 'office-$i', name: 'Work $i'),
      );
      app.stateOf('local')!.agents = List.generate(
        12,
        (i) => Agent(id: 'local-$i', name: 'Work $i'),
      );
      app.resourceValues.addAll({
        'local': const MachineResources(
          cpuPercent: 18,
          memoryUsedBytes: 10 * 1024 * 1024 * 1024,
          memoryTotalBytes: 32 * 1024 * 1024 * 1024,
        ),
        'office': const MachineResources(
          cpuPercent: 42,
          memoryUsedBytes: 24 * 1024 * 1024 * 1024,
          memoryTotalBytes: 64 * 1024 * 1024 * 1024,
        ),
      });
      app.add('server', 'Build server', online: false);
      await mount(tester, size: const Size(1280, 800), workspace: true);
      final localInput = find.byKey(const ValueKey('remote-password-field'));
      final remoteInput = find.byKey(
        const ValueKey('remote-password-connect-field'),
      );
      expect(resourceScope('@'), findsOneWidget);
      expect(find.byKey(const ValueKey('machines-panel')), findsNothing);
      expect(find.byType(Dialog), findsNothing);
      expect(resourceSearch(tester).rows.map((row) => row.title), [
        'Mac mini',
        'M2',
        'iMac · Office',
        'Build server',
        'Add machine',
      ]);
      expect(app.resourceReadIds, containsAll(['local', 'office']));
      await selectResource(tester, 'machine:local');
      await tap(
        tester,
        find.byKey(const ValueKey('resource-action:picker.resource_settings')),
      );
      expect(tester.widget<TextField>(localInput).focusNode!.hasFocus, isTrue);
      expect(resourceScope('@'), findsOneWidget);
      await key(tester, LogicalKeyboardKey.escape);
      await key(tester, LogicalKeyboardKey.escape);
      await selectResource(tester, 'machine:remote');
      await tap(
        tester,
        find.byKey(const ValueKey('resource-action:picker.resource_connect')),
      );
      expect(tester.widget<TextField>(remoteInput).focusNode!.hasFocus, isTrue);
      expect(resourceScope('@'), findsOneWidget);
      await capture(tester, 'machines-picker-connect');
      tester.view.physicalSize = const Size(760, 620);
      await tester.pumpAndSettle();
      expect(remoteInput, findsOneWidget);
      expect(tester.takeException(), isNull);
      await key(tester, LogicalKeyboardKey.escape);
      await key(tester, LogicalKeyboardKey.escape);
      await key(tester, LogicalKeyboardKey.escape);
      await tester.pumpAndSettle();
      expect(resourceField, findsNothing);
      await mount(tester, workspace: true, brightness: Brightness.light);
      expect(resourceScope('@'), findsOneWidget);
      await key(tester, LogicalKeyboardKey.escape);
      await tester.pumpAndSettle();
      expect(
        find.byKey(const ValueKey('swarm-new-tab-button')).hitTestable(),
        findsOneWidget,
      );
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'Set password reveals the password field and Save makes this computer available',
    (tester) async {
      await mount(tester);
      expect(find.text('Add a second machine'), findsOneWidget);
      expect(localInput, findsNothing);
      expect(find.text('Save'), findsNothing);
      expect(find.text('Set password'), findsOneWidget);
      expect(
        find.text('2. Open Machines and choose Set password.'),
        findsNothing,
      );
      await capture(tester, 'first-machine');
      await tap(tester, find.byTooltip('Add a machine'));
      expect(
        find.text('2. Open Machines and choose Set password.'),
        findsOneWidget,
      );
      await capture(tester, 'desktop-setup');
      await tap(tester, find.text('Set password'));
      expect(localInput, findsOneWidget);
      expect(tester.widget<TextField>(localInput).focusNode!.hasFocus, isTrue);
      await capture(tester, 'first-machine-setup');
      await tester.enterText(localInput, 'fixture reusable password');
      await key(tester, LogicalKeyboardKey.enter);
      await tester.pumpAndSettle();
      expect(cli.passwords, ['fixture reusable password']);
      expect(cli.status.hasPassword, isTrue);
      expect(find.textContaining('Available'), findsNothing);
      expect(localInput, findsNothing);
      await capture(tester, 'available-machine');
      await changePassword(tester);
      expect(tester.widget<TextField>(localInput).controller!.text, isEmpty);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'new machine appears live; retry password and open it from the same panel',
    (tester) async {
      await mount(tester);
      app.add('remote', 'Mac mini');
      await tester.pumpAndSettle();
      expect(remoteInput, findsNothing);
      await tap(tester, find.byKey(const ValueKey('connect-machine-remote')));
      expect(remoteInput, findsOneWidget);
      expect(
        find.text('On Mac mini, open Machines → Set password.'),
        findsNothing,
      );
      await tap(
        tester,
        find.descendant(
          of: find.byKey(const ValueKey('machine-remote')),
          matching: find.byTooltip('Password help'),
        ),
      );
      expect(
        find.text('On Mac mini, open Machines → Set password.'),
        findsOneWidget,
      );
      await capture(tester, 'connect-machine');
      cli.connectError = 'Incorrect password. Try again.';
      await tester.enterText(remoteInput, 'fixture wrong password');
      await key(tester, LogicalKeyboardKey.enter);
      await tester.pumpAndSettle();
      expect(find.text(cli.connectError!), findsOneWidget);
      expect(find.text('Machines'), findsOneWidget);
      cli.connectError = null;
      await tester.enterText(remoteInput, 'fixture correct password');
      await key(tester, LogicalKeyboardKey.enter);
      await tester.pumpAndSettle();
      expect(app.stateOf('remote')!.needsLink, isFalse);
      app.connected('remote');
      await tester.pumpAndSettle();
      expect(find.text('Connected'), findsNothing);
      expect(
        tester
            .widget<Text>(find.byKey(const ValueKey('machine-stats-remote')))
            .data,
        '0 harnesses',
      );
      expect(cli.connections, [
        ('remote', 'fixture wrong password'),
        ('remote', 'fixture correct password'),
      ]);
      expect(
        cli.passwords,
        isEmpty,
        reason: 'Connecting out never requires setting a local password',
      );
      await capture(tester, 'connected-machine');
      await tap(tester, find.byKey(const ValueKey('machine-remote')));
      expect(opened, 'remote');
      expect(find.text('Machines'), findsNothing);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'password save continues across closing and reopening without duplicate submission',
    (tester) async {
      cli.setReply = Completer<RemotePasswordSetResult>();
      await mount(tester);
      await tap(tester, find.text('Set password'));
      await tester.enterText(localInput, 'fixture password');
      await key(tester, LogicalKeyboardKey.enter);
      await key(tester, LogicalKeyboardKey.enter);
      expect(cli.passwords, hasLength(1));
      await key(tester, LogicalKeyboardKey.escape);
      await tester.pumpAndSettle();
      await tap(tester, find.text('open machines'));
      expect(find.text('Saving password…'), findsOneWidget);
      cli.setReply!.complete(const RemotePasswordSetResult());
      await tester.pumpAndSettle();
      expect(cli.status.hasPassword, isTrue);
      expect(find.textContaining('Available'), findsNothing);
      expect(cli.passwords, hasLength(1));
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets('another arrival never replaces the password being typed', (
    tester,
  ) async {
    app.add('remote', 'Mac mini');
    await mount(tester);
    await tap(tester, find.byKey(const ValueKey('connect-machine-remote')));
    expect(tester.widget<TextField>(remoteInput).focusNode!.hasFocus, isTrue);
    await tester.enterText(remoteInput, 'fixture password');
    app.add('another', 'A build server');
    await tester.pumpAndSettle();
    expect(
      tester.widget<TextField>(remoteInput).controller!.text,
      'fixture password',
    );
    expect(tester.widget<TextField>(remoteInput).focusNode!.hasFocus, isTrue);
    await key(tester, LogicalKeyboardKey.enter);
    await tester.pumpAndSettle();
    expect(cli.connections, [('remote', 'fixture password')]);
    expect(app.stateOf('another')!.needsLink, isTrue);
    await tap(tester, find.byKey(const ValueKey('connect-machine-another')));
    final another = find.byKey(
      const ValueKey('connect-password-another-input'),
    );
    expect(tester.widget<TextField>(another).focusNode!.hasFocus, isTrue);
    await tester.pumpWidget(const SizedBox());
  });

  testWidgets('a frozen device list is offered beside a machine\'s password', (
    tester,
  ) async {
    app
      ..add('remote', 'Mac mini')
      ..deviceListNeedsReviewForTest = true;
    await mount(tester);
    // The row, and the form it opens: the password stays, the review sits under it.
    expect(find.text('Your device list needs a review.'), findsOneWidget);
    await tap(tester, find.byKey(const ValueKey('connect-machine-remote')));
    expect(find.text('Your device list needs a review.'), findsOneWidget);
    expect(find.byKey(const Key('device-list-review')), findsOneWidget);
    expect(remoteInput, findsOneWidget);

    app.deviceListNeedsReviewForTest = false;
    await tester.pumpAndSettle();
    expect(find.text('Your device list needs a review.'), findsNothing);
    await tester.pumpWidget(const SizedBox());
  });

  testWidgets(
    'too many devices on the account is said beside a machine\'s password',
    (tester) async {
      app
        ..add('remote', 'Mac mini')
        ..deviceListTooManyForTest = true;
      await mount(tester);
      const line =
          'This device couldn’t join: your account has too many devices. Remove ones you no longer use.';
      expect(find.text(line), findsOneWidget);
      expect(find.text('Your device list needs a review.'), findsNothing);
      expect(find.byKey(const Key('device-list-review')), findsOneWidget);

      app.deviceListTooManyForTest = false;
      await tester.pumpAndSettle();
      expect(find.text(line), findsNothing);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets('connection continues across closing and reopening', (
    tester,
  ) async {
    app.add('remote', 'Mac mini');
    cli.connectReply = Completer<CliLinkConnectResult>();
    await mount(tester);
    await tap(tester, find.byKey(const ValueKey('connect-machine-remote')));
    await tester.enterText(remoteInput, 'fixture password');
    await key(tester, LogicalKeyboardKey.enter);
    await key(tester, LogicalKeyboardKey.escape);
    await tester.pumpAndSettle();
    await tap(tester, find.text('open machines'));
    expect(tester.widget<TextField>(remoteInput).readOnly, isTrue);
    expect(tester.widget<TextField>(remoteInput).controller!.text, isEmpty);
    cli.connectReply!.complete(
      const CliLinkConnectResult(linkedMachineId: 'remote'),
    );
    await tester.pumpAndSettle();
    app.connected('remote');
    await tester.pumpAndSettle();
    expect(find.text('Connected'), findsNothing);
    expect(
      tester
          .widget<Text>(find.byKey(const ValueKey('machine-stats-remote')))
          .data,
      '0 harnesses',
    );
    expect(cli.connections, hasLength(1));
    await tester.pumpWidget(const SizedBox());
  });

  testWidgets(
    'loading, inventory failure, and password failure never look like an empty account',
    (tester) async {
      app.machinesLoading = true;
      cli.status = const RemotePasswordStatus(error: 'CLI unavailable');
      await mount(tester);
      expect(find.text('Looking for your machines…'), findsOneWidget);
      expect(
        find.text('1. Open Harness on your other computer.'),
        findsNothing,
      );
      expect(localInput, findsNothing);
      expect(find.text('CLI unavailable'), findsOneWidget);
      app.machinesLoading = false;
      app.inventoryError = 'Network unavailable';
      app.notifyListeners();
      await tester.pumpAndSettle();
      expect(
        find.textContaining('Couldn’t update your machines.'),
        findsOneWidget,
      );
      await tap(tester, find.text('Try again').last);
      expect(app.refreshes, 1);
      expect(
        find.text('1. Open Harness on your other computer.'),
        findsNothing,
      );
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'offline machine keeps clear recovery and no password submission',
    (tester) async {
      app.add('remote', 'Mac mini', online: false);
      await mount(tester);
      expect(find.textContaining('Offline'), findsNothing);
      expect(find.byIcon(AppIcons.monitorOff), findsOneWidget);
      expect(
        find.byTooltip('Open Harness on Mac mini to bring it online.'),
        findsOneWidget,
      );
      expect(remoteInput, findsNothing);
      expect(find.byKey(const ValueKey('open-machine-remote')), findsNothing);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'guest sees sign-in and remains on the same panel when signed in',
    (tester) async {
      app.signedIn = false;
      await mount(tester);
      expect(find.text('Sign in'), findsOneWidget);
      expect(localInput, findsNothing);
      app.signedIn = true;
      app.notifyListeners();
      await tester.pumpAndSettle();
      expect(localInput, findsNothing);
      expect(find.text('Set password'), findsOneWidget);
      await tap(tester, find.byTooltip('Add a machine'));
      expect(
        find.text('1. Open Harness on your other computer.'),
        findsOneWidget,
      );
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'custom prompt keys and composing text do not accidentally submit',
    (tester) async {
      final map = MemoryKeymap();
      addTearDown(map.dispose);
      map.apply(
        '{"bindings":[{"when":"picker","keys":"enter","command":null},{"when":"picker","keys":"ctrl+s","command":"picker.accept"}]}',
      );
      await mount(tester, keymap: map);
      await tap(tester, find.text('Set password'));
      await key(tester, LogicalKeyboardKey.tab);
      await key(tester, LogicalKeyboardKey.keyS, ctrl: true);
      expect(tester.widget<TextField>(localInput).obscureText, isFalse);
      await tester.enterText(localInput, 'fixture password');
      await key(tester, LogicalKeyboardKey.enter);
      expect(cli.passwords, isEmpty);
      final controller = tester.widget<TextField>(localInput).controller!;
      controller.value = const TextEditingValue(
        text: 'fixture password',
        selection: TextSelection.collapsed(offset: 7),
        composing: TextRange(start: 0, end: 7),
      );
      await key(tester, LogicalKeyboardKey.escape);
      expect(find.text('Machines'), findsOneWidget);
      controller.value = controller.value.copyWith(composing: TextRange.empty);
      await key(tester, LogicalKeyboardKey.keyS, ctrl: true);
      await tester.pumpAndSettle();
      expect(cli.passwords, ['fixture password']);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'setup and forms remain usable in a narrow window at large text size',
    (tester) async {
      await mount(tester, size: const Size(460, 740), scale: 1.6);
      await tap(tester, find.byTooltip('Add a machine'));
      await capture(tester, 'narrow-setup');
      await tap(tester, find.text('Set up a server…'));
      expect(tester.takeException(), isNull);
      app.add('remote', 'A machine with a long descriptive name');
      await tester.pumpAndSettle();
      await tap(tester, find.byKey(const ValueKey('connect-machine-remote')));
      await tester.ensureVisible(remoteInput);
      await tester.enterText(remoteInput, 'fixture password');
      await key(tester, LogicalKeyboardKey.enter);
      await tester.pumpAndSettle();
      expect(cli.connections, hasLength(1));
      expect(tester.takeException(), isNull);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'stats ignore late offline replies and stop polling when the panel closes',
    (tester) async {
      app.add('remote', 'Mac mini', linked: true);
      app.resourceReplies['remote'] = Completer<MachineResources?>();
      await mount(tester);
      app.notifyListeners();
      await tester.pump();
      expect(app.resourceReadIds.where((id) => id == 'remote'), hasLength(1));
      app.stateOf('remote')!.nodeOnline = false;
      app.notifyListeners();
      await tester.pump();
      app.resourceReplies
          .remove('remote')!
          .complete(const MachineResources(cpuPercent: 92));
      await tester.pumpAndSettle();
      final stats = find.byKey(const ValueKey('machine-stats-remote'));
      expect(stats, findsNothing);
      expect(find.byIcon(AppIcons.monitorOff), findsOneWidget);
      expect(find.text('Offline'), findsNothing);
      app.resourceValues['remote'] = const MachineResources(cpuPercent: 7);
      app.stateOf('remote')!.nodeOnline = true;
      app.notifyListeners();
      await tester.pumpAndSettle();
      expect(tester.widget<Text>(stats).data, '0 harnesses · CPU 7%');
      final reads = app.resourceReadIds.length;
      await key(tester, LogicalKeyboardKey.escape);
      await tester.pumpAndSettle();
      await tester.pump(const Duration(minutes: 1));
      app.notifyListeners();
      await tester.pump();
      expect(app.resourceReadIds, hasLength(reads));
      expect(tester.takeException(), isNull);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'resource polling pauses behind connection settings and resumes on return',
    (tester) async {
      await mount(tester);
      final reads = app.resourceReadIds.length;
      await options(tester, 'local');
      await tap(tester, find.text('Connection settings…'));
      await tester.pump(const Duration(minutes: 1));
      expect(app.resourceReadIds, hasLength(reads));
      await key(tester, LogicalKeyboardKey.escape);
      await tester.pumpAndSettle();
      expect(app.resourceReadIds.length, greaterThan(reads));
      expect(find.byKey(const ValueKey('machines-panel')), findsOneWidget);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'a replacement machine cannot inherit cached stats or a delayed reply',
    (tester) async {
      app.add('remote', 'Mac mini', linked: true);
      final oldReply = Completer<MachineResources?>();
      app.resourceReplies['remote'] = oldReply;
      await mount(tester);
      app.resourceReplies.remove('remote');
      app.resourceValues['remote'] = const MachineResources(cpuPercent: 7);
      app.add('remote', 'Replacement', linked: true);
      await tester.pumpAndSettle();
      oldReply.complete(const MachineResources(cpuPercent: 99));
      await tester.pumpAndSettle();
      final stats = find.byKey(const ValueKey('machine-stats-remote'));
      expect(tester.widget<Text>(stats).data, '0 harnesses · CPU 7%');
      expect(app.resourceReadIds.where((id) => id == 'remote'), hasLength(2));
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'required sign-in stays open until an account arrives, then returns to machines',
    (tester) async {
      app.signedIn = false;
      await mount(tester);
      await tap(tester, find.text('Sign in'));
      expect(find.byKey(const ValueKey('machines-panel')), findsNothing);
      expect(find.byType(SignInStep), findsOneWidget);
      expect(find.byTooltip('Close'), findsNothing);
      await key(tester, LogicalKeyboardKey.escape);
      await tester.pumpAndSettle();
      expect(find.byType(SignInStep), findsOneWidget);
      expect(app.refreshes, 0);
      app.signedIn = true;
      app.notifyListeners();
      await tester.pumpAndSettle();
      expect(find.text('Set password'), findsOneWidget);
      expect(app.refreshes, 1);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'starting inventory, account instructions and shared machines stay clear',
    (tester) async {
      app.machineStates.clear();
      app.machines = [];
      app.machinesLoading = true;
      app.currentUser = const CurrentUserProfile(email: '[email protected]');
      await mount(tester);
      expect(find.text('Starting Harness…'), findsOneWidget);
      expect(find.text('Looking for your machines…'), findsOneWidget);
      await tap(tester, find.text('Add a second machine'));
      expect(find.text('Sign in as [email protected].'), findsOneWidget);
      await tap(tester, find.text('Set up a server…'));
      expect(find.text('Sign in as [email protected].'), findsOneWidget);
      await tap(tester, find.text('Hide server setup'));
      await tap(tester, find.byTooltip('Hide setup steps'));
      app.add('remote', 'Shared work', linked: true);
      final shared = app.stateOf('remote')!;
      app.machineStates['remote'] = MachineState(
        const Machine(
          machineId: 'remote',
          name: 'Shared work',
          isShared: true,
          authMode: MachineAuthMode.remote,
        ),
      )..connectionStatus = shared.connectionStatus;
      app.notifyListeners();
      await tester.pumpAndSettle();
      expect(find.textContaining('Shared with you'), findsOneWidget);
      expect(find.byTooltip('Options for Shared work'), findsNothing);
      expect(find.text('View harnesses'), findsNothing);
      await tap(tester, find.text('Shared work'));
      expect(opened, 'remote');
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets('the row menu opens OVER the list, never instead of it', (
    tester,
  ) async {
    // A secondary click opens the context menu above the anchored panel.
    // It must not navigate to the machine or hide the rest of the list.
    app.add('remote', 'Mac mini');
    await mount(tester);
    await options(tester, 'remote');
    expect(find.text('Rename'), findsOneWidget);
    expect(
      find.byKey(const ValueKey('machines-panel')),
      findsOneWidget,
      reason: 'the panel stays on screen behind its own menu',
    );
    expect(find.text('Mac mini'), findsWidgets);
    expect(find.text('M2'), findsWidgets, reason: 'the other rows stay too');
    await capture(tester, 'machines-row-menu');

    // Escape closes the menu first, and only then the panel.
    await key(tester, LogicalKeyboardKey.escape);
    await tester.pumpAndSettle();
    expect(find.text('Rename'), findsNothing);
    expect(find.byKey(const ValueKey('machines-panel')), findsOneWidget);
    await key(tester, LogicalKeyboardKey.escape);
    await tester.pumpAndSettle();
    expect(find.byKey(const ValueKey('machines-panel')), findsNothing);
    await tester.pumpWidget(const SizedBox());
  });

  testWidgets(
    'removing a machine requires the existing confirmation and returns to the list',
    (tester) async {
      app.add('remote', 'Mac mini');
      await mount(tester);
      await options(tester, 'remote');
      await key(tester, LogicalKeyboardKey.escape);
      await tester.pumpAndSettle();
      expect(find.text('Mac mini'), findsOneWidget);
      await options(tester, 'remote');
      await tap(tester, find.text('Remove from account…'));
      expect(app.deletes, isEmpty);
      await tap(tester, find.text('Cancel'));
      expect(find.text('Mac mini'), findsOneWidget);
      await options(tester, 'remote');
      await tap(tester, find.text('Remove from account…'));
      await tap(tester, find.byKey(const Key('machine-delete-confirm')));
      expect(app.deletes, ['remote']);
      expect(find.text('Mac mini'), findsNothing);
      expect(find.text('Add a second machine'), findsOneWidget);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'password validation, visibility, help and cancel keep values private',
    (tester) async {
      cli.status = const RemotePasswordStatus(hasPassword: true);
      await mount(tester);
      await changePassword(tester);
      await key(tester, LogicalKeyboardKey.enter);
      await tester.pumpAndSettle();
      expect(find.text('Enter a password.'), findsOneWidget);
      expect(cli.passwords, isEmpty);
      await tester.enterText(localInput, 'fixture');
      await tester.pump();
      expect(find.text('Enter a password.'), findsNothing);
      await tap(tester, find.byTooltip('Show password'));
      expect(tester.widget<TextField>(localInput).obscureText, isFalse);
      await tap(tester, find.byTooltip('Hide password'));
      expect(tester.widget<TextField>(localInput).obscureText, isTrue);
      tester.widget<TextField>(localInput).focusNode!.requestFocus();
      await tester.pump();
      await key(tester, LogicalKeyboardKey.tab);
      await key(tester, LogicalKeyboardKey.enter);
      await tester.pumpAndSettle();
      expect(tester.widget<TextField>(localInput).obscureText, isFalse);
      await tap(tester, find.byTooltip('Password help'));
      expect(
        find.text(
          'Use this password to connect to M2 from your other computers.',
        ),
        findsOneWidget,
      );
      final field = tester.widget<TextField>(localInput);
      field.controller!.text = 'line one\nline two';
      await tap(tester, find.text('Save'));
      expect(find.text('Use a password on one line.'), findsOneWidget);
      await key(tester, LogicalKeyboardKey.keyU, ctrl: true);
      await tester.pump();
      expect(find.text('Use a password on one line.'), findsNothing);
      await tap(tester, find.text('Cancel'));
      expect(localInput, findsNothing);
      await changePassword(tester);
      expect(tester.widget<TextField>(localInput).controller!.text, isEmpty);
      // The platform's Done action uses the same validation and submission.
      await tester.enterText(localInput, 'fixture replacement');
      await tester.testTextInput.receiveAction(TextInputAction.done);
      await tester.pumpAndSettle();
      expect(cli.passwords, ['fixture replacement']);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'failed password status retries and a reopened failed save can be cancelled',
    (tester) async {
      cli.status = const RemotePasswordStatus(error: 'Status unavailable');
      await mount(tester);
      expect(find.text('Status unavailable'), findsOneWidget);
      cli.status = const RemotePasswordStatus(hasPassword: true);
      await tap(tester, find.text('Try again'));
      await changePassword(tester);
      cli.setReply = Completer<RemotePasswordSetResult>();
      await tester.enterText(localInput, 'fixture replacement');
      await tap(tester, find.text('Save'));
      await key(tester, LogicalKeyboardKey.escape);
      await tester.pumpAndSettle();
      await tap(tester, find.text('open machines'));
      expect(find.text('Saving password…'), findsOneWidget);
      cli.setReply!.complete(
        const RemotePasswordSetResult(error: 'Save unavailable'),
      );
      await tester.pumpAndSettle();
      expect(find.text('Save unavailable'), findsOneWidget);
      expect(localInput, findsOneWidget);
      await tap(tester, find.text('Cancel'));
      expect(find.text('Change password'), findsNothing);
      await options(tester, 'local');
      expect(find.text('Change password'), findsOneWidget);
      expect(cli.passwords, ['fixture replacement']);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'copy actions recover, ignore older results and reset their feedback',
    (tester) async {
      final copies = <String>[];
      final replies = <Completer<Object?>>[];
      tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
        SystemChannels.platform,
        (call) async {
          if (call.method != 'Clipboard.setData') return null;
          copies.add((call.arguments as Map)['text'] as String);
          final reply = Completer<Object?>();
          replies.add(reply);
          return reply.future;
        },
      );
      addTearDown(
        () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
          SystemChannels.platform,
          null,
        ),
      );
      await mount(tester);
      await tap(tester, find.text('Add a second machine'));
      await tap(tester, find.text('Copy download link'));
      replies.last.completeError(
        PlatformException(code: 'clipboard-unavailable'),
      );
      await tester.pumpAndSettle();
      await tap(tester, find.text('Couldn’t copy. Try again'));
      final older = replies.last;
      await tap(tester, find.text('Couldn’t copy. Try again'));
      replies.last.complete(null);
      await tester.pumpAndSettle();
      older.completeError(PlatformException(code: 'late-error'));
      await tester.pumpAndSettle();
      expect(find.text('Copied'), findsOneWidget);
      expect(copies.toSet(), hasLength(1));
      expect(Uri.parse(copies.first).scheme, 'https');
      await tester.pump(const Duration(seconds: 3));
      expect(find.text('Copy download link'), findsOneWidget);
      await tap(tester, find.text('Set up a server…'));
      await tap(tester, find.byTooltip('Copy commands'));
      expect(copies.last, contains('harness login'));
      expect(copies.last, contains('harness start'));
      await capture(tester, 'server-setup');
      replies.last.completeError(
        PlatformException(code: 'clipboard-unavailable'),
      );
      await tester.pumpAndSettle();
      await tap(tester, find.byTooltip('Couldn’t copy. Try again'));
      replies.last.complete(null);
      await tester.pumpAndSettle();
      expect(find.byTooltip('Copied'), findsOneWidget);
      await tester.pump(const Duration(seconds: 3));
      await tap(tester, find.byTooltip('Copy commands'));
      await key(tester, LogicalKeyboardKey.escape);
      await tester.pumpAndSettle();
      replies.last.complete(null);
      await tester.pumpAndSettle();
      expect(tester.takeException(), isNull);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'desktop setup leads with the app and recovers from browser failures',
    (tester) async {
      const channel = MethodChannel('plugins.flutter.io/url_launcher');
      final launches = <String>[];
      Object? result = false;
      tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (
        call,
      ) async {
        launches.add((call.arguments as Map)['url'] as String);
        if (result is Exception) {
          throw result;
        }
        if (result is Completer<bool>) {
          return result.future;
        }
        return result;
      });
      addTearDown(
        () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
          channel,
          null,
        ),
      );
      await mount(tester);
      await tap(tester, find.text('Add a second machine'));
      expect(
        find.text('1. Open Harness on your other computer.'),
        findsOneWidget,
      );
      expect(find.textContaining('curl -fsSL'), findsNothing);
      await tap(tester, find.text('Download Harness'));
      expect(
        find.text('Couldn’t open your browser. Copy the link instead.'),
        findsOneWidget,
      );
      result = PlatformException(code: 'browser-unavailable');
      await tap(tester, find.text('Download Harness'));
      expect(find.text('Copy download link'), findsOneWidget);
      result = true;
      await tap(tester, find.text('Download Harness'));
      expect(find.textContaining('Couldn’t open your browser'), findsNothing);
      expect(launches.toSet(), {'https://www.autonomous.ai/harness'});
      final pending = Completer<bool>();
      result = pending;
      await tap(tester, find.text('Download Harness'));
      await key(tester, LogicalKeyboardKey.escape);
      await tester.pumpAndSettle();
      pending.complete(false);
      await tester.pumpAndSettle();
      expect(tester.takeException(), isNull);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'resource refresh backs off on failure and pauses while the app is inactive',
    (tester) async {
      app.resourceValues['local'] = const MachineResources(cpuPercent: 12);
      await mount(tester);
      expect(app.resourceReadIds, ['local']);
      await tester.pump(const Duration(seconds: 5));
      await tester.pump();
      expect(app.resourceReadIds, ['local', 'local']);
      app.resourceValues.clear();
      await tester.pump(const Duration(seconds: 5));
      await tester.pump();
      expect(find.text('0 harnesses'), findsOneWidget);
      final reads = app.resourceReadIds.length;
      for (var i = 0; i < 5; i++) {
        await tester.pump(const Duration(seconds: 5));
      }
      expect(app.resourceReadIds, hasLength(reads));
      await tester.pump(const Duration(seconds: 5));
      await tester.pump();
      expect(app.resourceReadIds, hasLength(reads + 1));
      tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
      await tester.pump(const Duration(minutes: 1));
      expect(app.resourceReadIds, hasLength(reads + 1));
      tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
      await tester.pumpAndSettle();
      expect(app.resourceReadIds, hasLength(reads + 2));
      app.machineStates.clear();
      app.notifyListeners();
      await tester.pumpAndSettle();
      expect(find.text('Starting Harness…'), findsOneWidget);
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets(
    'Machines picker remains usable in a short narrow workspace at large text size',
    (tester) async {
      final previous = newHarnessOpensInBox;
      newHarnessOpensInBox = true;
      addTearDown(() => newHarnessOpensInBox = previous);
      await mount(
        tester,
        size: const Size(480, 360),
        scale: 1.7,
        workspace: true,
      );
      expect(resourceScope('@'), findsOneWidget);
      expect(find.byKey(const ValueKey('machines-panel')), findsNothing);
      await key(tester, LogicalKeyboardKey.escape);
      await tester.pumpAndSettle();
      expect(
        find.byKey(const ValueKey('swarm-new-tab-button')).hitTestable(),
        findsOneWidget,
      );
      await tester.pumpWidget(const SizedBox());
    },
  );
}