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.3 KB
Raw Blame
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness_mobile/swarm/swarm_code_block.dart';
import 'package:harness_mobile/swarm/swarm_instructions_page.dart';
import 'package:harness_mobile/swarm/swarm_reader.dart';
import 'package:harness_mobile/swarm/swarm_request.dart';

import 'swarm_fixture.dart';

/// The phone's read-only page of a project's instructions and the
/// coordinator's notes (project instructions, phase 5, task 10).
void main() {
  const path = '/api/super-tabs/$tab/projects/$project/instructions';

  Map<String, dynamic> head(int revision, {int notes = 1}) => headData(
    projects: [
      {
        ...(headData()['projects'] as List).first as Map<String, dynamic>,
        'coordinatorName': 'Product Manager',
        'instructionsRevision': revision,
        'notesRevision': notes,
      },
    ],
  );

  Map<String, dynamic> doc(
    int revision,
    String text, {
    List<Map<String, dynamic>> notes = const [],
  }) => {
    'projectId': project,
    'revision': revision,
    'hash': 'h$revision',
    'text': text,
    'template': false,
    'coordinatorName': 'Product Manager',
    'notesRevision': 1,
    'notes': notes,
  };

  Map<String, dynamic> note(
    String id,
    String text,
    String at, {
    String askedBy = me,
    String? expiresAt,
  }) => {
    'id': id,
    'text': text,
    'askedBy': askedBy,
    'at': at,
    'expiresAt': expiresAt,
  };

  Future<({FakeSwarm fake, SwarmReader reader})> open(
    WidgetTester tester, {
    required Object? Function(Map<String, dynamic>? body) route,
    int revision = 3,
    Object? Function(Map<String, dynamic>? body)? commands,
    String role = 'owner',
  }) async {
    final fake = FakeSwarm();
    fake.routes['/api/super-tabs/$tab/parts'] = (_) {
      final load = firstLoad(head: head(revision));
      // The member's username differs from their id.
      load['members'] = [
        part(me, 1, {...person(me, role: role), 'username': 'ken'}),
      ];
      return load;
    };
    fake.routes['/api/super-tabs/$tab/changes'] = (_) => changes(cursor: 'c2');
    fake.routes[path] = route;
    if (commands != null) {
      fake.routes['/api/super-tabs/$tab/commands'] = commands;
    }
    final reader = SwarmReader(
      tabId: tab,
      accountId: me,
      request: fake.request,
    );
    addTearDown(reader.dispose);
    await reader.refresh();
    await tester.pumpWidget(
      MaterialApp(
        home: SwarmInstructionsPage(reader: reader, projectId: project),
      ),
    );
    await tester.pumpAndSettle();
    return (fake: fake, reader: reader);
  }

  Finder key(String value) => find.byKey(ValueKey(value));
  int reads(FakeSwarm fake) => fake.paths.where((p) => p == path).length;

  testWidgets('shows the document and the notes, read only', (tester) async {
    final (:fake, reader: _) = await open(
      tester,
      route: (_) => doc(
        3,
        '## Roles\n### PM\n- Talks with people\n\n```sh\nmake test\n```',
        notes: [
          note('n1', 'Older note', '2026-10-29T01:00:00.000Z'),
          note('n2', 'Newer note', '2026-10-29T02:00:00.000Z'),
          note('n3', 'Third note', '2026-10-29T00:00:00.000Z', askedBy: 'gone'),
        ],
      ),
    );
    expect(reads(fake), 1);
    expect(find.textContaining('revision 3'), findsOneWidget);
    expect(
      find.descendant(
        of: key('swarm-instructions-text'),
        matching: find.textContaining('Talks with people', findRichText: true),
      ),
      findsOneWidget,
    );
    expect(find.byType(SwarmCodeBlock), findsOneWidget);
    expect(find.text('Product Manager’s notes'), findsOneWidget);
    expect(
      tester.getTopLeft(key('swarm-instructions-note-n2')).dy,
      lessThan(tester.getTopLeft(key('swarm-instructions-note-n1')).dy),
    );
    expect(find.textContaining('asked by ken'), findsNWidgets(2));
    expect(find.textContaining('asked by a member'), findsOneWidget);
    expect(find.textContaining(me), findsNothing);
    expect(find.byType(TextField), findsNothing);
    expect(find.byType(EditableText), findsNothing);
    expect(find.text('Save'), findsNothing);
    expect(find.text('Restore'), findsNothing);
  });

  testWidgets('says No notes. with none', (tester) async {
    await open(tester, route: (_) => doc(3, 'Text'));
    expect(find.text('No notes.'), findsOneWidget);
  });

  testWidgets('shows the revision a member saved from Desktop', (tester) async {
    var revision = 3;
    final (:fake, :reader) = await open(
      tester,
      route: (_) => doc(revision, 'Text of revision $revision'),
    );
    expect(find.textContaining('Text of revision 3'), findsOneWidget);
    expect(reads(fake), 1);

    revision = 4;
    fake.routes['/api/super-tabs/$tab/changes'] = (_) => {
      ...changes(cursor: 'c3'),
      'head': part('head', 2, head(4)),
    };
    await reader.refresh();
    await tester.pumpAndSettle();

    expect(find.textContaining('Text of revision 4'), findsOneWidget);
    expect(find.textContaining('revision 4'), findsWidgets);
    expect(reads(fake), 2);
  });

  testWidgets('a backend without instructions says so', (tester) async {
    await open(
      tester,
      route: (_) => throw const SuperTabFailure(
        'BACKEND_UNAVAILABLE',
        'No such route',
        statusCode: 404,
      ),
    );
    expect(
      find.text('This Swarm has no project instructions yet.'),
      findsOneWidget,
    );
    expect(find.text('Try again'), findsNothing);
  });

  testWidgets('a failed read says why and tries again', (tester) async {
    var fail = true;
    final (:fake, reader: _) = await open(
      tester,
      route: (_) {
        if (fail) {
          throw const SuperTabFailure(
            'BACKEND_UNAVAILABLE',
            'The server is busy.',
            statusCode: 503,
          );
        }
        return doc(3, 'Back again');
      },
    );
    expect(find.text('The server is busy.'), findsOneWidget);
    fail = false;
    await tester.tap(find.text('Try again'));
    await tester.pumpAndSettle();
    expect(find.textContaining('Back again'), findsOneWidget);
    expect(reads(fake), 2);
  });

  testWidgets('a note ends at a time today, else a date', (tester) async {
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day, 23, 59);
    final later = DateTime(now.year + 1, 3, 4, 18, 0);
    await open(
      tester,
      route: (_) => doc(
        3,
        'Text',
        notes: [
          note(
            'n1',
            'Today',
            now.toUtc().toIso8601String(),
            expiresAt: today.toUtc().toIso8601String(),
          ),
          note(
            'n2',
            'Later',
            now.toUtc().toIso8601String(),
            expiresAt: later.toUtc().toIso8601String(),
          ),
        ],
      ),
    );
    expect(find.textContaining('until 23:59'), findsOneWidget);
    expect(
      find.textContaining('until 4 Mar ${now.year + 1}, 18:00'),
      findsOneWidget,
    );
  });

  testWidgets('a failed re-read shows its error above the document', (
    tester,
  ) async {
    var fail = false;
    final (:fake, :reader) = await open(
      tester,
      route: (_) {
        if (fail) {
          throw const SuperTabFailure(
            'BACKEND_UNAVAILABLE',
            'The server is busy.',
            statusCode: 503,
          );
        }
        return doc(3, 'Kept text');
      },
    );
    fail = true;
    fake.routes['/api/super-tabs/$tab/changes'] = (_) => {
      ...changes(cursor: 'c3'),
      'head': part('head', 2, head(4)),
    };
    await reader.refresh();
    await tester.pumpAndSettle();
    expect(find.text('The server is busy.'), findsOneWidget);
    expect(find.textContaining('Kept text'), findsOneWidget);
  });

  group('Delete on a note', () {
    final notes = [note('n1', 'Do not use Codex.', '2026-10-01T10:00:00.000Z')];
    final remove = find.byKey(const ValueKey('swarm-instructions-delete-n1'));

    testWidgets('an allowed person confirms, and one delete goes', (
      tester,
    ) async {
      final sent = <Map<String, dynamic>>[];
      var kept = notes;
      await open(
        tester,
        route: (_) => doc(3, 'Rules.', notes: kept),
        commands: (body) {
          sent.add(body!);
          kept = [];
          return {'result': <String, dynamic>{}};
        },
      );
      expect(remove, findsOneWidget);
      await tester.tap(remove);
      await tester.pumpAndSettle();
      expect(find.text('Delete this note?'), findsOneWidget);
      expect(sent, isEmpty, reason: 'nothing goes before they confirm');
      await tester.tap(find.text('Delete').last);
      await tester.pumpAndSettle();
      expect(sent, hasLength(1));
      expect(sent.single['command'], {
        'type': 'note.remove',
        'projectId': project,
        'noteId': 'n1',
      });
      expect(sent.single['operationId'], isA<String>());
      expect(find.text('Do not use Codex.'), findsNothing);
    });

    testWidgets('Cancel sends nothing', (tester) async {
      final sent = <Object?>[];
      await open(
        tester,
        route: (_) => doc(3, 'Rules.', notes: notes),
        commands: (body) {
          sent.add(body);
          return {'result': <String, dynamic>{}};
        },
      );
      await tester.tap(remove);
      await tester.pumpAndSettle();
      await tester.tap(find.text('Cancel'));
      await tester.pumpAndSettle();
      expect(sent, isEmpty);
      expect(find.text('Do not use Codex.'), findsOneWidget);
    });

    testWidgets('a person who may not sees no Delete', (tester) async {
      await open(
        tester,
        route: (_) => doc(3, 'Rules.', notes: notes),
        role: 'viewer',
      );
      expect(find.text('Do not use Codex.'), findsOneWidget);
      expect(remove, findsNothing);
    });

    testWidgets('a refusal says why and keeps the note', (tester) async {
      await open(
        tester,
        route: (_) => doc(3, 'Rules.', notes: notes),
        commands: (_) => throw const SuperTabFailure(
          'NOT_FOUND',
          'This note is unavailable.',
          statusCode: 404,
        ),
      );
      await tester.tap(remove);
      await tester.pumpAndSettle();
      await tester.tap(find.text('Delete').last);
      await tester.pumpAndSettle();
      expect(find.text('This note is unavailable.'), findsOneWidget);
      expect(find.text('Do not use Codex.'), findsOneWidget);
    });
  });
}