Skip to content

Commit 8f5867c

Browse files
committed
test: add unit test for backend wallet
1 parent a176827 commit 8f5867c

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { TEST_CLIENT } from "~test/test-clients.js";
3+
import { getClientFetch } from "../../../../utils/fetch.js";
4+
import { stringify } from "../../../../utils/json.js";
5+
import { backendAuthenticate } from "./backend.js";
6+
import { getLoginUrl } from "./getLoginPath.js";
7+
8+
// Mock dependencies
9+
vi.mock("../../../../utils/fetch.js");
10+
vi.mock("./getLoginPath.js");
11+
12+
describe("backendAuthenticate", () => {
13+
it("should successfully authenticate and return token", async () => {
14+
// Mock response data
15+
const mockResponse = {
16+
token: "mock-token",
17+
cookieString: "mock-cookie",
18+
};
19+
20+
// Mock fetch implementation
21+
const mockFetch = vi.fn().mockResolvedValue({
22+
ok: true,
23+
json: () => Promise.resolve(mockResponse),
24+
});
25+
26+
// Mock dependencies
27+
vi.mocked(getClientFetch).mockReturnValue(mockFetch);
28+
vi.mocked(getLoginUrl).mockReturnValue("/auth/login");
29+
30+
const result = await backendAuthenticate({
31+
client: TEST_CLIENT,
32+
walletSecret: "test-secret",
33+
});
34+
35+
// Verify the fetch call
36+
expect(mockFetch).toHaveBeenCalledWith("/auth/login", {
37+
method: "POST",
38+
headers: {
39+
"Content-Type": "application/json",
40+
},
41+
body: stringify({ walletSecret: "test-secret" }),
42+
});
43+
44+
// Verify response
45+
expect(result).toEqual(mockResponse);
46+
});
47+
48+
it("should throw error when authentication fails", async () => {
49+
// Mock failed fetch response
50+
const mockFetch = vi.fn().mockResolvedValue({
51+
ok: false,
52+
});
53+
54+
// Mock dependencies
55+
vi.mocked(getClientFetch).mockReturnValue(mockFetch);
56+
vi.mocked(getLoginUrl).mockReturnValue("/auth/login");
57+
58+
// Test inputs
59+
const args = {
60+
client: TEST_CLIENT,
61+
walletSecret: "test-secret",
62+
};
63+
64+
// Verify error is thrown
65+
await expect(backendAuthenticate(args)).rejects.toThrow(
66+
"Failed to generate backend account",
67+
);
68+
});
69+
});

0 commit comments

Comments
 (0)