caps_test.gno
4.69 Kb · 169 lines
1package agentvault
2
3import (
4 "testing"
5)
6
7func TestDayIndex(t *testing.T) {
8 if DayIndex(0) != 0 {
9 t.Fatal("day 0 expected for unix 0")
10 }
11 if DayIndex(86399) != 0 {
12 t.Fatal("day 0 expected for unix 86399")
13 }
14 if DayIndex(86400) != 1 {
15 t.Fatal("day 1 expected for unix 86400")
16 }
17 if DayIndex(172800) != 2 {
18 t.Fatal("day 2 expected for unix 172800")
19 }
20}
21
22func TestValidateCaps(t *testing.T) {
23 if ValidateCaps(100, 1000) != "" {
24 t.Fatal("valid caps rejected")
25 }
26 if ValidateCaps(0, 1000) == "" {
27 t.Fatal("zero per-tx cap accepted")
28 }
29 if ValidateCaps(-5, 1000) == "" {
30 t.Fatal("negative per-tx cap accepted")
31 }
32 if ValidateCaps(100, 0) == "" {
33 t.Fatal("zero daily cap accepted")
34 }
35 if ValidateCaps(100, -1) == "" {
36 t.Fatal("negative daily cap accepted")
37 }
38 if ValidateCaps(2000, 1000) == "" {
39 t.Fatal("per-tx cap above daily cap accepted")
40 }
41 if ValidateCaps(1000, 1000) != "" {
42 t.Fatal("per-tx cap equal to daily cap rejected")
43 }
44}
45
46func TestNewBudget(t *testing.T) {
47 b := NewBudget(100, 1000)
48 if b.PerTxCap != 100 || b.DailyCap != 1000 {
49 t.Fatal("budget caps not stored")
50 }
51 if b.SpentToday != 0 || b.DayIndex != 0 {
52 t.Fatal("new budget must start unspent")
53 }
54}
55
56func TestCheckSpend(t *testing.T) {
57 b := NewBudget(100, 1000)
58
59 // Within both caps: allowed.
60 if ok, reason := CheckSpend(b, 50); !ok {
61 t.Fatal("spend within caps rejected:", reason)
62 }
63 // Exactly at the per-tx cap: allowed.
64 if ok, reason := CheckSpend(b, 100); !ok {
65 t.Fatal("spend at per-tx cap rejected:", reason)
66 }
67 // One over the per-tx cap: rejected with the per-tx reason.
68 if ok, reason := CheckSpend(b, 101); ok || reason != "exceeds per-transaction cap" {
69 t.Fatal("spend over per-tx cap not rejected with per-tx reason:", ok, reason)
70 }
71 // Non-positive amounts: rejected.
72 if ok, reason := CheckSpend(b, 0); ok || reason != "amount must be positive" {
73 t.Fatal("zero spend not rejected:", ok, reason)
74 }
75 if ok, _ := CheckSpend(b, -10); ok {
76 t.Fatal("negative spend allowed")
77 }
78 // Cumulative spend past the daily cap: rejected with the daily reason.
79 // Spend 950 (within per-tx? no: 950 > 100). Use a budget with a
80 // higher per-tx cap to isolate the daily check.
81 b2 := NewBudget(1000, 1000)
82 ApplySpend(&b2, 600)
83 if ok, reason := CheckSpend(b2, 500); ok || reason != "exceeds daily cap" {
84 t.Fatal("spend over daily cap not rejected with daily reason:", ok, reason)
85 }
86 // Exactly the remainder of the daily cap: allowed.
87 if ok, reason := CheckSpend(b2, 400); !ok {
88 t.Fatal("spend at daily remainder rejected:", reason)
89 }
90}
91
92func TestRefreshDayAndRemaining(t *testing.T) {
93 b := NewBudget(100, 1000)
94 day0 := int64(1000) // unix 1000 -> day 0
95 day1 := int64(86400 + 1000) // unix 87400 -> day 1
96
97 ApplySpend(&b, 400)
98 b.DayIndex = DayIndex(day0)
99 if RemainingToday(b, day0) != 600 {
100 t.Fatal("remaining today wrong after spend:", RemainingToday(b, day0))
101 }
102 // Same day: refresh changes nothing.
103 RefreshDay(&b, day0)
104 if b.SpentToday != 400 {
105 t.Fatal("refresh on same day cleared spent amount")
106 }
107 // Next day: refresh clears the spent amount.
108 RefreshDay(&b, day1)
109 if b.SpentToday != 0 {
110 t.Fatal("refresh on new day did not clear spent amount")
111 }
112 if b.DayIndex != DayIndex(day1) {
113 t.Fatal("refresh did not move day index")
114 }
115 if RemainingToday(b, day1) != 1000 {
116 t.Fatal("remaining on new day is not the full daily cap")
117 }
118 // A budget that never spent: remaining is the full cap even
119 // when the stored day is stale.
120 stale := NewBudget(50, 500)
121 if RemainingToday(stale, day1) != 500 {
122 t.Fatal("stale-day budget remaining wrong")
123 }
124}
125
126func TestApplySpend(t *testing.T) {
127 b := NewBudget(100, 1000)
128 ApplySpend(&b, 30)
129 ApplySpend(&b, 20)
130 if b.SpentToday != 50 {
131 t.Fatal("spent today not accumulated:", b.SpentToday)
132 }
133}
134
135func TestEscrowRefundable(t *testing.T) {
136 e := Escrow{ID: 1, Amount: 500, DeadlineHeight: 100, Status: EscrowHeld}
137 if EscrowRefundable(e, 100) {
138 t.Fatal("escrow refundable at the deadline block itself")
139 }
140 if !EscrowRefundable(e, 101) {
141 t.Fatal("escrow not refundable past the deadline")
142 }
143 if EscrowRefundable(e, 50) {
144 t.Fatal("escrow refundable before the deadline")
145 }
146 e.Status = EscrowReleased
147 if EscrowRefundable(e, 200) {
148 t.Fatal("released escrow refundable")
149 }
150 e.Status = EscrowRefunded
151 if EscrowRefundable(e, 200) {
152 t.Fatal("refunded escrow refundable again")
153 }
154}
155
156func TestEscrowStatusString(t *testing.T) {
157 if EscrowStatusString(EscrowHeld) != "HELD" {
158 t.Fatal("HELD string wrong")
159 }
160 if EscrowStatusString(EscrowReleased) != "RELEASED" {
161 t.Fatal("RELEASED string wrong")
162 }
163 if EscrowStatusString(EscrowRefunded) != "REFUNDED" {
164 t.Fatal("REFUNDED string wrong")
165 }
166 if EscrowStatusString(EscrowStatus(99)) != "UNKNOWN" {
167 t.Fatal("unknown status string wrong")
168 }
169}