@@ -12,6 +12,7 @@ import { FediverseBridge } from '@/activitypub/fediverse-bridge';
1212import type { UriBuilder } from '@/activitypub/uri' ;
1313import type { FedifyContext } from '@/app' ;
1414import { Post , PostType } from '@/post/post.entity' ;
15+ import type { KnexPostRepository } from '@/post/post.repository.knex' ;
1516import { PostCreatedEvent } from '@/post/post-created.event' ;
1617import { PostDeletedEvent } from '@/post/post-deleted.event' ;
1718import { PostUpdatedEvent } from '@/post/post-updated.event' ;
@@ -29,6 +30,7 @@ vi.mock('node:crypto', async (importOriginal) => {
2930describe ( 'FediverseBridge' , ( ) => {
3031 let events : EventEmitter ;
3132 let accountService : AccountService ;
33+ let postRepository : KnexPostRepository ;
3234 let context : FedifyContext ;
3335 let fedifyContextFactory : FedifyContextFactory ;
3436 let mockUriBuilder : UriBuilder < FedifyObject > ;
@@ -39,6 +41,9 @@ describe('FediverseBridge', () => {
3941 accountService = {
4042 getAccountById : vi . fn ( ) ,
4143 } as unknown as AccountService ;
44+ postRepository = {
45+ getById : vi . fn ( ) ,
46+ } as unknown as KnexPostRepository ;
4247 mockUriBuilder = {
4348 buildObjectUri : vi . fn ( ) . mockImplementation ( ( object , { id } ) => {
4449 return new URL (
@@ -75,6 +80,7 @@ describe('FediverseBridge', () => {
7580 events ,
7681 fedifyContextFactory ,
7782 accountService ,
83+ postRepository ,
7884 ) ;
7985 } ) ;
8086
@@ -163,6 +169,7 @@ describe('FediverseBridge', () => {
163169 events ,
164170 fedifyContextFactory ,
165171 accountService ,
172+ postRepository ,
166173 ) ;
167174 await bridge . init ( ) ;
168175
@@ -246,6 +253,7 @@ describe('FediverseBridge', () => {
246253 events ,
247254 fedifyContextFactory ,
248255 accountService ,
256+ postRepository ,
249257 ) ;
250258 await bridge . init ( ) ;
251259
@@ -291,6 +299,7 @@ describe('FediverseBridge', () => {
291299 events ,
292300 fedifyContextFactory ,
293301 accountService ,
302+ postRepository ,
294303 ) ;
295304 await bridge . init ( ) ;
296305
@@ -333,6 +342,7 @@ describe('FediverseBridge', () => {
333342 events ,
334343 fedifyContextFactory ,
335344 accountService ,
345+ postRepository ,
336346 ) ;
337347 await bridge . init ( ) ;
338348
@@ -515,10 +525,13 @@ describe('FediverseBridge', () => {
515525 post . apId = new URL ( 'https://example.com/article/post-456' ) ;
516526 post . publishedAt = new Date ( '2025-01-01T00:00:00Z' ) ;
517527
518- const event = new PostUpdatedEvent ( post ) ;
528+ vi . mocked ( postRepository . getById ) . mockResolvedValue ( post ) ;
529+
530+ const event = new PostUpdatedEvent ( 456 ) ;
519531 events . emit ( PostUpdatedEvent . getName ( ) , event ) ;
520532
521533 await nextTick ( ) ;
534+ expect ( postRepository . getById ) . toHaveBeenCalledWith ( 456 ) ;
522535 expect ( sendActivity ) . toHaveBeenCalledOnce ( ) ;
523536 expect ( context . data . globaldb . set ) . toHaveBeenCalledTimes ( 2 ) ;
524537
@@ -547,10 +560,29 @@ describe('FediverseBridge', () => {
547560 post . content = 'Updated post content' ;
548561 post . apId = new URL ( 'https://external.com/article/post-456' ) ;
549562
550- const event = new PostUpdatedEvent ( post ) ;
563+ vi . mocked ( postRepository . getById ) . mockResolvedValue ( post ) ;
564+
565+ const event = new PostUpdatedEvent ( 456 ) ;
566+ events . emit ( PostUpdatedEvent . getName ( ) , event ) ;
567+
568+ await nextTick ( ) ;
569+ expect ( postRepository . getById ) . toHaveBeenCalledWith ( 456 ) ;
570+ expect ( sendActivity ) . not . toHaveBeenCalled ( ) ;
571+ expect ( context . data . globaldb . set ) . not . toHaveBeenCalled ( ) ;
572+ } ) ;
573+
574+ it ( 'should not send update activities on the PostUpdatedEvent if post is not found' , async ( ) => {
575+ await bridge . init ( ) ;
576+
577+ const sendActivity = vi . spyOn ( context , 'sendActivity' ) ;
578+
579+ vi . mocked ( postRepository . getById ) . mockResolvedValue ( null ) ;
580+
581+ const event = new PostUpdatedEvent ( 999 ) ;
551582 events . emit ( PostUpdatedEvent . getName ( ) , event ) ;
552583
553584 await nextTick ( ) ;
585+ expect ( postRepository . getById ) . toHaveBeenCalledWith ( 999 ) ;
554586 expect ( sendActivity ) . not . toHaveBeenCalled ( ) ;
555587 expect ( context . data . globaldb . set ) . not . toHaveBeenCalled ( ) ;
556588 } ) ;
0 commit comments