55 * of the MIT license. See the LICENSE file for details.
66 */
77
8+ import { Either } from 'effect' ;
89import { describe , expect , it } from 'vitest' ;
910
1011import { StepType } from '../types.js' ;
@@ -64,33 +65,42 @@ describe('createJourneyObject', () => {
6465} ) ;
6566
6667describe ( 'handleJourneyResponse' , ( ) => {
67- it ( 'returns Step data when FetchBaseQueryError has numeric status and object body' , ( ) => {
68+ it ( 'returns Right( Step) when FetchBaseQueryError has numeric status and object body' , ( ) => {
6869 const body = { code : 401 , message : 'Access Denied' , reason : 'Unauthorized' } ;
6970 const error = { status : 401 , data : body } ;
7071
7172 const result = handleJourneyResponse ( undefined , error ) ;
7273
73- expect ( result ) . toBe ( body ) ;
74+ expect ( Either . isRight ( result ) ) . toBe ( true ) ;
75+ if ( Either . isRight ( result ) ) {
76+ expect ( result . right ) . toBe ( body ) ;
77+ }
7478 } ) ;
7579
76- it ( 'returns GenericError when FetchBaseQueryError has numeric status but non-object body' , ( ) => {
80+ it ( 'returns Left( GenericError) when FetchBaseQueryError has numeric status but non-object body' , ( ) => {
7781 const error = { status : 500 , data : 'Internal Server Error' } ;
7882
7983 const result = handleJourneyResponse ( undefined , error ) ;
8084
81- expect ( result ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
85+ expect ( Either . isLeft ( result ) ) . toBe ( true ) ;
86+ if ( Either . isLeft ( result ) ) {
87+ expect ( result . left ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
88+ }
8289 } ) ;
8390
84- it ( 'returns GenericError for FETCH_ERROR' , ( ) => {
91+ it ( 'returns Left( GenericError) for FETCH_ERROR' , ( ) => {
8592 const error = { status : 'FETCH_ERROR' as const , error : 'Network error' } ;
8693
8794 const result = handleJourneyResponse ( undefined , error ) ;
8895
89- expect ( result ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
90- expect ( ( result as { message : string } ) . message ) . toContain ( 'Network error' ) ;
96+ expect ( Either . isLeft ( result ) ) . toBe ( true ) ;
97+ if ( Either . isLeft ( result ) ) {
98+ expect ( result . left ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
99+ expect ( result . left . message ) . toContain ( 'Network error' ) ;
100+ }
91101 } ) ;
92102
93- it ( 'returns GenericError for PARSING_ERROR' , ( ) => {
103+ it ( 'returns Left( GenericError) for PARSING_ERROR' , ( ) => {
94104 const error = {
95105 status : 'PARSING_ERROR' as const ,
96106 originalStatus : 200 ,
@@ -100,48 +110,66 @@ describe('handleJourneyResponse', () => {
100110
101111 const result = handleJourneyResponse ( undefined , error ) ;
102112
103- expect ( result ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
104- expect ( ( result as { message : string } ) . message ) . toContain ( 'JSON parse error' ) ;
113+ expect ( Either . isLeft ( result ) ) . toBe ( true ) ;
114+ if ( Either . isLeft ( result ) ) {
115+ expect ( result . left ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
116+ expect ( result . left . message ) . toContain ( 'JSON parse error' ) ;
117+ }
105118 } ) ;
106119
107- it ( 'returns GenericError for TIMEOUT_ERROR' , ( ) => {
120+ it ( 'returns Left( GenericError) for TIMEOUT_ERROR' , ( ) => {
108121 const error = { status : 'TIMEOUT_ERROR' as const , error : 'Request timed out' } ;
109122
110123 const result = handleJourneyResponse ( undefined , error ) ;
111124
112- expect ( result ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
113- expect ( ( result as { message : string } ) . message ) . toContain ( 'Request timed out' ) ;
125+ expect ( Either . isLeft ( result ) ) . toBe ( true ) ;
126+ if ( Either . isLeft ( result ) ) {
127+ expect ( result . left ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
128+ expect ( result . left . message ) . toContain ( 'Request timed out' ) ;
129+ }
114130 } ) ;
115131
116- it ( 'returns GenericError for CUSTOM_ERROR' , ( ) => {
132+ it ( 'returns Left( GenericError) for CUSTOM_ERROR' , ( ) => {
117133 const error = { status : 'CUSTOM_ERROR' as const , error : 'Custom error occurred' } ;
118134
119135 const result = handleJourneyResponse ( undefined , error ) ;
120136
121- expect ( result ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
122- expect ( ( result as { message : string } ) . message ) . toContain ( 'Custom error occurred' ) ;
137+ expect ( Either . isLeft ( result ) ) . toBe ( true ) ;
138+ if ( Either . isLeft ( result ) ) {
139+ expect ( result . left ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
140+ expect ( result . left . message ) . toContain ( 'Custom error occurred' ) ;
141+ }
123142 } ) ;
124143
125- it ( 'returns GenericError for SerializedError' , ( ) => {
144+ it ( 'returns Left( GenericError) for SerializedError' , ( ) => {
126145 const error = { name : 'Error' , message : 'Something went wrong' , stack : '...' } ;
127146
128147 const result = handleJourneyResponse ( undefined , error ) ;
129148
130- expect ( result ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
131- expect ( ( result as { message : string } ) . message ) . toContain ( 'Something went wrong' ) ;
149+ expect ( Either . isLeft ( result ) ) . toBe ( true ) ;
150+ if ( Either . isLeft ( result ) ) {
151+ expect ( result . left ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
152+ expect ( result . left . message ) . toContain ( 'Something went wrong' ) ;
153+ }
132154 } ) ;
133155
134- it ( 'returns GenericError when no data and no error' , ( ) => {
156+ it ( 'returns Left( GenericError) when no data and no error' , ( ) => {
135157 const result = handleJourneyResponse ( undefined , undefined ) ;
136158
137- expect ( result ) . toMatchObject ( { error : 'no_response_data' , type : 'unknown_error' } ) ;
159+ expect ( Either . isLeft ( result ) ) . toBe ( true ) ;
160+ if ( Either . isLeft ( result ) ) {
161+ expect ( result . left ) . toMatchObject ( { error : 'no_response_data' , type : 'unknown_error' } ) ;
162+ }
138163 } ) ;
139164
140- it ( 'returns data when no error and data is present' , ( ) => {
165+ it ( 'returns Right(Step) when no error and data is present' , ( ) => {
141166 const data : Step = { authId : 'test-auth-id' , callbacks : [ ] } ;
142167
143168 const result = handleJourneyResponse ( data , undefined ) ;
144169
145- expect ( result ) . toBe ( data ) ;
170+ expect ( Either . isRight ( result ) ) . toBe ( true ) ;
171+ if ( Either . isRight ( result ) ) {
172+ expect ( result . right ) . toBe ( data ) ;
173+ }
146174 } ) ;
147175} ) ;
0 commit comments