tokenhub_test.gno

8.17 Kb ยท 194 lines
  1package tokenhub
  2
  3import (
  4	"std"
  5	"strings"
  6	"testing"
  7
  8	"gno.land/p/demo/grc/grc1155"
  9	"gno.land/p/demo/grc/grc20"
 10	"gno.land/p/demo/grc/grc721"
 11	"gno.land/p/demo/uassert"
 12	"gno.land/p/demo/urequire"
 13)
 14
 15func TestTokenRegistration(t *testing.T) {
 16	testing.SetRealm(std.NewCodeRealm("gno.land/r/matijamarjanovic/home"))
 17
 18	token, _ := grc20.NewToken("Test Token", "TEST", 6)
 19	RegisterToken(token.Getter(), "test_token")
 20
 21	retrievedToken := GetToken("gno.land/r/matijamarjanovic/tokenhub.gno.land/r/matijamarjanovic/home.test_token")
 22	urequire.True(t, retrievedToken != nil, "Should retrieve registered token")
 23
 24	uassert.Equal(t, "Test Token", retrievedToken.GetName(), "Token name should match")
 25	uassert.Equal(t, "TEST", retrievedToken.GetSymbol(), "Token symbol should match")
 26}
 27
 28func TestNFTRegistration(t *testing.T) {
 29	testing.SetRealm(std.NewCodeRealm("gno.land/r/matijamarjanovic/home"))
 30
 31	nft := grc721.NewBasicNFT("Test NFT", "TNFT")
 32	nft.Mint(std.CurrentRealm().Address(), grc721.TokenID("1"))
 33	err := RegisterNFT(nft.Getter(), "test_nft", "1")
 34	urequire.NoError(t, err, "Should register NFT without error")
 35
 36	retrievedNFT := GetNFT("gno.land/r/matijamarjanovic/home.test_nft.1")
 37	urequire.True(t, retrievedNFT != nil, "Should retrieve registered NFT")
 38
 39	metadata, ok := retrievedNFT.(grc721.IGRC721CollectionMetadata)
 40	urequire.True(t, ok, "NFT should implement IGRC721CollectionMetadata")
 41	uassert.Equal(t, "Test NFT", metadata.Name(), "NFT name should match")
 42	uassert.Equal(t, "TNFT", metadata.Symbol(), "NFT symbol should match")
 43}
 44
 45func TestGRC1155Registration(t *testing.T) {
 46	testing.SetRealm(std.NewCodeRealm("gno.land/r/matijamarjanovic/home"))
 47
 48	mt := grc1155.NewBasicGRC1155Token("test-uri")
 49	err := RegisterMultiToken(mt.Getter(), "1")
 50	urequire.NoError(t, err, "Should register multi-token without error")
 51
 52	multiToken := GetMultiToken("gno.land/r/matijamarjanovic/home.1")
 53	urequire.True(t, multiToken != nil, "Should retrieve multi-token")
 54	_, ok := multiToken.(grc1155.IGRC1155)
 55	urequire.True(t, ok, "Retrieved multi-token should implement IGRC1155")
 56
 57	err = RegisterMultiToken(mt.Getter(), "1")
 58	uassert.True(t, err != nil, "Should not allow duplicate registration")
 59}
 60
 61func TestBalanceRetrieval(t *testing.T) {
 62	testing.SetRealm(std.NewCodeRealm("gno.land/r/matijamarjanovic/home"))
 63
 64	token, ledger := grc20.NewToken("Test Token", "TEST", 6)
 65	RegisterToken(token.Getter(), "test_tokenn")
 66	ledger.Mint(std.CurrentRealm().Address(), 1000)
 67
 68	mt := grc1155.NewBasicGRC1155Token("test-uri")
 69	RegisterMultiToken(mt.Getter(), "11")
 70	mt.SafeMint(std.CurrentRealm().Address(), grc1155.TokenID("11"), 5)
 71
 72	balances := GetUserTokenBalances(std.CurrentRealm().Address().String())
 73	uassert.True(t, strings.Contains(balances, "Token:gno.land/r/matijamarjanovic/tokenhub.gno.land/r/matijamarjanovic/home.test_tokenn:1000"), "Should show correct GRC20 balance")
 74
 75	balances = GetUserNFTBalances(std.CurrentRealm().Address().String())
 76	uassert.True(t, strings.Contains(balances, "NFT:gno.land/r/matijamarjanovic/home.test_nft.1"), "Should show correct NFT balance") //already minted in test register
 77
 78	balances = GetUserMultiTokenBalances(std.CurrentRealm().Address().String())
 79	uassert.True(t, strings.Contains(balances, "MultiToken:gno.land/r/matijamarjanovic/home.11:5"), "Should show multi-token balance")
 80
 81	nonZeroBalances := GetUserTokenBalancesNonZero(std.CurrentRealm().Address().String())
 82	uassert.True(t, strings.Contains(nonZeroBalances, "Token:gno.land/r/matijamarjanovic/tokenhub.gno.land/r/matijamarjanovic/home.test_tokenn:1000"), "Should show non-zero GRC20 balance")
 83}
 84
 85func TestErrorCases(t *testing.T) {
 86	testing.SetRealm(std.NewCodeRealm("gno.land/r/matijamarjanovic/home"))
 87
 88	nft := grc721.NewBasicNFT("Test NFT", "TNFT")
 89	err := RegisterNFT(nft.Getter(), "test_nft", "1")
 90	uassert.True(t, err != nil, "Should not allow duplicate registration")
 91
 92	err = RegisterNFT(nft.Getter(), "test_nft", "1")
 93	uassert.True(t, err != nil, "Should not allow duplicate registration")
 94
 95	mt := grc1155.NewBasicGRC1155Token("test-uri")
 96	err = RegisterMultiToken(mt.Getter(), "1")
 97	uassert.True(t, err != nil, "Should not allow duplicate registration")
 98
 99	err = RegisterMultiToken(mt.Getter(), "1")
100	uassert.True(t, err != nil, "Should not allow duplicate registration")
101}
102
103func TestTokenListingFunctions(t *testing.T) {
104	testing.SetRealm(std.NewCodeRealm("gno.land/r/matijamarjanovic/home"))
105
106	grc20Token, _ := grc20.NewToken("Test Token", "TEST", 6)
107	RegisterToken(grc20Token.Getter(), "listing_token")
108
109	nftToken := grc721.NewBasicNFT("Listing NFT", "LNFT")
110	nftToken.Mint(std.Address("g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y"), grc721.TokenID("1"))
111	RegisterNFT(nftToken.Getter(), "listing_nft", "1")
112
113	multiToken := grc1155.NewBasicGRC1155Token("test-uri")
114	RegisterMultiToken(multiToken.Getter(), "listing_mt")
115
116	nftList := GetAllNFTs()
117	uassert.True(t, strings.Contains(nftList, "NFT:gno.land/r/matijamarjanovic/home.listing_nft.1"),
118		"GetAllNFTs should list registered NFT")
119
120	grc20List := GetAllTokens()
121	uassert.True(t, strings.Contains(grc20List, "Token:gno.land/r/matijamarjanovic/tokenhub.gno.land/r/matijamarjanovic/home.listing_token"),
122		"GetAllGRC20Tokens should list registered token")
123
124	grc1155List := GetAllMultiTokens()
125	uassert.True(t, strings.Contains(grc1155List, "MultiToken:gno.land/r/matijamarjanovic/home.listing_mt"),
126		"GetAllMultiTokens should list registered multi-token")
127
128	completeList := GetAllRegistered()
129	uassert.True(t, strings.Contains(completeList, "NFT:gno.land/r/matijamarjanovic/home.listing_nft.1"),
130		"GetAllTokens should list NFTs")
131	uassert.True(t, strings.Contains(completeList, "Token:gno.land/r/matijamarjanovic/tokenhub.gno.land/r/matijamarjanovic/home.listing_token"),
132		"GetAllTokens should list GRC20 tokens")
133	uassert.True(t, strings.Contains(completeList, "MultiToken:gno.land/r/matijamarjanovic/home.listing_mt"),
134		"GetAllTokens should list multi-tokens")
135}
136
137func TestMustGetFunctions(t *testing.T) {
138	testing.SetRealm(std.NewCodeRealm("gno.land/r/matijamarjanovic/home"))
139
140	token, _ := grc20.NewToken("Must Token", "MUST", 6)
141	RegisterToken(token.Getter(), "must_token")
142
143	retrievedToken := MustGetToken("gno.land/r/matijamarjanovic/tokenhub.gno.land/r/matijamarjanovic/home.must_token")
144	uassert.Equal(t, "Must Token", retrievedToken.GetName(), "Token name should match")
145
146	defer func() {
147		r := recover()
148		uassert.True(t, r != nil, "MustGetToken should panic for non-existent token")
149		uassert.True(t, strings.Contains(r.(string), "unknown token"), "Panic message should mention unknown token")
150	}()
151	MustGetToken("non_existent_token")
152
153	nft := grc721.NewBasicNFT("Must NFT", "MNFT")
154	nft.Mint(std.CurrentRealm().Address(), grc721.TokenID("1"))
155	RegisterNFT(nft.Getter(), "must_nft", "1")
156
157	retrievedNFT := MustGetNFT("gno.land/r/matijamarjanovic/home.must_nft.1")
158	metadata, ok := retrievedNFT.(grc721.IGRC721CollectionMetadata)
159	urequire.True(t, ok, "NFT should implement IGRC721CollectionMetadata")
160	uassert.Equal(t, "Must NFT", metadata.Name(), "NFT name should match")
161
162	defer func() {
163		r := recover()
164		uassert.True(t, r != nil, "MustGetNFT should panic for non-existent NFT")
165		uassert.True(t, strings.Contains(r.(string), "unknown NFT"), "Panic message should mention unknown NFT")
166	}()
167	MustGetNFT("non_existent_nft")
168
169	mt := grc1155.NewBasicGRC1155Token("must-uri")
170	RegisterMultiToken(mt.Getter(), "must_mt")
171
172	retrievedMT := MustGetMultiToken("gno.land/r/matijamarjanovic/home.must_mt")
173	_, ok = retrievedMT.(grc1155.IGRC1155)
174	urequire.True(t, ok, "Retrieved multi-token should implement IGRC1155")
175
176	defer func() {
177		r := recover()
178		uassert.True(t, r != nil, "MustGetMultiToken should panic for non-existent multi-token")
179		uassert.True(t, strings.Contains(r.(string), "unknown multi-token"), "Panic message should mention unknown multi-token")
180	}()
181	MustGetMultiToken("non_existent_mt")
182}
183
184func TestGetAddressForUsername(t *testing.T) {
185	testing.SetRealm(std.NewCodeRealm("gno.land/r/matijamarjanovic/home"))
186
187	validAddr := "g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y"
188	addr := getAddressForUsername(validAddr)
189	uassert.Equal(t, validAddr, addr.String(), "Should return same address for valid address input")
190
191	invalidInput := "invalid_input"
192	addr = getAddressForUsername(invalidInput)
193	uassert.Equal(t, "", addr.String(), "Should return empty address for invalid input")
194}