fix(awards): apply the reference Prefix where it can actually help

Prefix exists so a field holding a bare value counts for an award whose codes
carry a letter: a French operator writes "74" in STATE, DDFM's codes are "D74".
Def's own doc says exactly that. But searchOne applied the prefix AFTER looking
the token up in the reference list — after the step that had just failed — so
the bare form matched nothing and the prefix decorated an empty result. The only
way through was a regex, in a mode where the operator had chosen "code" and
explicitly not "pattern".

The token lookup now tries the prefixed form when the bare one is not a known
reference. The list stays the authority: an unknown number still matches
nothing, so the prefix completes references rather than inventing them.

Second bug in the same pass: the blanket prefix also hit codes that came
straight OUT of the list, so a field already holding "D74" produced "DD74" as
soon as a prefix was configured — for both the token lookup and the
description matcher. Branches that yield whole codes are now excluded from it;
the ones that yield a raw capture (regex, whole-field split) still get it.
This commit is contained in:
2026-08-09 09:43:27 +02:00
parent 4352b9aec5
commit 79427ccd18
3 changed files with 103 additions and 1 deletions
+81
View File
@@ -0,0 +1,81 @@
package award
import (
"testing"
"hamlog/internal/qso"
)
// ddfmLike is a small stand-in for the French departments award: predefined
// references whose codes carry a letter the operator does not type.
func ddfmLike() []Def {
return []Def{{
Code: "DDFM", Name: "Departments", Type: TypeQSOFields,
Field: "state", MatchBy: "code", Prefix: "D",
}}
}
func ddfmRefs() map[string][]RefMeta {
return map[string][]RefMeta{"DDFM": {
{Code: "D29", Name: "Finistère", Valid: true},
{Code: "D49", Name: "Maine-et-Loire", Valid: true},
{Code: "D74", Name: "Haute-Savoie", Valid: true},
}}
}
func refsOf(t *testing.T, defs []Def, q qso.QSO) []string {
t.Helper()
res := Compute(defs, []qso.QSO{q}, ddfmRefs(), nil)
if len(res) != 1 {
t.Fatalf("want 1 result, got %d", len(res))
}
var out []string
for _, r := range res[0].Refs {
if r.Worked {
out = append(out, r.Ref)
}
}
return out
}
// The reported case: the operator writes just "74" in STATE, the award's codes
// are "D74", match-by is "code" and a Prefix of "D" is set. That found nothing —
// the prefix was applied only AFTER the list lookup, i.e. after the step that
// had already failed — so the only workaround was a regex, in a mode where the
// operator had explicitly chosen "code" rather than "pattern".
func TestPrefixCompletesABareReference(t *testing.T) {
got := refsOf(t, ddfmLike(), qso.QSO{Callsign: "F5AYE", State: "74"})
if len(got) != 1 || got[0] != "D74" {
t.Errorf("bare state 74 with prefix D → %v, want [D74]", got)
}
}
// And a field that ALREADY holds the whole code must not be prefixed twice.
// The blanket pass turned "D74" into "DD74" whenever a prefix was configured.
func TestPrefixDoesNotDoubleUpOnACompleteCode(t *testing.T) {
got := refsOf(t, ddfmLike(), qso.QSO{Callsign: "F5AYE", State: "D74"})
if len(got) != 1 || got[0] != "D74" {
t.Errorf("state D74 with prefix D → %v, want [D74]", got)
}
}
// A token that is neither a code nor a prefixable one stays unmatched: the
// prefix must not invent references.
func TestPrefixDoesNotInventReferences(t *testing.T) {
if got := refsOf(t, ddfmLike(), qso.QSO{Callsign: "F5AYE", State: "99"}); len(got) != 0 {
t.Errorf("unknown department 99 → %v, want none", got)
}
}
// Without a prefix nothing changes: a bare number matches nothing, a full code
// matches itself.
func TestNoPrefixKeepsExactCodeMatching(t *testing.T) {
defs := ddfmLike()
defs[0].Prefix = ""
if got := refsOf(t, defs, qso.QSO{Callsign: "F5AYE", State: "74"}); len(got) != 0 {
t.Errorf("no prefix, state 74 → %v, want none", got)
}
if got := refsOf(t, defs, qso.QSO{Callsign: "F5AYE", State: "D74"}); len(got) != 1 || got[0] != "D74" {
t.Errorf("no prefix, state D74 → %v, want [D74]", got)
}
}