forked from Chainmove/Chain-move
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivity.test.ts
More file actions
151 lines (131 loc) · 4.61 KB
/
Copy pathactivity.test.ts
File metadata and controls
151 lines (131 loc) · 4.61 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { NextResponse } from "next/server"
import { beforeEach, describe, expect, it, vi } from "vitest"
const { requireAuthenticatedUser, finalizeAuthenticatedResponse, getStellarConfig, find, loadAccount } = vi.hoisted(() => ({
requireAuthenticatedUser: vi.fn(),
finalizeAuthenticatedResponse: vi.fn(async (response: unknown) => response),
getStellarConfig: vi.fn(),
find: vi.fn(),
loadAccount: vi.fn(),
}))
vi.mock("@/lib/api/route-guard", () => ({ requireAuthenticatedUser, finalizeAuthenticatedResponse }))
vi.mock("@/lib/dbConnect", () => ({ default: vi.fn() }))
vi.mock("@/lib/stellar/config", () => ({ getStellarConfig }))
vi.mock("@/lib/stellar/client", () => ({
getStellarClient: vi.fn(() => ({
horizon: {
loadAccount,
},
})),
}))
vi.mock("@/models/StellarIndexedEvent", () => ({
default: {
find,
},
}))
import { GET } from "@/app/api/stellar/activity/route"
function buildRequest() {
return new Request("http://localhost/api/stellar/activity", {
method: "GET",
})
}
function buildUser(overrides: Record<string, unknown> = {}): Record<string, any> {
return {
_id: "user-1",
name: "Test User",
email: "test@example.com",
role: "investor",
stellarPublicKey: "GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H",
...overrides,
}
}
describe("GET /api/stellar/activity", () => {
beforeEach(() => {
requireAuthenticatedUser.mockReset()
finalizeAuthenticatedResponse.mockClear()
getStellarConfig.mockReset()
find.mockReset()
loadAccount.mockReset()
})
it("returns 401/error if not authenticated", async () => {
requireAuthenticatedUser.mockResolvedValue({
response: NextResponse.json({ message: "Unauthorized" }, { status: 401 }),
})
const response = await GET(buildRequest())
expect(response.status).toBe(401)
})
it("returns mock data when mock mode is enabled", async () => {
const user = buildUser()
requireAuthenticatedUser.mockResolvedValue({ user })
getStellarConfig.mockReturnValue({
mock: true,
network: "testnet",
horizonUrl: "https://horizon-testnet.stellar.org",
rpcUrl: "https://soroban-testnet.stellar.org",
contractId: "C123",
issuerPublicKey: "GD123",
})
const response = await GET(buildRequest())
const payload = await response.json()
expect(response.status).toBe(200)
expect(payload.mock).toBe(true)
expect(payload.balances.length).toBeGreaterThan(0)
expect(payload.activities.length).toBeGreaterThan(0)
expect(loadAccount).not.toHaveBeenCalled()
})
it("returns empty balances/activities if live mode but no public key is linked", async () => {
const user = buildUser({ stellarPublicKey: null })
requireAuthenticatedUser.mockResolvedValue({ user })
getStellarConfig.mockReturnValue({
mock: false,
network: "testnet",
})
const response = await GET(buildRequest())
const payload = await response.json()
expect(response.status).toBe(200)
expect(payload.balances).toEqual([])
expect(payload.activities).toEqual([])
})
it("fetches live balances and database events when live mode and key is linked", async () => {
const user = buildUser()
requireAuthenticatedUser.mockResolvedValue({ user })
getStellarConfig.mockReturnValue({
mock: false,
network: "testnet",
})
loadAccount.mockResolvedValue({
balances: [
{ asset_type: "native", balance: "150.00" },
{ asset_type: "credit_alphanum4", asset_code: "USDC", balance: "20.00", asset_issuer: "GDUSDC" },
],
})
find.mockReturnValue({
sort: vi.fn().mockReturnValue({
limit: vi.fn().mockReturnValue({
lean: vi.fn().mockResolvedValue([
{
_id: "tx-1",
chainMoveRecordType: "repayment",
eventType: "payment",
amount: "100.00",
asset: "CMOVE",
sourceAccount: user.stellarPublicKey,
destinationAccount: "GBX",
createdAt: new Date(),
raw: { transaction_hash: "hash-1" },
},
]),
}),
}),
})
const response = await GET(buildRequest())
const payload = await response.json()
expect(response.status).toBe(200)
expect(loadAccount).toHaveBeenCalledWith(user.stellarPublicKey)
expect(payload.balances).toEqual([
{ asset: "XLM", balance: "150.00", type: "native", issuer: null },
{ asset: "USDC", balance: "20.00", type: "credit_alphanum4", issuer: "GDUSDC" },
])
expect(payload.activities[0].id).toBe("tx-1")
expect(payload.activities[0].title).toBe("Repayment Settlement")
})
})