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.

5.0 KB
Raw Blame
import 'support/open_harness.dart';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/shared/theme/appearance_prefs_store.dart';
import 'package:harness/state/pane_arrangement.dart';
import 'package:harness/state/swarm_navigation.dart';
import 'package:harness/state/swarm_search.dart';
import 'package:harness/terminal/terminal_binary.dart';
import 'package:xterm/xterm.dart';

import 'swarm_screen_test.dart' show mount, terminal;
import 'swarm_state_test.dart' show createApp;

void main() {
  // These pin exact rectangles after a split, with Autobalance splits off.
  setUp(() {
    final previous = appearancePrefsStore.value;
    addTearDown(() => appearancePrefsStore.value = previous);
    appearancePrefsStore.value = previous.copyWith(
      panes: previous.panes.copyWith(autobalanceSplits: false),
    );
  });

  testWidgets('New Pane shortcut reuses the chosen session in this workspace', (
    tester,
  ) async {
    final app = createApp(connected: true);
    final input = <TerminalBinaryFrame>[];
    final shared = app.adoptSessionForTest(terminal('a0', input));
    final source = app.activeSwarm;
    app.newSwarm(name: 'Review');
    final existing = app.adoptSessionForTest(terminal('a1', input));
    final target = app.activeSwarm;
    await mount(tester, app);
    tester.view.physicalSize = const Size(880, 560);
    await tester.pump();
    expect(find.byKey(const ValueKey('swarm-new-pane-button')), findsNothing);
    expect(find.byType(FloatingActionButton), findsNothing);
    await openHarnessPicker(tester);
    await tester.enterText(
      find.byKey(const ValueKey('swarm-search-input')),
      'Agent 0',
    );
    await tester.pump();
    expect(find.byKey(const ValueKey('swarm-search-hints')), findsNothing);
    expect(find.byKey(const ValueKey('swarm-search-count')), findsNothing);

    expect(find.byType(AlertDialog), findsNothing);
    await tester.sendKeyEvent(LogicalKeyboardKey.enter);
    await tester.pump();
    expect(app.activeSwarm, same(target));
    expect(target.panes, [existing, shared]);
    expect(source.panes, [shared]);
    expect(input, isEmpty);
    expect(tester.takeException(), isNull);
    await tester.pumpWidget(const SizedBox());
    app.dispose();
  });

  for (final axis in PaneResizeAxis.values) {
    testWidgets(
      'placing an existing agent into a ${axis.name} split preserves its neighbors',
      (tester) async {
        final app = createApp();
        final input = <TerminalBinaryFrame>[];
        final first = app.adoptSessionForTest(terminal('a0', input));
        final neighbor = app.adoptSessionForTest(terminal('a1', input));
        final target = app.activeSwarm;
        app.newSwarm(name: 'Elsewhere');
        final shared = app.adoptSessionForTest(terminal('a2', input));
        final source = app.activeSwarm;
        app.selectSwarm(target.id);
        app.focusPane(first.id);
        await mount(tester, app);
        tester.view.physicalSize = const Size(3000, 1800);
        await tester.pump();
        final neighborRect = tester.getRect(find.byKey(neighbor.cellKey));
        final expected = app.preparePaneSplit(axis)!;
        // Split commands now create a new harness directly. Existing sessions
        // still use this placement path when assigned to a split destination.
        await app.assignAgentToPane(
          null,
          'm',
          'a2',
          swarmId: target.id,
          split: expected,
        );
        await tester.pump();
        expect(app.panes, [first, shared, neighbor]);
        expect(source.panes, [shared]);
        expect(app.activeSwarm.manualLayout!.tiles, expected.after.tiles);
        expect(tester.getRect(find.byKey(neighbor.cellKey)), neighborRect);
        expect(input, isEmpty);
        expect(find.byType(TerminalView), findsNWidgets(3));
        await tester.pumpWidget(const SizedBox());
        app.dispose();
      },
    );
  }

  test(
    'adding a group uses this tab, skips duplicates and keeps the source',
    () async {
      final app = createApp();
      addTearDown(app.dispose);
      app.adoptSessionForTest(terminal('a0', []));
      app.adoptSessionForTest(terminal('a1', []));
      final source = app.activeSwarm;
      app.renameSwarm(source.id, 'Release');
      app.newSwarm(name: 'Review');
      final target = app.activeSwarm;
      await app.addAgentToSwarm('m', 'a0');
      final search = SwarmSearchController(app, [], adding: true);
      addTearDown(search.dispose);
      final row = search.rows.singleWhere(
        (row) => row.id == swarmDestinationId(source.id),
      );
      final choice = search.submit(row)!;
      expect(choice.action, SwarmSearchAction.addHere);
      expect(
        await activateSwarmSearchSelection(
          app,
          choice,
          destinationSwarmId: target.id,
        ),
        isTrue,
      );
      expect(app.activeSwarm, same(target));
      expect(target.panes, source.panes);
      expect(target.name, 'Review');
      expect(search.canAdd(row), isFalse);
    },
  );
}