gnomem_test.gno
2.83 Kb · 82 lines
1package gnomem
2
3import (
4 "testing"
5
6 "gno.land/p/nt/testutils/v0"
7 "gno.land/p/nt/uassert/v0"
8)
9
10var (
11 researcher = testutils.TestAddress("researcher")
12 skeptic = testutils.TestAddress("skeptic")
13 judge = testutils.TestAddress("judge")
14)
15
16// The canonical scenario from the design: a researcher proposes, a skeptic
17// contests with an exploit, an adjudicator supersedes with the corrected
18// claim.
19func TestContestedThenSuperseded(cur realm, t *testing.T) {
20 testing.SetRealm(testing.NewUserRealm(researcher))
21 id := ProposeClaim(cross(cur), "foo/v2", "is", "safe to deploy", "report#H1", 82)
22 uassert.Equal(t, uint64(1), id)
23 uassert.Equal(t, string(StatusProposed), string(Get(id).Status))
24
25 // Skeptic contests with a refutation.
26 testing.SetRealm(testing.NewUserRealm(skeptic))
27 ContestClaim(cross(cur), id, "unbounded allocation", "exploit#H2")
28 uassert.Equal(t, string(StatusContested), string(Get(id).Status))
29 uassert.True(t, IsOpen(id))
30
31 // Adjudicator supersedes with the corrected claim.
32 testing.SetRealm(testing.NewUserRealm(judge))
33 newID := SupersedeClaim(cross(cur), id, "foo/v2", "is", "unsafe before commit abc123", "report#H3", 95)
34 uassert.Equal(t, uint64(2), newID)
35
36 old := Get(id)
37 uassert.Equal(t, string(StatusSuperseded), string(old.Status))
38 uassert.Equal(t, newID, old.SupersededBy)
39 uassert.False(t, IsOpen(id)) // superseded is terminal
40
41 newC := Get(newID)
42 uassert.Equal(t, id, newC.Supersedes)
43 uassert.Equal(t, 1, len(newC.Evidence)) // the initial report#H3
44
45 uassert.True(t, len(Render("")) > 0)
46 uassert.True(t, len(Render("1")) > 0)
47 uassert.True(t, len(Render("2")) > 0)
48}
49
50func TestSupportThenAccept(cur realm, t *testing.T) {
51 testing.SetRealm(testing.NewUserRealm(researcher))
52 id := ProposeClaim(cross(cur), "gnovm", "supports", "persistent object graphs", "", 90)
53
54 testing.SetRealm(testing.NewUserRealm(skeptic))
55 SupportClaim(cross(cur), id, 88, "confirmed in docs")
56 uassert.Equal(t, string(StatusSupported), string(Get(id).Status))
57
58 testing.SetRealm(testing.NewUserRealm(judge))
59 ResolveClaim(cross(cur), id, true)
60 uassert.Equal(t, string(StatusAccepted), string(Get(id).Status))
61 uassert.False(t, IsOpen(id))
62}
63
64func TestCannotMutateTerminal(cur realm, t *testing.T) {
65 testing.SetRealm(testing.NewUserRealm(researcher))
66 id := ProposeClaim(cross(cur), "x", "is", "y", "", 50)
67 ResolveClaim(cross(cur), id, false) // retracted, terminal
68
69 uassert.AbortsWithMessage(t, cur, "claim is resolved (terminal): retracted", func(cur realm) {
70 SupportClaim(cur, id, 10, "too late")
71 })
72}
73
74func TestBadInputs(cur realm, t *testing.T) {
75 testing.SetRealm(testing.NewUserRealm(researcher))
76 uassert.AbortsWithMessage(t, cur, "empty triple field", func(cur realm) {
77 ProposeClaim(cur, "", "is", "y", "", 10)
78 })
79 uassert.AbortsWithMessage(t, cur, "confidence must be 0..100", func(cur realm) {
80 ProposeClaim(cur, "a", "b", "c", "", 200)
81 })
82}