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.

15.3 KB
Raw Blame
import 'dart:async';

import 'package:flutter_test/flutter_test.dart';
import 'package:harness/api/api_client.dart';
import 'package:harness/auth/auth_session.dart';
import 'package:harness/auth/cli_login.dart';
import 'package:harness/core/config.dart';
import 'package:harness/core/models.dart';
import 'package:harness/state/app_state.dart';
import 'package:harness/state/swarm_catalog.dart';
import 'package:harness/ws/local_cli_discovery.dart';
import 'package:harness/ws/ws_conn.dart';

/// A daemon that answers whatever the test says, with the supervisor's callbacks captured so the
/// test can fire "it became ready" itself.
class _ScriptedDiscovery extends LocalCliDiscovery {
  _ScriptedDiscovery(this.answers) : super(config: AppConfig.dev);

  final List<LocalCliProbe> answers;
  int ensureCalls = 0;
  int superviseCalls = 0;
  void Function(LocalCliEndpoint endpoint)? onReady;
  void Function(LocalCliEndpoint endpoint)? onSnapshot;
  void Function(bool online)? onBackendOnline;

  @override
  Future<LocalCliProbe> ensureRunning({
    Duration timeout = const Duration(seconds: 15),
    Duration readyTimeout = LocalCliDiscovery.defaultReadyTimeout,
  }) async {
    ensureCalls++;
    return answers.length > 1 ? answers.removeAt(0) : answers.single;
  }

  @override
  Timer startSupervising({
    Duration checkInterval = const Duration(seconds: 5),
    Duration graceStep = const Duration(milliseconds: 500),
    Duration graceWindow = const Duration(seconds: 5),
    Duration initialBackoff = const Duration(seconds: 2),
    Duration maxBackoff = const Duration(seconds: 30),
    int spawnAfter = 2,
    bool Function(DateTime now)? spawnAllowedAt,
    Future<bool> Function()? stillSignedIn,
    void Function()? onSignedOut,
    void Function(LocalCliEndpoint endpoint)? onReady,
    void Function(LocalCliEndpoint endpoint)? onSnapshot,
    void Function(bool online)? onBackendOnline,
    Future<void> Function(int pid)? checkOwner,
    bool Function()? uninstalling,
  }) {
    superviseCalls++;
    this.onReady = onReady;
    this.onSnapshot = onSnapshot;
    this.onBackendOnline = onBackendOnline;
    return Timer(const Duration(days: 1), () {});
  }
}

class _SignedInCli extends CliLogin {
  @override
  Future<CliAuthStatus> checkStatus() async => CliAuthStatus(loggedIn: true);
}

/// Stops at the machine list: this test is about the daemon gate, not what comes after it.
/// A socket that answers nothing: the load a connect triggers must not reach for a real pool.
class _QuietConnection extends WsConn {
  _QuietConnection()
    : super(
        wsBaseUrl: 'ws://fixture.invalid',
        autonomousEnv: 'test',
        machineId: 'm-local',
        accessTokenProvider: (_, _) async => '',
        onAuthFailure: (_) {},
        onEvent: (_) {},
        onStatus: (_) {},
      );
  @override
  Future<void> waitUntilReady({required Duration timeout}) async {}
  @override
  Future<Map<String, dynamic>> request(
    String type, {
    Map<String, dynamic> payload = const {},
    Duration timeout = const Duration(seconds: 20),
  }) async => const {'agents': []};
}

class _Notifier extends AppNotifier {
  int refreshes = 0;
  _Notifier(LocalCliDiscovery discovery)
    : super(
        config: AppConfig.dev,
        authSession: AuthSession(),
        configStore: null,
        localCliDiscovery: discovery,
        cliLogin: _SignedInCli(),
        connectionForTest: (_) => _QuietConnection(),
      ) {
    // Already known, so the retry path does not go looking for it over the
    // network — this test is about the daemon gate, not the profile.
    currentUser = const CurrentUserProfile(
      id: 'user-1',
      name: 'Tester',
      email: '[email protected]',
    );
  }

  @override
  Future<bool> refreshMachines() async {
    refreshes++;
    return true;
  }
}

/// A daemon whose copy of the device log still has one device nobody looked at.
class _PendingDevicesApi extends ApiClient {
  _PendingDevicesApi() : super(config: AppConfig.dev, session: AuthSession());

  int reads = 0;

  /// Whether the daemon has its listing ready (null otherwise: still connecting).
  bool ready = true;

  /// A daemon that predates the persisted marks: it answers, with no `pending`.
  bool legacy = false;

  @override
  Future<Map<String, dynamic>?> daemonDevices() async {
    reads++;
    if (!ready) return null;
    if (legacy) return {'members': <Object?>[]};
    return {
      'members': [
        {
          'pub': 'a',
          'label': 'Phone',
          'kind': 'viewer',
          'machineId': '',
          'addedAt': 1000,
          'fingerprint': 'FPA',
          'self': false,
          'seq': 2,
        },
      ],
      'pending': ['a'],
    };
  }
}

final _endpoint = LocalCliEndpoint(
  computerId: '0123456789abcdef0123456789abcdef',
  wsUri: Uri.parse('ws://127.0.0.1:18473/api/local-ws'),
  protocolVersion: 1,
  terminalProtocolVersion: 3,
);

void main() {
  test('local folder snapshots refresh projects without leaking to peers or changing membership', () async {
    final discovery = _ScriptedDiscovery([LocalCliProbe.ready(_endpoint)]);
    final notifier = _Notifier(discovery);
    addTearDown(notifier.dispose);
    const oldAgent = Agent(id: 'shared-id', name: 'Existing agent');
    const wireProject = AgentProject(
      name: 'Reported repository',
      cwd: '/wire',
      remote: 'host/org/repo',
    );
    final local =
        MachineState(
            const Machine(
              machineId: 'local',
              name: 'Local',
              authMode: MachineAuthMode.remote,
            ),
          )
          ..localEndpoint = _endpoint
          ..agents = [
            oldAgent,
            const Agent(id: 'native', name: 'New daemon', project: wireProject),
          ];
    final peer = MachineState(
      const Machine(
        machineId: 'peer',
        name: 'Peer',
        authMode: MachineAuthMode.remote,
      ),
    )..agents = [oldAgent];
    notifier.machineStates.addAll({'local': local, 'peer': peer});
    await notifier.ensureCliDaemonReady();
    final swarm = notifier.activeSwarm;
    var notifications = 0;
    notifier.addListener(() => notifications++);
    LocalCliEndpoint snapshot(String computerId, String folder) =>
        LocalCliEndpoint(
          computerId: computerId,
          wsUri: _endpoint.wsUri,
          protocolVersion: 1,
          terminalProtocolVersion: 3,
          agentProjects: {
            'shared-id': AgentProject(name: folder, cwd: '/work/$folder'),
            'native': const AgentProject(name: 'Fallback', cwd: '/fallback'),
          },
        );
    final current = snapshot(_endpoint.computerId, 'Current project');
    discovery.onSnapshot!(current);
    expect(local.projectOf(oldAgent)!.name, 'Current project');
    expect(peer.projectOf(oldAgent), isNull);
    expect(local.projectOf(local.agents.last), wireProject);
    expect(swarmAgents(notifier, 'Current project').map((a) => a.machineId), [
      'local',
    ]);
    expect(
      swarmProjects(notifier, const [
        SavedSwarmProject(
          machineId: 'local',
          path: '/work/Current project',
          name: 'Saved folder',
        ),
      ]),
      hasLength(2),
    );
    discovery.onSnapshot!(current);
    discovery.onSnapshot!(snapshot('another-computer', 'Wrong project'));
    expect(notifications, 1);
    discovery.onSnapshot!(snapshot(_endpoint.computerId, 'Moved project'));
    expect(local.projectOf(oldAgent)!.name, 'Moved project');
    expect(notifications, 2);
    expect(notifier.activeSwarm, same(swarm));
    expect(notifier.swarms, hasLength(1));
    expect(notifier.panes, isEmpty);
    expect(notifier.refreshes, 0);
  });

  test('a daemon that answers but is still scanning is reported as such, and supervised', () async {
    final discovery = _ScriptedDiscovery([
      const LocalCliProbe.notReady(
        'still scanning for agents',
        version: '9.9.9',
      ),
    ]);
    final notifier = _Notifier(discovery);
    addTearDown(notifier.dispose);

    await expectLater(
      notifier.ensureCliDaemonReady(),
      throwsA(
        isA<StateError>().having(
          (e) => e.message,
          'message',
          allOf(
            contains('Harness is running (v9.9.9)'),
            contains('still scanning for agents'),
          ),
        ),
      ),
    );
    // Not "did not start" — that sends people to run `harness start` against a daemon that is up.
    expect(
      discovery.superviseCalls,
      1,
      reason: 'the supervisor is what turns this into a recovery',
    );
  });

  test(
    'the socket that just connected restores a cleared local endpoint',
    () async {
      // A refresh during the daemon's restart cleared this row's endpoint; the socket then came back
      // on its own retry. `connected` is the proof the endpoint is good — put it back, or the tiles
      // stay "Offline" and `_canAttachPane` refuses them for good.
      final discovery = _ScriptedDiscovery([LocalCliProbe.ready(_endpoint)]);
      final notifier = _Notifier(discovery)..status = AppStatus.authenticated;
      addTearDown(notifier.dispose);
      await notifier.ensureCliDaemonReady();

      const row = Machine(
        machineId: 'm-local',
        computerId: '0123456789abcdef0123456789abcdef',
        authMode: MachineAuthMode.remote,
        status: 'online',
      );
      notifier.machines = [row];
      final state = MachineState(row)
        ..localOnly = true
        ..localEndpoint = null
        ..connectionStatus = ConnectionStatus.connected;
      notifier.machineStates['m-local'] = state;
      expect(state.usesLocalTransport, isFalse);

      notifier.onMachineConnectedForTest('m-local');

      expect(state.usesLocalTransport, isTrue);
      expect(state.localEndpoint, same(_endpoint));
      expect(state.transportMode, MachineTransportMode.localPlaintext);
    },
  );

  test('a daemon nobody answers for is still "did not start"', () async {
    final discovery = _ScriptedDiscovery([
      const LocalCliProbe.down('connection refused'),
    ]);
    final notifier = _Notifier(discovery);
    addTearDown(notifier.dispose);

    await expectLater(
      notifier.ensureCliDaemonReady(),
      throwsA(
        isA<StateError>().having(
          (e) => e.message,
          'message',
          contains('did not start'),
        ),
      ),
    );
    expect(discovery.superviseCalls, 0);
  });

  test(
    'a daemon with no backend is READY: this computer works over the loopback',
    () async {
      // The case this whole gate used to fail: a daemon up, tmux up, agents up, and no route to the
      // backend. It sat on "Starting local service…" for 45s and then on an error strip — and every
      // retry threw the same error. Offline is a fact the daemon reports, not a reason to wait.
      final offline = LocalCliEndpoint(
        computerId: _endpoint.computerId,
        wsUri: _endpoint.wsUri,
        protocolVersion: 1,
        terminalProtocolVersion: 3,
        machineId: 'm' * 32,
        backendOnline: false,
      );
      // Two answers: offline at boot, online for the retry the reconnect triggers.
      final discovery = _ScriptedDiscovery([
        LocalCliProbe.ready(offline),
        LocalCliProbe.ready(_endpoint),
      ]);
      final notifier = _Notifier(discovery)..status = AppStatus.authenticated;
      addTearDown(notifier.dispose);

      await notifier.ensureCliDaemonReady();

      expect(notifier.backendOnline, isFalse);
      expect(notifier.lastError, isNull);
      expect(discovery.superviseCalls, 1);
      expect(discovery.onBackendOnline, isNotNull);

      // The backend comes back (the supervisor's 5s probe sees `connected:true`): the machines are
      // fetched again without a click, and the profile too if it was never loaded.
      discovery.onBackendOnline!(true);
      await notifier.retryMachines();
      expect(notifier.backendOnline, isTrue);
      expect(notifier.refreshes, 1);
    },
  );

  test('the supervisor reporting ready after a not-ready boot retries the machines without a click', () async {
    final discovery = _ScriptedDiscovery([
      const LocalCliProbe.notReady('still scanning for agents'),
      LocalCliProbe.ready(_endpoint),
    ]);
    final notifier = _Notifier(discovery)..status = AppStatus.authenticated;
    addTearDown(notifier.dispose);

    // The boot path: the gate throws, the error strip shows, supervision is on.
    await notifier.retryMachines();
    expect(notifier.lastError, contains('still scanning for agents'));
    expect(notifier.lastErrorRetryable, isTrue);
    expect(notifier.refreshes, 0);
    expect(discovery.onReady, isNotNull);

    // …and the daemon finishes its handshake. The callback fires the retry
    // without awaiting it; `retryMachines` hands back that same in-flight run.
    discovery.onReady!(_endpoint);
    await notifier.retryMachines();

    expect(notifier.lastError, isNull);
    expect(notifier.refreshes, 1);
    expect(discovery.ensureCalls, 2);
    expect(
      discovery.superviseCalls,
      1,
      reason: 'one supervisor for the app, not one per attempt',
    );
  });

  test('a retry that gets through reads again which devices are still waiting to be seen', () async {
    // The boot found the daemon still connecting, so its own read of the pending devices never ran
    // (or found nothing yet): the retry that finishes the boot reads them.
    final notifier = _Notifier(
      _ScriptedDiscovery([LocalCliProbe.ready(_endpoint)]),
    )..status = AppStatus.authenticated;
    addTearDown(notifier.dispose);
    final api = _PendingDevicesApi();
    notifier.api = api;

    await notifier.retryMachines();
    for (var i = 0; i < 5; i++) {
      await Future<void>.delayed(Duration.zero);
    }

    expect(api.reads, greaterThanOrEqualTo(1));
    expect(notifier.newDevices.map((d) => d.pub), ['a']);
  });

  test(
    'recovery ticks read the pending devices only until one read has succeeded',
    () async {
      final notifier = _Notifier(
        _ScriptedDiscovery([LocalCliProbe.ready(_endpoint)]),
      )..status = AppStatus.authenticated;
      addTearDown(notifier.dispose);
      final api = _PendingDevicesApi()..ready = false;
      notifier.api = api;
      Future<void> tick() async {
        await notifier.retryMachines();
        for (var i = 0; i < 5; i++) {
          await Future<void>.delayed(Duration.zero);
        }
      }

      // The daemon has nothing to say yet: the next tick asks again.
      await tick();
      expect(api.reads, 1);
      await tick();
      expect(api.reads, 2);

      api.ready = true;
      await tick();
      expect(api.reads, 3);
      expect(notifier.newDevices.map((d) => d.pub), ['a']);

      // It has been read: the log's own changes and the daemon's frames say when to read again.
      await tick();
      await tick();
      expect(api.reads, 3);
    },
  );

  test('a daemon that predates the pending list is asked once, not on every recovery tick', () async {
    final notifier = _Notifier(
      _ScriptedDiscovery([LocalCliProbe.ready(_endpoint)]),
    )..status = AppStatus.authenticated;
    addTearDown(notifier.dispose);
    final api = _PendingDevicesApi()..legacy = true;
    notifier.api = api;
    Future<void> tick() async {
      await notifier.retryMachines();
      for (var i = 0; i < 5; i++) {
        await Future<void>.delayed(Duration.zero);
      }
    }

    await tick();
    expect(api.reads, 1);
    await tick();
    await tick();
    expect(api.reads, 1, reason: 'it answered; it has nothing more to say');
    expect(notifier.newDevices, isEmpty);
  });
}