package extsvc

import "testing"

// TQSL's own exit codes, from its cmdline documentation:
//
//	0  success
//	8  NO QSOs were processed — already uploaded OR out of date range
//	9  some processed, some ignored — same two reasons
//	14 some already uploaded, the rest signed
//
// 8 and 9 both used to be read as plain success, and that is how a contact came
// to be stamped "uploaded to LoTW" while LoTW had never seen it. The one that
// bites is "out of date range": a contact older than the callsign certificate's
// validity is silently left out of the submission.
func TestTQSLDetailKeepsTheReason(t *testing.T) {
	out := `13:22:01: Signing using Callsign G0ABC, DXCC Entity ENGLAND
13:22:02: /tmp/x.adi: 20 QSO records are out of date range
13:22:02: Final Status: No QSOs were processed (8)`
	got := tqslDetail(out, "summary")
	if got == "summary" {
		t.Fatal("threw away TQSL's explanation — that sentence is the whole answer to \"why is my contact not on LoTW\"")
	}
	if !contains(got, "out of date range") {
		t.Errorf("the reason was lost: %q", got)
	}
}

func TestTQSLDetailFallsBackToTheSummary(t *testing.T) {
	if got := tqslDetail("nothing useful here", "summary"); got != "summary" {
		t.Errorf("got %q, want the plain summary when TQSL said nothing specific", got)
	}
}

func contains(h, n string) bool {
	return len(h) >= len(n) && (func() bool {
		for i := 0; i+len(n) <= len(h); i++ {
			if h[i:i+len(n)] == n {
				return true
			}
		}
		return false
	})()
}
