@@ -330,10 +330,10 @@ describe('Users API', () => {
330330 owner_address : address ,
331331 } )
332332
333- // Create some test apps for the user
333+ // Create some test apps for the user (must be moderated to be counted)
334334 await db ( 'apps' ) . insert ( [
335- { name : 'Test App 1' , owner_address : address , template_id : templateId } ,
336- { name : 'Test App 2' , owner_address : address , template_id : templateId } ,
335+ { name : 'Test App 1' , owner_address : address , template_id : templateId , moderated : true } ,
336+ { name : 'Test App 2' , owner_address : address , template_id : templateId , moderated : true } ,
337337 ] )
338338
339339 const response = await request ( app ) . get ( '/api/with-app-counts' )
@@ -359,6 +359,58 @@ describe('Users API', () => {
359359 expect ( Number ( testUser ?. app_count ) ) . toBe ( 2 ) // Count is returned as string from the database
360360 } )
361361
362+ it ( 'should only count moderated apps, not non-moderated apps' , async ( ) => {
363+ // Create test users
364+ const address1 = '0x1234567890123456789012345678901234567890'
365+ const address2 = '0x2345678901234567890123456789012345678901'
366+ await db ( 'users' ) . insert ( [ { address : address1 } , { address : address2 } ] )
367+
368+ // Create a test template
369+ const [ templateId ] = await db ( 'templates' ) . insert ( {
370+ title : 'Test Template' ,
371+ url : 'https://example.com' ,
372+ json_data : '{}' ,
373+ owner_address : address1 ,
374+ } )
375+
376+ // User 1: 2 moderated apps, 1 non-moderated app
377+ await db ( 'apps' ) . insert ( [
378+ { name : 'Moderated App 1' , owner_address : address1 , template_id : templateId , moderated : true } ,
379+ { name : 'Moderated App 2' , owner_address : address1 , template_id : templateId , moderated : true } ,
380+ { name : 'Non-moderated App' , owner_address : address1 , template_id : templateId , moderated : false } ,
381+ ] )
382+
383+ // User 2: Only non-moderated apps (should not appear in results)
384+ await db ( 'apps' ) . insert ( [
385+ { name : 'Non-moderated App 3' , owner_address : address2 , template_id : templateId , moderated : false } ,
386+ { name : 'Non-moderated App 4' , owner_address : address2 , template_id : templateId , moderated : false } ,
387+ ] )
388+
389+ const response = await request ( app ) . get ( '/api/with-app-counts' )
390+
391+ expect ( response . status ) . toBe ( 200 )
392+ expect ( response . body ) . toHaveProperty ( 'users' )
393+
394+ // Type the response body properly
395+ interface UserResponse {
396+ trimmed_address : string
397+ app_count : string
398+ }
399+
400+ const responseBody = response . body . users as UserResponse [ ]
401+
402+ // User 1 should appear with count of 2 (only moderated apps)
403+ const expectedTrimmedAddress1 = `${ address1 . substring ( 0 , 7 ) } ...${ address1 . substring ( address1 . length - 5 ) } `
404+ const testUser1 = responseBody . find ( user => user . trimmed_address === expectedTrimmedAddress1 )
405+ expect ( testUser1 ) . toBeDefined ( )
406+ expect ( Number ( testUser1 ?. app_count ) ) . toBe ( 2 ) // Only moderated apps counted
407+
408+ // User 2 should NOT appear because they have no moderated apps
409+ const expectedTrimmedAddress2 = `${ address2 . substring ( 0 , 7 ) } ...${ address2 . substring ( address2 . length - 5 ) } `
410+ const testUser2 = responseBody . find ( user => user . trimmed_address === expectedTrimmedAddress2 )
411+ expect ( testUser2 ) . toBeUndefined ( )
412+ } )
413+
362414 it ( 'should handle database errors gracefully' , async ( ) => {
363415 // Silence console.error during this test
364416 console . error = jest . fn ( )
0 commit comments