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.

16.6 KB
Raw Blame
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:harness_mobile/notify/system_notices.dart';
import 'package:harness_mobile/settings/experimental_features.dart';
import 'package:harness_mobile/shared/theme/app_theme.dart' as grid;
import 'package:harness_mobile/shared/theme/color_palette.dart';
import 'package:harness_mobile/swarm/swarm_conversation.dart'
    show swarmMentionedAgentLeft;
import 'package:harness_mobile/swarm/swarm_conversation_page.dart';
import 'package:harness_mobile/swarm/swarm_file_picker.dart';
import 'package:harness_mobile/swarm/swarm_session.dart';
import 'package:harness_mobile/swarm/swarm_outgoing_files.dart';
import 'package:image_picker/image_picker.dart' show XFile;

import '../swarm/swarm_fixture.dart';
import '../voice_fakes.dart';

/// Renders a phone Swarm chat's threads as Tony's mockup of 10-29 draws them —
/// the chat with a root's "N replies" line, a reply also sent and Reply in
/// thread, then the thread itself, and its reply box with files attached and
/// going up — in light and dark, at iPhone 14 size in the system fonts, to
/// PNGs a reviewer can look at without a phone. Scripted data only.
///
/// Skipped unless PHONE_RENDER_DIR names a directory to write into:
///
///     PHONE_RENDER_DIR=/tmp/renders flutter test test/render/swarm_thread_render_test.dart
///
/// macOS only: it loads SF Pro and SF Mono from /System/Library/Fonts.
final _outDir = Platform.environment['PHONE_RENDER_DIR'];

Future<void> _loadFonts() async {
  Future<void> load(String family, String file) async {
    final bytes = await File('/System/Library/Fonts/$file').readAsBytes();
    await (FontLoader(
      family,
    )..addFont(Future.value(ByteData.view(bytes.buffer)))).load();
  }

  for (final family in ['.AppleSystemUIFont', 'SF Pro Text', 'Roboto']) {
    await load(family, 'SFNS.ttf');
  }
  for (final family in ['.AppleSystemUIFontMonospaced', 'SF Mono']) {
    await load(family, 'SFNSMono.ttf');
  }
  // The icons, from the package in the pub cache.
  final lucide =
      '${Platform.environment['HOME']}/.pub-cache/hosted/pub.dev/lucide_icons_flutter-3.1.19/assets';
  Future<void> icons(String family, String file) async {
    final bytes = await File('$lucide/$file').readAsBytes();
    await (FontLoader(
      family,
    )..addFont(Future.value(ByteData.view(bytes.buffer)))).load();
  }

  await icons('packages/lucide_icons_flutter/Lucide', 'lucide.ttf');
  for (final weight in [100, 200, 300, 400, 500, 600]) {
    await icons(
      'packages/lucide_icons_flutter/Lucide$weight',
      'build_font/LucideVariable-w$weight.ttf',
    );
  }
}

/// A made-up photo: sky, ground and a sun.
Future<Uint8List> _photo(Color sky, Color ground) async {
  final recorder = ui.PictureRecorder();
  final canvas = Canvas(recorder);
  canvas.drawRect(const Rect.fromLTWH(0, 0, 120, 80), Paint()..color = sky);
  canvas.drawRect(const Rect.fromLTWH(0, 50, 120, 30), Paint()..color = ground);
  canvas.drawCircle(const Offset(90, 22), 10, Paint()..color = Colors.amber);
  final image = await recorder.endRecording().toImage(120, 80);
  final png = await image.toByteData(format: ui.ImageByteFormat.png);
  return png!.buffer.asUint8List();
}

void main() {
  final skip = _outDir == null;
  const messages = '/api/super-tabs/$tab/conversations/g1/messages';

  final original = (
    photos: pickSwarmPhotos,
    documents: pickSwarmFiles,
    copies: swarmPickerCopyFolders,
  );
  setUp(() => swarmPickerCopyFolders = () async => const []);
  tearDown(() {
    swarmPickerCopyFolders = original.copies;
    pickSwarmPhotos = original.photos;
    pickSwarmFiles = original.documents;
  });

  /// An upload 60% of the way up, that never ends.
  Future<Map<String, dynamic>> stuck({
    required String tabId,
    required String conversationId,
    required String name,
    required String mime,
    required SwarmOutgoingFile file,
    required dynamic cancel,
    required void Function(double sent) onProgress,
  }) {
    onProgress(0.6);
    return Completer<Map<String, dynamic>>().future;
  }

  FakeSwarm backend() {
    final now = DateTime.now().toUtc();
    Map<String, dynamic> message(
      String id,
      int sequence,
      String text, {
      required String author,
      required int minutesAgo,
      String? thread,
      bool alsoInChat = false,
    }) => {
      'id': id,
      'conversationId': 'g1',
      'text': text,
      'sequence': sequence,
      'thread': thread ?? id,
      'replyTo': ?thread,
      if (alsoInChat) 'alsoInChat': true,
      'author': author == me
          ? {'kind': 'person', 'id': me, 'name': 'tony'}
          : {
              'kind': 'agent',
              'id': author,
              'name': author == 'agent-lead' ? 'River' : 'Nova',
            },
      'attachments': <Object>[],
      'reactions': <Object>[],
      'createdAt': now
          .subtract(Duration(minutes: minutesAgo))
          .toIso8601String(),
    };

    final page = [
      message(
        'm1',
        1,
        'Can you check the drawer mockup?',
        author: 'agent-lead',
        minutesAgo: 20,
      ),
      message(
        'm2',
        2,
        'Looks good. One question on the bold names.',
        author: 'agent-builder',
        minutesAgo: 18,
        thread: 'm1',
      ),
      message(
        'm3',
        3,
        'Ship it as drawn.',
        author: me,
        minutesAgo: 17,
        thread: 'm1',
      ),
      message(
        'm4',
        4,
        'Pushed the fix.',
        author: 'agent-builder',
        minutesAgo: 5,
        thread: 'm1',
        alsoInChat: true,
      ),
      message(
        'm5',
        5,
        'Next: the leaderboard.',
        author: 'agent-builder',
        minutesAgo: 4,
      ),
    ];
    final line = {
      'id': 'm1',
      'replies': 3,
      'lastSequence': 4,
      'lastAt': now.subtract(const Duration(minutes: 5)).toIso8601String(),
      'authors': [
        {'kind': 'agent', 'id': 'agent-builder', 'name': 'Nova'},
        {'kind': 'person', 'id': me, 'name': '[email protected]'},
      ],
      'people': [me],
    };

    final fake = FakeSwarm();
    fake.routes['/api/super-tabs/list'] = (_) => {
      'tabs': [
        {'id': tab, 'name': 'Phone Swarm', 'ownerId': me},
      ],
      'next': null,
    };
    fake.routes['/api/super-tabs/$tab/parts'] = (_) => {
      ...firstLoad(
        groups: [
          groupData(
            'g1',
            title: 'Mobile: bold names and @N',
            agentIds: ['agent-lead', 'agent-builder'],
            status: 'working',
            lastMessageSequence: 5,
          ),
        ],
      ),
      'members': [part(me, 1, person(me))],
    };
    fake.routes['/api/super-tabs/$tab/changes'] = (_) => changes(cursor: 'c2');
    fake.routes['/api/super-tabs/$tab/decisions?state=open'] = (_) => {
      'decisions': <Object>[],
    };
    fake.routes['/api/super-tabs/$tab/mentions?state=waiting'] = (_) => {
      'mentions': <Object>[],
    };
    fake.routes[messages] = (_) => {
      'messages': page.reversed.toList(),
      'next': null,
      'threads': [line],
    };
    fake.routes['$messages?thread=m1'] = (_) => {
      'messages': [
        for (final m in page.reversed)
          if (m['thread'] == 'm1') m,
      ],
      'next': null,
      'threads': [line],
    };
    return fake;
  }

  Future<void> shoot(WidgetTester tester, GlobalKey key, String name) async {
    await tester.runAsync(() async {
      final boundary =
          key.currentContext!.findRenderObject()! as RenderRepaintBoundary;
      final image = await boundary.toImage(pixelRatio: 3);
      final png = await image.toByteData(format: ui.ImageByteFormat.png);
      final file = File('$_outDir/$name.png');
      await file.parent.create(recursive: true);
      await file.writeAsBytes(png!.buffer.asUint8List());
    });
  }

  /// The Swarm and the app of the last [chat] pumped, to change under it.
  late FakeSwarm swarm;
  late SwarmHost app;

  for (final brightness in Brightness.values) {
    final look = brightness.name;

    Future<GlobalKey> chat(WidgetTester tester) async {
      await tester.runAsync(_loadFonts);
      final photos = (await tester.runAsync(
        () async => [
          await _photo(const Color(0xFF7FB2E5), const Color(0xFF5E8C4A)),
          await _photo(const Color(0xFFE9A86B), const Color(0xFF3F5B73)),
        ],
      ))!;
      pickSwarmPhotos = (limit) async => [
        XFile.fromData(photos[0], path: '/copies/IMG_2041.jpg'),
        XFile.fromData(photos[1], path: '/copies/IMG_2050.jpg'),
      ];
      pickSwarmFiles = () async => [
        XFile.fromData(
          Uint8List(640 * 1024),
          path: '/copies/report.pdf',
          length: 640 * 1024,
        ),
      ];
      // A palette carries its brightness: a light one is how the app goes light.
      final palette = HarnessPalette.values.firstWhere(
        (p) => p.brightness == brightness,
      );
      final previous = (
        grid.AppTheme.brightness.value,
        grid.AppTheme.palette.value,
      );
      grid.AppTheme.palette.value = palette;
      grid.AppTheme.brightness.value = brightness;
      addTearDown(() {
        grid.AppTheme.brightness.value = previous.$1;
        grid.AppTheme.palette.value = previous.$2;
      });
      tester.view.physicalSize = const Size(1170, 2532);
      tester.view.devicePixelRatio = 3;
      tester.view.padding = const FakeViewPadding(top: 47 * 3, bottom: 34 * 3);
      tester.view.viewPadding = const FakeViewPadding(
        top: 47 * 3,
        bottom: 34 * 3,
      );
      addTearDown(tester.view.reset);

      final features = ExperimentalFeaturesStore(storage: MemoryKeyValueStore())
        ..value = {PhoneExperiment.swarm};
      final account = ValueNotifier<String?>(me);
      final host = SwarmHost(
        accountId: () => account.value,
        account: account,
        request: (swarm = backend()).request,
        upload: stuck,
        notices: SilentSystemNotices(),
        features: features,
        // The app's mic, in the chat's box and the thread's.
        voice: () => FakeVoiceRecorder(),
      )..foreground = false;
      app = host;
      addTearDown(host.dispose);
      final key = GlobalKey();
      await tester.pumpWidget(
        RepaintBoundary(
          key: key,
          child: grid.BrightnessScope(
            child: MaterialApp(
              debugShowCheckedModeBanner: false,
              theme: grid.buildAppTheme(brightness: brightness),
              builder: (context, child) =>
                  SwarmScope(notifier: host, child: child!),
              home: const SwarmConversationPage(
                swarmId: tab,
                conversationId: 'g1',
              ),
            ),
          ),
        ),
      );
      await tester.pumpAndSettle();
      return key;
    }

    testWidgets(
      'the chat: a replies line, a reply also sent, Reply in '
      'thread ($look)',
      skip: skip,
      (tester) async {
        final key = await chat(tester);
        await tester.tap(find.text('Next: the leaderboard.'));
        await tester.pumpAndSettle();
        await shoot(tester, key, 'swarm-thread-chat-$look');
      },
    );

    testWidgets('the thread ($look)', skip: skip, (tester) async {
      final key = await chat(tester);
      await tester.tap(find.byKey(const ValueKey('swarm-thread-line-m1')));
      await tester.pumpAndSettle();
      await shoot(tester, key, 'swarm-thread-open-$look');
    });

    testWidgets(
      'the thread, Reply options open, Also send ticked ($look)',
      skip: skip,
      (tester) async {
        final key = await chat(tester);
        await tester.tap(find.byKey(const ValueKey('swarm-thread-line-m1')));
        await tester.pumpAndSettle();
        await tester.tap(find.byKey(const ValueKey('swarm-thread-options')));
        await tester.pumpAndSettle();
        await tester.tap(find.byKey(const ValueKey('swarm-thread-also-send')));
        await tester.pumpAndSettle();
        await shoot(tester, key, 'swarm-thread-options-open-$look');
        // Folded again: the line says what is on.
        await tester.tap(find.byKey(const ValueKey('swarm-thread-options')));
        await tester.pumpAndSettle();
        await shoot(tester, key, 'swarm-thread-options-folded-on-$look');
      },
    );

    testWidgets('the thread, its @ picker open ($look)', skip: skip, (
      tester,
    ) async {
      final key = await chat(tester);
      await tester.tap(find.byKey(const ValueKey('swarm-thread-line-m1')));
      await tester.pumpAndSettle();
      await tester.tap(find.byKey(const ValueKey('swarm-tag')));
      await tester.pumpAndSettle();
      await shoot(tester, key, 'swarm-thread-tag-$look');
      // The draft's save, a moment after it changed.
      await tester.pump(const Duration(seconds: 1));
    });

    testWidgets(
      'the thread, a picked agent who left the chat refused '
      '($look)',
      skip: skip,
      (tester) async {
        final key = await chat(tester);
        await tester.tap(find.byKey(const ValueKey('swarm-thread-line-m1')));
        await tester.pumpAndSettle();
        await tester.tap(find.byKey(const ValueKey('swarm-tag')));
        await tester.pumpAndSettle();
        await tester.tap(
          find.byKey(const ValueKey('swarm-pick-agent-builder')),
        );
        await tester.pumpAndSettle();
        // Nova leaves the chat, and the Swarm is read again.
        final parts = swarm.routes['/api/super-tabs/$tab/parts']!;
        swarm.routes['/api/super-tabs/$tab/parts'] = (body) {
          final read = parts(body) as Map<String, dynamic>;
          return {
            ...read,
            'groups': [
              for (final g in (read['groups'] as List).cast<Map>())
                g['key'] == 'g1'
                    ? part('g1', (g['version'] as int) + 1, {
                        ...(g['data'] as Map<String, dynamic>),
                        'agentIds': ['agent-lead'],
                      })
                    : g,
            ],
          };
        };
        swarm.routes['/api/super-tabs/$tab/changes'] = (_) => {'reload': true};
        await tester.runAsync(() => app.session!.inbox.reader(tab)!.refresh());
        await tester.pumpAndSettle();
        await tester.tap(find.byKey(const ValueKey('swarm-thread-send')));
        await tester.pumpAndSettle();
        expect(find.text(swarmMentionedAgentLeft), findsOneWidget);
        FocusManager.instance.primaryFocus?.unfocus();
        await tester.pumpAndSettle();
        await shoot(tester, key, 'swarm-thread-agent-left-$look');
        // The draft's save, a moment after it changed.
        await tester.pump(const Duration(seconds: 1));
      },
    );

    testWidgets('the thread, its mic listening ($look)', skip: skip, (
      tester,
    ) async {
      final key = await chat(tester);
      await tester.tap(find.byKey(const ValueKey('swarm-thread-line-m1')));
      await tester.pumpAndSettle();
      await tester.tap(find.byKey(const ValueKey('swarm-mic')));
      for (var i = 0; i < 5; i++) {
        await tester.pump();
      }
      await shoot(tester, key, 'swarm-thread-mic-listening-$look');
    });

    Future<void> pick(WidgetTester tester, String row) async {
      await tester.tap(find.byKey(const ValueKey('swarm-thread-attach')));
      await tester.pumpAndSettle();
      await tester.tap(find.byKey(ValueKey(row)));
      await tester.pumpAndSettle();
      // Thumbnails decode on the real clock, not the test's.
      await tester.runAsync(
        () => Future<void>.delayed(const Duration(milliseconds: 300)),
      );
      await tester.pumpAndSettle();
    }

    testWidgets('the thread, files in the reply box ($look)', skip: skip, (
      tester,
    ) async {
      final key = await chat(tester);
      await tester.tap(find.byKey(const ValueKey('swarm-thread-line-m1')));
      await tester.pumpAndSettle();
      await pick(tester, 'swarm-attach-library');
      await pick(tester, 'swarm-attach-files');
      await tester.enterText(
        find.byKey(const ValueKey('swarm-thread-field')),
        'Here are the shots',
      );
      await tester.pumpAndSettle();
      await shoot(tester, key, 'swarm-thread-files-$look');
    });

    testWidgets('the thread, files going up ($look)', skip: skip, (
      tester,
    ) async {
      final key = await chat(tester);
      await tester.tap(find.byKey(const ValueKey('swarm-thread-line-m1')));
      await tester.pumpAndSettle();
      await pick(tester, 'swarm-attach-library');
      await tester.tap(find.byKey(const ValueKey('swarm-thread-send')));
      await tester.pump();
      await tester.pump();
      await shoot(tester, key, 'swarm-thread-uploading-$look');
    });
  }
}