forked from Movalabs-crew/mova-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.test.ts
More file actions
81 lines (70 loc) · 2.93 KB
/
Copy patherrors.test.ts
File metadata and controls
81 lines (70 loc) · 2.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
import { describe, it, expect } from "vitest";
import {
parseError,
STELLAR_ERRORS,
createError,
getUserMessage,
isRecoverable,
} from "../../lib/errors";
import { WalletError } from "../../lib/stellar/freighter";
describe("Error Handling & Stellar Error Reconciliation Tests", () => {
it("classifies WalletError instances by their .code property", () => {
const errFreighterNotFound = new WalletError("Extension missing", "FREIGHTER_NOT_FOUND");
const parsedFreighterNotFound = parseError(errFreighterNotFound);
expect(parsedFreighterNotFound.code).toBe("WALLET_NOT_INSTALLED");
expect(parsedFreighterNotFound.userMessage).toContain("Freighter wallet extension");
const errWrongNetwork = new WalletError("Wrong net detected", "WRONG_NETWORK");
const parsedWrongNetwork = parseError(errWrongNetwork);
expect(parsedWrongNetwork.code).toBe("WALLET_WRONG_NETWORK");
const errTxSend = new WalletError("Tx failed", "TX_SEND_ERROR");
const parsedTxSend = parseError(errTxSend);
expect(parsedTxSend.code).toBe("TX_FAILED");
const errSim = new WalletError("Sim failed", "TX_SIMULATION_ERROR");
const parsedSim = parseError(errSim);
expect(parsedSim.code).toBe("TX_SIMULATION_FAILED");
const errContract = new WalletError("No contract ID", "CONTRACT_NOT_CONFIGURED");
const parsedContract = parseError(errContract);
expect(parsedContract.code).toBe("STELLAR_CONTRACT_NOT_CONFIGURED");
});
it("ensures every STELLAR_ERRORS code maps to a defined and emitted code key", () => {
const expectedKeys = [
"FREIGHTER_NOT_FOUND",
"FREIGHTER_REQUEST_DENIED",
"FREIGHTER_NETWORK_ERROR",
"WRONG_NETWORK",
"FREIGHTER_SIGN_ERROR",
"USER_REJECTED",
"ACCOUNT_NOT_FOUND",
"FRIENDBOT_ERROR",
"PAYMENT_NOT_READY",
"INSUFFICIENT_BALANCE",
"NO_TRUSTLINE",
"CONTRACT_NOT_CONFIGURED",
"INVALID_AMOUNT",
"TX_SIMULATION_ERROR",
"TX_SEND_ERROR",
"TX_TIMEOUT",
];
for (const key of expectedKeys) {
expect(STELLAR_ERRORS[key]).toBeDefined();
expect(STELLAR_ERRORS[key].code).toBeTruthy();
expect(STELLAR_ERRORS[key].userMessage).toBeTruthy();
}
});
it("handles string messages, null/undefined, and custom AppErrors properly", () => {
expect(parseError(null).code).toBe("UNKNOWN_ERROR");
expect(parseError(undefined).code).toBe("UNKNOWN_ERROR");
const custom = createError("CUSTOM_CODE", "Internal msg", "Friendly msg", {
severity: "warning",
recoverable: false,
});
expect(custom.code).toBe("CUSTOM_CODE");
expect(custom.userMessage).toBe("Friendly msg");
expect(custom.severity).toBe("warning");
expect(custom.recoverable).toBe(false);
expect(getUserMessage(new WalletError("Funds low", "INSUFFICIENT_BALANCE"))).toContain(
"don't have enough funds"
);
expect(isRecoverable(new WalletError("Wrong net", "WRONG_NETWORK"))).toBe(true);
});
});