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.

12.1 KB
Raw Blame
// Optional pictures: HARNESS_SETTINGS_CAPTURE_DIR=/private/tmp/settings-review
// flutter test --no-pub test/settings_review_render_test.dart
// Uses the real screen with synthetic account data. It never starts a CLI,
// loads user usage logs, edits preferences, or invokes a settings action.
import 'dart:async';
import 'dart:io';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:harness/auth/auth_session.dart';
import 'package:harness/core/config.dart';
import 'package:harness/core/models.dart';
import 'package:harness/settings/settings_nav.dart';
import 'package:harness/settings/experimental_features.dart';
import 'package:harness/settings/settings_screen.dart';
import 'package:harness/settings/settings_section.dart';
import 'package:harness/shared/theme/app_theme.dart' as grid;
import 'package:harness/shared/widgets/section_scaffold.dart';
import 'package:harness/state/app_state.dart';
import 'package:harness/update/desktop_updater.dart';
import 'package:harness/update/manual_update_check.dart';
import 'package:harness/widgets/shortcuts_sheet.dart';
import 'package:harness/widgets/update_notice.dart';
import 'package:harness/widgets/laid_out_list.dart';

import 'support/experimental_settings.dart';
import 'support/real_fonts.dart';
import 'swarm_state_test.dart' show MemoryStore;

void main() {
  setUpAll(() async {
    // Real Apple faces include the modifier glyphs. The metric-compatible
    // Arial used by the shared layout helper does not contain Command/Shift.
    if (Platform.isMacOS) {
      for (final (family, path) in [
        ('.AppleSystemUIFont', '/System/Library/Fonts/SFNS.ttf'),
        ('SF Pro Text', '/System/Library/Fonts/SFNS.ttf'),
        ('Roboto', '/System/Library/Fonts/SFNS.ttf'),
        ('.AppleSystemUIFontMonospaced', '/System/Library/Fonts/SFNSMono.ttf'),
        ('Menlo', '/System/Library/Fonts/SFNSMono.ttf'),
        ('Helvetica Neue', '/System/Library/Fonts/Apple Symbols.ttf'),
      ]) {
        final bytes = ByteData.sublistView(await File(path).readAsBytes());
        await (FontLoader(family)..addFont(Future.value(bytes))).load();
      }
    } else {
      await loadRealFonts();
    }
    for (final (family, path) in [
      ('MaterialIcons', 'fonts/MaterialIcons-Regular.otf'),
      (
        'packages/lucide_icons_flutter/Lucide',
        'packages/lucide_icons_flutter/assets/lucide.ttf',
      ),
      (
        'packages/lucide_icons_flutter/Lucide400',
        'packages/lucide_icons_flutter/assets/build_font/LucideVariable-w400.ttf',
      ),
    ]) {
      await (FontLoader(family)..addFont(rootBundle.load(path))).load();
    }
    PackageInfo.setMockInitialValues(
      appName: 'Harness',
      packageName: 'ai.autonomous.harness',
      version: '1.0.0',
      buildNumber: '1',
      buildSignature: '',
    );
  });

  for (final brightness in Brightness.values) {
    for (final scale in [1.0, 1.8]) {
      for (final section
          in settingsGroupsFor(debugSurface: false)
              .expand((group) => group.sections)
              .where((section) => section != SettingsSection.customize)) {
        testWidgets(
          '${section.name} at minimum window, ${brightness.name}, $scale text',
          (tester) async {
            tester.view.devicePixelRatio = 1;
            tester.view.physicalSize = const Size(880, 560);
            addTearDown(tester.view.reset);
            final oldBrightness = grid.AppTheme.brightness.value;
            grid.AppTheme.brightness.value = brightness;
            addTearDown(() => grid.AppTheme.brightness.value = oldBrightness);
            final shadows = debugDisableShadows;
            debugDisableShadows = false;
            addTearDown(() => debugDisableShadows = shadows);
            final app =
                AppNotifier(
                    config: AppConfig.dev,
                    authSession: AuthSession(),
                    configStore: null,
                  )
                  ..currentUser = const CurrentUserProfile(
                    name: 'Morgan Rivera',
                    email: '[email protected]',
                  );
            final experiments = MemoryExperimentalFeaturesStore(
              storage: MemoryStore(),
            );
            addTearDown(experiments.dispose);
            if (section == SettingsSection.experimental &&
                brightness == Brightness.dark) {
              await experiments.set(ExperimentalFeature.focusBarCreature, true);
            }
            final boundary = GlobalKey();
            Future<void> capture(String name) async {
              final output =
                  Platform.environment['HARNESS_SETTINGS_CAPTURE_DIR'];
              if (output == null) return;
              final render =
                  boundary.currentContext!.findRenderObject()!
                      as RenderRepaintBoundary;
              await tester.runAsync(() async {
                final image = await render.toImage(pixelRatio: 1);
                final bytes = await image.toByteData(
                  format: ui.ImageByteFormat.png,
                );
                await Directory(output).create(recursive: true);
                await File('$output/$name.png')
                    .writeAsBytes(bytes!.buffer.asUint8List());
                image.dispose();
              });
            }

            addTearDown(() async {
              await tester.pumpWidget(const SizedBox());
              app.dispose();
            });
            await tester.pumpWidget(
              RepaintBoundary(
                key: boundary,
                child: MaterialApp(
                  debugShowCheckedModeBanner: false,
                  theme: grid.buildAppTheme(brightness: brightness),
                  builder: (context, child) => MediaQuery(
                    data: MediaQuery.of(context)
                        .copyWith(textScaler: TextScaler.linear(scale)),
                    child: grid.BrightnessScope(child: child!),
                  ),
                  home: SettingsScreen(
                    notifier: app,
                    experimentalFeatures: experiments,
                  ),
                ),
              ),
            );
            await tester.pumpAndSettle();
            final search = find.byKey(const Key('settings-search-field'));
            expect(
              tester.widget<TextField>(search).focusNode!.hasFocus,
              isTrue,
            );
            await tester.enterText(search, section.label);
            await tester.sendKeyEvent(LogicalKeyboardKey.enter);
            await tester.pumpAndSettle();
            expect(
              tester.widget<SettingsNav>(find.byType(SettingsNav)).section,
              section,
            );
            await tester.enterText(search, '');
            await tester.pumpAndSettle();
            if (section == SettingsSection.shortcuts) {
              expect(
                find.byKey(const ValueKey('shortcuts-search')),
                findsOneWidget,
              );
            } else {
              final scaffold = find.byType(SectionScaffold);
              final heading = find.descendant(
                of: scaffold,
                matching: find.text(
                  tester.widget<SectionScaffold>(scaffold).title,
                ),
              );
              expect(
                tester.getCenter(find.text('Back to app')).dy,
                closeTo(tester.getCenter(heading).dy, 1),
                reason: 'Back and the section title share a header at every text size.',
              );
            }
            if (section == SettingsSection.about) {
              await tester.runAsync(() async {
                await precacheImage(
                  const AssetImage('assets/app_icon.png'),
                  boundary.currentContext!,
                );
              });
              await tester.pumpAndSettle();
            }
            final name = '${section.name}-${brightness.name}-$scale';
            await capture(name);
            debugDisableShadows = shadows;
            if (section == SettingsSection.usage) {
              final label = tester.renderObject<RenderParagraph>(
                find.text('Last 30 days'),
              );
              expect(
                label.getMaxIntrinsicWidth(double.infinity),
                lessThanOrEqualTo(label.size.width + 0.1),
                reason: 'The current Usage range must be readable at a glance.',
              );
              await tester.ensureVisible(find.text('Last 30 days'));
              await tester.pumpAndSettle();
              debugDisableShadows = false;
              await capture('$name-range');
              debugDisableShadows = shadows;
            }
            if (section == SettingsSection.about) {
              await tester.ensureVisible(
                find.byKey(const Key('settings-check-updates-button')),
              );
              await tester.pumpAndSettle();
              debugDisableShadows = false;
              await capture('$name-actions');
              debugDisableShadows = shadows;
              const available = DesktopUpdateCheck.available(
                UpdateInfo(
                  version: '1.0.4',
                  url: 'https://updates.example.test/Harness.zip',
                  sha256: 'unused',
                  size: 48000000,
                ),
              );
              for (final result in const [
                ManualUpdateCheck(check: DesktopUpdateCheck.upToDate()),
                ManualUpdateCheck(check: DesktopUpdateCheck.failed()),
                ManualUpdateCheck(check: DesktopUpdateCheck.disabled()),
                ManualUpdateCheck(check: available),
                ManualUpdateCheck(check: available, isSkipped: true),
              ]) {
                unawaited(
                  showUpdateCheckDialog(
                    tester.element(find.byType(SettingsScreen)),
                    app,
                    result,
                  ),
                );
                await tester.pumpAndSettle();
                debugDisableShadows = false;
                await capture(
                  '$name-update-${result.status.name}'
                  '${result.isSkipped ? '-skipped' : ''}',
                );
                debugDisableShadows = shadows;
                expect(
                  tester.takeException(),
                  isNull,
                  reason: result.status.name,
                );
                await tester.sendKeyEvent(LogicalKeyboardKey.escape);
                await tester.pumpAndSettle();
                expect(find.byType(Dialog), findsNothing);
              }
            }
            if (section == SettingsSection.shortcuts) {
              // Rows outside the viewport are now built on demand.
              await tester.scrollUntilVisible(
                find.text('Workspace'),
                200,
                scrollable: find
                    .descendant(
                      of: find.byType(LaidOutListView),
                      matching: find.byType(Scrollable),
                    )
                    .last,
              );
              final label = tester.renderObject<RenderParagraph>(
                find.text('Workspace'),
              );
              expect(
                label.getMaxIntrinsicWidth(double.infinity),
                lessThanOrEqualTo(label.size.width + 0.1),
                reason: 'Shortcut group headings remain readable at every text size.',
              );
              unawaited(
                showShortcutsSheet(tester.element(find.byType(SettingsScreen))),
              );
              await tester.pumpAndSettle();
              debugDisableShadows = false;
              await capture('$name-sheet');
              debugDisableShadows = shadows;
              await tester.sendKeyEvent(LogicalKeyboardKey.escape);
              await tester.pumpAndSettle();
              expect(find.byType(Dialog), findsNothing);
            }
            expect(tester.takeException(), isNull);
          },
          variant: TargetPlatformVariant.only(TargetPlatform.macOS),
        );
      }
    }
  }
}