home_test.gno

2.88 Kb ยท 99 lines
 1package home
 2
 3import (
 4	"std"
 5	"testing"
 6
 7	"gno.land/p/demo/testutils"
 8	"gno.land/p/demo/uassert"
 9	"gno.land/r/mouss/config"
10)
11
12var (
13	user1    = testutils.TestAddress("user1")
14	user2    = testutils.TestAddress("user2")
15	mainAddr = config.OwnableMain.Owner()
16)
17
18func TestProfile(t *testing.T) {
19	uassert.NotEmpty(t, profile.AboutMe, "AboutMe should not be empty")
20	uassert.NotEmpty(t, profile.Avatar, "Avatar should not be empty")
21	uassert.NotEmpty(t, profile.Email, "Email should not be empty")
22	uassert.NotEmpty(t, profile.Github, "Github should not be empty")
23	uassert.NotEmpty(t, profile.LinkedIn, "LinkedIn should not be empty")
24}
25
26func TestAddRecipe(t *testing.T) {
27	std.TestSetOriginCaller(user1)
28	name := "Test Recipe"
29	origin := "Test Origin"
30	ingredients := "Ingredient 1\nIngredient 2"
31	instructions := "Step 1\nStep 2"
32	tips := "Test Tips"
33
34	result := AddRecipe(name, origin, ingredients, instructions, tips)
35	uassert.Equal(t, "Recipe added successfully", result)
36	uassert.Equal(t, 1, recipes.Size())
37	value, exist := recipes.Get(name)
38	uassert.True(t, exist)
39	recipe := value.(*Recipe)
40	uassert.Equal(t, name, recipe.Name)
41	uassert.Equal(t, origin, recipe.Origin)
42	uassert.Equal(t, ingredients, recipe.Ingredients)
43	uassert.Equal(t, instructions, recipe.Instructions)
44	uassert.Equal(t, tips, recipe.Tips)
45	uassert.Equal(t, user1, recipe.Author)
46
47	// Verify recipe is correctly stored in AVL tree with matching fields
48	var found bool
49	recipes.Iterate("", "", func(key string, value interface{}) bool {
50		if key == name {
51			found = true
52			foundRecipe := value.(*Recipe)
53			uassert.Equal(t, recipe.Name, foundRecipe.Name)
54			uassert.Equal(t, recipe.Origin, foundRecipe.Origin)
55			uassert.Equal(t, recipe.Ingredients, foundRecipe.Ingredients)
56			uassert.Equal(t, recipe.Instructions, foundRecipe.Instructions)
57			uassert.Equal(t, recipe.Tips, foundRecipe.Tips)
58			uassert.Equal(t, recipe.Author, foundRecipe.Author)
59			return true
60		}
61		return false
62	})
63	uassert.Equal(t, true, found)
64}
65
66func TestFollow(t *testing.T) {
67	// Test user following admin's profile
68	std.TestSetOriginCaller(user1)
69	err := Follow()
70	uassert.NoError(t, err, "user should be able to follow admin's profile")
71
72	// Test admin trying to follow themselves
73	std.TestSetOriginCaller(mainAddr)
74	err = Follow()
75	uassert.Error(t, err, "you cannot follow yourself")
76
77	// Test following same address twice
78	std.TestSetOriginCaller(user1)
79	err = Follow()
80	uassert.Error(t, err, "should not be able to follow same address twice")
81
82	// Test multiple users following admin
83	std.TestSetOriginCaller(user2)
84	err = Follow()
85	uassert.NoError(t, err, "another user should be able to follow admin's profile")
86}
87
88func TestUnfollow(t *testing.T) {
89	// Test successful unfollow
90	std.TestSetOriginCaller(user1)
91	err := Unfollow()
92	uassert.NoError(t, err)
93	uassert.False(t, profile.Followers.Has(user1))
94
95	// Test unfollowing when not following
96	err = Unfollow()
97	uassert.Error(t, err)
98
99}