package lookup

import (
	"encoding/xml"
	"strings"
	"testing"
)

// QRZ sends the island reference and OpsLog read past it.
//
// It matters more for IOTA than it would for another award: unlike POTA there
// is no live "who is on an island right now" feed anywhere, so the callbook
// record is the practical source — and it is known BEFORE the contact is
// logged, which is when it is useful.
func TestQRZCallsignCarriesIOTA(t *testing.T) {
	const body = `<?xml version="1.0" encoding="utf-8" ?>
<QRZDatabase version="1.34">
<Callsign>
<call>F5IRH</call>
<fname>Max</fname>
<country>France</country>
<grid>IN87ki</grid>
<cqzone>14</cqzone>
<iota>EU-048</iota>
</Callsign>
<Session><Key>abc</Key></Session>
</QRZDatabase>`

	var resp qrzDB
	if err := xml.Unmarshal([]byte(body), &resp); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if resp.Callsign.IOTA != "EU-048" {
		t.Fatalf("the <iota> tag decoded as %q — the field is not being read", resp.Callsign.IOTA)
	}
	// And a record with no island must not invent one.
	var none qrzDB
	if err := xml.Unmarshal([]byte(strings.Replace(body, "<iota>EU-048</iota>", "", 1)), &none); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if none.Callsign.IOTA != "" {
		t.Errorf("IOTA = %q for a record without one", none.Callsign.IOTA)
	}
}
