Files
buzz/mobile/test/shared/huddle/huddle_wire_test.dart
klopez4212 8c0f42e187 Add mobile Huddles voice MVP (#6056)
## Summary
- add foreground mobile Huddles on Android and iOS with native Opus
capture/playback, mute, speaker routing, participants, lifecycle, and
minimized drawer UI
- keep mobile Huddle cards and roster state live, including ended rooms,
relay-resolved profiles, and agents
- broadcast desktop agent TTS through the existing Huddle audio protocol

## Scope
Foreground human-to-human voice MVP only. Agent setup/transcripts,
background calling, recording, and advanced device controls remain out
of scope.

## Validation
- `just mobile-check`
- `just mobile-test` — 1,500 passed
- `just desktop-check` and `just desktop-test` — 4,957 passed
- desktop typecheck, strict Clippy, and Tauri tests — 2,445 passed, 15
ignored
- mobile worktree identity contract checks
- physical Pixel/iPhone behavior reviewed during development

---------

Signed-off-by: kenny lopez <klopez4212@gmail.com>
Signed-off-by: Kenny Lopez <klopez4212@gmail.com>
Signed-off-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Carl <3c4caeafb646d23867f1c4832e68211d77e2561946171625f75c3ce1a3f2670f@buzz.block.builderlab.xyz>
Co-authored-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
Co-authored-by: leader <71e9f2c44a6932b6772caaaccda1911d010463c3e2c6c40410b8329956046801@buzz.block.builderlab.xyz>
Co-authored-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Mongo <9cfd347903944d5b85aa6c93d2ab67381b978a92a31914bca69998968752a1d7@buzz.block.builderlab.xyz>
2026-08-22 05:28:16 -07:00

148 lines
4.1 KiB
Dart

import 'dart:typed_data';
import 'package:buzz/shared/huddle/huddle_auth.dart';
import 'package:buzz/shared/huddle/huddle_wire.dart';
import 'package:flutter_test/flutter_test.dart';
const _privateKey =
'09b3065e3570a3a4054660dccd66e12774a99a904fdb0ca02dbc6c3136249506';
const _parentChannelId = '11111111-2222-4333-8444-555555555555';
const _ephemeralChannelId = 'aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee';
void main() {
group('HuddleWireV3', () {
test('encodes the desktop-compatible big-endian v3 golden frame', () {
const header = HuddleAudioHeader(
sequence: 0x1234,
timestamp48k: 0x01020304,
levelDbov: -42,
flags: HuddleWireV3.dtxFlag,
);
expect(
HuddleWireV3.encodeClientFrame(
header,
Uint8List.fromList([0xaa, 0xbb]),
),
[0x12, 0x34, 0x01, 0x02, 0x03, 0x04, 0xd6, 0x01, 0xaa, 0xbb],
);
});
test('decodes peer prefix, v2 header, and Opus payload', () {
final frame = HuddleWireV3.decodeRelayFrame(
Uint8List.fromList([
0x07,
0x00,
0x12,
0x34,
0x01,
0x02,
0x03,
0x04,
0xd6,
0x01,
0xaa,
0xbb,
]),
);
expect(frame.peerIndex, 7);
expect(frame.epoch, 0);
expect(frame.header.sequence, 0x1234);
expect(frame.header.timestamp48k, 0x01020304);
expect(frame.header.levelDbov, -42);
expect(frame.header.isDtx, isTrue);
expect(frame.opusPayload, [0xaa, 0xbb]);
});
test('coerces invalid positive dBov telemetry to the silent floor', () {
final header = HuddleAudioHeader.decode(
Uint8List.fromList([0, 1, 0, 0, 3, 0xc0, 1, 0]),
);
expect(header.levelDbov, -127);
});
test('checked headers enforce wire ranges outside debug assertions', () {
expect(
() => HuddleAudioHeader.checked(
sequence: -1,
timestamp48k: 0,
levelDbov: -127,
flags: 0,
),
throwsRangeError,
);
});
test('rejects empty and oversized payloads', () {
const header = HuddleAudioHeader(
sequence: 0,
timestamp48k: 0,
levelDbov: -127,
flags: 0,
);
expect(
() => HuddleWireV3.encodeClientFrame(header, Uint8List(0)),
throwsA(isA<HuddleWireException>()),
);
expect(
() => HuddleWireV3.encodeClientFrame(
header,
Uint8List(HuddleWireV3.maxBinaryFrameLength),
),
throwsA(isA<HuddleWireException>()),
);
});
});
group('HuddleAuthV3', () {
test('uses the base relay URL and fixed v3 auth envelope', () {
final parameters = HuddleConnectionParameters(
relayWebSocketUrl: 'wss://buzz.example',
nsec: _privateKey,
parentChannelId: _parentChannelId,
ephemeralChannelId: _ephemeralChannelId,
);
final auth = HuddleAuthV3.buildMessage(
parameters: parameters,
challenge: 'relay-challenge',
createdAt: 1_700_000_000,
);
final event = auth['event']! as Map<String, dynamic>;
final tags = (event['tags']! as List<dynamic>)
.map((tag) => (tag as List<dynamic>).cast<String>())
.toList();
expect(auth['type'], 'auth');
expect(auth['protocol_version'], 3);
expect(auth['parent_channel_id'], _parentChannelId);
expect(event['kind'], 22242);
expect(tags[0], ['relay', 'wss://buzz.example']);
expect(tags[1], ['challenge', 'relay-challenge']);
expect(
parameters.audioWebSocketUri.toString(),
'wss://buzz.example/huddle/$_ephemeralChannelId/audio',
);
expect(
tags.any((tag) => tag.length > 1 && tag[1].contains('/huddle/')),
isFalse,
);
});
test('rejects malformed channel IDs before opening a socket', () {
expect(
() => HuddleConnectionParameters(
relayWebSocketUrl: 'wss://buzz.example',
nsec: _privateKey,
parentChannelId: 'not-a-channel',
ephemeralChannelId: _ephemeralChannelId,
),
throwsArgumentError,
);
});
});
}