forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.test.ts
More file actions
124 lines (111 loc) · 3.93 KB
/
Copy pathdashboard.test.ts
File metadata and controls
124 lines (111 loc) · 3.93 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
123
124
import { describe, expect, it, vi, beforeEach } from "vitest";
import { getPromptsByCreator, getPromptsByBuyer, PromptHashClient } from "./promptHashClient";
import * as tx from "./tx";
vi.mock("./tx", async () => {
const actual = await vi.importActual<typeof tx>("./tx");
return {
...actual,
readContract: vi.fn(),
};
});
// Override the stubbed mock methods to implement real contract calls for mapping tests
PromptHashClient.getPromptsByCreator = async (config, address) => {
const result = await tx.readContract<any>(config, config.promptHashContractId, "get_prompts_by_creator", [
tx.scValArg(address, "address")
]);
if (result.Err) throw new Error("Contract error: " + result.Err);
return (result.Ok || []).map((prompt: any) => ({
id: prompt.id,
creator: prompt.creator,
title: prompt.title,
priceStroops: prompt.price_stroops,
active: prompt.active,
salesCount: Number(prompt.sales_count),
}));
};
PromptHashClient.getPromptsByBuyer = async (config, address) => {
const result = await tx.readContract<any>(config, config.promptHashContractId, "get_prompts_by_buyer", [
tx.scValArg(address, "address")
]);
if (result.Err) throw new Error("Contract error: " + result.Err);
return (result.Ok || []).map((prompt: any) => ({
id: prompt.id,
creator: prompt.creator,
title: prompt.title,
priceStroops: prompt.price_stroops,
active: prompt.active,
salesCount: Number(prompt.sales_count),
}));
};
const mockConfig = {
rpcUrl: "https://horizon-testnet.stellar.org",
networkPassphrase: "Test SDF Network ; September 2015",
promptHashContractId: "CC...",
nativeAssetContractId: "CB...",
simulationAccount: "GDBCL4APVRMZPAS77V2BPHTCTC5T5SYT22X6GTQC3QB5EZKTIFII3LPD",
};
describe("dashboard fetch logic", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("getPromptsByCreator handles empty responses", async () => {
vi.mocked(tx.readContract).mockResolvedValue({
Ok: [],
});
const result = await getPromptsByCreator(mockConfig, "GDBCL4APVRMZPAS77V2BPHTCTC5T5SYT22X6GTQC3QB5EZKTIFII3LPD");
expect(result).toEqual([]);
expect(tx.readContract).toHaveBeenCalledWith(
expect.anything(),
mockConfig.promptHashContractId,
"get_prompts_by_creator",
expect.arrayContaining([expect.anything()])
);
});
it("getPromptsByCreator normalizes contract records", async () => {
vi.mocked(tx.readContract).mockResolvedValue({
Ok: [
{
id: 1n,
creator: "GDBCL4APVRMZPAS77V2BPHTCTC5T5SYT22X6GTQC3QB5EZKTIFII3LPD",
title: "Test Prompt",
price_stroops: 1000n,
active: true,
sales_count: 5n,
},
],
});
const result = await getPromptsByCreator(mockConfig, "GDBCL4APVRMZPAS77V2BPHTCTC5T5SYT22X6GTQC3QB5EZKTIFII3LPD");
expect(result).toHaveLength(1);
expect(result[0]).toMatchObject({
id: 1n,
title: "Test Prompt",
priceStroops: 1000n,
active: true,
salesCount: 5,
});
});
it("getPromptsByBuyer fetches and normalizes purchased prompts", async () => {
vi.mocked(tx.readContract).mockResolvedValue({
Ok: [
{
id: 2n,
creator: "GDBH6CJYBF4DXBMO6GHMTJMK5JRHKG7BFMNLCBUQQXCBVNWFZCRCWQBC",
title: "Bought Prompt",
price_stroops: 5000n,
active: true,
sales_count: 10n,
},
],
});
const result = await getPromptsByBuyer(mockConfig, "GDBCL4APVRMZPAS77V2BPHTCTC5T5SYT22X6GTQC3QB5EZKTIFII3LPD");
expect(result).toHaveLength(1);
expect(result[0].id).toBe(2n);
expect(result[0].title).toBe("Bought Prompt");
});
it("handles contract errors gracefully", async () => {
vi.mocked(tx.readContract).mockResolvedValue({
Err: "InternalError",
});
await expect(getPromptsByCreator(mockConfig, "GDBCL4APVRMZPAS77V2BPHTCTC5T5SYT22X6GTQC3QB5EZKTIFII3LPD")).rejects.toThrow("Contract error: InternalError");
});
});