@@ -417,6 +417,49 @@ describe('AccountSearchView', () => {
417417 expect ( accounts [ 2 ] . name ) . toBe ( 'Test Charlie' ) ;
418418 } ) ;
419419
420+ it ( 'should prioritize Ghost sites (internal accounts) over external accounts' , async ( ) => {
421+ const [ viewer ] = await fixtureManager . createInternalAccount ( ) ;
422+
423+ // Create an internal account (Ghost site) - has user record
424+ // Use a name starting with Z to ensure alphabetical sort would put it last
425+ const [ ghostSite ] = await fixtureManager . createInternalAccount ( ) ;
426+
427+ await db ( 'accounts' )
428+ . where ( 'id' , ghostSite . id )
429+ . update ( { name : 'Test Zebra Ghost Site' } ) ;
430+
431+ // Create external accounts - no user record
432+ // Use names starting with A and B to ensure alphabetical sort would put them first
433+ await db ( 'accounts' ) . insert ( [
434+ {
435+ ap_id : 'https://example.com/users/alice' ,
436+ username : 'alice' ,
437+ domain : 'example.com' ,
438+ ap_inbox_url : 'https://example.com/users/alice/inbox' ,
439+ name : 'Test Alice External' ,
440+ } ,
441+ {
442+ ap_id : 'https://example.com/users/bob' ,
443+ username : 'bob' ,
444+ domain : 'example.com' ,
445+ ap_inbox_url : 'https://example.com/users/bob/inbox' ,
446+ name : 'Test Bob External' ,
447+ } ,
448+ ] ) ;
449+
450+ const accounts = await accountSearchView . searchByName (
451+ 'Test' ,
452+ viewer . id ,
453+ ) ;
454+
455+ expect ( accounts ) . toHaveLength ( 3 ) ;
456+ // Ghost site should appear first despite having name starting with Z
457+ expect ( accounts [ 0 ] . name ) . toBe ( 'Test Zebra Ghost Site' ) ;
458+ // External accounts should be sorted alphabetically after Ghost sites
459+ expect ( accounts [ 1 ] . name ) . toBe ( 'Test Alice External' ) ;
460+ expect ( accounts [ 2 ] . name ) . toBe ( 'Test Bob External' ) ;
461+ } ) ;
462+
420463 it ( 'should limit results to maximum' , async ( ) => {
421464 const [ viewer ] = await fixtureManager . createInternalAccount ( ) ;
422465
@@ -799,5 +842,50 @@ describe('AccountSearchView', () => {
799842 expect ( accounts ) . toHaveLength ( 1 ) ;
800843 expect ( accounts [ 0 ] . handle ) . toBe ( '@alice@example.com' ) ;
801844 } ) ;
845+
846+ it ( 'should prioritize Ghost sites (internal accounts) over external accounts' , async ( ) => {
847+ const [ viewer ] = await fixtureManager . createInternalAccount ( ) ;
848+
849+ // Create an internal account (Ghost site)
850+ // Use a name starting with Z to ensure alphabetical sort would put it last
851+ const [ ghostSite ] = await fixtureManager . createInternalAccount ( ) ;
852+ await db ( 'accounts' )
853+ . where ( 'id' , ghostSite . id )
854+ . update ( { name : 'Zebra Ghost Site' } ) ;
855+
856+ const ghostSiteDomain = new URL ( ghostSite . apId . toString ( ) ) . hostname ;
857+
858+ // Create external accounts on the same domain as the ghost site - no user record
859+ // Use names starting with A and B to ensure alphabetical sort would put them first
860+ await db ( 'accounts' ) . insert ( [
861+ {
862+ ap_id : `https://${ ghostSiteDomain } /users/alice` ,
863+ username : 'alice' ,
864+ domain : ghostSiteDomain ,
865+ ap_inbox_url : `https://${ ghostSiteDomain } /users/alice/inbox` ,
866+ name : 'Alice External' ,
867+ } ,
868+ {
869+ ap_id : `https://${ ghostSiteDomain } /users/bob` ,
870+ username : 'bob' ,
871+ domain : ghostSiteDomain ,
872+ ap_inbox_url : `https://${ ghostSiteDomain } /users/bob/inbox` ,
873+ name : 'Bob External' ,
874+ } ,
875+ ] ) ;
876+
877+ const accounts = await accountSearchView . searchByDomain (
878+ ghostSiteDomain ,
879+ viewer . id ,
880+ ) ;
881+
882+ // Should have 3 accounts (ghostSite + 2 external)
883+ expect ( accounts ) . toHaveLength ( 3 ) ;
884+ // Ghost site should appear first despite having name starting with Z
885+ expect ( accounts [ 0 ] . name ) . toBe ( 'Zebra Ghost Site' ) ;
886+ // External accounts should be sorted alphabetically after Ghost sites
887+ expect ( accounts [ 1 ] . name ) . toBe ( 'Alice External' ) ;
888+ expect ( accounts [ 2 ] . name ) . toBe ( 'Bob External' ) ;
889+ } ) ;
802890 } ) ;
803891} ) ;
0 commit comments