package main

import (
	"os"
	"regexp"
	"strconv"
	"testing"

	"hamlog/internal/cat/civ"
)

// The Icom model picker in the settings and civ.ModelName are two hand-kept
// copies of the same table, and they had drifted: the UI offered the IC-7700 at
// 0x88 and the IC-7800 at 0x80, which are the IC-7100's and the IC-7410's
// factory addresses. Picking either set an address the rig never answers on —
// the symptom is a rig that simply never replies — and the backend then named it
// as the other model.
//
// The list is read out of the .tsx itself, so a model added on one side alone
// fails here rather than on someone's radio.
func TestIcomModelAddressesMatch(t *testing.T) {
	src, err := os.ReadFile("frontend/src/components/SettingsModal.tsx")
	if err != nil {
		t.Skipf("frontend source not available: %v", err)
	}
	re := regexp.MustCompile(`\{ name: '(IC-[A-Za-z0-9-]+)', addr: 0x([0-9A-Fa-f]{2}) \}`)
	ms := re.FindAllStringSubmatch(string(src), -1)
	if len(ms) < 5 {
		t.Fatalf("only %d models found — the parse is wrong, not the data", len(ms))
	}
	for _, m := range ms {
		name := m[1]
		addr, err := strconv.ParseUint(m[2], 16, 8)
		if err != nil {
			t.Fatalf("bad address %q for %s", m[2], name)
		}
		if got := civ.ModelName(byte(addr)); got != name {
			t.Errorf("settings offer %s at 0x%02X, but civ.ModelName(0x%02X) = %q",
				name, addr, addr, got)
		}
	}
}
