@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,918 +0,0 @@
1
- import { createActor } from '@incodetech/infra';
2
- import { beforeEach, describe, expect, it, vi } from 'vitest';
3
- import { mockPhoneConfig, mockPhoneConfigNoOtp } from './__mocks__/phoneMocks';
4
- import { type PhoneEvent, phoneMachine } from './phoneStateMachine';
5
-
6
- // Mock the phone services
7
- vi.mock('./phoneServices', () => ({
8
- fetchPhone: vi.fn().mockResolvedValue({ phone: '+14155551234' }),
9
- fetchStartInfo: vi.fn().mockResolvedValue({
10
- ipIsoCode: 'US',
11
- phonePrefix: '+1',
12
- country: 'United States',
13
- }),
14
- addPhone: vi.fn().mockResolvedValue({ success: true }),
15
- addPhoneInstantVerify: vi.fn().mockResolvedValue({ success: true }),
16
- sendOtp: vi.fn().mockResolvedValue(undefined),
17
- verifyOtp: vi.fn().mockResolvedValue({ success: true }),
18
- }));
19
-
20
- // Mock the events module
21
- vi.mock('../events/addEvent', () => ({
22
- addEvent: vi.fn().mockResolvedValue(undefined),
23
- }));
24
-
25
- describe('phoneMachine', () => {
26
- beforeEach(() => {
27
- vi.clearAllMocks();
28
- });
29
-
30
- describe('Initial state', () => {
31
- it('should start in idle state', () => {
32
- const actor = createActor(phoneMachine, {
33
- input: { config: mockPhoneConfig },
34
- }).start();
35
-
36
- expect(actor.getSnapshot().value).toBe('idle');
37
-
38
- actor.stop();
39
- });
40
-
41
- it('should have initial context with config', () => {
42
- const actor = createActor(phoneMachine, {
43
- input: { config: mockPhoneConfig },
44
- }).start();
45
-
46
- const { context } = actor.getSnapshot();
47
- expect(context.config).toEqual(mockPhoneConfig);
48
- expect(context.phone).toBe('');
49
- expect(context.isValid).toBe(false);
50
- // Country code and phone prefix are empty until start info is loaded
51
- expect(context.countryCode).toBe('');
52
- expect(context.phonePrefix).toBe('');
53
-
54
- actor.stop();
55
- });
56
- });
57
-
58
- describe('LOAD event', () => {
59
- it('should transition to loadingStartInfo when prefill is disabled', () => {
60
- const actor = createActor(phoneMachine, {
61
- input: { config: mockPhoneConfig },
62
- }).start();
63
-
64
- actor.send({ type: 'LOAD' });
65
-
66
- expect(actor.getSnapshot().value).toBe('loadingStartInfo');
67
-
68
- actor.stop();
69
- });
70
-
71
- it('should transition to loadingPrefill when prefill is enabled', () => {
72
- const configWithPrefill = { ...mockPhoneConfig, prefill: true };
73
- const actor = createActor(phoneMachine, {
74
- input: { config: configWithPrefill },
75
- }).start();
76
-
77
- actor.send({ type: 'LOAD' });
78
-
79
- expect(actor.getSnapshot().value).toBe('loadingPrefill');
80
-
81
- actor.stop();
82
- });
83
- });
84
-
85
- describe('Loading flow', () => {
86
- it('should transition to inputting after loading start info', async () => {
87
- const actor = createActor(phoneMachine, {
88
- input: { config: mockPhoneConfig },
89
- }).start();
90
-
91
- actor.send({ type: 'LOAD' });
92
-
93
- await vi.waitFor(() => {
94
- expect(actor.getSnapshot().value).toBe('inputting');
95
- });
96
-
97
- const { context } = actor.getSnapshot();
98
- expect(context.countryCode).toBe('US');
99
- expect(context.phonePrefix).toBe('+1');
100
-
101
- actor.stop();
102
- });
103
- });
104
-
105
- describe('Phone input', () => {
106
- it('should update phone on PHONE_CHANGED event', async () => {
107
- const actor = createActor(phoneMachine, {
108
- input: { config: mockPhoneConfig },
109
- }).start();
110
-
111
- actor.send({ type: 'LOAD' });
112
-
113
- await vi.waitFor(() => {
114
- expect(actor.getSnapshot().value).toBe('inputting');
115
- });
116
-
117
- actor.send({
118
- type: 'PHONE_CHANGED',
119
- phone: '+14155551234',
120
- isValid: true,
121
- } as PhoneEvent);
122
-
123
- const { context } = actor.getSnapshot();
124
- expect(context.phone).toBe('+14155551234');
125
- expect(context.isValid).toBe(true);
126
-
127
- actor.stop();
128
- });
129
-
130
- it('should update opt-in on OPT_IN_CHANGED event', async () => {
131
- const actor = createActor(phoneMachine, {
132
- input: { config: mockPhoneConfig },
133
- }).start();
134
-
135
- actor.send({ type: 'LOAD' });
136
-
137
- await vi.waitFor(() => {
138
- expect(actor.getSnapshot().value).toBe('inputting');
139
- });
140
-
141
- actor.send({ type: 'OPT_IN_CHANGED', granted: true } as PhoneEvent);
142
-
143
- const { context } = actor.getSnapshot();
144
- expect(context.optInGranted).toBe(true);
145
-
146
- actor.stop();
147
- });
148
- });
149
-
150
- describe('SUBMIT event', () => {
151
- it('should not submit when phone is invalid', async () => {
152
- const actor = createActor(phoneMachine, {
153
- input: { config: mockPhoneConfig },
154
- }).start();
155
-
156
- actor.send({ type: 'LOAD' });
157
-
158
- await vi.waitFor(() => {
159
- expect(actor.getSnapshot().value).toBe('inputting');
160
- });
161
-
162
- // Phone is invalid by default
163
- actor.send({ type: 'SUBMIT' });
164
-
165
- // Should still be in inputting state
166
- expect(actor.getSnapshot().value).toBe('inputting');
167
-
168
- actor.stop();
169
- });
170
-
171
- it('should transition to submitting when phone is valid', async () => {
172
- const actor = createActor(phoneMachine, {
173
- input: { config: mockPhoneConfig },
174
- }).start();
175
-
176
- actor.send({ type: 'LOAD' });
177
-
178
- await vi.waitFor(() => {
179
- expect(actor.getSnapshot().value).toBe('inputting');
180
- });
181
-
182
- actor.send({
183
- type: 'PHONE_CHANGED',
184
- phone: '+14155551234',
185
- isValid: true,
186
- } as PhoneEvent);
187
- actor.send({ type: 'SUBMIT' });
188
-
189
- expect(actor.getSnapshot().value).toBe('submitting');
190
-
191
- actor.stop();
192
- });
193
- });
194
-
195
- describe('Phone submission without OTP', () => {
196
- it('should transition to success after submitting phone', async () => {
197
- const actor = createActor(phoneMachine, {
198
- input: { config: mockPhoneConfigNoOtp },
199
- }).start();
200
-
201
- actor.send({ type: 'LOAD' });
202
-
203
- await vi.waitFor(() => {
204
- expect(actor.getSnapshot().value).toBe('inputting');
205
- });
206
-
207
- actor.send({
208
- type: 'PHONE_CHANGED',
209
- phone: '+14155551234',
210
- isValid: true,
211
- } as PhoneEvent);
212
- actor.send({ type: 'SUBMIT' });
213
-
214
- await vi.waitFor(() => {
215
- expect(actor.getSnapshot().value).toBe('success');
216
- });
217
-
218
- actor.stop();
219
- });
220
- });
221
-
222
- describe('OTP flow', () => {
223
- it('should transition to sendingOtp after submitting phone with OTP enabled', async () => {
224
- const actor = createActor(phoneMachine, {
225
- input: { config: mockPhoneConfig },
226
- }).start();
227
-
228
- actor.send({ type: 'LOAD' });
229
-
230
- await vi.waitFor(() => {
231
- expect(actor.getSnapshot().value).toBe('inputting');
232
- });
233
-
234
- actor.send({
235
- type: 'PHONE_CHANGED',
236
- phone: '+14155551234',
237
- isValid: true,
238
- } as PhoneEvent);
239
- actor.send({ type: 'SUBMIT' });
240
-
241
- await vi.waitFor(() => {
242
- const value = actor.getSnapshot().value;
243
- return value === 'sendingOtp' || value === 'awaitingOtp';
244
- });
245
-
246
- actor.stop();
247
- });
248
-
249
- it('should transition to awaitingOtp after sending OTP', async () => {
250
- const actor = createActor(phoneMachine, {
251
- input: { config: mockPhoneConfig },
252
- }).start();
253
-
254
- actor.send({ type: 'LOAD' });
255
-
256
- await vi.waitFor(() => {
257
- expect(actor.getSnapshot().value).toBe('inputting');
258
- });
259
-
260
- actor.send({
261
- type: 'PHONE_CHANGED',
262
- phone: '+14155551234',
263
- isValid: true,
264
- } as PhoneEvent);
265
- actor.send({ type: 'SUBMIT' });
266
-
267
- await vi.waitFor(() => {
268
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
269
- });
270
-
271
- const { context } = actor.getSnapshot();
272
- expect(context.attemptsRemaining).toBe(3);
273
-
274
- actor.stop();
275
- });
276
-
277
- it('should update OTP code on OTP_CHANGED event', async () => {
278
- const actor = createActor(phoneMachine, {
279
- input: { config: mockPhoneConfig },
280
- }).start();
281
-
282
- actor.send({ type: 'LOAD' });
283
-
284
- await vi.waitFor(() => {
285
- expect(actor.getSnapshot().value).toBe('inputting');
286
- });
287
-
288
- actor.send({
289
- type: 'PHONE_CHANGED',
290
- phone: '+14155551234',
291
- isValid: true,
292
- } as PhoneEvent);
293
- actor.send({ type: 'SUBMIT' });
294
-
295
- await vi.waitFor(() => {
296
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
297
- });
298
-
299
- actor.send({ type: 'OTP_CHANGED', code: 'ABC123' } as PhoneEvent);
300
-
301
- const { context } = actor.getSnapshot();
302
- expect(context.otpCode).toBe('ABC123');
303
-
304
- actor.stop();
305
- });
306
-
307
- it('should transition to verifyingOtp on VERIFY_OTP event', async () => {
308
- const actor = createActor(phoneMachine, {
309
- input: { config: mockPhoneConfig },
310
- }).start();
311
-
312
- actor.send({ type: 'LOAD' });
313
-
314
- await vi.waitFor(() => {
315
- expect(actor.getSnapshot().value).toBe('inputting');
316
- });
317
-
318
- actor.send({
319
- type: 'PHONE_CHANGED',
320
- phone: '+14155551234',
321
- isValid: true,
322
- } as PhoneEvent);
323
- actor.send({ type: 'SUBMIT' });
324
-
325
- await vi.waitFor(() => {
326
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
327
- });
328
-
329
- actor.send({ type: 'OTP_CHANGED', code: 'ABC123' } as PhoneEvent);
330
- actor.send({ type: 'VERIFY_OTP' });
331
-
332
- expect(actor.getSnapshot().value).toBe('verifyingOtp');
333
-
334
- actor.stop();
335
- });
336
-
337
- it('should transition to success on valid OTP', async () => {
338
- const actor = createActor(phoneMachine, {
339
- input: { config: mockPhoneConfig },
340
- }).start();
341
-
342
- actor.send({ type: 'LOAD' });
343
-
344
- await vi.waitFor(() => {
345
- expect(actor.getSnapshot().value).toBe('inputting');
346
- });
347
-
348
- actor.send({
349
- type: 'PHONE_CHANGED',
350
- phone: '+14155551234',
351
- isValid: true,
352
- } as PhoneEvent);
353
- actor.send({ type: 'SUBMIT' });
354
-
355
- await vi.waitFor(() => {
356
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
357
- });
358
-
359
- actor.send({ type: 'OTP_CHANGED', code: 'ABC123' } as PhoneEvent);
360
- actor.send({ type: 'VERIFY_OTP' });
361
-
362
- await vi.waitFor(() => {
363
- expect(actor.getSnapshot().value).toBe('success');
364
- });
365
-
366
- actor.stop();
367
- });
368
- });
369
-
370
- describe('BACK event', () => {
371
- it('should transition from awaitingOtp to inputting', async () => {
372
- const actor = createActor(phoneMachine, {
373
- input: { config: mockPhoneConfig },
374
- }).start();
375
-
376
- actor.send({ type: 'LOAD' });
377
-
378
- await vi.waitFor(() => {
379
- expect(actor.getSnapshot().value).toBe('inputting');
380
- });
381
-
382
- actor.send({
383
- type: 'PHONE_CHANGED',
384
- phone: '+14155551234',
385
- isValid: true,
386
- } as PhoneEvent);
387
- actor.send({ type: 'SUBMIT' });
388
-
389
- await vi.waitFor(() => {
390
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
391
- });
392
-
393
- actor.send({ type: 'BACK' });
394
-
395
- expect(actor.getSnapshot().value).toBe('inputting');
396
-
397
- actor.stop();
398
- });
399
- });
400
-
401
- describe('RESET event', () => {
402
- it('should not reset from success state (final state)', async () => {
403
- const actor = createActor(phoneMachine, {
404
- input: { config: mockPhoneConfigNoOtp },
405
- }).start();
406
-
407
- actor.send({ type: 'LOAD' });
408
-
409
- await vi.waitFor(() => {
410
- expect(actor.getSnapshot().value).toBe('inputting');
411
- });
412
-
413
- actor.send({
414
- type: 'PHONE_CHANGED',
415
- phone: '+14155551234',
416
- isValid: true,
417
- } as PhoneEvent);
418
- actor.send({ type: 'SUBMIT' });
419
-
420
- await vi.waitFor(() => {
421
- expect(actor.getSnapshot().value).toBe('success');
422
- });
423
-
424
- // Final states don't accept transitions - this is correct behavior for orchestration
425
- actor.send({ type: 'RESET' });
426
-
427
- // Should remain in success state (final state)
428
- expect(actor.getSnapshot().value).toBe('success');
429
-
430
- actor.stop();
431
- });
432
-
433
- it('should not reset context from success state (final state)', async () => {
434
- const actor = createActor(phoneMachine, {
435
- input: { config: mockPhoneConfigNoOtp },
436
- }).start();
437
-
438
- actor.send({ type: 'LOAD' });
439
-
440
- await vi.waitFor(() => {
441
- expect(actor.getSnapshot().value).toBe('inputting');
442
- });
443
-
444
- actor.send({
445
- type: 'PHONE_CHANGED',
446
- phone: '+14155551234',
447
- isValid: true,
448
- } as PhoneEvent);
449
- actor.send({ type: 'SUBMIT' });
450
-
451
- await vi.waitFor(() => {
452
- expect(actor.getSnapshot().value).toBe('success');
453
- });
454
-
455
- // Final states don't accept transitions - context should remain unchanged
456
- actor.send({ type: 'RESET' });
457
-
458
- const { context } = actor.getSnapshot();
459
- // Context remains unchanged because final state doesn't accept RESET
460
- expect(context.phone).toBe('+14155551234');
461
- expect(context.isValid).toBe(true);
462
-
463
- actor.stop();
464
- });
465
-
466
- it('should not reset from inputting state (RESET not handled)', async () => {
467
- const actor = createActor(phoneMachine, {
468
- input: { config: mockPhoneConfig },
469
- }).start();
470
-
471
- actor.send({ type: 'LOAD' });
472
-
473
- await vi.waitFor(() => {
474
- expect(actor.getSnapshot().value).toBe('inputting');
475
- });
476
-
477
- actor.send({ type: 'RESET' });
478
-
479
- // RESET is not handled in inputting state
480
- expect(actor.getSnapshot().value).toBe('inputting');
481
-
482
- actor.stop();
483
- });
484
- });
485
-
486
- describe('Subscriptions', () => {
487
- it('should notify subscribers of state changes', async () => {
488
- const actor = createActor(phoneMachine, {
489
- input: { config: mockPhoneConfig },
490
- });
491
-
492
- const states: string[] = [];
493
- actor.subscribe((snapshot) => {
494
- states.push(snapshot.value as string);
495
- });
496
-
497
- actor.start();
498
- actor.send({ type: 'LOAD' });
499
-
500
- await vi.waitFor(() => {
501
- expect(actor.getSnapshot().value).toBe('inputting');
502
- });
503
-
504
- expect(states).toContain('idle');
505
- expect(states).toContain('loadingStartInfo');
506
- expect(states).toContain('inputting');
507
-
508
- actor.stop();
509
- });
510
- });
511
-
512
- describe('Prefill flow', () => {
513
- it('should load prefilled phone and transition to inputting', async () => {
514
- const configWithPrefill = { ...mockPhoneConfig, prefill: true };
515
- const actor = createActor(phoneMachine, {
516
- input: { config: configWithPrefill },
517
- }).start();
518
-
519
- actor.send({ type: 'LOAD' });
520
-
521
- await vi.waitFor(() => {
522
- expect(actor.getSnapshot().value).toBe('inputting');
523
- });
524
-
525
- const { context } = actor.getSnapshot();
526
- expect(context.prefilledPhone).toBe('+14155551234');
527
- expect(context.phone).toBe('+14155551234');
528
-
529
- actor.stop();
530
- });
531
-
532
- it('should handle prefill fetch error and still transition to loadingStartInfo', async () => {
533
- const { fetchPhone } = await import('./phoneServices');
534
- vi.mocked(fetchPhone).mockRejectedValueOnce(new Error('Network error'));
535
-
536
- const configWithPrefill = { ...mockPhoneConfig, prefill: true };
537
- const actor = createActor(phoneMachine, {
538
- input: { config: configWithPrefill },
539
- }).start();
540
-
541
- actor.send({ type: 'LOAD' });
542
-
543
- await vi.waitFor(() => {
544
- expect(actor.getSnapshot().value).toBe('inputting');
545
- });
546
-
547
- actor.stop();
548
- });
549
- });
550
-
551
- describe('OTP error handling', () => {
552
- it('should transition to otpError when OTP verification fails with attempts remaining', async () => {
553
- const { verifyOtp } = await import('./phoneServices');
554
- vi.mocked(verifyOtp).mockResolvedValueOnce({ success: false });
555
-
556
- const actor = createActor(phoneMachine, {
557
- input: { config: mockPhoneConfig },
558
- }).start();
559
-
560
- actor.send({ type: 'LOAD' });
561
-
562
- await vi.waitFor(() => {
563
- expect(actor.getSnapshot().value).toBe('inputting');
564
- });
565
-
566
- actor.send({
567
- type: 'PHONE_CHANGED',
568
- phone: '+14155551234',
569
- isValid: true,
570
- } as PhoneEvent);
571
- actor.send({ type: 'SUBMIT' });
572
-
573
- await vi.waitFor(() => {
574
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
575
- });
576
-
577
- actor.send({ type: 'OTP_CHANGED', code: 'WRONG1' } as PhoneEvent);
578
- actor.send({ type: 'VERIFY_OTP' });
579
-
580
- await vi.waitFor(() => {
581
- expect(actor.getSnapshot().value).toBe('otpError');
582
- });
583
-
584
- const { context } = actor.getSnapshot();
585
- expect(context.attemptsRemaining).toBe(2);
586
- expect(context.otpError).toBe('Invalid OTP code');
587
-
588
- actor.stop();
589
- });
590
-
591
- it('should allow retry from otpError state via OTP_CHANGED', async () => {
592
- const { verifyOtp } = await import('./phoneServices');
593
- vi.mocked(verifyOtp)
594
- .mockResolvedValueOnce({ success: false })
595
- .mockResolvedValueOnce({ success: true });
596
-
597
- const actor = createActor(phoneMachine, {
598
- input: { config: mockPhoneConfig },
599
- }).start();
600
-
601
- actor.send({ type: 'LOAD' });
602
-
603
- await vi.waitFor(() => {
604
- expect(actor.getSnapshot().value).toBe('inputting');
605
- });
606
-
607
- actor.send({
608
- type: 'PHONE_CHANGED',
609
- phone: '+14155551234',
610
- isValid: true,
611
- } as PhoneEvent);
612
- actor.send({ type: 'SUBMIT' });
613
-
614
- await vi.waitFor(() => {
615
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
616
- });
617
-
618
- actor.send({ type: 'OTP_CHANGED', code: 'WRONG1' } as PhoneEvent);
619
- actor.send({ type: 'VERIFY_OTP' });
620
-
621
- await vi.waitFor(() => {
622
- expect(actor.getSnapshot().value).toBe('otpError');
623
- });
624
-
625
- // Retry with correct code
626
- actor.send({ type: 'OTP_CHANGED', code: 'CORRECT' } as PhoneEvent);
627
-
628
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
629
-
630
- actor.send({ type: 'VERIFY_OTP' });
631
-
632
- await vi.waitFor(() => {
633
- expect(actor.getSnapshot().value).toBe('success');
634
- });
635
-
636
- actor.stop();
637
- });
638
-
639
- it('should transition to error when all OTP attempts are exhausted', async () => {
640
- const { verifyOtp } = await import('./phoneServices');
641
- vi.mocked(verifyOtp).mockResolvedValue({ success: false });
642
-
643
- const configWith1Attempt = { ...mockPhoneConfig, maxOtpAttempts: 1 };
644
- const actor = createActor(phoneMachine, {
645
- input: { config: configWith1Attempt },
646
- }).start();
647
-
648
- actor.send({ type: 'LOAD' });
649
-
650
- await vi.waitFor(() => {
651
- expect(actor.getSnapshot().value).toBe('inputting');
652
- });
653
-
654
- actor.send({
655
- type: 'PHONE_CHANGED',
656
- phone: '+14155551234',
657
- isValid: true,
658
- } as PhoneEvent);
659
- actor.send({ type: 'SUBMIT' });
660
-
661
- await vi.waitFor(() => {
662
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
663
- });
664
-
665
- actor.send({ type: 'OTP_CHANGED', code: 'WRONG' } as PhoneEvent);
666
- actor.send({ type: 'VERIFY_OTP' });
667
-
668
- await vi.waitFor(() => {
669
- const value = actor.getSnapshot().value;
670
- return value === 'error' || value === 'otpError';
671
- });
672
-
673
- actor.stop();
674
- });
675
-
676
- it('should handle BACK from otpError state', async () => {
677
- const { verifyOtp } = await import('./phoneServices');
678
- vi.mocked(verifyOtp).mockResolvedValueOnce({ success: false });
679
-
680
- const actor = createActor(phoneMachine, {
681
- input: { config: mockPhoneConfig },
682
- }).start();
683
-
684
- actor.send({ type: 'LOAD' });
685
-
686
- await vi.waitFor(() => {
687
- expect(actor.getSnapshot().value).toBe('inputting');
688
- });
689
-
690
- actor.send({
691
- type: 'PHONE_CHANGED',
692
- phone: '+14155551234',
693
- isValid: true,
694
- } as PhoneEvent);
695
- actor.send({ type: 'SUBMIT' });
696
-
697
- await vi.waitFor(() => {
698
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
699
- });
700
-
701
- actor.send({ type: 'OTP_CHANGED', code: 'WRONG' } as PhoneEvent);
702
- actor.send({ type: 'VERIFY_OTP' });
703
-
704
- await vi.waitFor(() => {
705
- expect(actor.getSnapshot().value).toBe('otpError');
706
- });
707
-
708
- actor.send({ type: 'BACK' });
709
-
710
- expect(actor.getSnapshot().value).toBe('inputting');
711
-
712
- actor.stop();
713
- });
714
- });
715
-
716
- describe('Resend OTP flow', () => {
717
- it('should not resend when timer is still active', async () => {
718
- const actor = createActor(phoneMachine, {
719
- input: { config: mockPhoneConfig },
720
- }).start();
721
-
722
- actor.send({ type: 'LOAD' });
723
-
724
- await vi.waitFor(() => {
725
- expect(actor.getSnapshot().value).toBe('inputting');
726
- });
727
-
728
- actor.send({
729
- type: 'PHONE_CHANGED',
730
- phone: '+14155551234',
731
- isValid: true,
732
- } as PhoneEvent);
733
- actor.send({ type: 'SUBMIT' });
734
-
735
- await vi.waitFor(() => {
736
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
737
- });
738
-
739
- // Timer should be active immediately after entering awaitingOtp
740
- const { context } = actor.getSnapshot();
741
- expect(context.resendTimerActive).toBe(true);
742
-
743
- // Try to resend (should be blocked by guard)
744
- actor.send({ type: 'RESEND_OTP' });
745
-
746
- // Should still be in awaitingOtp, not sendingOtp
747
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
748
-
749
- actor.stop();
750
- });
751
-
752
- it('should resend when timer has expired', async () => {
753
- const actor = createActor(phoneMachine, {
754
- input: { config: mockPhoneConfig },
755
- }).start();
756
-
757
- actor.send({ type: 'LOAD' });
758
-
759
- await vi.waitFor(() => {
760
- expect(actor.getSnapshot().value).toBe('inputting');
761
- });
762
-
763
- actor.send({
764
- type: 'PHONE_CHANGED',
765
- phone: '+14155551234',
766
- isValid: true,
767
- } as PhoneEvent);
768
- actor.send({ type: 'SUBMIT' });
769
-
770
- await vi.waitFor(() => {
771
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
772
- });
773
-
774
- // Wait for timer to expire
775
- await vi.waitFor(
776
- () => {
777
- const { context } = actor.getSnapshot();
778
- return !context.resendTimerActive;
779
- },
780
- { timeout: 35000 },
781
- );
782
-
783
- actor.send({ type: 'RESEND_OTP' });
784
-
785
- await vi.waitFor(() => {
786
- return actor.getSnapshot().value === 'sendingOtp';
787
- });
788
-
789
- actor.stop();
790
- });
791
- });
792
-
793
- describe('Error handling', () => {
794
- it('should handle phone submission error', async () => {
795
- const { addPhone } = await import('./phoneServices');
796
- vi.mocked(addPhone).mockRejectedValueOnce(new Error('Network error'));
797
-
798
- const actor = createActor(phoneMachine, {
799
- input: { config: mockPhoneConfigNoOtp },
800
- }).start();
801
-
802
- actor.send({ type: 'LOAD' });
803
-
804
- await vi.waitFor(() => {
805
- expect(actor.getSnapshot().value).toBe('inputting');
806
- });
807
-
808
- actor.send({
809
- type: 'PHONE_CHANGED',
810
- phone: '+14155551234',
811
- isValid: true,
812
- } as PhoneEvent);
813
- actor.send({ type: 'SUBMIT' });
814
-
815
- await vi.waitFor(() => {
816
- const value = actor.getSnapshot().value;
817
- return value === 'inputting' || value === 'error';
818
- });
819
-
820
- actor.stop();
821
- });
822
-
823
- it('should handle sendOtp error and still transition to awaitingOtp', async () => {
824
- const { sendOtp } = await import('./phoneServices');
825
- vi.mocked(sendOtp).mockRejectedValueOnce(new Error('SMS failed'));
826
-
827
- const actor = createActor(phoneMachine, {
828
- input: { config: mockPhoneConfig },
829
- }).start();
830
-
831
- actor.send({ type: 'LOAD' });
832
-
833
- await vi.waitFor(() => {
834
- expect(actor.getSnapshot().value).toBe('inputting');
835
- });
836
-
837
- actor.send({
838
- type: 'PHONE_CHANGED',
839
- phone: '+14155551234',
840
- isValid: true,
841
- } as PhoneEvent);
842
- actor.send({ type: 'SUBMIT' });
843
-
844
- // Even if sendOtp fails, should transition to awaitingOtp (with error in context)
845
- await vi.waitFor(() => {
846
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
847
- });
848
-
849
- actor.stop();
850
- });
851
-
852
- it('should reset from error state', async () => {
853
- const { addPhone } = await import('./phoneServices');
854
- vi.mocked(addPhone).mockRejectedValueOnce(new Error('Fatal error'));
855
-
856
- const actor = createActor(phoneMachine, {
857
- input: { config: mockPhoneConfigNoOtp },
858
- }).start();
859
-
860
- actor.send({ type: 'LOAD' });
861
-
862
- await vi.waitFor(() => {
863
- expect(actor.getSnapshot().value).toBe('inputting');
864
- });
865
-
866
- actor.send({
867
- type: 'PHONE_CHANGED',
868
- phone: '+14155551234',
869
- isValid: true,
870
- } as PhoneEvent);
871
- actor.send({ type: 'SUBMIT' });
872
-
873
- await vi.waitFor(() => {
874
- const value = actor.getSnapshot().value;
875
- return value === 'inputting' || value === 'error';
876
- });
877
-
878
- // If we're in error state, test RESET
879
- if (actor.getSnapshot().value === 'error') {
880
- actor.send({ type: 'RESET' });
881
- expect(actor.getSnapshot().value).toBe('idle');
882
- }
883
-
884
- actor.stop();
885
- });
886
- });
887
-
888
- describe('Resend timer', () => {
889
- it('should initialize resend timer when entering awaitingOtp', async () => {
890
- const actor = createActor(phoneMachine, {
891
- input: { config: mockPhoneConfig },
892
- }).start();
893
-
894
- actor.send({ type: 'LOAD' });
895
-
896
- await vi.waitFor(() => {
897
- expect(actor.getSnapshot().value).toBe('inputting');
898
- });
899
-
900
- actor.send({
901
- type: 'PHONE_CHANGED',
902
- phone: '+14155551234',
903
- isValid: true,
904
- } as PhoneEvent);
905
- actor.send({ type: 'SUBMIT' });
906
-
907
- await vi.waitFor(() => {
908
- expect(actor.getSnapshot().value).toBe('awaitingOtp');
909
- });
910
-
911
- const { context } = actor.getSnapshot();
912
- expect(context.resendTimer).toBe(30);
913
- expect(context.resendTimerActive).toBe(true);
914
-
915
- actor.stop();
916
- });
917
- });
918
- });