hearthcore_test.gno
3.84 Kb · 107 lines
1package hearthcore
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/nt/uassert/v0"
8)
9
10func TestComputeSplitConservation(t *testing.T) {
11 cases := []struct {
12 escrow int64
13 bps uint64
14 wantH int64
15 wantS int64
16 }{
17 {1000000, 0, 0, 1000000},
18 {1000000, 10000, 1000000, 0},
19 {1000000, 5000, 500000, 500000},
20 {1000000, 7500, 750000, 250000},
21 {999999, 3333, 333299, 666700},
22 {1, 5000, 0, 1},
23 {0, 5000, 0, 0},
24 {9000000000000000, 3333, 2999700000000000, 6000300000000000},
25 }
26
27 for _, tc := range cases {
28 h, s := ComputeSplit(tc.escrow, tc.bps)
29 uassert.Equal(t, tc.wantH, h)
30 uassert.Equal(t, tc.wantS, s)
31 uassert.Equal(t, tc.escrow, h+s)
32 }
33}
34
35func TestFormatGNOT(t *testing.T) {
36 uassert.Equal(t, "0.00 GNOT", FormatGNOT(0))
37 uassert.Equal(t, "1.00 GNOT", FormatGNOT(1000000))
38 uassert.Equal(t, "2.50 GNOT", FormatGNOT(2500000))
39 uassert.Equal(t, "0.10 GNOT", FormatGNOT(100000))
40 uassert.Equal(t, "0.123456 GNOT", FormatGNOT(123456))
41 uassert.Equal(t, "42.05 GNOT", FormatGNOT(42050000))
42}
43
44func TestComputeTier(t *testing.T) {
45 uassert.Equal(t, string(TierApprentice), string(ComputeTier(0, 0, 0, 0)))
46 uassert.Equal(t, string(TierApprentice), string(ComputeTier(1, 5000000, 5, 1)))
47 uassert.Equal(t, string(TierJourneyman), string(ComputeTier(2, 5000000, 9, 2)))
48 uassert.Equal(t, string(TierJourneyman), string(ComputeTier(1, 10000000, 5, 1)))
49 uassert.Equal(t, string(TierMaster), string(ComputeTier(5, 10000000, 23, 5)))
50 uassert.Equal(t, string(TierMaster), string(ComputeTier(1, 100000000, 5, 1)))
51 uassert.Equal(t, string(TierGuildmaster), string(ComputeTier(10, 50000000, 48, 10)))
52 // Low rating blocks Guildmaster promotion.
53 uassert.Equal(t, string(TierMaster), string(ComputeTier(10, 50000000, 30, 10)))
54}
55
56func TestFormatRating(t *testing.T) {
57 uassert.Equal(t, "Unrated", FormatRating(0, 0))
58 uassert.Equal(t, "5.0 / 5.0", FormatRating(15, 3))
59 uassert.Equal(t, "4.7 / 5.0", FormatRating(14, 3))
60}
61
62func TestCategoryAndStatusHelpers(t *testing.T) {
63 cat, ok := ParseCategory(" AGENT ")
64 uassert.True(t, ok)
65 uassert.Equal(t, string(CatAgent), string(cat))
66 uassert.Equal(t, "AI Agent", cat.Label())
67 uassert.Equal(t, "ai", cat.IconName())
68
69 _, bad := ParseCategory("invalid")
70 uassert.False(t, bad)
71
72 uassert.True(t, StatusOpen.IsValid())
73 uassert.False(t, Status("bogus").IsValid())
74 uassert.Equal(t, 3, StatusSettled.StepIndex())
75}
76
77func TestValidationHelpers(t *testing.T) {
78 uassert.Equal(t, "https://github.com/org/repo/pull/1", ValidateProofURL(" https://github.com/org/repo/pull/1 "))
79 uassert.Equal(t, "ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi", ValidateProofURL("ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi"))
80 uassert.Equal(t, DefaultDeadlineBlocks, ValidateDeadlineBlocks(0))
81 uassert.Equal(t, int64(250), ValidateDeadlineBlocks(250))
82 uassert.Equal(t, uint8(5), ValidateRating(5))
83 uassert.Equal(t, `cost is \$50 and \$GNOT`, EscapeMath("cost is $50 and $GNOT"))
84}
85
86func TestJSONBuilder(t *testing.T) {
87 obj := NewJSONObject().
88 Str("title", "Build \"alpha\"\nrealm").
89 Int64("escrow_ugnot", 2500000).
90 Uint64("id", 7).
91 Bool("active", true).
92 Raw("tags", JoinJSONArray([]string{QuoteJSON("realm"), QuoteJSON("mcp")})).
93 String()
94
95 uassert.Equal(t, `{"title":"Build \"alpha\"\nrealm","escrow_ugnot":2500000,"id":7,"active":true,"tags":["realm","mcp"]}`, obj)
96}
97
98func TestSVGVisuals(t *testing.T) {
99 hero := RenderHeroBannerSVG(3, 2, 1, 4, 12500000, 48000000)
100 uassert.True(t, strings.HasPrefix(hero, ")
101
102 step := RenderLifecycleSVG(StatusSubmitted, 3500000)
103 uassert.True(t, strings.HasPrefix(step, ")
104
105 badge := RenderTierBadgeSVG("agent<script>", TierGuildmaster, 12, 600000000, true)
106 uassert.True(t, strings.HasPrefix(badge, ")
107}