package webpub

import (
	"testing"

	"hamlog/internal/qso"
)

// The Distance column was published empty for every QSO logged in OpsLog: the
// stored DISTANCE field is only ever filled by an ADIF import that carried one,
// and nothing computes it when logging. It falls back to the two locators.
func TestDistanceFallsBackToTheGrids(t *testing.T) {
	// JN36 (French Alps) to IO91 (southern England): a few hundred kilometres.
	got := distanceKm(&qso.QSO{MyGrid: "JN36DG", Grid: "IO91"})
	if got == "" {
		t.Fatal("no distance from two perfectly good locators")
	}
	if got == "0" {
		t.Errorf("distance = %q", got)
	}
}

// A stored distance wins: it came from the log that recorded the QSO, which knew
// more than two four-character squares do.
func TestStoredDistanceWins(t *testing.T) {
	d := 1234.0
	if got := distanceKm(&qso.QSO{Distance: &d, MyGrid: "JN36", Grid: "IO91"}); got != "1234" {
		t.Errorf("distance = %q, want the stored 1234", got)
	}
}

// No grids, no invention. An empty cell is honest; a zero is a claim.
func TestNoGridsNoDistance(t *testing.T) {
	for _, q := range []qso.QSO{{}, {MyGrid: "JN36"}, {Grid: "IO91"}, {MyGrid: "??", Grid: "IO91"}} {
		if got := distanceKm(&q); got != "" {
			t.Errorf("distance(%+v) = %q, want empty", q, got)
		}
	}
}
