using System.Net; using System.Net.Sockets; using System.Text; namespace Nonemm.Network.Tests; public class RadioMirrorTests { [Fact] public async Task A_radio_message_sent_to_the_port_is_raised_and_other_data_is_not() { int port = FreePort(); using RadioMirror mirror = new(port); TaskCompletionSource received = new(); mirror.Received += (_, message) => received.TrySetResult(message); using UdpClient sender = new(); IPEndPoint target = new(IPAddress.Loopback, port); byte[] other = Encoding.UTF8.GetBytes("DL1ABC"); await sender.SendAsync(other, target); byte[] radio = Encoding.UTF8.GetBytes(BroadcastMessages.Radio( new RadioReport(1, 7_012_300, 7_012_300, "CW", "OK1ABC", "OK1ABC", false, 1, false, 1, false, "", true), "RUN-PC")); await sender.SendAsync(radio, target); RadioBroadcast message = await received.Task.WaitAsync(TimeSpan.FromSeconds(5)); Assert.Equal("RUN-PC", message.StationName); Assert.Equal(7_012_300, message.Radio.Frequency); } private static int FreePort() { using UdpClient probe = new(new IPEndPoint(IPAddress.Loopback, 0)); return ((IPEndPoint)probe.Client.LocalEndPoint!).Port; } }