@testerpaykit/api-client 1.1.0

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.
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './TesterPayKitApiClient';
@@ -0,0 +1,367 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import axios from 'axios';
3
+ import { TesterPayKitApiClient } from '../src/TesterPayKitApiClient';
4
+ import { TokenManager } from '@testerpaykit/core';
5
+
6
+ // Mock axios
7
+ vi.mock('axios');
8
+ const mockedAxios = axios as any;
9
+
10
+ describe('TesterPayKitApiClient', () => {
11
+ let client: TesterPayKitApiClient;
12
+ let mockAxiosInstance: any;
13
+ let tokenManager: TokenManager;
14
+
15
+ beforeEach(() => {
16
+ // Reset mocks
17
+ vi.clearAllMocks();
18
+
19
+ // Create mock axios instance
20
+ mockAxiosInstance = {
21
+ post: vi.fn(),
22
+ get: vi.fn(),
23
+ interceptors: {
24
+ request: {
25
+ use: vi.fn(),
26
+ },
27
+ response: {
28
+ use: vi.fn(),
29
+ },
30
+ },
31
+ };
32
+
33
+ mockedAxios.create = vi.fn(() => mockAxiosInstance);
34
+
35
+ tokenManager = new TokenManager();
36
+ client = new TesterPayKitApiClient({
37
+ apiKey: 'test-api-key',
38
+ baseURL: 'https://test.api.com',
39
+ tokenManager,
40
+ });
41
+ });
42
+
43
+ describe('constructor', () => {
44
+ it('should create axios instance with correct config', () => {
45
+ expect(mockedAxios.create).toHaveBeenCalledWith({
46
+ baseURL: 'https://test.api.com',
47
+ headers: {
48
+ 'Content-Type': 'application/json',
49
+ 'X-API-Key': 'test-api-key',
50
+ },
51
+ });
52
+ });
53
+
54
+ it('should use default baseURL if not provided', () => {
55
+ vi.clearAllMocks();
56
+ const defaultClient = new TesterPayKitApiClient({
57
+ apiKey: 'test-api-key',
58
+ });
59
+
60
+ expect(mockedAxios.create).toHaveBeenCalledWith({
61
+ baseURL: 'https://api.testerpaykit.com',
62
+ headers: {
63
+ 'Content-Type': 'application/json',
64
+ 'X-API-Key': 'test-api-key',
65
+ },
66
+ });
67
+ });
68
+
69
+ it('should setup interceptors', () => {
70
+ expect(mockAxiosInstance.interceptors.request.use).toHaveBeenCalled();
71
+ expect(mockAxiosInstance.interceptors.response.use).toHaveBeenCalled();
72
+ });
73
+ });
74
+
75
+ describe('login', () => {
76
+ it('should successfully login and store tokens', async () => {
77
+ const loginRequest = {
78
+ email: 'test@example.com',
79
+ password: 'password123',
80
+ };
81
+
82
+ const loginResponse = {
83
+ data: {
84
+ accessToken: 'access-token-123',
85
+ refreshToken: 'refresh-token-456',
86
+ tester: {
87
+ id: '1',
88
+ email: 'test@example.com',
89
+ fullName: 'Test User',
90
+ },
91
+ },
92
+ };
93
+
94
+ mockAxiosInstance.post.mockResolvedValue(loginResponse);
95
+
96
+ const result = await client.login(loginRequest);
97
+
98
+ expect(mockAxiosInstance.post).toHaveBeenCalledWith(
99
+ '/auth/login',
100
+ loginRequest
101
+ );
102
+ expect(result).toEqual(loginResponse.data);
103
+ expect(tokenManager.getAccessToken()).toBe('access-token-123');
104
+ expect(tokenManager.getRefreshToken()).toBe('refresh-token-456');
105
+ });
106
+
107
+ it('should handle login without tokens', async () => {
108
+ const loginRequest = {
109
+ email: 'test@example.com',
110
+ password: 'password123',
111
+ };
112
+
113
+ const loginResponse = {
114
+ data: {
115
+ tester: {
116
+ id: '1',
117
+ email: 'test@example.com',
118
+ fullName: 'Test User',
119
+ },
120
+ },
121
+ };
122
+
123
+ mockAxiosInstance.post.mockResolvedValue(loginResponse);
124
+
125
+ const result = await client.login(loginRequest);
126
+
127
+ expect(result).toEqual(loginResponse.data);
128
+ expect(tokenManager.getAccessToken()).toBeNull();
129
+ });
130
+ });
131
+
132
+ describe('refreshToken', () => {
133
+ it('should refresh access token', async () => {
134
+ const refreshResponse = {
135
+ data: {
136
+ accessToken: 'new-access-token',
137
+ refreshToken: 'refresh-token-456',
138
+ },
139
+ };
140
+
141
+ mockAxiosInstance.post.mockResolvedValue(refreshResponse);
142
+
143
+ const result = await client.refreshToken('refresh-token-456');
144
+
145
+ expect(mockAxiosInstance.post).toHaveBeenCalledWith('/v1/auth/refresh', {
146
+ refreshToken: 'refresh-token-456',
147
+ });
148
+ expect(result).toEqual(refreshResponse.data);
149
+ expect(tokenManager.getAccessToken()).toBe('new-access-token');
150
+ });
151
+ });
152
+
153
+ describe('submitSummary', () => {
154
+ it('should submit weekly summary', async () => {
155
+ const summary = {
156
+ testerId: '1',
157
+ weekStartDate: '2024-01-01',
158
+ weekEndDate: '2024-01-07',
159
+ totalTestingTime: 40,
160
+ screenViews: 100,
161
+ taps: 200,
162
+ sessions: 10,
163
+ };
164
+
165
+ const submitResponse = {
166
+ data: {
167
+ ...summary,
168
+ id: 'summary-1',
169
+ status: 'pending',
170
+ },
171
+ };
172
+
173
+ mockAxiosInstance.post.mockResolvedValue(submitResponse);
174
+
175
+ const result = await client.submitSummary(summary as any);
176
+
177
+ expect(mockAxiosInstance.post).toHaveBeenCalledWith(
178
+ '/v1/submissions',
179
+ summary
180
+ );
181
+ expect(result).toEqual(submitResponse.data);
182
+ });
183
+ });
184
+
185
+ describe('tester sessions', () => {
186
+ it('should start a tester session with server contract field names', async () => {
187
+ const response = {
188
+ data: {
189
+ session: {
190
+ id: 'session-1',
191
+ project_id: 'project-1',
192
+ tester_id: 'tester-1',
193
+ started_at: '2026-05-24T10:00:00Z',
194
+ },
195
+ },
196
+ };
197
+ mockAxiosInstance.post.mockResolvedValue(response);
198
+
199
+ const result = await client.startTesterSession({
200
+ projectId: 'project-1',
201
+ deviceInfo: { platform: 'ios' },
202
+ appVersion: '1.0.0',
203
+ sdkVersion: '@testerpaykit/expo/0.2.0',
204
+ });
205
+
206
+ expect(mockAxiosInstance.post).toHaveBeenCalledWith(
207
+ '/v1/testers/sessions',
208
+ {
209
+ project_id: 'project-1',
210
+ device_info: { platform: 'ios' },
211
+ app_version: '1.0.0',
212
+ sdk_version: '@testerpaykit/expo/0.2.0',
213
+ },
214
+ );
215
+ expect(result).toEqual(response.data.session);
216
+ });
217
+
218
+ it('should end a tester session with end reason and counters', async () => {
219
+ mockAxiosInstance.put = vi.fn().mockResolvedValue({ data: { session: { id: 'session-1' } } });
220
+
221
+ await client.endTesterSession({
222
+ sessionId: 'session-1',
223
+ endReason: 'completed',
224
+ bugsFound: 1,
225
+ tasksCompleted: 2,
226
+ foregroundSeconds: 300,
227
+ endedAt: new Date('2026-05-24T10:05:00.000Z'),
228
+ });
229
+
230
+ expect(mockAxiosInstance.put).toHaveBeenCalledWith(
231
+ '/v1/testers/sessions/session-1',
232
+ {
233
+ ended_at: '2026-05-24T10:05:00.000Z',
234
+ bugs_found: 1,
235
+ tasks_completed: 2,
236
+ end_reason: 'completed',
237
+ foreground_seconds: 300,
238
+ },
239
+ );
240
+ });
241
+
242
+ it('should add session events in the server batch contract shape', async () => {
243
+ const response = {
244
+ data: {
245
+ inserted: 2,
246
+ events: [
247
+ { id: 'event-1', timestamp: '2026-05-24T10:01:00Z' },
248
+ { id: 'event-2', timestamp: '2026-05-24T10:02:00Z' },
249
+ ],
250
+ },
251
+ };
252
+ mockAxiosInstance.post.mockResolvedValue(response);
253
+
254
+ const result = await client.addSessionEvents('session-1', [
255
+ {
256
+ eventType: 'screenView',
257
+ eventData: { source: 'navigation' },
258
+ screenRoute: 'Checkout',
259
+ timestamp: new Date('2026-05-24T10:01:00.000Z'),
260
+ },
261
+ {
262
+ eventType: 'tapBucket',
263
+ eventData: { target: 'pay_button' },
264
+ screenRoute: 'Checkout',
265
+ timestamp: '2026-05-24T10:02:00.000Z',
266
+ },
267
+ ]);
268
+
269
+ expect(mockAxiosInstance.post).toHaveBeenCalledWith(
270
+ '/v1/testers/sessions/session-1/events',
271
+ {
272
+ events: [
273
+ {
274
+ event_type: 'screenView',
275
+ event_data: { source: 'navigation' },
276
+ screen_route: 'Checkout',
277
+ timestamp: '2026-05-24T10:01:00.000Z',
278
+ },
279
+ {
280
+ event_type: 'tapBucket',
281
+ event_data: { target: 'pay_button' },
282
+ screen_route: 'Checkout',
283
+ timestamp: '2026-05-24T10:02:00.000Z',
284
+ },
285
+ ],
286
+ },
287
+ );
288
+ expect(result).toEqual(response.data);
289
+ });
290
+
291
+ it('should add session breadcrumbs in the server batch contract shape', async () => {
292
+ const response = {
293
+ data: {
294
+ inserted: 1,
295
+ breadcrumbs: [
296
+ { id: 'breadcrumb-1', timestamp: '2026-05-24T10:03:00Z' },
297
+ ],
298
+ },
299
+ };
300
+ mockAxiosInstance.post.mockResolvedValue(response);
301
+
302
+ const result = await client.addSessionBreadcrumbs('session-1', [
303
+ {
304
+ breadcrumbType: 'tap',
305
+ message: 'pay_button',
306
+ metadata: { screen: 'Checkout' },
307
+ timestamp: new Date('2026-05-24T10:03:00.000Z'),
308
+ },
309
+ ]);
310
+
311
+ expect(mockAxiosInstance.post).toHaveBeenCalledWith(
312
+ '/v1/testers/sessions/session-1/breadcrumbs',
313
+ {
314
+ breadcrumbs: [
315
+ {
316
+ breadcrumb_type: 'tap',
317
+ message: 'pay_button',
318
+ metadata: { screen: 'Checkout' },
319
+ timestamp: '2026-05-24T10:03:00.000Z',
320
+ },
321
+ ],
322
+ },
323
+ );
324
+ expect(result).toEqual(response.data);
325
+ });
326
+ });
327
+
328
+ describe('getPendingSubmissions', () => {
329
+ it('should get pending submissions', async () => {
330
+ const submissions = [
331
+ {
332
+ id: 'summary-1',
333
+ testerId: '1',
334
+ weekStartDate: '2024-01-01',
335
+ weekEndDate: '2024-01-07',
336
+ status: 'pending',
337
+ },
338
+ {
339
+ id: 'summary-2',
340
+ testerId: '1',
341
+ weekStartDate: '2024-01-08',
342
+ weekEndDate: '2024-01-14',
343
+ status: 'pending',
344
+ },
345
+ ];
346
+
347
+ const getResponse = {
348
+ data: submissions,
349
+ };
350
+
351
+ mockAxiosInstance.get.mockResolvedValue(getResponse);
352
+
353
+ const result = await client.getPendingSubmissions();
354
+
355
+ expect(mockAxiosInstance.get).toHaveBeenCalledWith('/v1/submissions', { params: undefined });
356
+ expect(result).toEqual(submissions);
357
+ });
358
+ });
359
+
360
+ describe('setAccessToken', () => {
361
+ it('should set access token', () => {
362
+ client.setAccessToken('manual-token');
363
+
364
+ expect(tokenManager.getAccessToken()).toBe('manual-token');
365
+ });
366
+ });
367
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src",
6
+ "composite": false
7
+ },
8
+ "include": ["src"],
9
+ "exclude": ["node_modules", "dist", "**/*.test.ts"]
10
+ }
@@ -0,0 +1,12 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ globals: true,
6
+ environment: 'node',
7
+ coverage: {
8
+ provider: 'v8',
9
+ reporter: ['text', 'json', 'html'],
10
+ },
11
+ },
12
+ });