@@ -159,6 +159,16 @@ describe('identifyUser', () => {
159159 expect ( user . emailVerified ) . toBeUndefined ( )
160160 } )
161161
162+ it ( 'always includes id even when fields omits it' , ( ) => {
163+ const log = createMockLogger ( )
164+ identifyUser ( log , createMockSession ( ) , { fields : [ 'name' , 'email' ] } )
165+
166+ const user = log . setCalls [ 0 ] . user as Record < string , unknown >
167+ expect ( user . id ) . toBe ( 'usr_123' )
168+ expect ( user . name ) . toBe ( 'Hugo Richard' )
169+ expect ( user . email ) . toBe ( 'hugo@example.com' )
170+ } )
171+
162172 it ( 'handles string dates in session' , ( ) => {
163173 const log = createMockLogger ( )
164174 const session = createMockSession ( {
@@ -270,7 +280,7 @@ describe('createAuthMiddleware', () => {
270280 expect ( authData . identified ) . toBe ( false )
271281 } )
272282
273- it ( 'catches errors silently ' , async ( ) => {
283+ it ( 'catches errors and sets auth metadata with error flag ' , async ( ) => {
274284 const log = createMockLogger ( )
275285 const auth = {
276286 api : {
@@ -281,6 +291,27 @@ describe('createAuthMiddleware', () => {
281291
282292 const result = await identify ( log , new Headers ( ) )
283293 expect ( result ) . toBe ( false )
294+ const authData = log . setCalls . find ( c => c . auth ) ?. auth as Record < string , unknown >
295+ expect ( authData ) . toBeDefined ( )
296+ expect ( authData . identified ) . toBe ( false )
297+ expect ( authData . error ) . toBe ( true )
298+ expect ( typeof authData . resolvedIn ) . toBe ( 'number' )
299+ } )
300+
301+ it ( 'calls onAnonymous on error path' , async ( ) => {
302+ const log = createMockLogger ( )
303+ const auth = {
304+ api : {
305+ getSession : vi . fn ( ( ) => Promise . reject ( new Error ( 'DB down' ) ) ) ,
306+ } ,
307+ }
308+ const onAnonymous = vi . fn ( )
309+ const identify = createAuthMiddleware ( auth , { onAnonymous } )
310+
311+ await identify ( log , new Headers ( ) )
312+
313+ expect ( onAnonymous ) . toHaveBeenCalledOnce ( )
314+ expect ( onAnonymous ) . toHaveBeenCalledWith ( log )
284315 } )
285316
286317 it ( 'passes identify options through' , async ( ) => {
0 commit comments