forked from Dshield-xyz/Dshield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexer.test.ts
More file actions
69 lines (62 loc) · 2.3 KB
/
Copy pathindexer.test.ts
File metadata and controls
69 lines (62 loc) · 2.3 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
import { describe, it, expect, vi, afterEach } from "vitest";
import * as StellarSdk from "@stellar/stellar-sdk";
afterEach(() => {
vi.resetModules();
vi.doUnmock("./stellar");
});
describe("indexer", () => {
it("syncDepositsFromChain is importable", async () => {
const mod = await import("./indexer");
expect(typeof mod.syncDepositsFromChain).toBe("function");
});
it("syncDepositsFromChain returns 0 when POOL_CONTRACT_ID is empty", async () => {
vi.doMock("./stellar", () => ({
POOL_CONTRACT_ID: "",
getRpcServer: vi.fn(),
queryContract: vi.fn(),
}));
const { syncDepositsFromChain } = await import("./indexer");
const result = await syncDepositsFromChain();
expect(result).toBe(0);
});
});
describe("fetchCommitmentsFromChain", () => {
it("returns null when no pool id is configured", async () => {
vi.doMock("./stellar", () => ({
POOL_CONTRACT_ID: "",
getRpcServer: vi.fn(),
queryContract: vi.fn(),
}));
const { fetchCommitmentsFromChain } = await import("./indexer");
expect(await fetchCommitmentsFromChain()).toBeNull();
});
it("returns null when the contract call fails", async () => {
vi.doMock("./stellar", () => ({
POOL_CONTRACT_ID: "POOL_X",
getRpcServer: vi.fn(),
queryContract: vi.fn().mockResolvedValue(null),
}));
const { fetchCommitmentsFromChain } = await import("./indexer");
expect(await fetchCommitmentsFromChain("POOL_X")).toBeNull();
});
it("returns ordered 0x-prefixed 32-byte hex commitments", async () => {
const leaf0 = new Uint8Array(32).fill(0);
leaf0[31] = 0xaa;
const leaf1 = new Uint8Array(32).fill(0);
leaf1[31] = 0xbb;
// get_commitments returns an ScVec of ScBytes; build it so scValToNative
// yields the array of byte buffers the function expects.
const scVal = StellarSdk.nativeToScVal([Buffer.from(leaf0), Buffer.from(leaf1)]);
vi.doMock("./stellar", () => ({
POOL_CONTRACT_ID: "POOL_X",
getRpcServer: vi.fn(),
queryContract: vi.fn().mockResolvedValue(scVal),
}));
const { fetchCommitmentsFromChain } = await import("./indexer");
const result = await fetchCommitmentsFromChain("POOL_X");
expect(result).toEqual([
"0x" + "00".repeat(31) + "aa",
"0x" + "00".repeat(31) + "bb",
]);
});
});