@@ -4,6 +4,8 @@ import { describe, expect, it, vi } from 'vitest'
44import { createPgRepositoryMock , now } from '@/helper/pg-repository-mock'
55import { AppException } from '~/common/errors/exception.types'
66import { ArticleTypeEnum } from '~/constants/article.constant'
7+ import { BusinessEvents , EventScope } from '~/constants/business-event.constant'
8+ import { EventBusEvents } from '~/constants/event-bus.constant'
79import {
810 CATEGORY_SERVICE_TOKEN ,
911 DRAFT_SERVICE_TOKEN ,
@@ -103,6 +105,7 @@ const createService = () => {
103105 commentService,
104106 contentMigrationCommitService,
105107 draftService,
108+ eventManager,
106109 fileReferenceService,
107110 repository,
108111 service,
@@ -268,6 +271,7 @@ describe('PostService', () => {
268271 const {
269272 commentService,
270273 draftService,
274+ eventManager,
271275 fileReferenceService,
272276 repository,
273277 service,
@@ -285,6 +289,29 @@ describe('PostService', () => {
285289 expect (
286290 fileReferenceService . removeReferencesForDocument ,
287291 ) . toHaveBeenCalledWith ( 'post-1' , FileReferenceType . Post )
292+ expect ( eventManager . emit ) . toHaveBeenCalledWith (
293+ EventBusEvents . CleanAggregateCache ,
294+ null ,
295+ { scope : EventScope . TO_SYSTEM } ,
296+ )
297+ expect ( eventManager . emit ) . toHaveBeenCalledWith (
298+ BusinessEvents . POST_DELETE ,
299+ { id : 'post-1' } ,
300+ {
301+ scope : EventScope . TO_SYSTEM_VISITOR ,
302+ nextTick : true ,
303+ } ,
304+ )
305+ } )
306+
307+ it ( 'does not invalidate aggregate caches when post deletion fails' , async ( ) => {
308+ const { eventManager, repository, service } = createService ( )
309+ repository . findById . mockResolvedValue ( createPost ( ) )
310+ repository . deleteById . mockRejectedValue ( new Error ( 'delete failed' ) )
311+
312+ await expect ( service . deletePost ( 'post-1' ) ) . rejects . toThrow ( 'delete failed' )
313+
314+ expect ( eventManager . emit ) . not . toHaveBeenCalled ( )
288315 } )
289316
290317 it ( 'rejects missing related post ids' , async ( ) => {
0 commit comments