forked from Dshield-xyz/Dshield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar.test.ts
More file actions
122 lines (108 loc) · 3.79 KB
/
Copy pathstellar.test.ts
File metadata and controls
122 lines (108 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { describe, it, expect, vi, afterEach } from "vitest";
import * as StellarSdk from "@stellar/stellar-sdk";
const ISSUER = "GABZWK2YLPOGBEOZT6VOCID6ROSSZGPSLAEPCTWIBGAJDHISO6DFKYYZ";
const PASSPHRASE = "Standalone Network ; February 2017";
// stellar.ts reads NEXT_PUBLIC_* env at module load, so each scenario reloads
// the module with the desired env (same pattern as indexer.test.ts).
async function loadStellar(issuer = "") {
vi.resetModules();
process.env.NEXT_PUBLIC_USDC_ISSUER = issuer;
process.env.NEXT_PUBLIC_NETWORK_PASSPHRASE = PASSPHRASE;
return await import("./stellar");
}
afterEach(() => {
vi.unstubAllGlobals();
});
describe("getUsdcAsset / getUsdcSacId", () => {
it("returns null when no issuer is configured", async () => {
const s = await loadStellar("");
expect(s.getUsdcAsset()).toBeNull();
expect(s.getUsdcSacId()).toBeNull();
});
it("derives the deterministic SAC id matching the SDK", async () => {
const s = await loadStellar(ISSUER);
const expected = new StellarSdk.Asset("USDC", ISSUER).contractId(PASSPHRASE);
expect(s.getUsdcSacId()).toBe(expected);
// Sanity: a contract (C...) strkey.
expect(StellarSdk.StrKey.isValidContract(s.getUsdcSacId()!)).toBe(true);
});
});
describe("faucetUsdc", () => {
it("no-ops (no request) when no asset is configured", async () => {
const s = await loadStellar("");
const fetchMock = vi.fn();
vi.stubGlobal("fetch", fetchMock);
await s.faucetUsdc(ISSUER, 100);
expect(fetchMock).not.toHaveBeenCalled();
});
it("POSTs the address and amount to /api/faucet", async () => {
const s = await loadStellar(ISSUER);
const fetchMock = vi
.fn()
.mockResolvedValue({ ok: true, json: async () => ({ hash: "h" }) });
vi.stubGlobal("fetch", fetchMock);
await s.faucetUsdc(ISSUER, BigInt(100));
expect(fetchMock).toHaveBeenCalledTimes(1);
const [url, init] = fetchMock.mock.calls[0];
expect(url).toBe("/api/faucet");
expect(init.method).toBe("POST");
expect(JSON.parse(init.body)).toEqual({ address: ISSUER, amount: "100" });
});
it("throws with the server error message on a failed response", async () => {
const s = await loadStellar(ISSUER);
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue({
ok: false,
status: 400,
json: async () => ({ error: "trustline missing" }),
}),
);
await expect(s.faucetUsdc(ISSUER, BigInt(100))).rejects.toThrow("trustline missing");
});
});
describe("relayWithdrawal", () => {
const params = {
poolId: "CBRRTOJWMDAJEYUK2R7MKY6NGABXL52GEKKBXCIUPXOFHOFM5YPIA7WF",
recipient: ISSUER,
publicInputs: "00",
proof: "00",
};
it("returns null on 503 so the caller can fall back to wallet signing", async () => {
const s = await loadStellar(ISSUER);
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue({ status: 503, ok: false, json: async () => ({}) }),
);
expect(await s.relayWithdrawal(params)).toBeNull();
});
it("returns the relay result on success", async () => {
const s = await loadStellar(ISSUER);
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue({
status: 200,
ok: true,
json: async () => ({ hash: "tx123", relayer: "GRELAYER" }),
}),
);
expect(await s.relayWithdrawal(params)).toEqual({
hash: "tx123",
relayer: "GRELAYER",
});
});
it("throws with the server error on a non-ok, non-503 response", async () => {
const s = await loadStellar(ISSUER);
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue({
status: 400,
ok: false,
json: async () => ({ error: "Withdrawal simulation failed" }),
}),
);
await expect(s.relayWithdrawal(params)).rejects.toThrow(
"Withdrawal simulation failed",
);
});
});