@@ -24,6 +24,7 @@ vi.mock("@/lib/common/logger", () => ({
2424 Logger : {
2525 getInstance : vi . fn ( ( ) => ( {
2626 debug : vi . fn ( ) ,
27+ error : vi . fn ( ) ,
2728 } ) ) ,
2829 } ,
2930} ) ) ;
@@ -104,7 +105,7 @@ describe("sendUpdatesToBackend", () => {
104105 if ( ! result . ok ) {
105106 expect ( result . error . code ) . toBe ( "network_error" ) ;
106107 expect ( result . error . message ) . toBe (
107- "Error updating user with userId user_123"
108+ "Error updating user with userId user_123" ,
108109 ) ;
109110 }
110111 } ) ;
@@ -128,7 +129,7 @@ describe("sendUpdatesToBackend", () => {
128129 appUrl : mockAppUrl ,
129130 environmentId : mockEnvironmentId ,
130131 updates : mockUpdates ,
131- } )
132+ } ) ,
132133 ) . rejects . toThrow ( "Network error" ) ;
133134 } ) ;
134135} ) ;
@@ -150,6 +151,7 @@ describe("sendUpdates", () => {
150151
151152 ( Logger . getInstance as Mock ) . mockImplementation ( ( ) => ( {
152153 debug : vi . fn ( ) ,
154+ error : vi . fn ( ) ,
153155 } ) ) ;
154156 } ) ;
155157
@@ -176,6 +178,9 @@ describe("sendUpdates", () => {
176178 } ) ;
177179
178180 expect ( result . ok ) . toBe ( true ) ;
181+ if ( result . ok ) {
182+ expect ( result . data . hasWarnings ) . toBe ( false ) ;
183+ }
179184 } ) ;
180185
181186 test ( "handles backend errors" , async ( ) => {
@@ -204,6 +209,74 @@ describe("sendUpdates", () => {
204209 }
205210 } ) ;
206211
212+ test ( "returns hasWarnings true when backend returns errors" , async ( ) => {
213+ const mockResponse = {
214+ ok : true ,
215+ data : {
216+ state : {
217+ data : {
218+ userId : mockUserId ,
219+ attributes : mockAttributes ,
220+ } ,
221+ expiresAt : new Date ( Date . now ( ) + 1000 * 60 * 30 ) ,
222+ } ,
223+ errors : [ "Attribute 'invalidKey' has an invalid key format" ] ,
224+ } ,
225+ } ;
226+
227+ ( ApiClient as Mock ) . mockImplementation ( function ( ) {
228+ return { createOrUpdateUser : vi . fn ( ) . mockResolvedValue ( mockResponse ) } ;
229+ } ) ;
230+
231+ const result = await sendUpdates ( {
232+ updates : { userId : mockUserId , attributes : mockAttributes } ,
233+ } ) ;
234+
235+ expect ( result . ok ) . toBe ( true ) ;
236+ if ( result . ok ) {
237+ expect ( result . data . hasWarnings ) . toBe ( true ) ;
238+ }
239+ } ) ;
240+
241+ test ( "logs backend errors at error level and messages at debug level" , async ( ) => {
242+ const mockError = vi . fn ( ) ;
243+ const mockDebug = vi . fn ( ) ;
244+ ( Logger . getInstance as Mock ) . mockImplementation ( ( ) => ( {
245+ debug : mockDebug ,
246+ error : mockError ,
247+ } ) ) ;
248+
249+ const mockResponse = {
250+ ok : true ,
251+ data : {
252+ state : {
253+ data : {
254+ userId : mockUserId ,
255+ attributes : mockAttributes ,
256+ } ,
257+ expiresAt : new Date ( Date . now ( ) + 1000 * 60 * 30 ) ,
258+ } ,
259+ messages : [ "Email attribute already exists" ] ,
260+ errors : [ "Attribute 'badKey' has an invalid format" ] ,
261+ } ,
262+ } ;
263+
264+ ( ApiClient as Mock ) . mockImplementation ( function ( ) {
265+ return { createOrUpdateUser : vi . fn ( ) . mockResolvedValue ( mockResponse ) } ;
266+ } ) ;
267+
268+ await sendUpdates ( {
269+ updates : { userId : mockUserId , attributes : mockAttributes } ,
270+ } ) ;
271+
272+ expect ( mockError ) . toHaveBeenCalledWith (
273+ "Attribute 'badKey' has an invalid format" ,
274+ ) ;
275+ expect ( mockDebug ) . toHaveBeenCalledWith (
276+ "User update message: Email attribute already exists" ,
277+ ) ;
278+ } ) ;
279+
207280 test ( "handles unexpected errors" , async ( ) => {
208281 ( ApiClient as Mock ) . mockImplementation ( function ( ) {
209282 return {
0 commit comments