|
| 1 | +import { describe, it, expect, beforeEach, vi } from "vitest"; |
| 2 | +import { Account, SorobanRpc } from "@stellar/stellar-sdk"; |
| 3 | +import { getNftMetadata, getNftOwner } from "./nft.js"; |
| 4 | +import type { InvoiceNftMetadata } from "../utils/xdrDecoder.js"; |
| 5 | +import { ILNError } from "../errors.js"; |
| 6 | + |
| 7 | +describe("NFT Query Methods", () => { |
| 8 | + let server: SorobanRpc.Server; |
| 9 | + let sourceAccount: Account; |
| 10 | + const contractAddress = "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF"; |
| 11 | + const networkPassphrase = "Test SDF Network ; September 2015"; |
| 12 | + const invoiceId = 42n; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + // Create a mock server |
| 16 | + server = { |
| 17 | + simulateTransaction: vi.fn(), |
| 18 | + } as unknown as SorobanRpc.Server; |
| 19 | + |
| 20 | + // Create a mock source account |
| 21 | + sourceAccount = new Account( |
| 22 | + "GBRPYHIL2CI3WHZDTOOQFC6EB4RBDAPPLYAT2FOSSYOWLCDTIR35IWJP", |
| 23 | + "0" |
| 24 | + ); |
| 25 | + }); |
| 26 | + |
| 27 | + describe("getNftMetadata", () => { |
| 28 | + it("should return NFT metadata when NFT exists", async () => { |
| 29 | + const mockMetadata: InvoiceNftMetadata = { |
| 30 | + invoiceId: 42n, |
| 31 | + amount: 1000000n, |
| 32 | + dueDate: 1704067200, |
| 33 | + discountRate: 300, |
| 34 | + token: "CCJZ375JREG7DSHBG44D7ZLBFOXQFSWBM3L4AZXC3NZYBV3MYQ4GOHB", |
| 35 | + owner: "GBRPYHIL2CI3WHZDTOOQFC6EB4RBDAPPLYAT2FOSSYOWLCDTIR35IWJP", |
| 36 | + mintedAt: 1704067200, |
| 37 | + }; |
| 38 | + |
| 39 | + // Mock successful simulation |
| 40 | + vi.mocked(server.simulateTransaction).mockResolvedValueOnce({ |
| 41 | + result: { |
| 42 | + retval: { |
| 43 | + type: "obj", |
| 44 | + fields: [ |
| 45 | + { key: { type: "sym", sym: "invoice_id" }, val: { type: "u64", u64: "42" } }, |
| 46 | + { key: { type: "sym", sym: "amount" }, val: { type: "i128", i128: "1000000" } }, |
| 47 | + { key: { type: "sym", sym: "due_date" }, val: { type: "u32", u32: "1704067200" } }, |
| 48 | + { key: { type: "sym", sym: "discount_rate" }, val: { type: "u32", u32: "300" } }, |
| 49 | + { key: { type: "sym", sym: "token" }, val: { type: "addr", addr: "CCJZ375JREG7DSHBG44D7ZLBFOXQFSWBM3L4AZXC3NZYBV3MYQ4GOHB" } }, |
| 50 | + { key: { type: "sym", sym: "owner" }, val: { type: "addr", addr: "GBRPYHIL2CI3WHZDTOOQFC6EB4RBDAPPLYAT2FOSSYOWLCDTIR35IWJP" } }, |
| 51 | + { key: { type: "sym", sym: "minted_at" }, val: { type: "u32", u32: "1704067200" } }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + }, |
| 55 | + } as any); |
| 56 | + |
| 57 | + const result = await getNftMetadata( |
| 58 | + server, |
| 59 | + contractAddress, |
| 60 | + invoiceId, |
| 61 | + sourceAccount, |
| 62 | + networkPassphrase |
| 63 | + ); |
| 64 | + |
| 65 | + expect(result).not.toBeNull(); |
| 66 | + expect(result?.invoiceId).toBe(42n); |
| 67 | + expect(result?.amount).toBe(1000000n); |
| 68 | + expect(result?.discountRate).toBe(300); |
| 69 | + expect(result?.owner).toBe("GBRPYHIL2CI3WHZDTOOQFC6EB4RBDAPPLYAT2FOSSYOWLCDTIR35IWJP"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should return null when NFT does not exist", async () => { |
| 73 | + // Mock simulation returning null for Option::None |
| 74 | + vi.mocked(server.simulateTransaction).mockResolvedValueOnce({ |
| 75 | + result: { |
| 76 | + retval: null, |
| 77 | + }, |
| 78 | + } as any); |
| 79 | + |
| 80 | + const result = await getNftMetadata( |
| 81 | + server, |
| 82 | + contractAddress, |
| 83 | + invoiceId, |
| 84 | + sourceAccount, |
| 85 | + networkPassphrase |
| 86 | + ); |
| 87 | + |
| 88 | + expect(result).toBeNull(); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should throw ILNError on simulation error", async () => { |
| 92 | + // Mock simulation error |
| 93 | + vi.mocked(server.simulateTransaction).mockResolvedValueOnce({ |
| 94 | + error: new Error("Simulation failed"), |
| 95 | + } as any); |
| 96 | + |
| 97 | + await expect( |
| 98 | + getNftMetadata(server, contractAddress, invoiceId, sourceAccount, networkPassphrase) |
| 99 | + ).rejects.toThrow(ILNError); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe("getNftOwner", () => { |
| 104 | + it("should return owner address when NFT exists", async () => { |
| 105 | + const ownerAddress = "GBRPYHIL2CI3WHZDTOOQFC6EB4RBDAPPLYAT2FOSSYOWLCDTIR35IWJP"; |
| 106 | + |
| 107 | + // Mock successful simulation |
| 108 | + vi.mocked(server.simulateTransaction).mockResolvedValueOnce({ |
| 109 | + result: { |
| 110 | + retval: { |
| 111 | + type: "addr", |
| 112 | + addr: ownerAddress, |
| 113 | + }, |
| 114 | + }, |
| 115 | + } as any); |
| 116 | + |
| 117 | + const result = await getNftOwner( |
| 118 | + server, |
| 119 | + contractAddress, |
| 120 | + invoiceId, |
| 121 | + sourceAccount, |
| 122 | + networkPassphrase |
| 123 | + ); |
| 124 | + |
| 125 | + expect(result).toBe(ownerAddress); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should return null when NFT does not exist", async () => { |
| 129 | + // Mock simulation returning null for Option::None |
| 130 | + vi.mocked(server.simulateTransaction).mockResolvedValueOnce({ |
| 131 | + result: { |
| 132 | + retval: null, |
| 133 | + }, |
| 134 | + } as any); |
| 135 | + |
| 136 | + const result = await getNftOwner( |
| 137 | + server, |
| 138 | + contractAddress, |
| 139 | + invoiceId, |
| 140 | + sourceAccount, |
| 141 | + networkPassphrase |
| 142 | + ); |
| 143 | + |
| 144 | + expect(result).toBeNull(); |
| 145 | + }); |
| 146 | + |
| 147 | + it("should throw ILNError on simulation error", async () => { |
| 148 | + // Mock simulation error |
| 149 | + vi.mocked(server.simulateTransaction).mockResolvedValueOnce({ |
| 150 | + error: new Error("Simulation failed"), |
| 151 | + } as any); |
| 152 | + |
| 153 | + await expect( |
| 154 | + getNftOwner(server, contractAddress, invoiceId, sourceAccount, networkPassphrase) |
| 155 | + ).rejects.toThrow(ILNError); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments