@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,264 @@
1
+ import type { ICameraCapability } from '@incodetech/infra';
2
+ import {
3
+ type ActorRefFrom,
4
+ assign,
5
+ fromPromise,
6
+ setup,
7
+ } from '@incodetech/infra';
8
+ import { createCameraServices } from './cameraServices';
9
+ import type { PermissionResult } from './types';
10
+
11
+ export type CameraContext = {
12
+ camera: ICameraCapability;
13
+ config: {
14
+ facingMode?: 'user' | 'environment';
15
+ deviceId?: string;
16
+ };
17
+ stream: MediaStream | undefined;
18
+ permissionResult: PermissionResult | undefined;
19
+ error: string | undefined;
20
+ errorType:
21
+ | 'permission_denied'
22
+ | 'not_found'
23
+ | 'not_readable'
24
+ | 'overconstrained'
25
+ | 'security'
26
+ | 'abort'
27
+ | 'unknown'
28
+ | undefined;
29
+ };
30
+
31
+ export type CameraEvent =
32
+ | { type: 'START' }
33
+ | { type: 'STOP' }
34
+ | { type: 'PERMISSION_RESULT'; result: PermissionResult }
35
+ | { type: 'STREAM_READY'; stream: MediaStream }
36
+ | {
37
+ type: 'STREAM_ERROR';
38
+ error: string;
39
+ errorType: CameraContext['errorType'];
40
+ }
41
+ | { type: 'RESET' };
42
+
43
+ export type CameraInput = {
44
+ camera: ICameraCapability;
45
+ config?: {
46
+ facingMode?: 'user' | 'environment';
47
+ deviceId?: string;
48
+ };
49
+ };
50
+
51
+ export const cameraMachine = setup({
52
+ types: {
53
+ context: {} as CameraContext,
54
+ events: {} as CameraEvent,
55
+ input: {} as CameraInput,
56
+ },
57
+ actors: {
58
+ checkPermission: fromPromise<
59
+ PermissionResult,
60
+ { camera: ICameraCapability }
61
+ >(async ({ input }) => {
62
+ const services = createCameraServices(input.camera);
63
+ return services.checkPermission();
64
+ }),
65
+ requestStream: fromPromise<
66
+ MediaStream,
67
+ { camera: ICameraCapability; config: CameraContext['config'] }
68
+ >(async ({ input }) => {
69
+ const services = createCameraServices(input.camera);
70
+ return services.requestStream(input.config);
71
+ }),
72
+ },
73
+ actions: {
74
+ setPermissionResult: assign({
75
+ permissionResult: ({ event }) => {
76
+ // Handle onDone events from fromPromise actors
77
+ if ('output' in event) {
78
+ return (event as { output: PermissionResult }).output;
79
+ }
80
+ // Handle manual PERMISSION_RESULT events
81
+ if (event.type === 'PERMISSION_RESULT') {
82
+ return event.result;
83
+ }
84
+ return undefined;
85
+ },
86
+ }),
87
+ setStream: assign({
88
+ stream: ({ event }) => {
89
+ // Handle onDone events from fromPromise actors
90
+ if ('output' in event) {
91
+ return (event as { output: MediaStream }).output;
92
+ }
93
+ // Handle manual STREAM_READY events
94
+ if (event.type === 'STREAM_READY') {
95
+ return event.stream;
96
+ }
97
+ return undefined;
98
+ },
99
+ }),
100
+ setError: assign({
101
+ error: ({ event }) => {
102
+ // Handle onError events from fromPromise actors
103
+ if ('error' in event && event.type?.startsWith?.('xstate.error')) {
104
+ const err = (event as unknown as { error: Error }).error;
105
+ return err.message || String(err);
106
+ }
107
+ // Handle manual STREAM_ERROR events
108
+ if (event.type === 'STREAM_ERROR') {
109
+ return event.error;
110
+ }
111
+ return undefined;
112
+ },
113
+ errorType: ({ event }) => {
114
+ // Handle onError events from fromPromise actors
115
+ if ('error' in event && event.type?.startsWith?.('xstate.error')) {
116
+ const err = (event as unknown as { error: Error }).error;
117
+ if (err.name === 'NotAllowedError') return 'permission_denied';
118
+ if (err.name === 'NotFoundError') return 'not_found';
119
+ if (err.name === 'NotReadableError') return 'not_readable';
120
+ if (err.name === 'OverconstrainedError') return 'overconstrained';
121
+ if (err.name === 'SecurityError') return 'security';
122
+ if (err.name === 'AbortError') return 'abort';
123
+ return 'unknown';
124
+ }
125
+ // Handle manual STREAM_ERROR events
126
+ if (event.type === 'STREAM_ERROR') {
127
+ return event.errorType;
128
+ }
129
+ return undefined;
130
+ },
131
+ }),
132
+ stopStream: ({ context }) => {
133
+ if (context.stream) {
134
+ const services = createCameraServices(context.camera);
135
+ services.stopStream(context.stream);
136
+ }
137
+ },
138
+ resetContext: assign(({ context }) => ({
139
+ camera: context.camera,
140
+ config: context.config,
141
+ stream: undefined,
142
+ permissionResult: undefined,
143
+ error: undefined,
144
+ errorType: undefined,
145
+ })),
146
+ },
147
+ guards: {
148
+ permissionGranted: ({ context }) => context.permissionResult === 'granted',
149
+ permissionDenied: ({ context }) => context.permissionResult === 'denied',
150
+ hasStream: ({ context }) => context.stream !== undefined,
151
+ },
152
+ }).createMachine({
153
+ id: 'camera',
154
+ initial: 'idle',
155
+ context: ({ input }) => ({
156
+ camera: input.camera,
157
+ config: input.config ?? {},
158
+ stream: undefined,
159
+ permissionResult: undefined,
160
+ error: undefined,
161
+ errorType: undefined,
162
+ }),
163
+ states: {
164
+ idle: {
165
+ on: {
166
+ START: 'checkingPermission',
167
+ },
168
+ },
169
+
170
+ checkingPermission: {
171
+ invoke: {
172
+ id: 'checkPermission',
173
+ src: 'checkPermission',
174
+ input: ({ context }) => ({ camera: context.camera }),
175
+ onDone: {
176
+ target: 'routing',
177
+ actions: 'setPermissionResult',
178
+ },
179
+ onError: {
180
+ target: 'routing',
181
+ actions: assign({
182
+ permissionResult: () => 'prompt' as PermissionResult,
183
+ }),
184
+ },
185
+ },
186
+ on: {
187
+ STOP: {
188
+ target: 'idle',
189
+ actions: 'resetContext',
190
+ },
191
+ },
192
+ },
193
+
194
+ routing: {
195
+ always: [
196
+ {
197
+ target: 'requesting',
198
+ guard: 'permissionGranted',
199
+ },
200
+ {
201
+ target: 'error',
202
+ guard: 'permissionDenied',
203
+ actions: assign({
204
+ error: () => 'Camera permission denied',
205
+ errorType: () => 'permission_denied' as const,
206
+ }),
207
+ },
208
+ {
209
+ target: 'requesting',
210
+ },
211
+ ],
212
+ },
213
+
214
+ requesting: {
215
+ invoke: {
216
+ id: 'requestStream',
217
+ src: 'requestStream',
218
+ input: ({ context }) => ({
219
+ camera: context.camera,
220
+ config: context.config,
221
+ }),
222
+ onDone: {
223
+ target: 'streaming',
224
+ actions: 'setStream',
225
+ },
226
+ onError: {
227
+ target: 'error',
228
+ actions: 'setError',
229
+ },
230
+ },
231
+ on: {
232
+ STOP: {
233
+ target: 'idle',
234
+ actions: 'resetContext',
235
+ },
236
+ },
237
+ },
238
+
239
+ streaming: {
240
+ on: {
241
+ STOP: {
242
+ target: 'idle',
243
+ actions: ['stopStream', 'resetContext'],
244
+ },
245
+ },
246
+ },
247
+
248
+ error: {
249
+ on: {
250
+ RESET: {
251
+ target: 'idle',
252
+ actions: 'resetContext',
253
+ },
254
+ STOP: {
255
+ target: 'idle',
256
+ actions: 'resetContext',
257
+ },
258
+ },
259
+ },
260
+ },
261
+ });
262
+
263
+ export type CameraMachine = typeof cameraMachine;
264
+ export type CameraActor = ActorRefFrom<CameraMachine>;
@@ -0,0 +1,5 @@
1
+ export { createCameraActor } from './cameraActor';
2
+ export type { CameraService, CameraState } from './cameraService';
3
+ export { createCameraService } from './cameraService';
4
+ export type { CameraActor, CameraMachine } from './cameraStateMachine';
5
+ export type { CameraConfig, CameraErrorType, PermissionResult } from './types';
@@ -0,0 +1,17 @@
1
+ import type { FacingMode } from '@incodetech/infra';
2
+
3
+ export type CameraConfig = {
4
+ facingMode?: FacingMode;
5
+ deviceId?: string;
6
+ };
7
+
8
+ export type PermissionResult = 'granted' | 'denied' | 'prompt';
9
+
10
+ export type CameraErrorType =
11
+ | 'permission_denied'
12
+ | 'not_found'
13
+ | 'not_readable'
14
+ | 'overconstrained'
15
+ | 'security'
16
+ | 'abort'
17
+ | 'unknown';
@@ -0,0 +1,31 @@
1
+ import { getUserAgent } from '@incodetech/infra';
2
+
3
+ export type Browser = 'chrome' | 'firefox' | 'safari' | 'edge' | 'other';
4
+
5
+ export function getBrowser(): Browser {
6
+ const userAgent = getUserAgent();
7
+
8
+ if (!userAgent) {
9
+ return 'other';
10
+ }
11
+
12
+ const ua = userAgent.toLowerCase();
13
+
14
+ if (ua.includes('edg/')) {
15
+ return 'edge';
16
+ }
17
+
18
+ if (ua.includes('chrome') && !ua.includes('edg/')) {
19
+ return 'chrome';
20
+ }
21
+
22
+ if (ua.includes('firefox')) {
23
+ return 'firefox';
24
+ }
25
+
26
+ if (ua.includes('safari') && !ua.includes('chrome')) {
27
+ return 'safari';
28
+ }
29
+
30
+ return 'other';
31
+ }
@@ -0,0 +1,29 @@
1
+ import { getDeviceInfo } from '@incodetech/infra';
2
+
3
+ export type DeviceClass = 'ios' | 'android' | 'desktop';
4
+
5
+ export function getDeviceClass(): DeviceClass {
6
+ const { userAgent, platform, maxTouchPoints } = getDeviceInfo();
7
+
8
+ if (!userAgent) {
9
+ return 'desktop';
10
+ }
11
+
12
+ const ua = userAgent.toLowerCase();
13
+
14
+ const isIOS =
15
+ /iphone|ipad|ipod/.test(ua) ||
16
+ (platform === 'MacIntel' && maxTouchPoints > 1);
17
+
18
+ if (isIOS) {
19
+ return 'ios';
20
+ }
21
+
22
+ const isAndroid = /android/.test(ua);
23
+
24
+ if (isAndroid) {
25
+ return 'android';
26
+ }
27
+
28
+ return 'desktop';
29
+ }
@@ -0,0 +1,2 @@
1
+ export { type Browser, getBrowser } from './getBrowser';
2
+ export { type DeviceClass, getDeviceClass } from './getDeviceClass';
@@ -0,0 +1,59 @@
1
+ import { vi } from 'vitest';
2
+ import type { EmailConfig, VerifyOtpResponse } from '../types';
3
+
4
+ export const mockEmailConfig: EmailConfig = {
5
+ otpVerification: true,
6
+ otpExpirationInMinutes: 5,
7
+ prefill: false,
8
+ maxOtpAttempts: 3,
9
+ };
10
+
11
+ export const mockEmailConfigNoOtp: EmailConfig = {
12
+ ...mockEmailConfig,
13
+ otpVerification: false,
14
+ };
15
+
16
+ export const mockEmailConfigWithPrefill: EmailConfig = {
17
+ ...mockEmailConfig,
18
+ prefill: true,
19
+ };
20
+
21
+ export const mockPrefilledEmail = 'user@example.com';
22
+
23
+ export function createMockFetchEmail(email = mockPrefilledEmail) {
24
+ return vi.fn().mockResolvedValue({ email });
25
+ }
26
+
27
+ export function createMockAddEmail(success = true) {
28
+ return vi.fn().mockResolvedValue({ success });
29
+ }
30
+
31
+ export function createMockSendEmailOtp() {
32
+ return vi.fn().mockResolvedValue(undefined);
33
+ }
34
+
35
+ export function createMockVerifyEmailOtp(
36
+ success = true,
37
+ ): () => Promise<VerifyOtpResponse> {
38
+ return vi.fn().mockResolvedValue({ success });
39
+ }
40
+
41
+ export function createFailingMockFetchEmail(error: string) {
42
+ return vi.fn().mockRejectedValue(new Error(error));
43
+ }
44
+
45
+ export function createFailingMockAddEmail(error: string) {
46
+ return vi.fn().mockRejectedValue(new Error(error));
47
+ }
48
+
49
+ export function createFailingMockVerifyEmailOtp(error: string) {
50
+ return vi.fn().mockRejectedValue(new Error(error));
51
+ }
52
+
53
+ export function createDelayedMock<T>(value: T, delayMs = 1000) {
54
+ return vi
55
+ .fn()
56
+ .mockImplementation(
57
+ () => new Promise((resolve) => setTimeout(() => resolve(value), delayMs)),
58
+ );
59
+ }
@@ -0,0 +1,15 @@
1
+ import { type ActorRefFrom, createActor } from '@incodetech/infra';
2
+ import { type EmailMachine, emailMachine } from './emailStateMachine';
3
+ import type { EmailConfig } from './types';
4
+
5
+ export type CreateEmailActorOptions = {
6
+ config: EmailConfig;
7
+ };
8
+
9
+ export type EmailActor = ActorRefFrom<EmailMachine>;
10
+
11
+ export function createEmailActor(options: CreateEmailActorOptions): EmailActor {
12
+ return createActor(emailMachine, {
13
+ input: { config: options.config },
14
+ }).start();
15
+ }