Harden item occurrence grounding
This commit is contained in:
@@ -8,10 +8,8 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/unicode/norm"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -51,17 +49,7 @@ func NormalizeDisplay(value string) string {
|
||||
|
||||
// ComparisonKey returns the stable key used for item-type identity comparisons.
|
||||
func ComparisonKey(value string) string {
|
||||
value = norm.NFKC.String(value)
|
||||
value = strings.Map(func(r rune) rune {
|
||||
switch r {
|
||||
case '\u2018', '\u2019', '\u02bc':
|
||||
return '\''
|
||||
default:
|
||||
return r
|
||||
}
|
||||
}, value)
|
||||
value = strings.Join(strings.Fields(value), " ")
|
||||
return cases.Fold().String(value)
|
||||
return shared.ComparisonKey(value)
|
||||
}
|
||||
|
||||
// DeriveID returns the deterministic ID for an item type. Empty identity keys
|
||||
|
||||
@@ -9,29 +9,6 @@ import (
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
)
|
||||
|
||||
func TestComparisonKeyNormalizesSupportedEquivalences(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
left string
|
||||
right string
|
||||
}{
|
||||
{name: "case", left: "Silver Key", right: "sILVER kEY"},
|
||||
{name: "compatibility", left: "Silver Key", right: "Silver Key"},
|
||||
{name: "whitespace", left: " Silver\u2003Key ", right: "Silver Key"},
|
||||
{name: "apostrophe", left: "Healer’s Kit", right: "healer's kit"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if ComparisonKey(test.left) != ComparisonKey(test.right) {
|
||||
t.Fatalf("ComparisonKey(%q) = %q, ComparisonKey(%q) = %q", test.left, ComparisonKey(test.left), test.right, ComparisonKey(test.right))
|
||||
}
|
||||
})
|
||||
}
|
||||
if ComparisonKey("Silver Key") == ComparisonKey("Gold Key") {
|
||||
t.Fatal("different item types received the same comparison key")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeriveIDUsesExactCompactIdentityBytes(t *testing.T) {
|
||||
const wantID = "item:sha256:2f211c60b9bdcf3a6dd64086cc4c05df5b3357ae02cb462f7ef8988d9bd2fa4f"
|
||||
const wantIdentity = `["dnd.item_registry.identity.v1","silver key"]`
|
||||
|
||||
Reference in New Issue
Block a user