Skip to content

Commit 51c0dcf

Browse files
committed
test(sdk): add unit tests for getReferralStats hex parsing and contract calls
1 parent 51a652a commit 51c0dcf

1 file changed

Lines changed: 142 additions & 0 deletions

File tree

sdk/tests/referralStats.test.ts

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { vi, describe, it, expect, beforeEach } from "vitest";
2+
import { getReferralStats } from "../src/methods/referralStats.js";
3+
import {
4+
SorobanRpc,
5+
xdr,
6+
} from "@stellar/stellar-sdk";
7+
8+
function mockSimulationResult(retval: xdr.ScVal | null) {
9+
return {
10+
result: retval ? { retval } : undefined,
11+
};
12+
}
13+
14+
describe("getReferralStats", () => {
15+
const mockServer = {
16+
simulateTransaction: vi.fn(),
17+
} as unknown as SorobanRpc.Server;
18+
19+
const CONTRACT_ID = "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSC4";
20+
const VALID_HEX = "ab" + "01".repeat(31); // 64 hex chars (32 bytes)
21+
const VALID_HEX_WITH_PREFIX = "0x" + VALID_HEX;
22+
23+
beforeEach(() => {
24+
vi.clearAllMocks();
25+
});
26+
27+
// ---------------------------------------------------------------------------
28+
// Success path
29+
// ---------------------------------------------------------------------------
30+
31+
it("returns the referral count for a valid hex code", async () => {
32+
const retval = xdr.ScVal.scvU64(xdr.Uint64.fromString("42"));
33+
vi.mocked(mockServer.simulateTransaction).mockResolvedValue(
34+
mockSimulationResult(retval) as any
35+
);
36+
37+
const count = await getReferralStats(
38+
mockServer,
39+
CONTRACT_ID,
40+
VALID_HEX
41+
);
42+
43+
expect(count).toBe(42);
44+
expect(mockServer.simulateTransaction).toHaveBeenCalledTimes(1);
45+
});
46+
47+
it("strips the 0x prefix and returns the referral count", async () => {
48+
const retval = xdr.ScVal.scvU64(xdr.Uint64.fromString("7"));
49+
vi.mocked(mockServer.simulateTransaction).mockResolvedValue(
50+
mockSimulationResult(retval) as any
51+
);
52+
53+
const count = await getReferralStats(
54+
mockServer,
55+
CONTRACT_ID,
56+
VALID_HEX_WITH_PREFIX
57+
);
58+
59+
expect(count).toBe(7);
60+
});
61+
62+
// ---------------------------------------------------------------------------
63+
// Zero default
64+
// ---------------------------------------------------------------------------
65+
66+
it("returns 0 when the simulation returns no retval", async () => {
67+
vi.mocked(mockServer.simulateTransaction).mockResolvedValue(
68+
mockSimulationResult(null) as any
69+
);
70+
71+
const count = await getReferralStats(
72+
mockServer,
73+
CONTRACT_ID,
74+
VALID_HEX
75+
);
76+
77+
expect(count).toBe(0);
78+
});
79+
80+
// ---------------------------------------------------------------------------
81+
// Hex validation errors
82+
// ---------------------------------------------------------------------------
83+
84+
it("throws when the hex string is too short", async () => {
85+
await expect(
86+
getReferralStats(mockServer, CONTRACT_ID, "abc123")
87+
).rejects.toThrow(/invalid referral code/i);
88+
expect(mockServer.simulateTransaction).not.toHaveBeenCalled();
89+
});
90+
91+
it("throws when the hex string is too long", async () => {
92+
await expect(
93+
getReferralStats(mockServer, CONTRACT_ID, VALID_HEX + "ff")
94+
).rejects.toThrow(/invalid referral code/i);
95+
expect(mockServer.simulateTransaction).not.toHaveBeenCalled();
96+
});
97+
98+
it("throws when the hex string contains non-hex characters", async () => {
99+
await expect(
100+
getReferralStats(mockServer, CONTRACT_ID, "z" + "01".repeat(31) + "xy")
101+
).rejects.toThrow(/invalid referral code/i);
102+
expect(mockServer.simulateTransaction).not.toHaveBeenCalled();
103+
});
104+
105+
it("throws when the hex string is empty", async () => {
106+
await expect(
107+
getReferralStats(mockServer, CONTRACT_ID, "")
108+
).rejects.toThrow(/invalid referral code/i);
109+
expect(mockServer.simulateTransaction).not.toHaveBeenCalled();
110+
});
111+
112+
it("throws when given an invalid 0x-prefixed hex string", async () => {
113+
await expect(
114+
getReferralStats(mockServer, CONTRACT_ID, "0x123")
115+
).rejects.toThrow(/invalid referral code/i);
116+
expect(mockServer.simulateTransaction).not.toHaveBeenCalled();
117+
});
118+
119+
// ---------------------------------------------------------------------------
120+
// Simulation errors
121+
// ---------------------------------------------------------------------------
122+
123+
it("throws when the simulation returns an error", async () => {
124+
vi.mocked(mockServer.simulateTransaction).mockResolvedValue({
125+
error: "Contract error: not found",
126+
} as any);
127+
128+
await expect(
129+
getReferralStats(mockServer, CONTRACT_ID, VALID_HEX)
130+
).rejects.toThrow(/simulation failed/i);
131+
});
132+
133+
it("throws when the simulation fails with a SorobanRPC error", async () => {
134+
vi.mocked(mockServer.simulateTransaction).mockResolvedValue({
135+
error: "Contract error #4",
136+
} as any);
137+
138+
await expect(
139+
getReferralStats(mockServer, CONTRACT_ID, VALID_HEX)
140+
).rejects.toThrow(/get_referral_stats simulation failed/i);
141+
});
142+
});

0 commit comments

Comments
 (0)