package udp

import "testing"

func TestWSJTSender(t *testing.T) {
	cases := []struct {
		msg      string
		wantCall string
		wantCQ   bool
		wantGrid string
	}{
		{"CQ K1ABC FN42", "K1ABC", true, "FN42"},
		{"CQ DX W2XYZ EM12", "W2XYZ", true, "EM12"},    // modifier "DX" skipped
		{"CQ NA VE3ABC FN03", "VE3ABC", true, "FN03"},  // region modifier skipped
		{"CQ 020 JA1XYZ PM95", "JA1XYZ", true, "PM95"}, // zone modifier skipped
		{"W2XYZ K1ABC -10", "K1ABC", false, ""},        // exchange → sender is 2nd call
		{"W2XYZ K1ABC R-10", "K1ABC", false, ""},
		{"W2XYZ K1ABC RR73", "K1ABC", false, ""},
		{"F4BPO K1ABC/P 73", "K1ABC/P", false, ""},     // portable call kept
		{"CQ F/DL1ABC JO31", "F/DL1ABC", true, "JO31"}, // compound prefix, grid still valid
		{"CQ K1ABC", "K1ABC", true, ""},                // CQ without a grid
		{"CQ K1ABC RR73", "K1ABC", true, ""},           // RR73 is a sign-off, NOT grid RR73
		{"CQ K1ABC FN4", "K1ABC", true, ""},            // 3 chars is not a field+square
		{"CQ K1ABC FN42AB", "K1ABC", true, ""},         // 6-char: WSJT-X never sends it here
		{"CQ K1ABC 73", "K1ABC", true, ""},             // bare sign-off
		{"CQ K1ABC SS42", "K1ABC", true, ""},           // S is past R — no such field
		// An unknown word after CQ made the parser skip a slot; a grid passes every
		// shape test a callsign does, so it came back AS the callsign.
		{"CQ FOO JN36", "", true, ""},
		// Non-callsign / free text → no sender.
		{"TNX 73 GL", "", false, ""},
		{"K1ABC RR73", "", false, ""}, // only one call + a token → 2nd token not a call
		{"", "", false, ""},
		{"CQ CQ CQ", "", true, ""}, // CQ but no resolvable call
	}
	for _, c := range cases {
		call, cq, grid := wsjtSender(c.msg)
		if call != c.wantCall || cq != c.wantCQ || grid != c.wantGrid {
			t.Errorf("wsjtSender(%q) = (%q,%v,%q), want (%q,%v,%q)",
				c.msg, call, cq, grid, c.wantCall, c.wantCQ, c.wantGrid)
		}
	}
}
