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.

7.5 KB
Raw Blame
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness_mobile/auth/auth_session.dart';
import 'package:harness_mobile/core/config.dart';
import 'package:harness_mobile/core/models.dart';
import 'package:harness_mobile/state/app_state.dart';
import 'package:harness_mobile/ws/ws_conn.dart';
import 'package:harness_mobile/phone/agent_tile.dart';
import 'package:harness_mobile/phone/agents_page.dart';

/// Records what the page asks the machine for, and answers everything with a bare success.
class _Conn extends WsConn {
  _Conn()
    : super(
        wsBaseUrl: 'ws://fixture.invalid',
        autonomousEnv: 'test',
        machineId: 'm',
        accessTokenProvider: (_, _) async => '',
        onAuthFailure: (_) {},
        onEvent: (_) {},
        onStatus: (_) {},
      );

  final calls = <(String, Map<String, dynamic>)>[];

  @override
  Future<Map<String, dynamic>> request(
    String type, {
    Map<String, dynamic> payload = const {},
    Duration timeout = const Duration(seconds: 20),
  }) async {
    calls.add((type, payload));
    // Delete Harness: a review, then the delete under it.
    if (type == 'agent_purge') {
      return payload['mode'] == 'inspect'
          ? {'reviewId': 'review-1'}
          : {'deleted': true};
    }
    return {};
  }

  List<(String, String)> get purged => [
    for (final (type, payload) in calls)
      if (type == 'agent_purge')
        (payload['agentId'] as String, payload['mode'] as String),
  ];

  List<String> get deleted => [
    for (final (type, payload) in calls)
      if (type == 'agent_delete') payload['agentId'] as String,
  ];
}

/// A machine answering with two agents: one whose terminal is up, one whose is not.
///
/// The second is the case the row's hold exists for — it cannot be opened, so a menu hung off the
/// tap would never reach it.
///
/// [withTerminal] adds a third, a terminal — the one harness Delete is not offered for.
AppNotifier _app(_Conn conn, {bool withTerminal = false}) {
  final app = AppNotifier(
    config: AppConfig.dev,
    authSession: AuthSession(),
    configStore: null,
    connectionForTest: (_) => conn,
  );
  const machine = Machine(
    machineId: 'm',
    authMode: MachineAuthMode.remote,
    name: 'Test host',
  );
  app.machines = [machine];
  // ⚠️ `connected` is load-bearing, and not for realism. Anything less leaves the header at
  // "Connecting…", whose StatusPill draws a CircularProgressIndicator — an animation that never
  // ends, so `pumpAndSettle` never returns and every test here times out before its first assert.
  app.machineStates['m'] = MachineState(machine)
    ..nodeOnline = true
    ..connectionStatus = ConnectionStatus.connected
    ..agentLoadStatus = AgentLoadStatus.loaded
    ..agents = [
      Agent(
        id: 'a0',
        name: 'Live one',
        engine: 'codex',
        sessionId: 's0',
        terminalAvailable: true,
        createdAt: DateTime.utc(2026, 10, 16, 1),
      ),
      const Agent(
        id: 'a1',
        name: 'Gone one',
        engine: 'codex',
        terminalAvailable: false,
      ),
      if (withTerminal)
        const Agent(
          id: 't0',
          name: 'Shell',
          engine: 'terminal',
          terminalAvailable: true,
        ),
    ];
  return app;
}

Future<void> _pump(WidgetTester tester, AppNotifier app) async {
  await tester.pumpWidget(
    MaterialApp(
      home: AgentsPage(notifier: app, machineId: 'm'),
    ),
  );
  await tester.pumpAndSettle();
}

void main() {
  testWidgets('holding an agent offers Delete, and Cancel deletes nothing', (
    tester,
  ) async {
    final conn = _Conn();
    final app = _app(conn);
    addTearDown(app.dispose);
    await _pump(tester, app);

    await tester.longPress(find.byType(AgentTile).first);
    await tester.pumpAndSettle();
    expect(find.text('Stop Harness…'), findsOneWidget);

    await tester.tap(find.text('Stop Harness…'));
    await tester.pumpAndSettle();
    expect(find.text('Stop Live one?'), findsOneWidget);

    await tester.tap(find.text('Cancel'));
    await tester.pumpAndSettle();

    expect(conn.deleted, isEmpty, reason: 'backing out must ask nothing');
    expect(app.machineStates['m']!.agents, hasLength(2));
  });

  testWidgets('confirming deletes that agent and no other', (tester) async {
    final conn = _Conn();
    final app = _app(conn);
    addTearDown(app.dispose);
    await _pump(tester, app);

    await tester.longPress(find.byType(AgentTile).first);
    await tester.pumpAndSettle();
    await tester.tap(find.text('Stop Harness…'));
    await tester.pumpAndSettle();
    await tester.tap(find.widgetWithText(FilledButton, 'Stop'));
    await tester.pumpAndSettle();

    expect(conn.deleted, ['a0']);
    expect(app.machineStates['m']!.agents.map((agent) => agent.id), [
      'a1',
    ], reason: 'the row goes because the notifier dropped it, not the page');
    expect(find.text('Live one'), findsNothing);
  });

  testWidgets('an agent with no terminal cannot be opened but can be held', (
    tester,
  ) async {
    final conn = _Conn();
    final app = _app(conn);
    addTearDown(app.dispose);
    await _pump(tester, app);

    final gone = find.byType(AgentTile).last;
    // A tap is the gesture this row refuses; nothing should open.
    await tester.tap(gone);
    await tester.pumpAndSettle();
    expect(find.text('Stop Harness…'), findsNothing);

    await tester.longPress(gone);
    await tester.pumpAndSettle();
    await tester.tap(find.text('Stop Harness…'));
    await tester.pumpAndSettle();
    await tester.tap(find.widgetWithText(FilledButton, 'Stop'));
    await tester.pumpAndSettle();

    expect(conn.deleted, ['a1']);
  });

  testWidgets('holding an agent offers Delete Harness too, and confirming '
      'reviews then deletes it, conversation and all', (tester) async {
    final conn = _Conn();
    final app = _app(conn);
    addTearDown(app.dispose);
    await _pump(tester, app);

    await tester.longPress(find.byType(AgentTile).first);
    await tester.pumpAndSettle();
    await tester.tap(find.text('Delete Harness…'));
    await tester.pumpAndSettle();
    expect(find.text('Delete Live one?'), findsOneWidget);
    expect(
      find.textContaining('it cannot be opened again'),
      findsOneWidget,
      reason: 'says what Stop does not',
    );
    await tester.tap(find.widgetWithText(FilledButton, 'Delete'));
    await tester.pumpAndSettle();

    expect(conn.purged, [('a0', 'inspect'), ('a0', 'delete')]);
    expect(conn.deleted, isEmpty, reason: 'not a Stop');
    expect(app.machineStates['m']!.agents.map((agent) => agent.id), ['a1']);
  });

  testWidgets('Delete Harness backed out of asks nothing', (tester) async {
    final conn = _Conn();
    final app = _app(conn);
    addTearDown(app.dispose);
    await _pump(tester, app);

    await tester.longPress(find.byType(AgentTile).first);
    await tester.pumpAndSettle();
    await tester.tap(find.text('Delete Harness…'));
    await tester.pumpAndSettle();
    await tester.tap(find.text('Cancel'));
    await tester.pumpAndSettle();

    expect(conn.purged, isEmpty);
    expect(app.machineStates['m']!.agents, hasLength(2));
  });

  testWidgets('a terminal offers Stop only — it keeps no conversation to '
      'delete', (tester) async {
    final conn = _Conn();
    final app = _app(conn, withTerminal: true);
    addTearDown(app.dispose);
    await _pump(tester, app);

    await tester.longPress(find.widgetWithText(AgentTile, 'Shell'));
    await tester.pumpAndSettle();
    expect(find.text('Stop Harness…'), findsOneWidget);
    expect(find.text('Delete Harness…'), findsNothing);
  });
}