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.

17.1 KB
Raw Blame
import 'package:harness/widgets/workspace_store_button.dart';

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/screens/swarm_screen.dart';
import 'package:harness/shared/theme/app_theme.dart' as grid;
import 'package:harness/shared/theme/workspace_bar_style.dart';
import 'package:harness/shortcuts/app_keymap.dart';
import 'package:harness/state/app_state.dart';
import 'package:harness/state/new_harness.dart';
import 'package:harness/state/swarm_catalog.dart';
import 'package:harness/terminal/terminal_text.dart';
import 'package:harness/widgets/delete_agent_dialog.dart';
import 'package:harness/widgets/desktop_chrome.dart';
import 'package:harness/widgets/desktop_workspace_tab.dart';
import 'package:harness/widgets/harness_activity_mark.dart';
import 'package:harness/widgets/new_harness_form.dart';
import 'package:harness/widgets/pane_header_actions.dart';
import 'package:harness/widgets/swarm_dialogs.dart';
import 'package:harness/widgets/swarm_search_preview.dart';
import 'package:harness/widgets/search_result_text.dart';
import 'package:harness/widgets/workspace_welcome.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:xterm/xterm.dart';

import 'keymap_host_test.dart' show MemoryKeymap, key;
import 'swarm_screen_test.dart' show terminal;
import 'swarm_state_test.dart' show createApp;

void main() {
  late TerminalStyle original;
  setUp(() {
    original = terminalFontStore.value;
    newHarnessOpensInBox = true;
    PackageInfo.setMockInitialValues(
      appName: 'Harness',
      packageName: 'ai.autonomous.harness',
      version: '1.1.25',
      buildNumber: '25',
      buildSignature: '',
    );
  });
  tearDown(() {
    terminalFontStore.value = original;
    newHarnessOpensInBox = false;
  });

  void selectFont(
    double size, [
    TerminalFontChoice choice = TerminalFontChoice.menlo,
  ]) {
    // In memory only: tests never alter the user's persisted preferences.
    terminalFontStore.value = TerminalStyle(
      fontSize: size,
      fontFamily: choice.fontFamily,
      fontFamilyFallback: choice.fontFamilyFallback,
    );
  }

  final ramp = <double>{
    grid.AppType.displaySize,
    grid.AppType.titleSize,
    grid.AppType.headingSize,
    grid.AppType.bodySize,
    grid.AppType.captionSize,
    DesktopChrome.heading().fontSize!,
    DesktopChrome.text().fontSize!,
  };

  /// General UI and workspace bars keep their fixed scales during terminal zoom.
  /// Return general UI sizes for comparisons across a terminal zoom.
  Map<String, double> checkText(WidgetTester tester, {int atLeast = 4}) {
    final sizes = <String, double>{};
    var checked = 0;
    void check(
      InlineSpan span,
      TextStyle inherited, {
      bool workspaceBar = false,
      bool desktopTab = false,
    }) {
      final style = inherited.merge(span.style);
      if (span is TextSpan) {
        final text = span.text?.trim();
        if (text?.isNotEmpty == true &&
            ![
              'MaterialIcons',
              'lucide',
              'LucideIcons',
            ].any((icon) => style.fontFamily?.contains(icon) == true)) {
          checked++;
          if (desktopTab) {
            expect(style.fontSize, 13, reason: text);
            expect(style.fontFamily, grid.AppType.sansFamily, reason: text);
          } else if (workspaceBar) {
            expect(style.fontSize, 13, reason: text);
            expect(
              style.fontFamily,
              workspaceBarTextStyle().fontFamily,
              reason: text,
            );
          } else {
            expect(ramp, contains(style.fontSize), reason: text);
            expect(
              [grid.AppType.sansFamily, grid.AppType.monoFamily],
              contains(style.fontFamily),
              reason: text,
            );
            sizes[text!] = style.fontSize!;
          }
        }
        for (final child in span.children ?? <InlineSpan>[]) {
          check(
            child,
            style,
            workspaceBar: workspaceBar,
            desktopTab: desktopTab,
          );
        }
      }
    }

    // The welcome composer and search use the shared desktop type scale;
    // their controls are checked separately below.
    final welcome = find.byType(WorkspaceWelcome);
    final setup = find.byWidgetPredicate(
      (widget) =>
          widget is NewHarnessForm ||
          widget.key == const ValueKey('swarm-search-results'),
    );
    for (final element in find.byType(RichText).evaluate()) {
      if (find
              .descendant(of: welcome, matching: find.byWidget(element.widget))
              .evaluate()
              .isNotEmpty ||
          find
              .descendant(of: setup, matching: find.byWidget(element.widget))
              .evaluate()
              .isNotEmpty) {
        continue;
      }
      check(
        (element.widget as RichText).text,
        const TextStyle(),
        desktopTab:
            (element.findAncestorWidgetOfExactType<DesktopWorkspaceTab>() !=
                    null &&
                element.findAncestorWidgetOfExactType<ActivityMark>() ==
                    null) ||
            element.findAncestorWidgetOfExactType<WorkspaceStoreButton>() !=
                null,
        workspaceBar: find
            .descendant(
              of: find.byWidgetPredicate(
                (widget) =>
                    widget.key == const ValueKey('workspace-status-bar') ||
                    widget.key == const ValueKey('workspace-tab-bar') ||
                    widget.key == const ValueKey('terminal-pane-title') ||
                    widget.key == const ValueKey('viewer-pane-title') ||
                    widget is ActivityMark ||
                    widget is PaneHeaderActions,
              ),
              matching: find.byWidget(element.widget),
            )
            .evaluate()
            .isNotEmpty,
      );
    }
    for (final element in find.byType(EditableText).evaluate()) {
      final widget = element.widget as EditableText;
      if (element.findAncestorWidgetOfExactType<TextField>()?.key ==
          const ValueKey('new-harness-task')) {
        expect(widget.style.fontSize, 13);
        expect(widget.style.fontFamily, grid.AppType.sansFamily);
        continue;
      }
      if (element.findAncestorWidgetOfExactType<TextField>()?.key ==
          const ValueKey('swarm-search-input')) {
        expect(widget.style.fontSize, 17);
        expect(widget.style.fontFamily, grid.AppType.sansFamily);
        continue;
      }
      // A field is on the scale, or it types into the terminal and follows it.
      expect({
        ...ramp,
        terminalFontStore.size,
      }, contains(widget.style.fontSize));
    }
    expect(checked, greaterThanOrEqualTo(atLeast));
    expect(tester.takeException(), isNull);
    return sizes;
  }

  /// The welcome page uses the same system-font composer at every terminal zoom.
  void checkWelcome(WidgetTester tester) {
    expect(
      tester.widget<NewHarnessForm>(find.byType(NewHarnessForm)).embedded,
      isTrue,
    );
    final task = tester.widget<TextField>(
      find.byKey(const ValueKey('new-harness-task')),
    );
    expect(task.style!.fontSize, 13);
    expect(task.style!.fontFamily, grid.AppType.sansFamily);
    expect(
      tester.getSize(find.byKey(const ValueKey('new-harness-composer'))).width,
      680,
    );
  }

  /// Zooming the terminal leaves every UI text where it was.
  void expectSameSizes(Map<String, double> before, Map<String, double> after) {
    for (final entry in after.entries) {
      if (before.containsKey(entry.key)) {
        expect(entry.value, before[entry.key], reason: entry.key);
      }
    }
  }

  Future<void> mount(
    WidgetTester tester,
    AppNotifier app,
    MemoryKeymap map, {
    bool native = false,
  }) async {
    tester.view.devicePixelRatio = 1;
    tester.view.physicalSize = const Size(1280, 800);
    addTearDown(tester.view.reset);
    final projects = SwarmProjectStore();
    addTearDown(projects.dispose);
    await tester.pumpWidget(
      ListenableBuilder(
        listenable: terminalFontStore,
        builder: (context, _) => MaterialApp(
          debugShowCheckedModeBanner: false,
          themeAnimationDuration: Duration.zero,
          theme: grid.buildAppTheme(brightness: Brightness.dark),
          builder: (_, child) => MediaQuery.withNoTextScaling(
            child: grid.BrightnessScope(
              child: KeymapProvider(keymap: map, child: child!),
            ),
          ),
          home: SwarmScreen(
            notifier: app,
            nativeTabs: native,
            projectStore: projects,
          ),
        ),
      ),
    );
    await tester.pumpAndSettle();
  }

  test('the theme keeps its scale whatever the terminal size', () {
    for (final size in [9.0, 13.0, 18.0, 22.0]) {
      selectFont(size);
      final theme = grid.buildAppTheme(brightness: Brightness.dark);
      final t = theme.textTheme;
      final sans = grid.AppType.sansFamily;
      // App headings, controls and fields use the system face. Terminal and
      // explicit code styles remain independent and are checked below.
      final expected = {
        t.displayLarge: (grid.AppType.displaySize, sans),
        t.headlineSmall: (grid.AppType.titleSize, sans),
        t.titleLarge: (grid.AppType.titleSize, sans),
        t.titleMedium: (grid.AppType.headingSize, sans),
        t.titleSmall: (grid.AppType.bodySize, sans),
        t.labelLarge: (grid.AppType.bodySize, sans),
        t.labelSmall: (grid.AppType.captionSize, sans),
        t.bodyLarge: (grid.AppType.bodySize, sans),
        t.bodyMedium: (grid.AppType.bodySize, sans),
        t.bodySmall: (grid.AppType.bodySize, sans),
        grid.kFieldTextStyle: (grid.AppType.bodySize, sans),
        theme.dialogTheme.titleTextStyle: (grid.AppType.headingSize, sans),
        theme.dialogTheme.contentTextStyle: (grid.AppType.bodySize, sans),
        theme.tooltipTheme.textStyle: (grid.AppType.captionSize, sans),
      };
      for (final entry in expected.entries) {
        expect(entry.key!.fontSize, entry.value.$1);
        expect(entry.key!.fontFamily, entry.value.$2);
      }
      expect(grid.AppControl.heightFieldScaled, 36);
      expect(grid.AppControl.heightScaled, 32);
    }
  });

  test('mono takes the terminal face but not its size', () {
    selectFont(22);
    for (final (style, size) in [
      (grid.AppType.mono(), grid.AppType.monoSize),
      (grid.AppType.monoLabel(), grid.AppType.monoLabelSize),
      (grid.AppType.monoMeta(), grid.AppType.monoMetaSize),
      (grid.AppFont.codeStyle(), grid.AppType.monoSize),
    ]) {
      expect(style.fontFamily, 'Menlo');
      expect(style.fontSize, size);
    }
    expect(terminalTextStyle().fontSize, 22);
  });

  testWidgets(
    'welcome, native setup, and workspace chrome keep their scale during terminal zoom',
    (tester) async {
      final app = createApp();
      app.machineStates['m']!.localOnly = true;
      app.gitProjectReaderForTest = (_, _) async => {'isGit': false};
      await app.agentPreference.remember('codex');
      await app.projectHistory.select('m', '/work/openharness');
      final map = MemoryKeymap();
      addTearDown(app.dispose);
      addTearDown(map.dispose);
      app.adoptSessionForTest(terminal('a0', []));
      selectFont(13);
      await mount(tester, app, map);
      final before = checkText(tester);
      selectFont(18, TerminalFontChoice.monaco);
      await tester.pumpAndSettle();
      expectSameSizes(before, checkText(tester));

      await key(tester, LogicalKeyboardKey.keyT, cmd: true);
      await tester.pumpAndSettle();
      expect(find.byType(WorkspaceWelcome), findsOneWidget);
      // Only the tab chrome is UI text here; the page itself is checked below.
      checkText(tester, atLeast: 1);
      checkWelcome(tester);
      selectFont(22);
      await tester.pumpAndSettle();
      checkWelcome(tester);
      selectFont(18, TerminalFontChoice.monaco);
      await tester.pumpAndSettle();
      await key(tester, LogicalKeyboardKey.keyN, cmd: true);
      expect(find.byType(NewHarnessForm), findsOneWidget);
      final box = checkText(tester, atLeast: 1);
      final start = find.descendant(
        of: find.byKey(const ValueKey('new-harness-field-start')),
        matching: find.text('New Harness'),
      );
      TextStyle startStyle() =>
          DefaultTextStyle.of(tester.element(start)).style
              .merge(tester.widget<Text>(start).style);
      expect(startStyle().fontSize, 13);
      expect(startStyle().fontFamily, grid.AppType.sansFamily);
      selectFont(22);
      await tester.pumpAndSettle();
      expectSameSizes(box, checkText(tester, atLeast: 1));
      expect(startStyle().fontSize, 13);
      expect(startStyle().fontFamily, grid.AppType.sansFamily);
      await key(tester, LogicalKeyboardKey.escape);
      for (final terminalSize in [9.0, 18.0]) {
        await key(tester, LogicalKeyboardKey.keyP, cmd: true);
        final input = find.byKey(const ValueKey('swarm-search-input'));
        await tester.enterText(input, 'Agent');
        await key(tester, LogicalKeyboardKey.arrowDown);
        await tester.pumpAndSettle();
        expect(find.byType(SwarmSearchPreview), findsOneWidget);
        final search = checkText(tester, atLeast: 1);
        final resultSizes = {
          for (final text in tester.widgetList<SearchResultText>(
            find.byType(SearchResultText),
          ))
            text.text: text.style.fontSize,
        };
        selectFont(terminalSize, TerminalFontChoice.monaco);
        await tester.pumpAndSettle();
        expect(tester.widget<TextField>(input).controller!.text, 'Agent');
        expect(tester.widget<TextField>(input).focusNode!.hasFocus, isTrue);
        expectSameSizes(search, checkText(tester, atLeast: 1));
        expect(tester.widget<TextField>(input).style!.fontSize, 17);
        expect(
          tester.widget<TextField>(input).style!.fontFamily,
          grid.AppType.sansFamily,
        );
        for (final text in tester.widgetList<SearchResultText>(
          find.byType(SearchResultText),
        )) {
          expect(text.style.fontSize, resultSizes[text.text]);
          expect([
            grid.AppType.sansFamily,
            grid.AppType.monoFamily,
          ], contains(text.style.fontFamily));
        }
        await key(tester, LogicalKeyboardKey.escape);
      }
      await tester.pumpWidget(const SizedBox());
    },
  );

  testWidgets('rename and stop forms keep the scale while the terminal zooms', (
    tester,
  ) async {
    final app = createApp();
    final map = MemoryKeymap();
    addTearDown(app.dispose);
    addTearDown(map.dispose);
    app.adoptSessionForTest(terminal('a0', []));
    selectFont(13);
    await mount(tester, app, map);
    final context = tester.element(find.byType(SwarmScreen));
    unawaited(showSwarmRenameDialog(context, 'My project', keymap: map));
    await tester.pumpAndSettle();
    final rename = checkText(tester);
    selectFont(22, TerminalFontChoice.monaco);
    await tester.pumpAndSettle();
    expectSameSizes(rename, checkText(tester));
    expect(
      tester
          .widget<TextField>(find.byKey(const Key('tab-rename-input')))
          .controller!
          .text,
      'My project',
    );
    await key(tester, LogicalKeyboardKey.escape);
    unawaited(
      confirmDeleteAgent(
        context,
        app,
        'm',
        'a0',
        'My project',
        engine: 'codex',
        keymap: map,
      ),
    );
    await tester.pumpAndSettle();
    final stop = checkText(tester);
    selectFont(9);
    await tester.pumpAndSettle();
    expectSameSizes(stop, checkText(tester));
    await key(tester, LogicalKeyboardKey.escape);
    await tester.pumpWidget(const SizedBox());
  });

  testWidgets(
    'native footer payload keeps its 13 pt status face during terminal font changes',
    (tester) async {
      final updates = <Map<dynamic, dynamic>>[];
      const channel = MethodChannel('harness/swarm_tabs');
      tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (
        call,
      ) async {
        if (call.method == 'update') updates.add(call.arguments as Map);
        return true;
      });
      addTearDown(
        () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
          channel,
          null,
        ),
      );
      final app = createApp();
      final map = MemoryKeymap();
      addTearDown(app.dispose);
      addTearDown(map.dispose);
      app.adoptSessionForTest(terminal('a0', []));
      await mount(tester, app, map, native: true);
      selectFont(22, TerminalFontChoice.monaco);
      await tester.pumpAndSettle();
      expect(
        updates.last['barStyle'],
        containsPair('family', workspaceBarTextStyle().fontFamily),
      );
      expect(updates.last['barStyle'], containsPair('size', 13.0));
      selectFont(14, TerminalFontChoice.sfMono);
      await tester.pumpAndSettle();
      expect(
        updates.last['barStyle'],
        containsPair('family', workspaceBarTextStyle().fontFamily),
      );
      expect(updates.last['barStyle'], containsPair('size', 13.0));
      expect(updates.last.containsKey('fontFallbacks'), isFalse);
      await tester.pumpWidget(const SizedBox());
    },
  );
}