@rooguys/sdk 0.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.
@@ -0,0 +1,330 @@
1
+ /**
2
+ * Mock API response fixtures for testing
3
+ */
4
+
5
+ export const mockResponses = {
6
+ userProfile: {
7
+ user_id: 'user123',
8
+ points: 100,
9
+ persona: 'Achiever',
10
+ level: {
11
+ id: 'level1',
12
+ name: 'Bronze',
13
+ level_number: 1,
14
+ points_required: 0,
15
+ },
16
+ next_level: {
17
+ id: 'level2',
18
+ name: 'Silver',
19
+ level_number: 2,
20
+ points_required: 500,
21
+ points_needed: 400,
22
+ },
23
+ metrics: {
24
+ logins: 10,
25
+ purchases: 2,
26
+ },
27
+ badges: [
28
+ {
29
+ id: 'badge1',
30
+ name: 'First Steps',
31
+ description: 'Completed first action',
32
+ icon_url: 'https://example.com/badge1.png',
33
+ earned_at: '2024-01-01T00:00:00Z',
34
+ },
35
+ ],
36
+ },
37
+
38
+ trackEventResponse: {
39
+ status: 'queued',
40
+ message: 'Event accepted for processing',
41
+ },
42
+
43
+ trackEventWithProfileResponse: {
44
+ status: 'queued',
45
+ message: 'Event accepted for processing',
46
+ profile: {
47
+ user_id: 'user123',
48
+ points: 110,
49
+ persona: 'Achiever',
50
+ level: {
51
+ id: 'level1',
52
+ name: 'Bronze',
53
+ level_number: 1,
54
+ points_required: 0,
55
+ },
56
+ next_level: {
57
+ id: 'level2',
58
+ name: 'Silver',
59
+ level_number: 2,
60
+ points_required: 500,
61
+ points_needed: 390,
62
+ },
63
+ metrics: {},
64
+ badges: [],
65
+ },
66
+ },
67
+
68
+ leaderboardResponse: {
69
+ timeframe: 'all-time',
70
+ page: 1,
71
+ limit: 50,
72
+ total: 100,
73
+ rankings: [
74
+ {
75
+ rank: 1,
76
+ user_id: 'user1',
77
+ points: 1000,
78
+ level: {
79
+ id: 'level3',
80
+ name: 'Gold',
81
+ level_number: 3,
82
+ },
83
+ },
84
+ {
85
+ rank: 2,
86
+ user_id: 'user2',
87
+ points: 900,
88
+ level: {
89
+ id: 'level2',
90
+ name: 'Silver',
91
+ level_number: 2,
92
+ },
93
+ },
94
+ ],
95
+ },
96
+
97
+ userRankResponse: {
98
+ user_id: 'user123',
99
+ rank: 42,
100
+ points: 850,
101
+ total_users: 1000,
102
+ },
103
+
104
+ badgesListResponse: {
105
+ badges: [
106
+ {
107
+ id: 'badge1',
108
+ name: 'First Steps',
109
+ description: 'Completed first action',
110
+ icon_url: 'https://example.com/badge1.png',
111
+ is_active: true,
112
+ unlock_criteria: {
113
+ metric: 'actions',
114
+ value: 1,
115
+ },
116
+ },
117
+ ],
118
+ pagination: {
119
+ page: 1,
120
+ limit: 50,
121
+ total: 10,
122
+ totalPages: 1,
123
+ },
124
+ },
125
+
126
+ levelsListResponse: {
127
+ levels: [
128
+ {
129
+ id: 'level1',
130
+ name: 'Bronze',
131
+ level_number: 1,
132
+ points_required: 0,
133
+ description: 'Starting level',
134
+ icon_url: 'https://example.com/bronze.png',
135
+ },
136
+ {
137
+ id: 'level2',
138
+ name: 'Silver',
139
+ level_number: 2,
140
+ points_required: 500,
141
+ description: 'Intermediate level',
142
+ icon_url: 'https://example.com/silver.png',
143
+ },
144
+ ],
145
+ pagination: {
146
+ page: 1,
147
+ limit: 50,
148
+ total: 5,
149
+ totalPages: 1,
150
+ },
151
+ },
152
+
153
+ questionnaireResponse: {
154
+ id: 'questionnaire1',
155
+ slug: 'user-persona',
156
+ title: 'User Persona Quiz',
157
+ description: 'Determine your user persona',
158
+ is_active: true,
159
+ questions: [
160
+ {
161
+ id: 'q1',
162
+ text: 'What motivates you?',
163
+ order: 1,
164
+ answer_options: [
165
+ {
166
+ id: 'a1',
167
+ text: 'Competition',
168
+ persona_weights: { Competitor: 1 },
169
+ },
170
+ {
171
+ id: 'a2',
172
+ text: 'Exploration',
173
+ persona_weights: { Explorer: 1 },
174
+ },
175
+ ],
176
+ },
177
+ ],
178
+ },
179
+
180
+ bulkUsersResponse: {
181
+ users: [
182
+ {
183
+ user_id: 'user1',
184
+ points: 100,
185
+ persona: null,
186
+ level: null,
187
+ next_level: null,
188
+ metrics: {},
189
+ badges: [],
190
+ },
191
+ {
192
+ user_id: 'user2',
193
+ points: 200,
194
+ persona: 'Achiever',
195
+ level: {
196
+ id: 'level1',
197
+ name: 'Bronze',
198
+ level_number: 1,
199
+ points_required: 0,
200
+ },
201
+ next_level: null,
202
+ metrics: {},
203
+ badges: [],
204
+ },
205
+ ],
206
+ },
207
+
208
+ ahaScoreResponse: {
209
+ success: true,
210
+ data: {
211
+ user_id: 'user123',
212
+ current_score: 75,
213
+ declarative_score: 80,
214
+ inferred_score: 70,
215
+ status: 'activated',
216
+ history: {
217
+ initial: 50,
218
+ initial_date: '2024-01-01T00:00:00Z',
219
+ previous: 70,
220
+ },
221
+ },
222
+ },
223
+
224
+ ahaDeclarationResponse: {
225
+ success: true,
226
+ message: 'Declarative score recorded. Calculation in progress.',
227
+ },
228
+
229
+ answerSubmissionResponse: {
230
+ status: 'accepted',
231
+ message: 'Answers queued for processing',
232
+ },
233
+
234
+ leaderboardsListResponse: {
235
+ leaderboards: [
236
+ {
237
+ id: 'lb1',
238
+ name: 'Top Players',
239
+ description: 'Overall top players',
240
+ type: 'POINTS',
241
+ timeframe: 'all-time',
242
+ is_active: true,
243
+ created_at: '2024-01-01T00:00:00Z',
244
+ },
245
+ ],
246
+ pagination: {
247
+ page: 1,
248
+ limit: 50,
249
+ total: 5,
250
+ totalPages: 1,
251
+ },
252
+ },
253
+
254
+ customLeaderboardResponse: {
255
+ leaderboard: {
256
+ id: 'lb1',
257
+ name: 'Top Players',
258
+ description: 'Overall top players',
259
+ type: 'POINTS',
260
+ timeframe: 'all-time',
261
+ is_active: true,
262
+ created_at: '2024-01-01T00:00:00Z',
263
+ },
264
+ rankings: [
265
+ {
266
+ rank: 1,
267
+ user_id: 'user1',
268
+ points: 1000,
269
+ level: null,
270
+ },
271
+ ],
272
+ pagination: {
273
+ page: 1,
274
+ limit: 50,
275
+ total: 10,
276
+ totalPages: 1,
277
+ },
278
+ },
279
+ };
280
+
281
+ export const mockErrors = {
282
+ validationError: {
283
+ error: 'Validation failed',
284
+ details: [
285
+ {
286
+ field: 'user_id',
287
+ message: 'User ID is required',
288
+ },
289
+ ],
290
+ },
291
+
292
+ notFoundError: {
293
+ error: 'User not found',
294
+ message: "User 'user123' does not exist in this project",
295
+ },
296
+
297
+ serverError: {
298
+ error: 'Internal server error',
299
+ },
300
+
301
+ queueFullError: {
302
+ error: 'Service unavailable',
303
+ message: 'Event queue is full. Please retry later.',
304
+ },
305
+
306
+ unauthorizedError: {
307
+ error: 'Unauthorized',
308
+ message: 'Invalid or missing API key',
309
+ },
310
+
311
+ invalidTimeframeError: {
312
+ error: 'Invalid timeframe',
313
+ message: "Timeframe must be one of: all-time, weekly, monthly",
314
+ },
315
+
316
+ invalidPaginationError: {
317
+ error: 'Invalid limit parameter',
318
+ message: 'Limit must be between 1 and 100',
319
+ },
320
+
321
+ ahaValueError: {
322
+ error: 'Validation failed',
323
+ details: [
324
+ {
325
+ field: 'value',
326
+ message: 'value must be an integer between 1 and 5',
327
+ },
328
+ ],
329
+ },
330
+ };
@@ -0,0 +1,203 @@
1
+ /**
2
+ * Property-Based Test: HTTP Request Construction
3
+ * Feature: sdk-testing-enhancement, Property 1: HTTP Request Construction
4
+ *
5
+ * Tests that any valid SDK method call constructs correct HTTP request
6
+ * with proper method, URL, headers, and body structure.
7
+ */
8
+
9
+ import fc from 'fast-check';
10
+ import { Rooguys } from '../../index';
11
+ import { createMockAxiosInstance } from '../utils/mockClient';
12
+ import { arbitraries } from '../utils/generators';
13
+ import axios from 'axios';
14
+
15
+ // Mock axios
16
+ jest.mock('axios');
17
+ const mockedAxios = axios as jest.Mocked<typeof axios>;
18
+
19
+ describe('Property: HTTP Request Construction', () => {
20
+ let mockClient: any;
21
+
22
+ beforeEach(() => {
23
+ mockClient = createMockAxiosInstance();
24
+ mockedAxios.create.mockReturnValue(mockClient);
25
+ });
26
+
27
+ afterEach(() => {
28
+ jest.clearAllMocks();
29
+ });
30
+
31
+ it('should construct valid POST request for event tracking', async () => {
32
+ await fc.assert(
33
+ fc.asyncProperty(
34
+ arbitraries.apiKey(),
35
+ arbitraries.eventName(),
36
+ arbitraries.userId(),
37
+ arbitraries.properties(),
38
+ async (apiKey, eventName, userId, properties) => {
39
+ // Arrange
40
+ mockClient.post.mockResolvedValue({
41
+ data: { status: 'queued', message: 'Event accepted' }
42
+ });
43
+ const sdk = new Rooguys(apiKey);
44
+
45
+ // Act
46
+ await sdk.events.track(eventName, userId, properties);
47
+
48
+ // Assert
49
+ expect(mockClient.post).toHaveBeenCalledWith(
50
+ '/event',
51
+ {
52
+ event_name: eventName,
53
+ user_id: userId,
54
+ properties,
55
+ },
56
+ expect.objectContaining({
57
+ params: expect.any(Object)
58
+ })
59
+ );
60
+ }
61
+ ),
62
+ { numRuns: 100 }
63
+ );
64
+ });
65
+
66
+ it('should construct valid GET request for user profile', async () => {
67
+ await fc.assert(
68
+ fc.asyncProperty(
69
+ arbitraries.apiKey(),
70
+ arbitraries.userId(),
71
+ async (apiKey, userId) => {
72
+ // Arrange
73
+ mockClient.get.mockResolvedValue({
74
+ data: { user_id: userId, points: 100 }
75
+ });
76
+ const sdk = new Rooguys(apiKey);
77
+
78
+ // Act
79
+ await sdk.users.get(userId);
80
+
81
+ // Assert
82
+ expect(mockClient.get).toHaveBeenCalledWith(`/user/${encodeURIComponent(userId)}`);
83
+ }
84
+ ),
85
+ { numRuns: 100 }
86
+ );
87
+ });
88
+
89
+ it('should construct valid POST request for bulk user fetch', async () => {
90
+ await fc.assert(
91
+ fc.asyncProperty(
92
+ arbitraries.apiKey(),
93
+ arbitraries.userIds(),
94
+ async (apiKey, userIds) => {
95
+ // Arrange
96
+ mockClient.post.mockResolvedValue({
97
+ data: { users: [] }
98
+ });
99
+ const sdk = new Rooguys(apiKey);
100
+
101
+ // Act
102
+ await sdk.users.getBulk(userIds);
103
+
104
+ // Assert
105
+ expect(mockClient.post).toHaveBeenCalledWith(
106
+ '/users/bulk',
107
+ { user_ids: userIds }
108
+ );
109
+ }
110
+ ),
111
+ { numRuns: 100 }
112
+ );
113
+ });
114
+
115
+ it('should construct valid GET request with query parameters for leaderboard', async () => {
116
+ await fc.assert(
117
+ fc.asyncProperty(
118
+ arbitraries.apiKey(),
119
+ arbitraries.timeframe(),
120
+ arbitraries.pagination(),
121
+ async (apiKey, timeframe, { page, limit }) => {
122
+ // Arrange
123
+ mockClient.get.mockResolvedValue({
124
+ data: { rankings: [], page, limit, total: 0 }
125
+ });
126
+ const sdk = new Rooguys(apiKey);
127
+
128
+ // Act
129
+ await sdk.leaderboards.getGlobal(timeframe as any, page, limit);
130
+
131
+ // Assert
132
+ expect(mockClient.get).toHaveBeenCalledWith(
133
+ '/leaderboard',
134
+ {
135
+ params: { timeframe, page, limit }
136
+ }
137
+ );
138
+ }
139
+ ),
140
+ { numRuns: 100 }
141
+ );
142
+ });
143
+
144
+ it('should construct valid POST request for Aha score declaration', async () => {
145
+ await fc.assert(
146
+ fc.asyncProperty(
147
+ arbitraries.apiKey(),
148
+ arbitraries.userId(),
149
+ arbitraries.ahaValue(),
150
+ async (apiKey, userId, value) => {
151
+ // Arrange
152
+ mockClient.post.mockResolvedValue({
153
+ data: { success: true, message: 'Score declared' }
154
+ });
155
+ const sdk = new Rooguys(apiKey);
156
+
157
+ // Act
158
+ await sdk.aha.declare(userId, value);
159
+
160
+ // Assert
161
+ expect(mockClient.post).toHaveBeenCalledWith(
162
+ '/aha/declare',
163
+ {
164
+ user_id: userId,
165
+ value,
166
+ }
167
+ );
168
+ }
169
+ ),
170
+ { numRuns: 100 }
171
+ );
172
+ });
173
+
174
+ it('should include API key in request headers', async () => {
175
+ await fc.assert(
176
+ fc.asyncProperty(
177
+ arbitraries.apiKey(),
178
+ arbitraries.userId(),
179
+ async (apiKey, userId) => {
180
+ // Arrange
181
+ mockClient.get.mockResolvedValue({
182
+ data: { user_id: userId, points: 100 }
183
+ });
184
+
185
+ // Act
186
+ const sdk = new Rooguys(apiKey);
187
+ await sdk.users.get(userId);
188
+
189
+ // Assert - verify axios.create was called with correct headers
190
+ expect(mockedAxios.create).toHaveBeenCalledWith(
191
+ expect.objectContaining({
192
+ headers: expect.objectContaining({
193
+ 'x-api-key': apiKey,
194
+ 'Content-Type': 'application/json',
195
+ })
196
+ })
197
+ );
198
+ }
199
+ ),
200
+ { numRuns: 100 }
201
+ );
202
+ });
203
+ });