import 'package:flutter_test/flutter_test.dart';

import 'package:chanora_flutter/services/audio_lifecycle_service.dart';
import 'package:chanora_flutter/src/rust/api.dart' as rust;

void main() {
  test('parseBridgeAudioRoute maps iOS-classified route strings', () {
    expect(parseBridgeAudioRoute('Earpiece'), rust.BridgeAudioRoute.earpiece);
    expect(parseBridgeAudioRoute('Speaker'), rust.BridgeAudioRoute.speaker);
    expect(
      parseBridgeAudioRoute('WiredHeadset'),
      rust.BridgeAudioRoute.wiredHeadset,
    );
    expect(
      parseBridgeAudioRoute('BluetoothHfp'),
      rust.BridgeAudioRoute.bluetoothHfp,
    );
    expect(
      parseBridgeAudioRoute('BluetoothA2dp'),
      rust.BridgeAudioRoute.bluetoothA2Dp,
    );
    expect(parseBridgeAudioRoute('Unknown'), rust.BridgeAudioRoute.unknown);
    expect(parseBridgeAudioRoute('Other'), rust.BridgeAudioRoute.unknown);
  });

  test('parseBridgeAudioRoute maps Android UsbHeadset to wiredHeadset', () {
    // AndroidAudioLifecycleController.classifyDevice emits 'UsbHeadset' for
    // AudioDeviceInfo.TYPE_USB_HEADSET. USB audio is functionally a
    // wired-class device — the Kotlin classifier's own preference ordering
    // (line 176 of AndroidAudioLifecycleController.kt) groups it with
    // WiredHeadset/BluetoothHfp/BluetoothA2dp.
    expect(parseBridgeAudioRoute('UsbHeadset'),
        rust.BridgeAudioRoute.wiredHeadset);
  });

  test('parseBridgeAudioRoute maps Android Hdmi to unknown', () {
    // HDMI is a display-out transport, not a voice-call audio path; no
    // existing BridgeAudioRoute variant fits. Treat as unknown rather
    // than misclassify as Speaker.
    expect(parseBridgeAudioRoute('Hdmi'), rust.BridgeAudioRoute.unknown);
  });

  test('wireMacosAudioLifecycle is a no-op on non-macOS and registers on macOS', () {
    // MethodChannel needs a binary messenger, which requires the test
    // binding to be initialised first.
    TestWidgetsFlutterBinding.ensureInitialized();
    // Channel name constant matches the Swift side (MacOSAudioLifecycle.swift).
    expect(macosAudioLifecycleChannelName, 'chanora/macos_audio_lifecycle');
    // The wire is a no-op on the test platform (CI defaults to host OS
    // which may be macOS or Linux). On Linux it returns early; on macOS
    // it installs a handler. Either way, it must not throw.
    expect(() => wireMacosAudioLifecycle(), returnsNormally);
  });
}

