package extsvc

import "testing"

func TestParseQRZResponse(t *testing.T) {
	cases := []struct {
		name    string
		body    string
		wantOK  bool
		wantErr bool
		logID   string
	}{
		{"insert ok", "STATUS=OK&LOGID=123456&COUNT=1", true, false, "123456"},
		{"duplicate is ok", "STATUS=FAIL&REASON=Unable+to+add+QSO+duplicate", true, false, ""},
		{"already present", "STATUS=FAIL&REASON=QSO+already+in+logbook", true, false, ""},
		{"real failure", "STATUS=FAIL&REASON=Bad+ADIF", false, true, ""},
		{"auth failure", "STATUS=AUTH&REASON=invalid+api+key", false, true, ""},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			res, err := parseQRZResponse(c.body)
			if (err != nil) != c.wantErr {
				t.Fatalf("err = %v, wantErr %v", err, c.wantErr)
			}
			if res.OK != c.wantOK {
				t.Errorf("OK = %v, want %v", res.OK, c.wantOK)
			}
			if c.logID != "" && res.LogID != c.logID {
				t.Errorf("LogID = %q, want %q", res.LogID, c.logID)
			}
		})
	}
}
