@incodetech/core 2.0.0-alpha.2 → 2.0.0-alpha.3

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 (134) hide show
  1. package/package.json +4 -1
  2. package/.turbo/turbo-build.log +0 -33
  3. package/.turbo/turbo-coverage.log +0 -22
  4. package/.turbo/turbo-format.log +0 -6
  5. package/.turbo/turbo-lint$colon$fix.log +0 -77
  6. package/.turbo/turbo-lint.log +0 -95
  7. package/.turbo/turbo-test.log +0 -870
  8. package/.turbo/turbo-typecheck.log +0 -5
  9. package/coverage/base.css +0 -224
  10. package/coverage/block-navigation.js +0 -87
  11. package/coverage/favicon.png +0 -0
  12. package/coverage/index.html +0 -221
  13. package/coverage/prettify.css +0 -1
  14. package/coverage/prettify.js +0 -2
  15. package/coverage/sort-arrow-sprite.png +0 -0
  16. package/coverage/sorter.js +0 -210
  17. package/coverage/src/camera/cameraService.ts.html +0 -580
  18. package/coverage/src/camera/cameraServices.ts.html +0 -163
  19. package/coverage/src/camera/cameraStateMachine.ts.html +0 -877
  20. package/coverage/src/camera/index.html +0 -146
  21. package/coverage/src/email/emailActor.ts.html +0 -130
  22. package/coverage/src/email/emailManager.ts.html +0 -1366
  23. package/coverage/src/email/emailStateMachine.ts.html +0 -1186
  24. package/coverage/src/email/index.html +0 -146
  25. package/coverage/src/flow/flowActor.ts.html +0 -124
  26. package/coverage/src/flow/flowAnalyzer.ts.html +0 -196
  27. package/coverage/src/flow/flowManager.ts.html +0 -790
  28. package/coverage/src/flow/flowServices.ts.html +0 -124
  29. package/coverage/src/flow/flowStateMachine.ts.html +0 -631
  30. package/coverage/src/flow/index.html +0 -221
  31. package/coverage/src/flow/moduleLoader.ts.html +0 -304
  32. package/coverage/src/flow/orchestratedFlowManager.ts.html +0 -778
  33. package/coverage/src/flow/orchestratedFlowStateMachine.ts.html +0 -1060
  34. package/coverage/src/http/api.ts.html +0 -355
  35. package/coverage/src/http/endpoints.ts.html +0 -136
  36. package/coverage/src/http/index.html +0 -131
  37. package/coverage/src/index.html +0 -116
  38. package/coverage/src/phone/index.html +0 -146
  39. package/coverage/src/phone/phoneActor.ts.html +0 -130
  40. package/coverage/src/phone/phoneManager.ts.html +0 -1459
  41. package/coverage/src/phone/phoneStateMachine.ts.html +0 -1351
  42. package/coverage/src/recordings/index.html +0 -116
  43. package/coverage/src/recordings/recordingsRepository.ts.html +0 -229
  44. package/coverage/src/selfie/index.html +0 -191
  45. package/coverage/src/selfie/selfieActor.ts.html +0 -136
  46. package/coverage/src/selfie/selfieErrorUtils.ts.html +0 -283
  47. package/coverage/src/selfie/selfieManager.ts.html +0 -988
  48. package/coverage/src/selfie/selfieStateMachine.ts.html +0 -2497
  49. package/coverage/src/selfie/selfieUploadService.ts.html +0 -328
  50. package/coverage/src/selfie/types.ts.html +0 -394
  51. package/coverage/src/setup.ts.html +0 -598
  52. package/src/camera/cameraActor.ts +0 -21
  53. package/src/camera/cameraService.test.ts +0 -437
  54. package/src/camera/cameraService.ts +0 -165
  55. package/src/camera/cameraServices.test.ts +0 -66
  56. package/src/camera/cameraServices.ts +0 -26
  57. package/src/camera/cameraStateMachine.test.ts +0 -602
  58. package/src/camera/cameraStateMachine.ts +0 -264
  59. package/src/camera/index.ts +0 -5
  60. package/src/camera/types.ts +0 -17
  61. package/src/device/getBrowser.ts +0 -31
  62. package/src/device/getDeviceClass.ts +0 -29
  63. package/src/device/index.ts +0 -2
  64. package/src/email/__mocks__/emailMocks.ts +0 -59
  65. package/src/email/emailActor.ts +0 -15
  66. package/src/email/emailManager.test.ts +0 -573
  67. package/src/email/emailManager.ts +0 -427
  68. package/src/email/emailServices.ts +0 -66
  69. package/src/email/emailStateMachine.test.ts +0 -741
  70. package/src/email/emailStateMachine.ts +0 -367
  71. package/src/email/index.ts +0 -39
  72. package/src/email/types.ts +0 -60
  73. package/src/events/addEvent.ts +0 -20
  74. package/src/events/types.ts +0 -7
  75. package/src/flow/__mocks__/flowMocks.ts +0 -84
  76. package/src/flow/flowActor.ts +0 -13
  77. package/src/flow/flowAnalyzer.test.ts +0 -266
  78. package/src/flow/flowAnalyzer.ts +0 -37
  79. package/src/flow/flowCompletionService.ts +0 -21
  80. package/src/flow/flowManager.test.ts +0 -560
  81. package/src/flow/flowManager.ts +0 -235
  82. package/src/flow/flowServices.test.ts +0 -109
  83. package/src/flow/flowServices.ts +0 -13
  84. package/src/flow/flowStateMachine.test.ts +0 -334
  85. package/src/flow/flowStateMachine.ts +0 -182
  86. package/src/flow/index.ts +0 -21
  87. package/src/flow/moduleLoader.test.ts +0 -136
  88. package/src/flow/moduleLoader.ts +0 -73
  89. package/src/flow/orchestratedFlowManager.test.ts +0 -240
  90. package/src/flow/orchestratedFlowManager.ts +0 -231
  91. package/src/flow/orchestratedFlowStateMachine.test.ts +0 -199
  92. package/src/flow/orchestratedFlowStateMachine.ts +0 -325
  93. package/src/flow/types.ts +0 -434
  94. package/src/http/__mocks__/api.ts +0 -88
  95. package/src/http/api.test.ts +0 -231
  96. package/src/http/api.ts +0 -90
  97. package/src/http/endpoints.ts +0 -17
  98. package/src/index.ts +0 -33
  99. package/src/permissions/index.ts +0 -2
  100. package/src/permissions/permissionServices.ts +0 -31
  101. package/src/permissions/types.ts +0 -3
  102. package/src/phone/__mocks__/phoneMocks.ts +0 -71
  103. package/src/phone/index.ts +0 -39
  104. package/src/phone/phoneActor.ts +0 -15
  105. package/src/phone/phoneManager.test.ts +0 -393
  106. package/src/phone/phoneManager.ts +0 -458
  107. package/src/phone/phoneServices.ts +0 -98
  108. package/src/phone/phoneStateMachine.test.ts +0 -918
  109. package/src/phone/phoneStateMachine.ts +0 -422
  110. package/src/phone/types.ts +0 -83
  111. package/src/recordings/recordingsRepository.test.ts +0 -87
  112. package/src/recordings/recordingsRepository.ts +0 -48
  113. package/src/recordings/streamingEvents.ts +0 -10
  114. package/src/selfie/__mocks__/selfieMocks.ts +0 -26
  115. package/src/selfie/index.ts +0 -14
  116. package/src/selfie/selfieActor.ts +0 -17
  117. package/src/selfie/selfieErrorUtils.test.ts +0 -116
  118. package/src/selfie/selfieErrorUtils.ts +0 -66
  119. package/src/selfie/selfieManager.test.ts +0 -297
  120. package/src/selfie/selfieManager.ts +0 -301
  121. package/src/selfie/selfieServices.ts +0 -362
  122. package/src/selfie/selfieStateMachine.test.ts +0 -283
  123. package/src/selfie/selfieStateMachine.ts +0 -804
  124. package/src/selfie/selfieUploadService.test.ts +0 -90
  125. package/src/selfie/selfieUploadService.ts +0 -81
  126. package/src/selfie/types.ts +0 -103
  127. package/src/session/index.ts +0 -5
  128. package/src/session/sessionService.ts +0 -78
  129. package/src/setup.test.ts +0 -61
  130. package/src/setup.ts +0 -171
  131. package/tsconfig.json +0 -13
  132. package/tsdown.config.ts +0 -22
  133. package/vitest.config.ts +0 -37
  134. package/vitest.setup.ts +0 -135
@@ -1,741 +0,0 @@
1
- import { createActor } from '@incodetech/infra';
2
- import { beforeEach, describe, expect, it, vi } from 'vitest';
3
- import { mockEmailConfig, mockEmailConfigNoOtp } from './__mocks__/emailMocks';
4
- import { type EmailEvent, emailMachine } from './emailStateMachine';
5
-
6
- // Mock the email services - using same pattern as phone tests
7
- vi.mock('./emailServices', () => ({
8
- fetchEmail: vi.fn().mockResolvedValue({ email: 'user@example.com' }),
9
- addEmail: vi.fn().mockResolvedValue({ success: true }),
10
- sendEmailOtp: vi.fn().mockResolvedValue(undefined),
11
- verifyEmailOtp: vi.fn().mockResolvedValue({ success: true }),
12
- }));
13
-
14
- // Mock the events module
15
- vi.mock('../events/addEvent', () => ({
16
- addEvent: vi.fn().mockResolvedValue(undefined),
17
- }));
18
-
19
- describe('emailMachine', () => {
20
- beforeEach(() => {
21
- vi.clearAllMocks();
22
- });
23
-
24
- describe('Initial state', () => {
25
- it('should start in idle state', () => {
26
- const actor = createActor(emailMachine, {
27
- input: { config: mockEmailConfig },
28
- }).start();
29
-
30
- expect(actor.getSnapshot().value).toBe('idle');
31
-
32
- actor.stop();
33
- });
34
-
35
- it('should have initial context with config', () => {
36
- const actor = createActor(emailMachine, {
37
- input: { config: mockEmailConfig },
38
- }).start();
39
-
40
- const { context } = actor.getSnapshot();
41
- expect(context.config).toEqual(mockEmailConfig);
42
- expect(context.email).toBe('');
43
- expect(context.isValid).toBe(false);
44
-
45
- actor.stop();
46
- });
47
- });
48
-
49
- describe('LOAD event', () => {
50
- it('should transition to inputting when prefill is disabled', () => {
51
- const actor = createActor(emailMachine, {
52
- input: { config: mockEmailConfig },
53
- }).start();
54
-
55
- actor.send({ type: 'LOAD' });
56
-
57
- expect(actor.getSnapshot().value).toBe('inputting');
58
-
59
- actor.stop();
60
- });
61
-
62
- it('should transition to loadingPrefill when prefill is enabled', () => {
63
- const configWithPrefill = { ...mockEmailConfig, prefill: true };
64
- const actor = createActor(emailMachine, {
65
- input: { config: configWithPrefill },
66
- }).start();
67
-
68
- actor.send({ type: 'LOAD' });
69
-
70
- expect(actor.getSnapshot().value).toBe('loadingPrefill');
71
-
72
- actor.stop();
73
- });
74
- });
75
-
76
- describe('EMAIL_CHANGED event', () => {
77
- it('should update email and isValid in context', () => {
78
- const actor = createActor(emailMachine, {
79
- input: { config: mockEmailConfig },
80
- }).start();
81
-
82
- actor.send({ type: 'LOAD' });
83
-
84
- actor.send({
85
- type: 'EMAIL_CHANGED',
86
- email: 'user@example.com',
87
- isValid: true,
88
- } as EmailEvent);
89
-
90
- const { context } = actor.getSnapshot();
91
- expect(context.email).toBe('user@example.com');
92
- expect(context.isValid).toBe(true);
93
- expect(context.emailError).toBeUndefined();
94
-
95
- actor.stop();
96
- });
97
-
98
- it('should set emailError when email is invalid', () => {
99
- const actor = createActor(emailMachine, {
100
- input: { config: mockEmailConfig },
101
- }).start();
102
-
103
- actor.send({ type: 'LOAD' });
104
-
105
- actor.send({
106
- type: 'EMAIL_CHANGED',
107
- email: 'invalid-email',
108
- isValid: false,
109
- } as EmailEvent);
110
-
111
- const { context } = actor.getSnapshot();
112
- expect(context.email).toBe('invalid-email');
113
- expect(context.isValid).toBe(false);
114
- expect(context.emailError).toBe('Invalid email address');
115
-
116
- actor.stop();
117
- });
118
- });
119
-
120
- describe('SUBMIT event', () => {
121
- it('should transition to submitting when email is valid', () => {
122
- const actor = createActor(emailMachine, {
123
- input: { config: mockEmailConfig },
124
- }).start();
125
-
126
- actor.send({ type: 'LOAD' });
127
- actor.send({
128
- type: 'EMAIL_CHANGED',
129
- email: 'user@example.com',
130
- isValid: true,
131
- } as EmailEvent);
132
- actor.send({ type: 'SUBMIT' });
133
-
134
- expect(actor.getSnapshot().value).toBe('submitting');
135
-
136
- actor.stop();
137
- });
138
-
139
- it('should not transition when email is invalid', () => {
140
- const actor = createActor(emailMachine, {
141
- input: { config: mockEmailConfig },
142
- }).start();
143
-
144
- actor.send({ type: 'LOAD' });
145
- actor.send({
146
- type: 'EMAIL_CHANGED',
147
- email: 'invalid-email',
148
- isValid: false,
149
- } as EmailEvent);
150
- actor.send({ type: 'SUBMIT' });
151
-
152
- expect(actor.getSnapshot().value).toBe('inputting');
153
-
154
- actor.stop();
155
- });
156
- });
157
-
158
- describe('OTP flow', () => {
159
- it('should transition to sendingOtp when otpVerification is enabled', async () => {
160
- const actor = createActor(emailMachine, {
161
- input: { config: mockEmailConfig },
162
- }).start();
163
-
164
- actor.send({ type: 'LOAD' });
165
- actor.send({
166
- type: 'EMAIL_CHANGED',
167
- email: 'user@example.com',
168
- isValid: true,
169
- } as EmailEvent);
170
- actor.send({ type: 'SUBMIT' });
171
-
172
- await vi.waitFor(() => {
173
- const value = actor.getSnapshot().value;
174
- return value === 'sendingOtp' || value === 'awaitingOtp';
175
- });
176
-
177
- actor.stop();
178
- });
179
-
180
- it('should transition to success when otpVerification is disabled', async () => {
181
- const actor = createActor(emailMachine, {
182
- input: { config: mockEmailConfigNoOtp },
183
- }).start();
184
-
185
- actor.send({ type: 'LOAD' });
186
- actor.send({
187
- type: 'EMAIL_CHANGED',
188
- email: 'user@example.com',
189
- isValid: true,
190
- } as EmailEvent);
191
- actor.send({ type: 'SUBMIT' });
192
-
193
- await vi.waitFor(() => {
194
- const value = actor.getSnapshot().value;
195
- return value === 'success';
196
- });
197
-
198
- actor.stop();
199
- });
200
-
201
- it('should handle OTP_CHANGED event', async () => {
202
- const actor = createActor(emailMachine, {
203
- input: { config: mockEmailConfig },
204
- }).start();
205
-
206
- actor.send({ type: 'LOAD' });
207
- actor.send({
208
- type: 'EMAIL_CHANGED',
209
- email: 'user@example.com',
210
- isValid: true,
211
- } as EmailEvent);
212
- actor.send({ type: 'SUBMIT' });
213
-
214
- await vi.waitFor(() => {
215
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
216
- });
217
-
218
- actor.send({ type: 'OTP_CHANGED', code: 'ABC123' } as EmailEvent);
219
-
220
- const { context } = actor.getSnapshot();
221
- expect(context.otpCode).toBe('ABC123');
222
- expect(context.otpError).toBeUndefined();
223
-
224
- actor.stop();
225
- });
226
-
227
- it('should handle VERIFY_OTP event', async () => {
228
- const actor = createActor(emailMachine, {
229
- input: { config: mockEmailConfig },
230
- }).start();
231
-
232
- actor.send({ type: 'LOAD' });
233
- actor.send({
234
- type: 'EMAIL_CHANGED',
235
- email: 'user@example.com',
236
- isValid: true,
237
- } as EmailEvent);
238
- actor.send({ type: 'SUBMIT' });
239
-
240
- await vi.waitFor(() => {
241
- return actor.getSnapshot().value === 'awaitingOtp';
242
- });
243
-
244
- actor.send({ type: 'OTP_CHANGED', code: 'ABC123' } as EmailEvent);
245
- actor.send({ type: 'VERIFY_OTP' });
246
-
247
- await vi.waitFor(() => {
248
- const value = actor.getSnapshot().value;
249
- return value === 'verifyingOtp' || value === 'success';
250
- });
251
-
252
- actor.stop();
253
- });
254
-
255
- it('should handle RESEND_OTP event', async () => {
256
- const actor = createActor(emailMachine, {
257
- input: { config: mockEmailConfig },
258
- }).start();
259
-
260
- actor.send({ type: 'LOAD' });
261
- actor.send({
262
- type: 'EMAIL_CHANGED',
263
- email: 'user@example.com',
264
- isValid: true,
265
- } as EmailEvent);
266
- actor.send({ type: 'SUBMIT' });
267
-
268
- await vi.waitFor(() => {
269
- return actor.getSnapshot().value === 'awaitingOtp';
270
- });
271
-
272
- // Wait for timer to expire
273
- await vi.waitFor(() => {
274
- const { context } = actor.getSnapshot();
275
- return !context.resendTimerActive;
276
- });
277
-
278
- actor.send({ type: 'RESEND_OTP' });
279
-
280
- await vi.waitFor(() => {
281
- return actor.getSnapshot().value === 'sendingOtp';
282
- });
283
-
284
- actor.stop();
285
- });
286
-
287
- it('should handle BACK event from awaitingOtp', async () => {
288
- const actor = createActor(emailMachine, {
289
- input: { config: mockEmailConfig },
290
- }).start();
291
-
292
- actor.send({ type: 'LOAD' });
293
- actor.send({
294
- type: 'EMAIL_CHANGED',
295
- email: 'user@example.com',
296
- isValid: true,
297
- } as EmailEvent);
298
- actor.send({ type: 'SUBMIT' });
299
-
300
- await vi.waitFor(() => {
301
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
302
- });
303
-
304
- actor.send({ type: 'BACK' });
305
-
306
- expect(actor.getSnapshot().value).toBe('inputting');
307
-
308
- actor.stop();
309
- });
310
- });
311
-
312
- describe('Error handling', () => {
313
- it('should handle email submission error', async () => {
314
- // Import and set up the rejection mock before creating the actor
315
- const emailServices = await import('./emailServices');
316
- vi.mocked(emailServices.addEmail).mockRejectedValueOnce(
317
- new Error('Network error'),
318
- );
319
-
320
- const actor = createActor(emailMachine, {
321
- input: { config: mockEmailConfig },
322
- }).start();
323
-
324
- actor.send({ type: 'LOAD' });
325
- actor.send({
326
- type: 'EMAIL_CHANGED',
327
- email: 'user@example.com',
328
- isValid: true,
329
- } as EmailEvent);
330
- actor.send({ type: 'SUBMIT' });
331
-
332
- // Wait for the state machine to handle the error and return to inputting
333
- await vi.waitFor(() => {
334
- const value = actor.getSnapshot().value;
335
- // Should either be back in inputting (with error) or stay in submitting
336
- return value === 'inputting' || value === 'error';
337
- });
338
-
339
- actor.stop();
340
- });
341
-
342
- it('should handle OTP verification error', async () => {
343
- const { verifyEmailOtp } = await import('./emailServices');
344
- vi.mocked(verifyEmailOtp).mockResolvedValueOnce({ success: false });
345
-
346
- const actor = createActor(emailMachine, {
347
- input: { config: mockEmailConfig },
348
- }).start();
349
-
350
- actor.send({ type: 'LOAD' });
351
- actor.send({
352
- type: 'EMAIL_CHANGED',
353
- email: 'user@example.com',
354
- isValid: true,
355
- } as EmailEvent);
356
- actor.send({ type: 'SUBMIT' });
357
-
358
- await vi.waitFor(() => {
359
- return actor.getSnapshot().value === 'awaitingOtp';
360
- });
361
-
362
- actor.send({ type: 'OTP_CHANGED', code: 'WRONG' } as EmailEvent);
363
- actor.send({ type: 'VERIFY_OTP' });
364
-
365
- await vi.waitFor(() => {
366
- const value = actor.getSnapshot().value;
367
- return value === 'otpError' || value === 'error';
368
- });
369
-
370
- actor.stop();
371
- });
372
- });
373
-
374
- describe('RESET event', () => {
375
- it('should reset context and return to idle from success', async () => {
376
- const actor = createActor(emailMachine, {
377
- input: { config: mockEmailConfigNoOtp },
378
- }).start();
379
-
380
- actor.send({ type: 'LOAD' });
381
-
382
- await vi.waitFor(() => {
383
- expect(actor.getSnapshot().value).toBe('inputting');
384
- });
385
-
386
- actor.send({
387
- type: 'EMAIL_CHANGED',
388
- email: 'user@example.com',
389
- isValid: true,
390
- } as EmailEvent);
391
- actor.send({ type: 'SUBMIT' });
392
-
393
- await vi.waitFor(() => {
394
- expect(actor.getSnapshot().value).toBe('success');
395
- });
396
-
397
- actor.send({ type: 'RESET' });
398
-
399
- expect(actor.getSnapshot().value).toBe('idle');
400
- const { context } = actor.getSnapshot();
401
- expect(context.email).toBe('');
402
- expect(context.isValid).toBe(false);
403
-
404
- actor.stop();
405
- });
406
-
407
- it('should reset context and return to idle from error', async () => {
408
- const { addEmail } = await import('./emailServices');
409
- vi.mocked(addEmail).mockRejectedValueOnce(new Error('Fatal error'));
410
-
411
- const actor = createActor(emailMachine, {
412
- input: { config: mockEmailConfigNoOtp },
413
- }).start();
414
-
415
- actor.send({ type: 'LOAD' });
416
-
417
- await vi.waitFor(() => {
418
- expect(actor.getSnapshot().value).toBe('inputting');
419
- });
420
-
421
- actor.send({
422
- type: 'EMAIL_CHANGED',
423
- email: 'user@example.com',
424
- isValid: true,
425
- } as EmailEvent);
426
- actor.send({ type: 'SUBMIT' });
427
-
428
- await vi.waitFor(() => {
429
- const value = actor.getSnapshot().value;
430
- return value === 'inputting' || value === 'error';
431
- });
432
-
433
- // If we're in error state, test RESET
434
- if (actor.getSnapshot().value === 'error') {
435
- actor.send({ type: 'RESET' });
436
- expect(actor.getSnapshot().value).toBe('idle');
437
- }
438
-
439
- actor.stop();
440
- });
441
- });
442
-
443
- describe('Prefill flow', () => {
444
- it('should load prefilled email and transition to inputting', async () => {
445
- const configWithPrefill = { ...mockEmailConfig, prefill: true };
446
- const actor = createActor(emailMachine, {
447
- input: { config: configWithPrefill },
448
- }).start();
449
-
450
- actor.send({ type: 'LOAD' });
451
-
452
- await vi.waitFor(() => {
453
- expect(actor.getSnapshot().value).toBe('inputting');
454
- });
455
-
456
- const { context } = actor.getSnapshot();
457
- expect(context.prefilledEmail).toBe('user@example.com');
458
- expect(context.email).toBe('user@example.com');
459
-
460
- actor.stop();
461
- });
462
-
463
- it('should handle prefill fetch error and still transition to inputting', async () => {
464
- const { fetchEmail } = await import('./emailServices');
465
- vi.mocked(fetchEmail).mockRejectedValueOnce(new Error('Network error'));
466
-
467
- const configWithPrefill = { ...mockEmailConfig, prefill: true };
468
- const actor = createActor(emailMachine, {
469
- input: { config: configWithPrefill },
470
- }).start();
471
-
472
- actor.send({ type: 'LOAD' });
473
-
474
- await vi.waitFor(() => {
475
- expect(actor.getSnapshot().value).toBe('inputting');
476
- });
477
-
478
- // Should still be in inputting even if prefill failed
479
- const { context } = actor.getSnapshot();
480
- expect(context.prefilledEmail).toBeUndefined();
481
-
482
- actor.stop();
483
- });
484
- });
485
-
486
- describe('OTP error handling', () => {
487
- it('should transition to otpError when OTP verification fails with attempts remaining', async () => {
488
- const { verifyEmailOtp } = await import('./emailServices');
489
- vi.mocked(verifyEmailOtp).mockResolvedValueOnce({ success: false });
490
-
491
- const actor = createActor(emailMachine, {
492
- input: { config: mockEmailConfig },
493
- }).start();
494
-
495
- actor.send({ type: 'LOAD' });
496
-
497
- await vi.waitFor(() => {
498
- expect(actor.getSnapshot().value).toBe('inputting');
499
- });
500
-
501
- actor.send({
502
- type: 'EMAIL_CHANGED',
503
- email: 'user@example.com',
504
- isValid: true,
505
- } as EmailEvent);
506
- actor.send({ type: 'SUBMIT' });
507
-
508
- await vi.waitFor(() => {
509
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
510
- });
511
-
512
- actor.send({ type: 'OTP_CHANGED', code: 'WRONG1' } as EmailEvent);
513
- actor.send({ type: 'VERIFY_OTP' });
514
-
515
- await vi.waitFor(() => {
516
- expect(actor.getSnapshot().value).toBe('otpError');
517
- });
518
-
519
- const { context } = actor.getSnapshot();
520
- expect(context.attemptsRemaining).toBe(2);
521
- expect(context.otpError).toBe('Invalid OTP code');
522
-
523
- actor.stop();
524
- });
525
-
526
- it('should allow retry from otpError state via OTP_CHANGED', async () => {
527
- const { verifyEmailOtp } = await import('./emailServices');
528
- vi.mocked(verifyEmailOtp).mockResolvedValueOnce({ success: false });
529
-
530
- const actor = createActor(emailMachine, {
531
- input: { config: mockEmailConfig },
532
- }).start();
533
-
534
- actor.send({ type: 'LOAD' });
535
-
536
- await vi.waitFor(() => {
537
- expect(actor.getSnapshot().value).toBe('inputting');
538
- });
539
-
540
- actor.send({
541
- type: 'EMAIL_CHANGED',
542
- email: 'user@example.com',
543
- isValid: true,
544
- } as EmailEvent);
545
- actor.send({ type: 'SUBMIT' });
546
-
547
- await vi.waitFor(() => {
548
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
549
- });
550
-
551
- actor.send({ type: 'OTP_CHANGED', code: 'WRONG1' } as EmailEvent);
552
- actor.send({ type: 'VERIFY_OTP' });
553
-
554
- await vi.waitFor(() => {
555
- expect(actor.getSnapshot().value).toBe('otpError');
556
- });
557
-
558
- // OTP_CHANGED from otpError should transition back to awaitingOtp
559
- actor.send({ type: 'OTP_CHANGED', code: 'RETRY' } as EmailEvent);
560
-
561
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
562
-
563
- actor.stop();
564
- });
565
-
566
- it('should transition to error when all OTP attempts are exhausted', async () => {
567
- const { verifyEmailOtp } = await import('./emailServices');
568
- vi.mocked(verifyEmailOtp).mockResolvedValue({ success: false });
569
-
570
- const configWith1Attempt = { ...mockEmailConfig, maxOtpAttempts: 1 };
571
- const actor = createActor(emailMachine, {
572
- input: { config: configWith1Attempt },
573
- }).start();
574
-
575
- actor.send({ type: 'LOAD' });
576
-
577
- await vi.waitFor(() => {
578
- expect(actor.getSnapshot().value).toBe('inputting');
579
- });
580
-
581
- actor.send({
582
- type: 'EMAIL_CHANGED',
583
- email: 'user@example.com',
584
- isValid: true,
585
- } as EmailEvent);
586
- actor.send({ type: 'SUBMIT' });
587
-
588
- await vi.waitFor(() => {
589
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
590
- });
591
-
592
- actor.send({ type: 'OTP_CHANGED', code: 'WRONG' } as EmailEvent);
593
- actor.send({ type: 'VERIFY_OTP' });
594
-
595
- await vi.waitFor(() => {
596
- const value = actor.getSnapshot().value;
597
- return value === 'error' || value === 'otpError';
598
- });
599
-
600
- actor.stop();
601
- });
602
-
603
- it('should handle BACK from otpError state', async () => {
604
- const { verifyEmailOtp } = await import('./emailServices');
605
- vi.mocked(verifyEmailOtp).mockResolvedValueOnce({ success: false });
606
-
607
- const actor = createActor(emailMachine, {
608
- input: { config: mockEmailConfig },
609
- }).start();
610
-
611
- actor.send({ type: 'LOAD' });
612
-
613
- await vi.waitFor(() => {
614
- expect(actor.getSnapshot().value).toBe('inputting');
615
- });
616
-
617
- actor.send({
618
- type: 'EMAIL_CHANGED',
619
- email: 'user@example.com',
620
- isValid: true,
621
- } as EmailEvent);
622
- actor.send({ type: 'SUBMIT' });
623
-
624
- await vi.waitFor(() => {
625
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
626
- });
627
-
628
- actor.send({ type: 'OTP_CHANGED', code: 'WRONG' } as EmailEvent);
629
- actor.send({ type: 'VERIFY_OTP' });
630
-
631
- await vi.waitFor(() => {
632
- expect(actor.getSnapshot().value).toBe('otpError');
633
- });
634
-
635
- actor.send({ type: 'BACK' });
636
-
637
- expect(actor.getSnapshot().value).toBe('inputting');
638
-
639
- actor.stop();
640
- });
641
- });
642
-
643
- describe('Resend OTP flow', () => {
644
- it('should not resend when timer is still active', async () => {
645
- const actor = createActor(emailMachine, {
646
- input: { config: mockEmailConfig },
647
- }).start();
648
-
649
- actor.send({ type: 'LOAD' });
650
-
651
- await vi.waitFor(() => {
652
- expect(actor.getSnapshot().value).toBe('inputting');
653
- });
654
-
655
- actor.send({
656
- type: 'EMAIL_CHANGED',
657
- email: 'user@example.com',
658
- isValid: true,
659
- } as EmailEvent);
660
- actor.send({ type: 'SUBMIT' });
661
-
662
- await vi.waitFor(() => {
663
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
664
- });
665
-
666
- // Timer should be active immediately after entering awaitingOtp
667
- const { context } = actor.getSnapshot();
668
- expect(context.resendTimerActive).toBe(true);
669
-
670
- // Try to resend (should be blocked by guard)
671
- actor.send({ type: 'RESEND_OTP' });
672
-
673
- // Should still be in awaitingOtp, not sendingOtp
674
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
675
-
676
- actor.stop();
677
- });
678
- });
679
-
680
- describe('SendOtp error handling', () => {
681
- it('should handle sendOtp error and still transition to awaitingOtp', async () => {
682
- const { sendEmailOtp } = await import('./emailServices');
683
- vi.mocked(sendEmailOtp).mockRejectedValueOnce(new Error('SMS failed'));
684
-
685
- const actor = createActor(emailMachine, {
686
- input: { config: mockEmailConfig },
687
- }).start();
688
-
689
- actor.send({ type: 'LOAD' });
690
-
691
- await vi.waitFor(() => {
692
- expect(actor.getSnapshot().value).toBe('inputting');
693
- });
694
-
695
- actor.send({
696
- type: 'EMAIL_CHANGED',
697
- email: 'user@example.com',
698
- isValid: true,
699
- } as EmailEvent);
700
- actor.send({ type: 'SUBMIT' });
701
-
702
- // Even if sendOtp fails, should transition to awaitingOtp (with error in context)
703
- await vi.waitFor(() => {
704
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
705
- });
706
-
707
- actor.stop();
708
- });
709
- });
710
-
711
- describe('Resend timer', () => {
712
- it('should initialize resend timer when entering awaitingOtp', async () => {
713
- const actor = createActor(emailMachine, {
714
- input: { config: mockEmailConfig },
715
- }).start();
716
-
717
- actor.send({ type: 'LOAD' });
718
-
719
- await vi.waitFor(() => {
720
- expect(actor.getSnapshot().value).toBe('inputting');
721
- });
722
-
723
- actor.send({
724
- type: 'EMAIL_CHANGED',
725
- email: 'user@example.com',
726
- isValid: true,
727
- } as EmailEvent);
728
- actor.send({ type: 'SUBMIT' });
729
-
730
- await vi.waitFor(() => {
731
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
732
- });
733
-
734
- const { context } = actor.getSnapshot();
735
- expect(context.resendTimer).toBe(30);
736
- expect(context.resendTimerActive).toBe(true);
737
-
738
- actor.stop();
739
- });
740
- });
741
- });