package main

import (
	"testing"
	"time"
)

// The reported failure, in order: MSHV is called on, WSJT-X and JTDX sit idle
// beside it, and every one of their Status packets used to empty the entry
// field that MSHV had just filled — once a second, with the map zooming in and
// out to match.
func TestUdpFocusKeepsTheFieldWithTheCallingProgram(t *testing.T) {
	var f udpFocus

	if !f.claim("MSHV") {
		t.Fatal("the first program to announce a station must take the field")
	}
	// The other two, announcing stations of their own, are refused.
	if f.claim("WSJT-X") {
		t.Error("WSJT-X took the field while MSHV was calling")
	}
	if f.claim("JTDX") {
		t.Error("JTDX took the field while MSHV was calling")
	}
	// And their clears do not empty it — this is the half that caused the flicker.
	if f.holds("WSJT-X") {
		t.Error("an idle WSJT-X was allowed to clear MSHV's callsign")
	}
	if !f.holds("MSHV") {
		t.Error("MSHV lost the right to clear its own callsign")
	}
	// MSHV moving to the next station keeps the field.
	if !f.claim("MSHV") {
		t.Error("the focused program must keep the field across stations")
	}
}

// Letting go, three ways.
func TestUdpFocusRelease(t *testing.T) {
	var f udpFocus

	// The focused program clears its own call.
	f.claim("MSHV")
	f.release("DX Call cleared")
	if !f.claim("WSJT-X") {
		t.Error("after a release the next program should be able to take the field")
	}

	// A QSO is logged.
	f.release("QSO logged")
	if !f.claim("JTDX") {
		t.Error("logging a QSO must free the field for whichever program hears the next station")
	}

	// The focused program is closed and stops sending. Its hold lapses rather
	// than locking the entry field for the rest of the evening.
	f.mu.Lock()
	f.at = time.Now().Add(-udpFocusIdle - time.Second)
	f.mu.Unlock()
	if !f.claim("MSHV") {
		t.Error("a silent program must not hold the field for ever")
	}
}

// A sender with no program id — an ADIF relay, a remote "set call" — is not
// something to arbitrate between, and must never be locked out.
func TestUdpFocusIgnoresUnnamedSenders(t *testing.T) {
	var f udpFocus
	f.claim("MSHV")
	if !f.claim("") {
		t.Error("an unnamed sender was refused the entry field")
	}
	if !f.holds("") {
		t.Error("an unnamed sender was refused a clear")
	}
}
