package udp

import (
	"strings"
	"testing"
)

// The diagnostic that matters: a text payload arriving on a WSJT port must be
// READABLE in the log. "bad magic 0x3132372e" alone told the operator nothing —
// those four bytes are ASCII "127.", i.e. some program broadcasting an address
// where WSJT-X binary was expected.
func TestDescribePacketShowsTextAndHex(t *testing.T) {
	got := describePacket([]byte("127.0.0.1:4532"))
	for _, want := range []string{`14 bytes`, `"127.0.0.1:4532"`, `31 32 37 2e`} {
		if !strings.Contains(got, want) {
			t.Errorf("describePacket missing %q\ngot: %s", want, got)
		}
	}
}

// Binary stays inspectable: unprintable bytes become dots in the preview and
// the hex carries the real values.
func TestDescribePacketHandlesBinary(t *testing.T) {
	got := describePacket([]byte{0xad, 0xbc, 0xcb, 0xda, 0x00})
	if !strings.Contains(got, `"....."`) || !strings.Contains(got, "ad bc cb da 00") {
		t.Errorf("binary preview wrong: %s", got)
	}
}

// A long datagram is truncated, and says so, rather than dumping a whole packet
// into the log on every line.
func TestDescribePacketTruncates(t *testing.T) {
	got := describePacket([]byte(strings.Repeat("A", 300)))
	if !strings.Contains(got, "300 bytes") {
		t.Errorf("lost the real length: %s", got)
	}
	if !strings.Contains(got, "…") {
		t.Errorf("truncation not marked: %s", got)
	}
	if strings.Count(got, "41 ") > 96 {
		t.Errorf("hex not capped: %s", got)
	}
}
