Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

impl_test.gno

2.40 Kb · 60 lines
 1package impl
 2
 3import (
 4	"testing"
 5
 6	facade "gno.land/r/nym-moultest123/x/upgrade/selfreg/facade/v0"
 7	implv0 "gno.land/r/nym-moultest123/x/upgrade/selfreg/impl/v0"
 8
 9	"gno.land/p/nt/uassert/v0"
10)
11
12// TestTakeoverOnDeploy checks that the last implementation to run its init owns
13// the facade, and that re-registering an older one rolls the behaviour back.
14//
15// Both impl realms are imported here, so both inits have already run by the
16// time this test starts; v1 is imported last and therefore holds the facade.
17func TestTakeoverOnDeploy(cur realm, t *testing.T) {
18	uassert.Equal(t, "gno.land/r/nym-moultest123/x/upgrade/selfreg/impl/v1", facade.Live())
19	uassert.Equal(t, "impl/v1", facade.Version())
20	uassert.Equal(t, "HELLO, world!", facade.Greet("world"))
21
22	// Rolling back is the same one call: the facade only ever holds a pointer.
23	implv0.Register(cross(cur))
24	uassert.Equal(t, "gno.land/r/nym-moultest123/x/upgrade/selfreg/impl/v0", facade.Live())
25	uassert.Equal(t, "hello, world", facade.Greet("world"))
26
27	// And forward again.
28	Register(cross(cur))
29	uassert.Equal(t, "impl/v1", facade.Version())
30	uassert.Equal(t, "selfreg/facade/v0 [open]: impl/v1 (gno.land/r/nym-moultest123/x/upgrade/selfreg/impl/v1)\nHELLO, world!\n", facade.Render(""))
31}
32
33// TestSeal ends the realm's upgradeability and checks that it is terminal.
34//
35// Runs after TestTakeoverOnDeploy on purpose: sealing is one-way, so every test
36// that needs to register must already have run.
37func TestSeal(cur realm, t *testing.T) {
38	// The live implementation is the only actor that may seal, because with no
39	// owner it is the only one the facade already trusts.
40	uassert.Equal(t, "gno.land/r/nym-moultest123/x/upgrade/selfreg/impl/v1", facade.Live())
41	uassert.Equal(t, facade.StageOpen, facade.Stage())
42	uassert.AbortsContains(t, cur, "only the live implementation may seal", func() {
43		implv0.Seal(cross(cur))
44	})
45
46	Seal(cross(cur))
47	uassert.Equal(t, facade.StageSealed, facade.Stage())
48
49	// Nothing registers again, not even the implementation that sealed it.
50	uassert.AbortsContains(t, cur, "is sealed", func() {
51		Register(cross(cur))
52	})
53	uassert.AbortsContains(t, cur, "is sealed", func() {
54		implv0.Register(cross(cur))
55	})
56
57	// What was live stays live, and says so.
58	uassert.Equal(t, "HELLO, world!", facade.Greet("world"))
59	uassert.Equal(t, "selfreg/facade/v0 [sealed]: impl/v1 (gno.land/r/nym-moultest123/x/upgrade/selfreg/impl/v1)\nHELLO, world!\n", facade.Render(""))
60}