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.0 KB
Raw Blame
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/core/first_task.dart';
import 'package:harness/state/new_harness.dart';
import 'package:harness/widgets/new_harness_form.dart';
import 'package:harness/ws/ws_conn.dart';

import 'swarm_state_test.dart' show createApp;

class _Connection extends WsConn {
  _Connection()
    : super(
        wsBaseUrl: 'ws://fixture.invalid',
        autonomousEnv: 'test',
        machineId: 'm',
        accessTokenProvider: (_, _) async => '',
        onAuthFailure: (_) {},
        onEvent: (_) {},
        onStatus: (_) {},
      );

  bool catalogUnavailable = false, installed = false;
  bool held = false, cancelLost = false;
  String? refusal;
  final cancels = <String>[];
  final install = Completer<Map<String, dynamic>>();
  int installs = 0;
  final starts = <Map<String, dynamic>>[];

  @override
  Future<Map<String, dynamic>> request(
    String type, {
    Map<String, dynamic> payload = const {},
    Duration timeout = const Duration(seconds: 20),
  }) async {
    switch (type) {
      case 'engines_probe':
        return {'engines': []};
      case 'git_project_info':
        return {'isGit': false};
      case 'fs_list_dir':
        return {'path': '/work', 'entries': []};
      case 'dsh_list':
        if (catalogUnavailable) {
          throw const WsRequestFailure(
            responseType: 'dsh_list',
            code: 'UNSUPPORTED',
          );
        }
        return {
          'dsh': [
            {
              'id': 'autonomous/blender',
              'name': 'Blender',
              'engine': 'claude',
              'installed': installed,
            },
          ],
        };
      case 'dsh_install':
        installs++;
        final reply = await install.future;
        installed = reply['ok'] == true;
        return reply;
      case 'agent_create':
        starts.add(Map.of(payload));
        if (refusal case final detail?) {
          return {
            'creationId': payload['creationId'],
            'state': 'failed',
            'failure': {'code': 'BUSY', 'detail': detail},
          };
        }
        if (held) {
          return {
            'creationId': payload['creationId'],
            'state': 'pending',
            'held': {
              'service': 'models',
              'detail': 'Waiting for the selected model.',
            },
          };
        }
        return {
          'creationId': payload['creationId'],
          'state': 'created',
          'agent': {
            'id': 'created',
            'name': 'Created',
            'engine': payload['engine'],
          },
        };
      case 'agent_create_cancel':
        cancels.add(payload['creationId'] as String);
        if (cancelLost) throw StateError('fixture disconnected');
        return {'creationId': payload['creationId'], 'state': 'cancelled'};
      default:
        return {};
    }
  }
}

void main() {
  // "Installing Hermes. The harness starts once it is installed." was red, as
  // if the start had failed (DESKTOP-0TFH0OJ, 2026-11-04).
  testWidgets('a request the machine is holding does not read as a failure', (
    tester,
  ) async {
    Future<Color?> colorOf(_Connection connection, String words) async {
      final app = createApp(
        connectionForTest: (_) => connection,
        connected: true,
      );
      final box = NewHarnessController(
        app,
        machineId: 'm',
        engine: 'claude',
        folder: '/work',
      );
      addTearDown(app.dispose);
      addTearDown(box.dispose);
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: NewHarnessForm(
              key: UniqueKey(),
              controller: box,
              desktop: true,
              onClose: () {},
              onCreated: () {},
            ),
          ),
        ),
      );
      await tester.runAsync(box.create);
      await tester.pump();
      final status = find.byKey(const ValueKey('new-harness-status'));
      expect(tester.widget<Text>(status).data, contains(words));
      return tester.widget<Text>(status).style?.color;
    }

    final error = ThemeData().colorScheme.error;
    expect(
      await colorOf(_Connection()..held = true, 'Waiting for the selected'),
      isNot(error),
    );
    expect(
      await colorOf(_Connection()..refusal = 'Try later', 'Try later'),
      error,
    );
    await tester.pumpWidget(const SizedBox());
    await tester.pump(const Duration(seconds: 1));
  });

  test(
    'held creation retains its choices until cancellation is confirmed',
    () async {
      final connection = _Connection()
        ..held = true
        ..cancelLost = true;
      final app = createApp(
        connectionForTest: (_) => connection,
        connected: true,
      );
      final box = NewHarnessController(
        app,
        machineId: 'm',
        engine: 'claude',
        folder: '/work',
      );
      addTearDown(app.dispose);
      addTearDown(box.dispose);
      await box.create();
      expect(box.checking, isTrue);
      expect(box.canCancelPending, isTrue);
      expect(box.error, 'Waiting for the selected model.');
      expect(box.requestDismiss(), isFalse);
      expect(box.error, contains('will continue when the model is available'));
      await box.cancelPending();
      expect(box.checking, isTrue);
      expect(box.error, contains('Cancellation is not confirmed'));
      connection.cancelLost = false;
      await box.cancelPending();
      expect(box.checking, isFalse);
      expect(box.canCancelPending, isFalse);
      expect(box.status, 'Creation cancelled.');
      expect(box.requestDismiss(), isTrue);
      expect(connection.starts, hasLength(1));
      expect(connection.cancels, [
        connection.starts.single['creationId'],
        connection.starts.single['creationId'],
      ]);
    },
  );

  for (final failure in [false, true]) {
    test('a missing Store harness waits for installation (failure=$failure)', () async {
      final connection = _Connection();
      final app = createApp(
        connectionForTest: (_) => connection,
        connected: true,
      );
      final box = NewHarnessController(
        app,
        machineId: 'm',
        engine: 'autonomous/blender',
        folder: '/work',
      );
      addTearDown(app.dispose);
      addTearDown(box.dispose);
      final creating = box.create();
      await Future<void>.delayed(Duration.zero);
      expect(box.status, startsWith('Installing Blender'));
      expect(box.busy, isTrue);
      expect(connection.installs, 1);
      expect(connection.starts, isEmpty);
      expect(await box.create(), NewHarnessOutcome.failed);
      expect(box.requestDismiss(), isFalse);

      connection.install.complete(
        failure ? {'ok': false, 'detail': 'Disk is full'} : {'ok': true},
      );
      expect(
        await creating,
        failure ? NewHarnessOutcome.failed : NewHarnessOutcome.created,
      );
      expect(box.busy, isFalse);
      if (failure) {
        expect(box.error, 'Disk is full');
        expect(connection.starts, isEmpty);
        // An installation completed outside the dialog is discovered on retry.
        connection.installed = true;
        expect(await box.create(), NewHarnessOutcome.created);
      }
      expect(connection.installs, 1);
      expect(connection.starts, hasLength(1));
      expect(connection.starts.single['engine'], 'claude');
      expect(connection.starts.single['dsh'], 'autonomous/blender');
    });
  }

  test(
    'an unsupported Store catalog is actionable and does not launch',
    () async {
      final connection = _Connection()..catalogUnavailable = true;
      final app = createApp(
        connectionForTest: (_) => connection,
        connected: true,
      );
      final box = NewHarnessController(
        app,
        machineId: 'm',
        engine: 'autonomous/blender',
        folder: '/work',
      );
      addTearDown(app.dispose);
      addTearDown(box.dispose);
      expect(await box.create(), NewHarnessOutcome.failed);
      expect(box.error, contains('Update Harness CLI'));
      expect(box.busy, isFalse);
      expect(connection.installs, 0);
      expect(connection.starts, isEmpty);
      connection.catalogUnavailable = false;
      connection.installed = true;
      expect(await box.create(), NewHarnessOutcome.created);
      expect(connection.starts, hasLength(1));
    },
  );

  test(
    'dismissal during installation cannot launch after the dialog is disposed',
    () async {
      final connection = _Connection();
      final app = createApp(
        connectionForTest: (_) => connection,
        connected: true,
      );
      final box = NewHarnessController(
        app,
        machineId: 'm',
        engine: 'autonomous/blender',
        folder: '/work',
      );
      addTearDown(app.dispose);
      final creating = box.create();
      await Future<void>.delayed(Duration.zero);
      expect(connection.installs, 1);
      box.dispose();
      connection.install.complete({'ok': true});
      expect(await creating, NewHarnessOutcome.failed);
      expect(connection.starts, isEmpty);
    },
  );

  test(
    'an oversized carried task is retained and can be shortened before retry',
    () async {
      final connection = _Connection();
      final app = createApp(
        connectionForTest: (_) => connection,
        connected: true,
      );
      final task = 'x' * (kFirstTaskMaxLength + 1);
      final box = NewHarnessController(
        app,
        machineId: 'm',
        engine: 'codex',
        folder: '/work',
        task: task,
      );
      addTearDown(app.dispose);
      addTearDown(box.dispose);
      expect(await box.create(), NewHarnessOutcome.failed);
      expect(
        box.error,
        contains('A first message can be $kFirstTaskMaxLength'),
      );
      expect(box.task, task);
      expect(connection.starts, isEmpty);
      box.focusField(NewHarnessField.task);
      box.setQuery('shortened task');
      expect(await box.create(), NewHarnessOutcome.created);
      expect(connection.starts.single['prompt'], 'shortened task');
    },
  );
}