Search Apps Documentation Source Content File Folder Download Copy Actions Download

board_test.gno

2.74 Kb · 111 lines
  1package hub_test
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/gnoland/boards"
  7	"gno.land/p/gnoland/boards/exts/permissions"
  8	"gno.land/p/nt/urequire/v0"
  9
 10	"gno.land/r/gnoland/boards2/v1/hub"
 11)
 12
 13func TestBoardIterateThreads(t *testing.T) {
 14	tests := []struct {
 15		name  string
 16		setup func(*boards.Board)
 17	}{
 18		{
 19			name: "no threads",
 20		},
 21		{
 22			name: "one thread",
 23			setup: func(b *boards.Board) {
 24				t := boards.MustNewThread(b, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "Title 1", "Body 1")
 25				b.Threads.Add(t)
 26			},
 27		},
 28		{
 29			name: "multiple threads",
 30			setup: func(b *boards.Board) {
 31				threads := []*boards.Post{
 32					boards.MustNewThread(b, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "Title 1", "Body 1"),
 33					boards.MustNewThread(b, "g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh", "Title 2", "Body 2"),
 34					boards.MustNewThread(b, "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj", "Title 3", "Body 3"),
 35				}
 36				for _, t := range threads {
 37					b.Threads.Add(t)
 38				}
 39			},
 40		},
 41	}
 42
 43	for _, tt := range tests {
 44		t.Run(tt.name, func(t *testing.T) {
 45			ref := boards.New(1)
 46			if tt.setup != nil {
 47				tt.setup(ref)
 48			}
 49
 50			board := hub.NewSafeBoard(ref)
 51
 52			urequire.Equal(t, ref.Threads.Size(), board.ThreadCount, "expect number of threads to match")
 53			board.IterateThreads(0, board.ThreadCount, func(thread hub.Thread) bool {
 54				id := boards.ID(thread.ID)
 55				expected, found := ref.Threads.Get(id)
 56
 57				urequire.True(t, found, "expect thread to be found")
 58				urequire.Equal(t, expected.Creator, thread.Creator, "expect creator to match")
 59				urequire.Equal(t, expected.Title, thread.Title, "expect title to match")
 60				urequire.Equal(t, expected.Body, thread.Body, "expect body to match")
 61				return false
 62			})
 63		})
 64	}
 65}
 66
 67func TestBoardIterateMembers(t *testing.T) {
 68	tests := []struct {
 69		name  string
 70		users []boards.User
 71	}{
 72		{
 73			name: "no members",
 74		},
 75		{
 76			name: "one member",
 77			users: []boards.User{
 78				{Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"},
 79			},
 80		},
 81		{
 82			name: "multiple members",
 83			users: []boards.User{
 84				{Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"},
 85				{Address: "g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh"},
 86				{Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"},
 87			},
 88		},
 89	}
 90
 91	for _, tt := range tests {
 92		t.Run(tt.name, func(t *testing.T) {
 93			var i int
 94			perms := permissions.New()
 95			for _, u := range tt.users {
 96				perms.SetUserRoles(u.Address)
 97			}
 98
 99			ref := boards.New(1)
100			ref.Permissions = perms
101			board := hub.NewSafeBoard(ref)
102
103			urequire.Equal(t, len(tt.users), board.MemberCount, "expect number of members to match")
104			board.IterateMembers(0, board.MemberCount, func(user boards.User) bool {
105				urequire.Equal(t, tt.users[i].Address, user.Address, "expect address to match")
106				i++
107				return false
108			})
109		})
110	}
111}