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

board_test.gno

3.12 Kb · 102 lines
  1package hub_test
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/gnoland/boards"
  7	"gno.land/p/gnoland/boards/exts/hub"
  8	"gno.land/p/gnoland/boards/exts/permissions"
  9	"gno.land/p/nt/urequire/v0"
 10)
 11
 12func TestNewSafeBoard(t *testing.T) {
 13	tests := []struct {
 14		name                     string
 15		setup                    func() *boards.Board
 16		threadCount, memberCount int
 17		createdAt                int64
 18	}{
 19		{
 20			name:      "new board",
 21			setup:     func() *boards.Board { return boards.New(1) },
 22			createdAt: testTime,
 23		},
 24		{
 25			name: "with threads",
 26			setup: func() *boards.Board {
 27				b := boards.New(1)
 28				b.Threads.Add(boards.MustNewThread(b, alice, "Title 1", "Body 1"))
 29				b.Threads.Add(boards.MustNewThread(b, bob, "Title 2", "Body 2"))
 30				return b
 31			},
 32			threadCount: 2,
 33			createdAt:   testTime,
 34		},
 35		{
 36			name: "with members",
 37			setup: func() *boards.Board {
 38				b := boards.New(1)
 39				perms := permissions.New()
 40				perms.SetUserRoles(alice)
 41				perms.SetUserRoles(bob)
 42				b.Permissions = perms
 43				return b
 44			},
 45			memberCount: 2,
 46			createdAt:   testTime,
 47		},
 48		{
 49			// A board with no storages assigned must not panic: the counts
 50			// are skipped and the zero creation time maps to a zero Unix time.
 51			name:  "bare board without storages",
 52			setup: func() *boards.Board { return &boards.Board{ID: 1} },
 53		},
 54	}
 55
 56	for _, tt := range tests {
 57		t.Run(tt.name, func(t *testing.T) {
 58			ref := tt.setup()
 59			ref.Name = "test123"
 60			ref.Creator = alice
 61
 62			board := hub.NewSafeBoard(ref)
 63
 64			urequire.Equal(t, uint64(1), board.ID(), "expect ID to match")
 65			urequire.Equal(t, "test123", board.Name(), "expect name to match")
 66			urequire.Equal(t, alice, board.Creator(), "expect creator to match")
 67			urequire.False(t, board.Readonly(), "expect board not to be readonly")
 68			urequire.Equal(t, tt.threadCount, board.ThreadCount(), "expect thread count to match")
 69			urequire.Equal(t, tt.memberCount, board.MemberCount(), "expect member count to match")
 70			urequire.Equal(t, tt.createdAt, board.CreatedAt(), "expect creation time to match")
 71			urequire.Equal(t, int64(0), board.UpdatedAt(), "expect zero update time to map to zero")
 72		})
 73	}
 74}
 75
 76// TestNewSafeBoardSnapshotsAliases asserts the invariant this package exists
 77// for: a safe type is a snapshot, so later writes to the source board must not
 78// be observable through a value already handed to a caller.
 79func TestNewSafeBoardSnapshotsAliases(t *testing.T) {
 80	ref := boards.New(1)
 81	ref.Name = "current"
 82	ref.Aliases = []string{"previous"}
 83
 84	board := hub.NewSafeBoard(ref)
 85
 86	ref.Aliases[0] = "mutated"
 87	ref.Aliases = append(ref.Aliases, "added")
 88	ref.Name = "renamed"
 89	ref.Readonly = true
 90
 91	aliases := board.Aliases()
 92	urequire.Equal(t, 1, len(aliases), "expect alias count to be unaffected")
 93	urequire.Equal(t, "previous", aliases[0], "expect alias to be unaffected")
 94	urequire.Equal(t, "current", board.Name(), "expect name to be unaffected")
 95	urequire.False(t, board.Readonly(), "expect readonly to be unaffected")
 96}
 97
 98func TestNewSafeBoardNilRef(cur realm, t *testing.T) {
 99	urequire.PanicsWithMessage(t, cur, "board reference is nil", func() {
100		hub.NewSafeBoard(nil)
101	}, "expect a nil board reference to be rejected")
102}