11import axios from 'axios' ;
22import MockAdapter from 'axios-mock-adapter' ;
33import { createAxiosClient } from '../axios-client' ;
4- import { RoutingConfigurationApiClient } from '../routing-config-api-client' ;
4+ import {
5+ isValidUuid ,
6+ RoutingConfigurationApiClient ,
7+ } from '../routing-config-api-client' ;
58import { RoutingConfigStatus } from '../types/generated' ;
9+ import { ErrorCase } from '../types/error-cases' ;
610
711jest . mock ( '../axios-client' , ( ) => {
812 const actual = jest . requireActual ( '../axios-client' ) ;
@@ -14,6 +18,10 @@ jest.mock('../axios-client', () => {
1418
1519const createAxiosClientMock = jest . mocked ( createAxiosClient ) ;
1620
21+ const validRoutingConfigId = '2a4b6c8d-0e1f-4a2b-9c3d-5e6f7a8b9c0d' ;
22+ const notFoundRoutingConfigId = '3b5d7f9a-1c2e-4b3d-8f0a-6e7d8c9b0a1f' ;
23+ const invalidRoutingConfigId = 'not-a-uuid' ;
24+
1725describe ( 'RoutingConfigurationApiClient' , ( ) => {
1826 const axiosMock = new MockAdapter ( axios ) ;
1927
@@ -24,30 +32,49 @@ describe('RoutingConfigurationApiClient', () => {
2432
2533 describe ( 'get' , ( ) => {
2634 it ( 'should return error when failing to fetch from API' , async ( ) => {
27- axiosMock . onGet ( '/v1/routing-configuration/routing-config-1' ) . reply ( 400 , {
28- statusCode : 400 ,
29- technicalMessage : 'Bad request' ,
30- details : { message : 'Broken' } ,
31- } ) ;
35+ axiosMock
36+ . onGet ( `/v1/routing-configuration/${ notFoundRoutingConfigId } ` )
37+ . reply ( 404 , {
38+ statusCode : 404 ,
39+ technicalMessage : 'Not Found' ,
40+ details : { message : 'Routing configuration not found' } ,
41+ } ) ;
3242
3343 const client = new RoutingConfigurationApiClient ( ) ;
3444
35- const response = await client . get ( 'mock-token' , 'routing-config-1' ) ;
45+ const response = await client . get ( 'mock-token' , notFoundRoutingConfigId ) ;
3646
3747 expect ( response . error ) . toEqual ( {
3848 errorMeta : {
39- code : 400 ,
40- description : 'Bad request ' ,
41- details : { message : 'Broken ' } ,
49+ code : 404 ,
50+ description : 'Not Found ' ,
51+ details : { message : 'Routing configuration not found ' } ,
4252 } ,
4353 } ) ;
4454 expect ( response . data ) . toBeUndefined ( ) ;
4555 expect ( axiosMock . history . get . length ) . toBe ( 1 ) ;
4656 } ) ;
4757
58+ it ( 'should return error for invalid routing config ID' , async ( ) => {
59+ const client = new RoutingConfigurationApiClient ( ) ;
60+
61+ const response = await client . get ( 'mock-token' , invalidRoutingConfigId ) ;
62+
63+ expect ( response . error ) . toEqual ( {
64+ errorMeta : {
65+ code : ErrorCase . VALIDATION_FAILED ,
66+ description : 'Invalid routing configuration ID format' ,
67+ details : { id : invalidRoutingConfigId } ,
68+ } ,
69+ actualError : undefined ,
70+ } ) ;
71+ expect ( response . data ) . toBeUndefined ( ) ;
72+ expect ( axiosMock . history . get . length ) . toBe ( 0 ) ;
73+ } ) ;
74+
4875 it ( 'should return routing configuration on success' , async ( ) => {
4976 const data = {
50- id : 'routing-config-1' ,
77+ id : validRoutingConfigId ,
5178 name : 'Test message plan' ,
5279 status : 'DRAFT' as RoutingConfigStatus ,
5380 clientId : 'client-1' ,
@@ -58,13 +85,15 @@ describe('RoutingConfigurationApiClient', () => {
5885 cascadeGroupOverrides : [ ] ,
5986 } ;
6087
61- axiosMock . onGet ( '/v1/routing-configuration/routing-config-1' ) . reply ( 200 , {
62- data,
63- } ) ;
88+ axiosMock
89+ . onGet ( `/v1/routing-configuration/${ validRoutingConfigId } ` )
90+ . reply ( 200 , {
91+ data,
92+ } ) ;
6493
6594 const client = new RoutingConfigurationApiClient ( ) ;
6695
67- const response = await client . get ( 'mock-token' , 'routing-config-1' ) ;
96+ const response = await client . get ( 'mock-token' , validRoutingConfigId ) ;
6897
6998 expect ( response . error ) . toBeUndefined ( ) ;
7099 expect ( response . data ) . toEqual ( data ) ;
@@ -74,16 +103,18 @@ describe('RoutingConfigurationApiClient', () => {
74103
75104 describe ( 'update' , ( ) => {
76105 it ( 'should return error when failing to update via API' , async ( ) => {
77- axiosMock . onPut ( '/v1/routing-configuration/routing-config-2' ) . reply ( 400 , {
78- statusCode : 400 ,
79- technicalMessage : 'Bad request' ,
80- details : { message : 'Broken' } ,
81- } ) ;
106+ axiosMock
107+ . onPut ( `/v1/routing-configuration/${ notFoundRoutingConfigId } ` )
108+ . reply ( 404 , {
109+ statusCode : 404 ,
110+ technicalMessage : 'Not Found' ,
111+ details : { message : 'Routing configuration not found' } ,
112+ } ) ;
82113
83114 const client = new RoutingConfigurationApiClient ( ) ;
84115
85116 const body = {
86- id : 'routing-config-2' ,
117+ id : notFoundRoutingConfigId ,
87118 name : 'Test plan' ,
88119 status : 'DRAFT' as RoutingConfigStatus ,
89120 clientId : 'client-1' ,
@@ -96,24 +127,57 @@ describe('RoutingConfigurationApiClient', () => {
96127
97128 const response = await client . update (
98129 'test-token' ,
99- 'routing-config-2' ,
130+ notFoundRoutingConfigId ,
100131 body
101132 ) ;
102133
103134 expect ( response . error ) . toEqual ( {
104135 errorMeta : {
105- code : 400 ,
106- description : 'Bad request ' ,
107- details : { message : 'Broken ' } ,
136+ code : 404 ,
137+ description : 'Not Found ' ,
138+ details : { message : 'Routing configuration not found ' } ,
108139 } ,
109140 } ) ;
110141 expect ( response . data ) . toBeUndefined ( ) ;
111142 expect ( axiosMock . history . put . length ) . toBe ( 1 ) ;
112143 } ) ;
113144
145+ it ( 'should return error for invalid routing config ID' , async ( ) => {
146+ const client = new RoutingConfigurationApiClient ( ) ;
147+
148+ const body = {
149+ id : invalidRoutingConfigId ,
150+ name : 'Test plan' ,
151+ status : 'DRAFT' as RoutingConfigStatus ,
152+ clientId : 'client-1' ,
153+ campaignId : 'campaign-1' ,
154+ createdAt : '2025-01-01T00:00:00.000Z' ,
155+ updatedAt : '2025-01-02T00:00:00.000Z' ,
156+ cascade : [ ] ,
157+ cascadeGroupOverrides : [ ] ,
158+ } ;
159+
160+ const response = await client . update (
161+ 'mock-token' ,
162+ invalidRoutingConfigId ,
163+ body
164+ ) ;
165+
166+ expect ( response . error ) . toEqual ( {
167+ errorMeta : {
168+ code : ErrorCase . VALIDATION_FAILED ,
169+ description : 'Invalid routing configuration ID format' ,
170+ details : { id : invalidRoutingConfigId } ,
171+ } ,
172+ actualError : undefined ,
173+ } ) ;
174+ expect ( response . data ) . toBeUndefined ( ) ;
175+ expect ( axiosMock . history . get . length ) . toBe ( 0 ) ;
176+ } ) ;
177+
114178 it ( 'should return updated routing configuration on success' , async ( ) => {
115179 const body = {
116- id : 'routing-config-2 ' ,
180+ id : '4c6e8f0a-2b3d-4c5e-9a1b-7d8c9b0a1f2e ' ,
117181 name : 'Updated Plan' ,
118182 status : 'DRAFT' as RoutingConfigStatus ,
119183 clientId : 'client-1' ,
@@ -132,7 +196,7 @@ describe('RoutingConfigurationApiClient', () => {
132196
133197 const response = await client . update (
134198 'test-token' ,
135- 'routing-config-2 ' ,
199+ '4c6e8f0a-2b3d-4c5e-9a1b-7d8c9b0a1f2e ' ,
136200 body
137201 ) ;
138202
@@ -141,4 +205,17 @@ describe('RoutingConfigurationApiClient', () => {
141205 expect ( axiosMock . history . put . length ) . toBe ( 1 ) ;
142206 } ) ;
143207 } ) ;
208+
209+ describe ( 'isValidUuid' , ( ) => {
210+ it ( 'returns true for valid UUID v4' , ( ) => {
211+ expect ( isValidUuid ( 'a3f1c2e4-5b6d-4e8f-9a2b-1c3d4e5f6a7b' ) ) . toBe ( true ) ;
212+ expect ( isValidUuid ( 'b7e2d3c4-8f9a-4b1c-9d2e-3f4a5b6c7d8e' ) ) . toBe ( true ) ;
213+ } ) ;
214+
215+ it ( 'returns false for invalid UUIDs' , ( ) => {
216+ expect ( isValidUuid ( 'not-a-uuid' ) ) . toBe ( false ) ;
217+ expect ( isValidUuid ( '123456' ) ) . toBe ( false ) ;
218+ expect ( isValidUuid ( '11111111-1111-1111-1111-111111111111' ) ) . toBe ( false ) ;
219+ } ) ;
220+ } ) ;
144221} ) ;
0 commit comments