@incodetech/core 2.0.0-alpha.1

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 (84) hide show
  1. package/package.json +51 -0
  2. package/src/camera/cameraActor.ts +21 -0
  3. package/src/camera/cameraService.test.ts +437 -0
  4. package/src/camera/cameraService.ts +165 -0
  5. package/src/camera/cameraServices.test.ts +66 -0
  6. package/src/camera/cameraServices.ts +26 -0
  7. package/src/camera/cameraStateMachine.test.ts +602 -0
  8. package/src/camera/cameraStateMachine.ts +264 -0
  9. package/src/camera/index.ts +5 -0
  10. package/src/camera/types.ts +17 -0
  11. package/src/device/getBrowser.ts +31 -0
  12. package/src/device/getDeviceClass.ts +29 -0
  13. package/src/device/index.ts +2 -0
  14. package/src/email/__mocks__/emailMocks.ts +59 -0
  15. package/src/email/emailActor.ts +15 -0
  16. package/src/email/emailManager.test.ts +573 -0
  17. package/src/email/emailManager.ts +427 -0
  18. package/src/email/emailServices.ts +66 -0
  19. package/src/email/emailStateMachine.test.ts +741 -0
  20. package/src/email/emailStateMachine.ts +367 -0
  21. package/src/email/index.ts +39 -0
  22. package/src/email/types.ts +60 -0
  23. package/src/events/addEvent.ts +20 -0
  24. package/src/events/types.ts +7 -0
  25. package/src/flow/__mocks__/flowMocks.ts +84 -0
  26. package/src/flow/flowActor.ts +13 -0
  27. package/src/flow/flowAnalyzer.test.ts +266 -0
  28. package/src/flow/flowAnalyzer.ts +37 -0
  29. package/src/flow/flowCompletionService.ts +21 -0
  30. package/src/flow/flowManager.test.ts +560 -0
  31. package/src/flow/flowManager.ts +235 -0
  32. package/src/flow/flowServices.test.ts +109 -0
  33. package/src/flow/flowServices.ts +13 -0
  34. package/src/flow/flowStateMachine.test.ts +334 -0
  35. package/src/flow/flowStateMachine.ts +182 -0
  36. package/src/flow/index.ts +21 -0
  37. package/src/flow/moduleLoader.test.ts +136 -0
  38. package/src/flow/moduleLoader.ts +73 -0
  39. package/src/flow/orchestratedFlowManager.test.ts +240 -0
  40. package/src/flow/orchestratedFlowManager.ts +231 -0
  41. package/src/flow/orchestratedFlowStateMachine.test.ts +199 -0
  42. package/src/flow/orchestratedFlowStateMachine.ts +325 -0
  43. package/src/flow/types.ts +434 -0
  44. package/src/http/__mocks__/api.ts +88 -0
  45. package/src/http/api.test.ts +231 -0
  46. package/src/http/api.ts +90 -0
  47. package/src/http/endpoints.ts +17 -0
  48. package/src/index.ts +33 -0
  49. package/src/permissions/index.ts +2 -0
  50. package/src/permissions/permissionServices.ts +31 -0
  51. package/src/permissions/types.ts +3 -0
  52. package/src/phone/__mocks__/phoneMocks.ts +71 -0
  53. package/src/phone/index.ts +39 -0
  54. package/src/phone/phoneActor.ts +15 -0
  55. package/src/phone/phoneManager.test.ts +393 -0
  56. package/src/phone/phoneManager.ts +458 -0
  57. package/src/phone/phoneServices.ts +98 -0
  58. package/src/phone/phoneStateMachine.test.ts +918 -0
  59. package/src/phone/phoneStateMachine.ts +422 -0
  60. package/src/phone/types.ts +83 -0
  61. package/src/recordings/recordingsRepository.test.ts +87 -0
  62. package/src/recordings/recordingsRepository.ts +48 -0
  63. package/src/recordings/streamingEvents.ts +10 -0
  64. package/src/selfie/__mocks__/selfieMocks.ts +26 -0
  65. package/src/selfie/index.ts +14 -0
  66. package/src/selfie/selfieActor.ts +17 -0
  67. package/src/selfie/selfieErrorUtils.test.ts +116 -0
  68. package/src/selfie/selfieErrorUtils.ts +66 -0
  69. package/src/selfie/selfieManager.test.ts +297 -0
  70. package/src/selfie/selfieManager.ts +301 -0
  71. package/src/selfie/selfieServices.ts +362 -0
  72. package/src/selfie/selfieStateMachine.test.ts +283 -0
  73. package/src/selfie/selfieStateMachine.ts +804 -0
  74. package/src/selfie/selfieUploadService.test.ts +90 -0
  75. package/src/selfie/selfieUploadService.ts +81 -0
  76. package/src/selfie/types.ts +103 -0
  77. package/src/session/index.ts +5 -0
  78. package/src/session/sessionService.ts +78 -0
  79. package/src/setup.test.ts +61 -0
  80. package/src/setup.ts +171 -0
  81. package/tsconfig.json +13 -0
  82. package/tsdown.config.ts +22 -0
  83. package/vitest.config.ts +37 -0
  84. package/vitest.setup.ts +135 -0
@@ -0,0 +1,741 @@
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
+ });