vault_test.gno
1.78 Kb · 58 lines
1package vault
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/nt/avl/v0"
8 "gno.land/p/nt/testutils/v0"
9 "gno.land/p/nt/uassert/v0"
10)
11
12func TestSetAndRender(cur realm, t *testing.T) {
13 alice := testutils.TestAddress("alice")
14 testing.SetRealm(testing.NewUserRealm(alice))
15
16 Set(cross(cur), "greeting", "hello")
17 Set(cross(cur), "lang", "gno")
18
19 out := renderAddress(alice.String())
20 uassert.True(t, strings.Contains(out, "greeting"), "should contain key greeting")
21 uassert.True(t, strings.Contains(out, "hello"), "should contain value hello")
22 uassert.True(t, strings.Contains(out, "Total entries: 2"), "should count 2 entries")
23
24 root := Render("")
25 uassert.True(t, strings.Contains(root, alice.String()), "root should list alice")
26}
27
28func TestDeleteAndIsolation(cur realm, t *testing.T) {
29 alice := testutils.TestAddress("alice2")
30 bob := testutils.TestAddress("bob2")
31
32 testing.SetRealm(testing.NewUserRealm(alice))
33 Set(cross(cur), "shared", "alice-value")
34
35 testing.SetRealm(testing.NewUserRealm(bob))
36 Set(cross(cur), "shared", "bob-value")
37
38 // Namespaces are isolated: same key, different owners.
39 av := vaults.Get(alice.String())
40 bv := vaults.Get(bob.String())
41 avVal := av.(*avl.Tree).Get("shared")
42 bvVal := bv.(*avl.Tree).Get("shared")
43 uassert.Equal(t, "alice-value", avVal.(string))
44 uassert.Equal(t, "bob-value", bvVal.(string))
45
46 // Bob deletes his own key; alice's remains.
47 Delete(cross(cur), "shared")
48 bobStill := vaults.Has(bob.String())
49 uassert.False(t, bobStill, "bob's vault should be gone after last key removed")
50 aliceStill := vaults.Has(alice.String())
51 uassert.True(t, aliceStill, "alice's vault should remain")
52
53 // Deleting a missing key aborts.
54 testing.SetRealm(testing.NewUserRealm(alice))
55 uassert.AbortsWithMessage(t, cur, "key not found", func() {
56 Delete(cross(cur), "nope")
57 })
58}