Search Apps Documentation Source Content File Folder Download Copy Actions Download

canonical_test.gno

1.97 Kb · 68 lines
 1package namereg
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/uassert/v0"
 7
 8	susers "gno.land/r/sys/users"
 9)
10
11// Canonicalize is now a delegating shim to susers.Canonicalize; verify
12// the local export and the upstream produce the same output for the
13// rules namereg/v1 cares about.
14func TestCanonicalize_DelegatesToSusers(t *testing.T) {
15	cases := []string{
16		"",
17		"a",
18		"alice",
19		"vitalik",
20		"vital1k",
21		"balloon",
22		"already-canonical",
23		"xyz123",
24		"nym-foolbar000",
25		"nym-foolbar001",
26	}
27	for _, in := range cases {
28		uassert.Equal(t, susers.Canonicalize(in), Canonicalize(in),
29			"namereg.Canonicalize must match susers.Canonicalize")
30	}
31}
32
33func TestReservedSet_BuiltFromBlacklist(t *testing.T) {
34	// Sanity: every entry from reservedNames AND its `+s` form must be
35	// in the canonicalized lookup.
36	for _, n := range reservedNames {
37		_, gotBare := reservedSet[Canonicalize(n)]
38		uassert.True(t, gotBare,
39			"reservedSet missing canonical(%q)", n)
40
41		_, gotPlural := reservedSet[Canonicalize(n+"s")]
42		uassert.True(t, gotPlural,
43			"reservedSet missing canonical(%q+s)", n)
44	}
45}
46
47func TestIsReserved_LiteralAndCanonical(t *testing.T) {
48	// Literal entry must be reserved.
49	uassert.True(t, IsReserved("admin"), "admin should be reserved")
50
51	// `blogs` covered via `blog`+s rule (blog is in reservedNames).
52	uassert.True(t, IsReserved("blogs"), "blogs covered by blog+s")
53
54	// l-substituted: `blog` canonicalizes to `biog`. Both forms blocked
55	// because we canonicalize the candidate before lookup.
56	uassert.True(t, IsReserved("biog"), "canonical(blog)=biog also blocked")
57
58	// 1-substituted under the new susers.Canonicalize rules: `1` → `i`.
59	// `b1og` canonicalizes to `biog`, same as `blog`. Confirms the shim
60	// picks up the broader rule set.
61	uassert.True(t, IsReserved("b1og"), "1→i variant also blocked")
62
63	// Non-reserved stem should not be flagged.
64	uassert.False(t, IsReserved("zulufoxtrot"), "zulufoxtrot is not reserved")
65
66	// Identity passthrough.
67	uassert.True(t, IsReserved("tendermint"))
68}