@frak-labs/core-sdk 0.0.19 → 0.1.0-beta.00226d62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/cdn/bundle.iife.js +14 -0
  2. package/dist/actions-CEEObPYc.js +1 -0
  3. package/dist/actions-DbQhWYx8.cjs +1 -0
  4. package/dist/actions.cjs +1 -1
  5. package/dist/actions.d.cts +3 -1400
  6. package/dist/actions.d.ts +3 -1400
  7. package/dist/actions.js +1 -1
  8. package/dist/bundle.cjs +1 -13
  9. package/dist/bundle.d.cts +6 -2022
  10. package/dist/bundle.d.ts +6 -2022
  11. package/dist/bundle.js +1 -13
  12. package/dist/index-7OZ39x1U.d.ts +195 -0
  13. package/dist/index-C6FxkWPC.d.cts +511 -0
  14. package/dist/index-UFX7xCg3.d.ts +351 -0
  15. package/dist/index-d8xS4ryI.d.ts +511 -0
  16. package/dist/index-p4FqSp8z.d.cts +351 -0
  17. package/dist/index-zDq-VlKx.d.cts +195 -0
  18. package/dist/index.cjs +1 -13
  19. package/dist/index.d.cts +4 -1373
  20. package/dist/index.d.ts +4 -1373
  21. package/dist/index.js +1 -13
  22. package/dist/interaction-DMJ3ZfaF.d.cts +45 -0
  23. package/dist/interaction-KX1h9a7V.d.ts +45 -0
  24. package/dist/interactions-DnfM3oe0.js +1 -0
  25. package/dist/interactions-EIXhNLf6.cjs +1 -0
  26. package/dist/interactions.cjs +1 -1
  27. package/dist/interactions.d.cts +2 -182
  28. package/dist/interactions.d.ts +2 -182
  29. package/dist/interactions.js +1 -1
  30. package/dist/openSso-D--Airj6.d.cts +1018 -0
  31. package/dist/openSso-DsKJ4y0j.d.ts +1018 -0
  32. package/dist/productTypes-BUkXJKZ7.cjs +1 -0
  33. package/dist/productTypes-CGb1MmBF.js +1 -0
  34. package/dist/src-B_xO0AR6.cjs +13 -0
  35. package/dist/src-D2d52OZa.js +13 -0
  36. package/dist/trackEvent-CHnYa85W.js +1 -0
  37. package/dist/trackEvent-GuQm_1Nm.cjs +1 -0
  38. package/package.json +27 -18
  39. package/src/actions/displayEmbeddedWallet.test.ts +194 -0
  40. package/src/actions/displayEmbeddedWallet.ts +20 -0
  41. package/src/actions/displayModal.test.ts +387 -0
  42. package/src/actions/displayModal.ts +131 -0
  43. package/src/actions/getProductInformation.test.ts +133 -0
  44. package/src/actions/getProductInformation.ts +14 -0
  45. package/src/actions/index.ts +29 -0
  46. package/src/actions/openSso.test.ts +407 -0
  47. package/src/actions/openSso.ts +116 -0
  48. package/src/actions/prepareSso.test.ts +223 -0
  49. package/src/actions/prepareSso.ts +48 -0
  50. package/src/actions/referral/processReferral.test.ts +357 -0
  51. package/src/actions/referral/processReferral.ts +230 -0
  52. package/src/actions/referral/referralInteraction.test.ts +153 -0
  53. package/src/actions/referral/referralInteraction.ts +57 -0
  54. package/src/actions/sendInteraction.test.ts +219 -0
  55. package/src/actions/sendInteraction.ts +32 -0
  56. package/src/actions/trackPurchaseStatus.test.ts +287 -0
  57. package/src/actions/trackPurchaseStatus.ts +53 -0
  58. package/src/actions/watchWalletStatus.test.ts +372 -0
  59. package/src/actions/watchWalletStatus.ts +94 -0
  60. package/src/actions/wrapper/modalBuilder.test.ts +253 -0
  61. package/src/actions/wrapper/modalBuilder.ts +212 -0
  62. package/src/actions/wrapper/sendTransaction.test.ts +164 -0
  63. package/src/actions/wrapper/sendTransaction.ts +62 -0
  64. package/src/actions/wrapper/siweAuthenticate.test.ts +290 -0
  65. package/src/actions/wrapper/siweAuthenticate.ts +94 -0
  66. package/src/bundle.ts +3 -0
  67. package/src/clients/DebugInfo.test.ts +418 -0
  68. package/src/clients/DebugInfo.ts +182 -0
  69. package/src/clients/createIFrameFrakClient.ts +287 -0
  70. package/src/clients/index.ts +3 -0
  71. package/src/clients/setupClient.test.ts +343 -0
  72. package/src/clients/setupClient.ts +73 -0
  73. package/src/clients/transports/iframeLifecycleManager.test.ts +399 -0
  74. package/src/clients/transports/iframeLifecycleManager.ts +90 -0
  75. package/src/constants/interactionTypes.test.ts +128 -0
  76. package/src/constants/interactionTypes.ts +44 -0
  77. package/src/constants/locales.ts +14 -0
  78. package/src/constants/productTypes.test.ts +130 -0
  79. package/src/constants/productTypes.ts +33 -0
  80. package/src/index.ts +101 -0
  81. package/src/interactions/index.ts +5 -0
  82. package/src/interactions/pressEncoder.test.ts +215 -0
  83. package/src/interactions/pressEncoder.ts +53 -0
  84. package/src/interactions/purchaseEncoder.test.ts +291 -0
  85. package/src/interactions/purchaseEncoder.ts +99 -0
  86. package/src/interactions/referralEncoder.test.ts +170 -0
  87. package/src/interactions/referralEncoder.ts +47 -0
  88. package/src/interactions/retailEncoder.test.ts +107 -0
  89. package/src/interactions/retailEncoder.ts +37 -0
  90. package/src/interactions/webshopEncoder.test.ts +56 -0
  91. package/src/interactions/webshopEncoder.ts +30 -0
  92. package/src/types/client.ts +14 -0
  93. package/src/types/compression.ts +22 -0
  94. package/src/types/config.ts +111 -0
  95. package/src/types/context.ts +13 -0
  96. package/src/types/index.ts +71 -0
  97. package/src/types/lifecycle/client.ts +46 -0
  98. package/src/types/lifecycle/iframe.ts +35 -0
  99. package/src/types/lifecycle/index.ts +2 -0
  100. package/src/types/rpc/displayModal.ts +84 -0
  101. package/src/types/rpc/embedded/index.ts +68 -0
  102. package/src/types/rpc/embedded/loggedIn.ts +55 -0
  103. package/src/types/rpc/embedded/loggedOut.ts +28 -0
  104. package/src/types/rpc/interaction.ts +43 -0
  105. package/src/types/rpc/modal/final.ts +46 -0
  106. package/src/types/rpc/modal/generic.ts +46 -0
  107. package/src/types/rpc/modal/index.ts +20 -0
  108. package/src/types/rpc/modal/login.ts +32 -0
  109. package/src/types/rpc/modal/openSession.ts +25 -0
  110. package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
  111. package/src/types/rpc/modal/transaction.ts +33 -0
  112. package/src/types/rpc/productInformation.ts +59 -0
  113. package/src/types/rpc/sso.ts +80 -0
  114. package/src/types/rpc/walletStatus.ts +35 -0
  115. package/src/types/rpc.ts +158 -0
  116. package/src/types/transport.ts +34 -0
  117. package/src/utils/FrakContext.test.ts +407 -0
  118. package/src/utils/FrakContext.ts +158 -0
  119. package/src/utils/compression/b64.test.ts +181 -0
  120. package/src/utils/compression/b64.ts +29 -0
  121. package/src/utils/compression/compress.test.ts +123 -0
  122. package/src/utils/compression/compress.ts +11 -0
  123. package/src/utils/compression/decompress.test.ts +145 -0
  124. package/src/utils/compression/decompress.ts +11 -0
  125. package/src/utils/compression/index.ts +3 -0
  126. package/src/utils/computeProductId.test.ts +80 -0
  127. package/src/utils/computeProductId.ts +11 -0
  128. package/src/utils/constants.test.ts +23 -0
  129. package/src/utils/constants.ts +4 -0
  130. package/src/utils/formatAmount.test.ts +113 -0
  131. package/src/utils/formatAmount.ts +18 -0
  132. package/src/utils/getCurrencyAmountKey.test.ts +44 -0
  133. package/src/utils/getCurrencyAmountKey.ts +15 -0
  134. package/src/utils/getSupportedCurrency.test.ts +51 -0
  135. package/src/utils/getSupportedCurrency.ts +14 -0
  136. package/src/utils/getSupportedLocale.test.ts +64 -0
  137. package/src/utils/getSupportedLocale.ts +16 -0
  138. package/src/utils/iframeHelper.test.ts +450 -0
  139. package/src/utils/iframeHelper.ts +143 -0
  140. package/src/utils/index.ts +21 -0
  141. package/src/utils/sso.test.ts +361 -0
  142. package/src/utils/sso.ts +119 -0
  143. package/src/utils/ssoUrlListener.test.ts +252 -0
  144. package/src/utils/ssoUrlListener.ts +60 -0
  145. package/src/utils/trackEvent.test.ts +162 -0
  146. package/src/utils/trackEvent.ts +26 -0
  147. package/cdn/bundle.js +0 -19
  148. package/cdn/bundle.js.LICENSE.txt +0 -10
@@ -0,0 +1,287 @@
1
+ /**
2
+ * Tests for trackPurchaseStatus action
3
+ * Tests webhook registration for purchase tracking
4
+ */
5
+
6
+ import {
7
+ afterEach,
8
+ beforeEach,
9
+ describe,
10
+ expect,
11
+ it,
12
+ vi,
13
+ } from "../../tests/vitest-fixtures";
14
+ import { trackPurchaseStatus } from "./trackPurchaseStatus";
15
+
16
+ describe("trackPurchaseStatus", () => {
17
+ let mockSessionStorage: {
18
+ getItem: ReturnType<typeof vi.fn>;
19
+ setItem: ReturnType<typeof vi.fn>;
20
+ removeItem: ReturnType<typeof vi.fn>;
21
+ };
22
+ let fetchSpy: any;
23
+ let consoleWarnSpy: any;
24
+
25
+ beforeEach(() => {
26
+ // Mock sessionStorage
27
+ mockSessionStorage = {
28
+ getItem: vi.fn(),
29
+ setItem: vi.fn(),
30
+ removeItem: vi.fn(),
31
+ };
32
+ Object.defineProperty(window, "sessionStorage", {
33
+ value: mockSessionStorage,
34
+ writable: true,
35
+ configurable: true,
36
+ });
37
+
38
+ // Mock fetch
39
+ fetchSpy = vi.fn().mockResolvedValue({
40
+ ok: true,
41
+ status: 200,
42
+ });
43
+ global.fetch = fetchSpy;
44
+
45
+ // Mock console.warn
46
+ consoleWarnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
47
+ });
48
+
49
+ afterEach(() => {
50
+ vi.clearAllMocks();
51
+ consoleWarnSpy.mockRestore();
52
+ });
53
+
54
+ describe("successful tracking", () => {
55
+ it("should send POST request with correct parameters", async () => {
56
+ mockSessionStorage.getItem.mockReturnValue("token-123");
57
+
58
+ await trackPurchaseStatus({
59
+ customerId: "cust-456",
60
+ orderId: "order-789",
61
+ token: "purchase-token",
62
+ });
63
+
64
+ expect(fetchSpy).toHaveBeenCalledWith(
65
+ "https://backend.frak.id/interactions/listenForPurchase",
66
+ {
67
+ method: "POST",
68
+ headers: {
69
+ Accept: "application/json",
70
+ "Content-Type": "application/json",
71
+ "x-wallet-sdk-auth": "token-123",
72
+ },
73
+ body: JSON.stringify({
74
+ customerId: "cust-456",
75
+ orderId: "order-789",
76
+ token: "purchase-token",
77
+ }),
78
+ }
79
+ );
80
+ });
81
+
82
+ it("should read interaction token from sessionStorage", async () => {
83
+ mockSessionStorage.getItem.mockReturnValue("my-token");
84
+
85
+ await trackPurchaseStatus({
86
+ customerId: "cust-1",
87
+ orderId: "order-1",
88
+ token: "token-1",
89
+ });
90
+
91
+ expect(mockSessionStorage.getItem).toHaveBeenCalledWith(
92
+ "frak-wallet-interaction-token"
93
+ );
94
+ expect(fetchSpy).toHaveBeenCalled();
95
+ });
96
+
97
+ it("should handle numeric customerId", async () => {
98
+ mockSessionStorage.getItem.mockReturnValue("token-123");
99
+
100
+ await trackPurchaseStatus({
101
+ customerId: 12345,
102
+ orderId: "order-789",
103
+ token: "purchase-token",
104
+ });
105
+
106
+ expect(fetchSpy).toHaveBeenCalledWith(
107
+ expect.any(String),
108
+ expect.objectContaining({
109
+ body: JSON.stringify({
110
+ customerId: 12345,
111
+ orderId: "order-789",
112
+ token: "purchase-token",
113
+ }),
114
+ })
115
+ );
116
+ });
117
+
118
+ it("should handle numeric orderId", async () => {
119
+ mockSessionStorage.getItem.mockReturnValue("token-123");
120
+
121
+ await trackPurchaseStatus({
122
+ customerId: "cust-456",
123
+ orderId: 67890,
124
+ token: "purchase-token",
125
+ });
126
+
127
+ expect(fetchSpy).toHaveBeenCalledWith(
128
+ expect.any(String),
129
+ expect.objectContaining({
130
+ body: JSON.stringify({
131
+ customerId: "cust-456",
132
+ orderId: 67890,
133
+ token: "purchase-token",
134
+ }),
135
+ })
136
+ );
137
+ });
138
+
139
+ it("should handle both numeric customerId and orderId", async () => {
140
+ mockSessionStorage.getItem.mockReturnValue("token-123");
141
+
142
+ await trackPurchaseStatus({
143
+ customerId: 12345,
144
+ orderId: 67890,
145
+ token: "purchase-token",
146
+ });
147
+
148
+ expect(fetchSpy).toHaveBeenCalledWith(
149
+ expect.any(String),
150
+ expect.objectContaining({
151
+ body: JSON.stringify({
152
+ customerId: 12345,
153
+ orderId: 67890,
154
+ token: "purchase-token",
155
+ }),
156
+ })
157
+ );
158
+ });
159
+ });
160
+
161
+ describe("missing interaction token", () => {
162
+ it("should warn when no interaction token found", async () => {
163
+ mockSessionStorage.getItem.mockReturnValue(null);
164
+
165
+ await trackPurchaseStatus({
166
+ customerId: "cust-456",
167
+ orderId: "order-789",
168
+ token: "purchase-token",
169
+ });
170
+
171
+ expect(consoleWarnSpy).toHaveBeenCalledWith(
172
+ "[Frak] No frak session found, skipping purchase check"
173
+ );
174
+ });
175
+
176
+ it("should not send request when no interaction token", async () => {
177
+ mockSessionStorage.getItem.mockReturnValue(null);
178
+
179
+ await trackPurchaseStatus({
180
+ customerId: "cust-456",
181
+ orderId: "order-789",
182
+ token: "purchase-token",
183
+ });
184
+
185
+ expect(fetchSpy).not.toHaveBeenCalled();
186
+ });
187
+
188
+ it("should not send request when interaction token is empty string", async () => {
189
+ mockSessionStorage.getItem.mockReturnValue("");
190
+
191
+ await trackPurchaseStatus({
192
+ customerId: "cust-456",
193
+ orderId: "order-789",
194
+ token: "purchase-token",
195
+ });
196
+
197
+ expect(fetchSpy).not.toHaveBeenCalled();
198
+ });
199
+ });
200
+
201
+ describe("network errors", () => {
202
+ it("should handle fetch rejection", async () => {
203
+ mockSessionStorage.getItem.mockReturnValue("token-123");
204
+ fetchSpy.mockRejectedValue(new Error("Network error"));
205
+
206
+ await expect(
207
+ trackPurchaseStatus({
208
+ customerId: "cust-456",
209
+ orderId: "order-789",
210
+ token: "purchase-token",
211
+ })
212
+ ).rejects.toThrow("Network error");
213
+ });
214
+
215
+ it("should handle fetch with error response", async () => {
216
+ mockSessionStorage.getItem.mockReturnValue("token-123");
217
+ fetchSpy.mockResolvedValue({
218
+ ok: false,
219
+ status: 500,
220
+ });
221
+
222
+ // Function doesn't check response, so it should complete
223
+ await trackPurchaseStatus({
224
+ customerId: "cust-456",
225
+ orderId: "order-789",
226
+ token: "purchase-token",
227
+ });
228
+
229
+ expect(fetchSpy).toHaveBeenCalled();
230
+ });
231
+ });
232
+
233
+ describe("request format", () => {
234
+ it("should include correct headers", async () => {
235
+ mockSessionStorage.getItem.mockReturnValue("my-auth-token");
236
+
237
+ await trackPurchaseStatus({
238
+ customerId: "cust-1",
239
+ orderId: "order-1",
240
+ token: "token-1",
241
+ });
242
+
243
+ expect(fetchSpy).toHaveBeenCalledWith(
244
+ expect.any(String),
245
+ expect.objectContaining({
246
+ headers: {
247
+ Accept: "application/json",
248
+ "Content-Type": "application/json",
249
+ "x-wallet-sdk-auth": "my-auth-token",
250
+ },
251
+ })
252
+ );
253
+ });
254
+
255
+ it("should use POST method", async () => {
256
+ mockSessionStorage.getItem.mockReturnValue("token-123");
257
+
258
+ await trackPurchaseStatus({
259
+ customerId: "cust-1",
260
+ orderId: "order-1",
261
+ token: "token-1",
262
+ });
263
+
264
+ expect(fetchSpy).toHaveBeenCalledWith(
265
+ expect.any(String),
266
+ expect.objectContaining({
267
+ method: "POST",
268
+ })
269
+ );
270
+ });
271
+
272
+ it("should target correct backend URL", async () => {
273
+ mockSessionStorage.getItem.mockReturnValue("token-123");
274
+
275
+ await trackPurchaseStatus({
276
+ customerId: "cust-1",
277
+ orderId: "order-1",
278
+ token: "token-1",
279
+ });
280
+
281
+ expect(fetchSpy).toHaveBeenCalledWith(
282
+ "https://backend.frak.id/interactions/listenForPurchase",
283
+ expect.any(Object)
284
+ );
285
+ });
286
+ });
287
+ });
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Function used to track the status of a purchase
3
+ * when a purchase is tracked, the `purchaseCompleted` interactions will be automatically send for the user when we receive the purchase confirmation via webhook.
4
+ *
5
+ * @param args.customerId - The customer id that made the purchase (on your side)
6
+ * @param args.orderId - The order id of the purchase (on your side)
7
+ * @param args.token - The token of the purchase
8
+ *
9
+ * @description This function will send a request to the backend to listen for the purchase status.
10
+ *
11
+ * @example
12
+ * async function trackPurchase(checkout) {
13
+ * const payload = {
14
+ * customerId: checkout.order.customer.id,
15
+ * orderId: checkout.order.id,
16
+ * token: checkout.token,
17
+ * };
18
+ *
19
+ * await trackPurchaseStatus(payload);
20
+ * }
21
+ *
22
+ * @remarks
23
+ * - The `trackPurchaseStatus` function requires the `frak-wallet-interaction-token` stored in the session storage to authenticate the request.
24
+ * - This function will print a warning if used in a non-browser environment or if the wallet interaction token is not available.
25
+ */
26
+ export async function trackPurchaseStatus(args: {
27
+ customerId: string | number;
28
+ orderId: string | number;
29
+ token: string;
30
+ }) {
31
+ if (typeof window === "undefined") {
32
+ console.warn("[Frak] No window found, can't track purchase");
33
+ return;
34
+ }
35
+ const interactionToken = window.sessionStorage.getItem(
36
+ "frak-wallet-interaction-token"
37
+ );
38
+ if (!interactionToken) {
39
+ console.warn("[Frak] No frak session found, skipping purchase check");
40
+ return;
41
+ }
42
+
43
+ // Submit the listening request
44
+ await fetch("https://backend.frak.id/interactions/listenForPurchase", {
45
+ method: "POST",
46
+ headers: {
47
+ Accept: "application/json",
48
+ "Content-Type": "application/json",
49
+ "x-wallet-sdk-auth": interactionToken,
50
+ },
51
+ body: JSON.stringify(args),
52
+ });
53
+ }
@@ -0,0 +1,372 @@
1
+ /**
2
+ * Tests for watchWalletStatus action
3
+ * Tests wallet status watching and side effects
4
+ */
5
+
6
+ import { vi } from "vitest";
7
+
8
+ // Mock Deferred before imports
9
+ vi.mock("@frak-labs/frame-connector", () => ({
10
+ Deferred: class {
11
+ promise: Promise<any>;
12
+ resolve: (value: any) => void;
13
+
14
+ constructor() {
15
+ let resolveFunc: (value: any) => void;
16
+ this.promise = new Promise((resolve) => {
17
+ resolveFunc = resolve;
18
+ });
19
+ this.resolve = resolveFunc!;
20
+ }
21
+ },
22
+ }));
23
+
24
+ import type { Address } from "viem";
25
+ import {
26
+ afterEach,
27
+ beforeEach,
28
+ describe,
29
+ expect,
30
+ it,
31
+ } from "../../tests/vitest-fixtures";
32
+ import type { FrakClient, WalletStatusReturnType } from "../types";
33
+ import { watchWalletStatus } from "./watchWalletStatus";
34
+
35
+ describe("watchWalletStatus", () => {
36
+ let mockSessionStorage: {
37
+ getItem: ReturnType<typeof vi.fn>;
38
+ setItem: ReturnType<typeof vi.fn>;
39
+ removeItem: ReturnType<typeof vi.fn>;
40
+ };
41
+
42
+ beforeEach(() => {
43
+ // Mock sessionStorage
44
+ mockSessionStorage = {
45
+ getItem: vi.fn(),
46
+ setItem: vi.fn(),
47
+ removeItem: vi.fn(),
48
+ };
49
+ Object.defineProperty(window, "sessionStorage", {
50
+ value: mockSessionStorage,
51
+ writable: true,
52
+ configurable: true,
53
+ });
54
+ });
55
+
56
+ afterEach(() => {
57
+ vi.clearAllMocks();
58
+ });
59
+
60
+ describe("without callback", () => {
61
+ it("should make one-shot request", async () => {
62
+ const mockStatus: WalletStatusReturnType = {
63
+ key: "connected",
64
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
65
+ interactionToken: "token-123",
66
+ };
67
+
68
+ const mockClient = {
69
+ request: vi.fn().mockResolvedValue(mockStatus),
70
+ openPanel: {
71
+ setGlobalProperties: vi.fn(),
72
+ },
73
+ } as unknown as FrakClient;
74
+
75
+ const result = await watchWalletStatus(mockClient);
76
+
77
+ expect(mockClient.request).toHaveBeenCalledWith({
78
+ method: "frak_listenToWalletStatus",
79
+ });
80
+ expect(result).toEqual(mockStatus);
81
+ });
82
+
83
+ it("should save interaction token to sessionStorage", async () => {
84
+ const mockStatus: WalletStatusReturnType = {
85
+ key: "connected",
86
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
87
+ interactionToken: "token-abc",
88
+ };
89
+
90
+ const mockClient = {
91
+ request: vi.fn().mockResolvedValue(mockStatus),
92
+ openPanel: {
93
+ setGlobalProperties: vi.fn(),
94
+ },
95
+ } as unknown as FrakClient;
96
+
97
+ await watchWalletStatus(mockClient);
98
+
99
+ expect(mockSessionStorage.setItem).toHaveBeenCalledWith(
100
+ "frak-wallet-interaction-token",
101
+ "token-abc"
102
+ );
103
+ });
104
+
105
+ it("should remove interaction token when not present", async () => {
106
+ const mockStatus: WalletStatusReturnType = {
107
+ key: "not-connected",
108
+ };
109
+
110
+ const mockClient = {
111
+ request: vi.fn().mockResolvedValue(mockStatus),
112
+ openPanel: {
113
+ setGlobalProperties: vi.fn(),
114
+ },
115
+ } as unknown as FrakClient;
116
+
117
+ await watchWalletStatus(mockClient);
118
+
119
+ expect(mockSessionStorage.removeItem).toHaveBeenCalledWith(
120
+ "frak-wallet-interaction-token"
121
+ );
122
+ });
123
+
124
+ it("should update OpenPanel global properties", async () => {
125
+ const mockStatus: WalletStatusReturnType = {
126
+ key: "connected",
127
+ wallet: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd" as Address,
128
+ };
129
+
130
+ const mockClient = {
131
+ request: vi.fn().mockResolvedValue(mockStatus),
132
+ openPanel: {
133
+ setGlobalProperties: vi.fn(),
134
+ },
135
+ } as unknown as FrakClient;
136
+
137
+ await watchWalletStatus(mockClient);
138
+
139
+ expect(
140
+ mockClient.openPanel?.setGlobalProperties
141
+ ).toHaveBeenCalledWith({
142
+ wallet: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd",
143
+ });
144
+ });
145
+
146
+ it("should set wallet to null when not connected", async () => {
147
+ const mockStatus: WalletStatusReturnType = {
148
+ key: "not-connected",
149
+ };
150
+
151
+ const mockClient = {
152
+ request: vi.fn().mockResolvedValue(mockStatus),
153
+ openPanel: {
154
+ setGlobalProperties: vi.fn(),
155
+ },
156
+ } as unknown as FrakClient;
157
+
158
+ await watchWalletStatus(mockClient);
159
+
160
+ expect(
161
+ mockClient.openPanel?.setGlobalProperties
162
+ ).toHaveBeenCalledWith({
163
+ wallet: null,
164
+ });
165
+ });
166
+
167
+ it("should work without openPanel", async () => {
168
+ const mockStatus: WalletStatusReturnType = {
169
+ key: "connected",
170
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
171
+ };
172
+
173
+ const mockClient = {
174
+ request: vi.fn().mockResolvedValue(mockStatus),
175
+ } as unknown as FrakClient;
176
+
177
+ const result = await watchWalletStatus(mockClient);
178
+
179
+ expect(result).toEqual(mockStatus);
180
+ });
181
+ });
182
+
183
+ describe("with callback", () => {
184
+ it("should setup listener request", async () => {
185
+ const mockStatus: WalletStatusReturnType = {
186
+ key: "connected",
187
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
188
+ };
189
+
190
+ const mockCallback = vi.fn();
191
+ const mockClient = {
192
+ listenerRequest: vi.fn((_params, callback) => {
193
+ // Simulate status update
194
+ setTimeout(() => callback(mockStatus), 0);
195
+ }),
196
+ openPanel: {
197
+ setGlobalProperties: vi.fn(),
198
+ },
199
+ } as unknown as FrakClient;
200
+
201
+ const resultPromise = watchWalletStatus(mockClient, mockCallback);
202
+
203
+ expect(mockClient.listenerRequest).toHaveBeenCalled();
204
+
205
+ await resultPromise;
206
+ });
207
+
208
+ it("should call callback with status updates", async () => {
209
+ const mockStatus: WalletStatusReturnType = {
210
+ key: "connected",
211
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
212
+ interactionToken: "token-123",
213
+ };
214
+
215
+ const mockCallback = vi.fn();
216
+ const mockClient = {
217
+ listenerRequest: vi.fn((_params, callback) => {
218
+ setTimeout(() => callback(mockStatus), 0);
219
+ }),
220
+ openPanel: {
221
+ setGlobalProperties: vi.fn(),
222
+ },
223
+ } as unknown as FrakClient;
224
+
225
+ await watchWalletStatus(mockClient, mockCallback);
226
+
227
+ expect(mockCallback).toHaveBeenCalledWith(mockStatus);
228
+ });
229
+
230
+ it("should resolve promise with first status", async () => {
231
+ const firstStatus: WalletStatusReturnType = {
232
+ key: "connected",
233
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
234
+ };
235
+
236
+ const secondStatus: WalletStatusReturnType = {
237
+ key: "connected",
238
+ wallet: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd" as Address,
239
+ };
240
+
241
+ const mockCallback = vi.fn();
242
+ const mockClient = {
243
+ listenerRequest: vi.fn((_params, callback) => {
244
+ setTimeout(() => {
245
+ callback(firstStatus);
246
+ callback(secondStatus);
247
+ }, 0);
248
+ }),
249
+ openPanel: {
250
+ setGlobalProperties: vi.fn(),
251
+ },
252
+ } as unknown as FrakClient;
253
+
254
+ const result = await watchWalletStatus(mockClient, mockCallback);
255
+
256
+ expect(result).toEqual(firstStatus);
257
+ expect(mockCallback).toHaveBeenCalledTimes(2);
258
+ });
259
+
260
+ it("should handle side effects for each status update", async () => {
261
+ const firstStatus: WalletStatusReturnType = {
262
+ key: "connected",
263
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
264
+ interactionToken: "token-1",
265
+ };
266
+
267
+ const secondStatus: WalletStatusReturnType = {
268
+ key: "connected",
269
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
270
+ interactionToken: "token-2",
271
+ };
272
+
273
+ const mockCallback = vi.fn();
274
+ const mockClient = {
275
+ listenerRequest: vi.fn((_params, callback) => {
276
+ setTimeout(() => {
277
+ callback(firstStatus);
278
+ callback(secondStatus);
279
+ }, 0);
280
+ }),
281
+ openPanel: {
282
+ setGlobalProperties: vi.fn(),
283
+ },
284
+ } as unknown as FrakClient;
285
+
286
+ await watchWalletStatus(mockClient, mockCallback);
287
+
288
+ // Both tokens should be saved
289
+ expect(mockSessionStorage.setItem).toHaveBeenCalledWith(
290
+ "frak-wallet-interaction-token",
291
+ "token-1"
292
+ );
293
+ expect(mockSessionStorage.setItem).toHaveBeenCalledWith(
294
+ "frak-wallet-interaction-token",
295
+ "token-2"
296
+ );
297
+ });
298
+
299
+ it("should save and remove interaction token based on status", async () => {
300
+ const connectedStatus: WalletStatusReturnType = {
301
+ key: "connected",
302
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
303
+ interactionToken: "token-123",
304
+ };
305
+
306
+ const disconnectedStatus: WalletStatusReturnType = {
307
+ key: "not-connected",
308
+ };
309
+
310
+ const mockCallback = vi.fn();
311
+ const mockClient = {
312
+ listenerRequest: vi.fn((_params, callback) => {
313
+ setTimeout(() => {
314
+ callback(connectedStatus);
315
+ callback(disconnectedStatus);
316
+ }, 0);
317
+ }),
318
+ openPanel: {
319
+ setGlobalProperties: vi.fn(),
320
+ },
321
+ } as unknown as FrakClient;
322
+
323
+ await watchWalletStatus(mockClient, mockCallback);
324
+
325
+ expect(mockSessionStorage.setItem).toHaveBeenCalledWith(
326
+ "frak-wallet-interaction-token",
327
+ "token-123"
328
+ );
329
+ expect(mockSessionStorage.removeItem).toHaveBeenCalledWith(
330
+ "frak-wallet-interaction-token"
331
+ );
332
+ });
333
+
334
+ it("should update OpenPanel properties with each status", async () => {
335
+ const firstStatus: WalletStatusReturnType = {
336
+ key: "connected",
337
+ wallet: "0x1111111111111111111111111111111111111111" as Address,
338
+ };
339
+
340
+ const secondStatus: WalletStatusReturnType = {
341
+ key: "connected",
342
+ wallet: "0x2222222222222222222222222222222222222222" as Address,
343
+ };
344
+
345
+ const mockCallback = vi.fn();
346
+ const mockClient = {
347
+ listenerRequest: vi.fn((_params, callback) => {
348
+ setTimeout(() => {
349
+ callback(firstStatus);
350
+ callback(secondStatus);
351
+ }, 0);
352
+ }),
353
+ openPanel: {
354
+ setGlobalProperties: vi.fn(),
355
+ },
356
+ } as unknown as FrakClient;
357
+
358
+ await watchWalletStatus(mockClient, mockCallback);
359
+
360
+ expect(
361
+ mockClient.openPanel?.setGlobalProperties
362
+ ).toHaveBeenCalledWith({
363
+ wallet: "0x1111111111111111111111111111111111111111",
364
+ });
365
+ expect(
366
+ mockClient.openPanel?.setGlobalProperties
367
+ ).toHaveBeenCalledWith({
368
+ wallet: "0x2222222222222222222222222222222222222222",
369
+ });
370
+ });
371
+ });
372
+ });