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.

2.9 KB
Raw Blame
import 'package:flutter_test/flutter_test.dart';
import 'package:harness_mobile/swarm/model.dart';
import 'package:harness_mobile/swarm/swarm_outgoing_files.dart';
import 'package:harness_mobile/swarm/voice_length.dart';

/// A chat voice message as the backend sends it (task #2584): one WAV file
/// whose `voice` carries its length, its waveform and whether it has words.
void main() {
  Map<String, dynamic> file({Object? voice}) => {
    'id': 'clip',
    'name': 'Voice message.wav',
    'mime': 'audio/wav',
    'size': 2592044,
    'url': 'https://cdn.example.test/super-tabs/tab/abc/Voice_message.wav',
    'voice': ?voice,
  };

  test('reads a voice message\'s length, waveform and words flag', () {
    final clip = SuperTabAttachment.fromJson(
      file(
        voice: {
          'durationMs': 81000,
          'waveform': [0, 12, 31, 40, -2],
          'transcribed': false,
          'failure': 'limit',
        },
      ),
    );
    expect(clip.voice!.length, const Duration(seconds: 81));
    // Out of range values are drawn at the nearest height a bar has.
    expect(clip.voice!.waveform, [0, 12, 31, 31, 0]);
    expect(clip.voice!.transcribed, isFalse);
    expect(clip.voice!.overLimit, isTrue);
  });

  test('reads a malformed voice as no voice, and keeps the file', () {
    final odd = SuperTabAttachment.fromJson(
      file(voice: {'durationMs': 'long'}),
    );
    expect(odd.voice, isNull);
    expect(odd.id, 'clip');
    expect(SuperTabAttachment.fromJson(file()).voice, isNull);
  });

  test('writes a length M:SS, rounded down, and labels a voice message', () {
    expect(swarmVoiceLength(const Duration(milliseconds: 81999)), '1:21');
    expect(swarmVoiceLength(const Duration(minutes: 10)), '10:00');
    expect(swarmVoiceLength(const Duration(milliseconds: 999)), '0:00');
    expect(swarmVoiceLength(const Duration(seconds: 9)), '0:09');
    expect(
      swarmVoiceLabel(const Duration(seconds: 81)),
      'Voice message (1:21)',
    );
  });

  test('a quote of a voice message says its length; any other, its text', () {
    Map<String, dynamic> row(String text, {bool voice = false}) => {
      'id': 'm1',
      'conversationId': 'g1',
      'text': text,
      'sequence': 1,
      'author': {'kind': 'person', 'id': 'dee', 'name': 'dee'},
      'attachments': [
        if (voice)
          file(
            voice: {
              'durationMs': 81000,
              'waveform': [3],
              'transcribed': true,
            },
          ),
      ],
      'reactions': <Object>[],
      'createdAt': DateTime.utc(2026, 11, 4).toIso8601String(),
    };
    expect(
      swarmQuoteText(SuperTabMessage.fromJson(row('hello team', voice: true))),
      'Voice message (1:21)',
    );
    expect(swarmQuoteText(SuperTabMessage.fromJson(row('Typed'))), 'Typed');
  });

  test('sends a .wav as audio/wav', () {
    expect(swarmMimeForName('Voice message.wav'), 'audio/wav');
  });
}