package main

import (
	"testing"

	"hamlog/internal/antgenius"
)

// The switch has two ports and the radio two antenna jacks; a QSO was made
// through exactly one of them. Naming the wrong port's antenna would be worse
// than naming the band default — it is a wrong answer that looks authoritative.
func TestAntennaNameComesFromTheDeviceList(t *testing.T) {
	st := antgenius.Status{
		Connected: true,
		PortA:     2,
		PortB:     5,
		Antennas: []antgenius.Antenna{
			{Index: 2, Name: "OB11-5 20/15/10"},
			{Index: 5, Name: "Vertical 80m"},
		},
	}
	if got := antGeniusNameOf(st, 2); got != "OB11-5 20/15/10" {
		t.Fatalf("port A antenna = %q", got)
	}
	if got := antGeniusNameOf(st, 5); got != "Vertical 80m" {
		t.Fatalf("port B antenna = %q", got)
	}
	// An index the device never described has no name to give, and inventing
	// one ("Antenna 7") would put a label in the log that exists nowhere else.
	if got := antGeniusNameOf(st, 7); got != "" {
		t.Fatalf("unknown index produced %q", got)
	}
}

// The jack-to-port wiring is the station's own. Defaulting ANT1 to port A is a
// convention, not a fact, so it is a setting — and the mapping has to hold both
// ways round.
func TestJackToPortMapping(t *testing.T) {
	app := &App{}
	// No settings store: settingOr falls back, which is ANT1 → port A.
	if got := app.antGeniusPortFor("ANT1"); got != 1 {
		t.Fatalf("ANT1 mapped to port %d, want A(1)", got)
	}
	if got := app.antGeniusPortFor("ANT2"); got != 2 {
		t.Fatalf("ANT2 mapped to port %d, want B(2)", got)
	}
	// A jack that is neither — a transverter port, or a rig that reports
	// nothing — has no port to name, and saying so beats guessing.
	if got := app.antGeniusPortFor("XVTR"); got != 0 {
		t.Fatalf("XVTR mapped to port %d, want none", got)
	}
	if got := app.antGeniusPortFor(""); got != 0 {
		t.Fatalf("an unreported jack mapped to port %d, want none", got)
	}
}
