1- import { describe , expect , it , vi } from 'vitest'
1+ import { afterEach , beforeEach , describe , expect , it } from 'vitest'
2+ import {
3+ type ChatStatusEvent ,
4+ chatPubSub ,
5+ publishChatStatusChanged ,
6+ } from '@/lib/mothership/chat-status'
27
3- const { publish } = vi . hoisted ( ( ) => ( { publish : vi . fn ( ) } ) )
4- vi . mock ( '@/lib/events/pubsub' , ( ) => ( {
5- createPubSubChannel : ( ) => ( { publish, subscribe : vi . fn ( ) , dispose : vi . fn ( ) } ) ,
6- } ) )
8+ /** Events as a subscriber receives them from the channel (process-local without Redis). */
9+ let received : ChatStatusEvent [ ] = [ ]
10+ let unsubscribe : ( ) => void = ( ) => { }
711
8- import { publishChatStatusChanged } from '@/lib/mothership/chat-status'
12+ beforeEach ( ( ) => {
13+ received = [ ]
14+ unsubscribe = chatPubSub ?. onStatusChanged ( ( event ) => received . push ( event ) ) ?? ( ( ) => { } )
15+ } )
16+
17+ afterEach ( ( ) => unsubscribe ( ) )
918
1019describe ( 'chat status ownership' , ( ) => {
1120 it ( 'names the chat owner on workspace events, so each member can tell their own chats' , ( ) => {
1221 publishChatStatusChanged (
1322 { workspaceId : 'ws-1' , userId : 'user-1' } ,
1423 { chatId : 'chat-1' , type : 'renamed' }
1524 )
16- expect ( publish ) . toHaveBeenCalledWith ( {
17- workspaceId : 'ws-1' ,
18- userId : 'user-1' ,
19- chatId : 'chat-1' ,
20- type : 'renamed' ,
21- } )
25+ expect ( received ) . toEqual ( [
26+ { workspaceId : 'ws-1' , userId : 'user-1' , chatId : 'chat-1' , type : 'renamed' } ,
27+ ] )
2228 } )
2329
2430 it ( 'publishes a workspace event without an owner when the publisher does not know it' , ( ) => {
2531 publishChatStatusChanged ( { workspaceId : 'ws-1' } , { chatId : 'chat-1' , type : 'renamed' } )
26- expect ( publish ) . toHaveBeenLastCalledWith ( {
27- workspaceId : 'ws-1' ,
28- chatId : 'chat-1' ,
29- type : 'renamed' ,
30- } )
32+ expect ( received ) . toEqual ( [ { workspaceId : 'ws-1' , chatId : 'chat-1' , type : 'renamed' } ] )
3133 } )
3234
3335 it . each ( [ 'completed' ] as const ) (
@@ -37,20 +39,17 @@ describe('chat status ownership', () => {
3739 { organizationId : 'org-1' , userId : 'user-1' } ,
3840 { chatId : 'chat-1' , type }
3941 )
40- expect ( publish ) . toHaveBeenCalledWith ( {
41- organizationId : 'org-1' ,
42- userId : 'user-1' ,
43- chatId : 'chat-1' ,
44- type,
45- } )
42+ expect ( received ) . toEqual ( [
43+ { organizationId : 'org-1' , userId : 'user-1' , chatId : 'chat-1' , type } ,
44+ ] )
4645 }
4746 )
4847
4948 it ( 'does not broadcast an organization event without its private owner' , ( ) => {
5049 expect ( ( ) =>
5150 publishChatStatusChanged ( { organizationId : 'org-1' } , { chatId : 'chat-1' , type : 'created' } )
5251 ) . toThrow ( 'Invalid organization chat owner' )
53- expect ( publish ) . not . toHaveBeenCalled ( )
52+ expect ( received ) . toEqual ( [ ] )
5453 } )
5554
5655 it ( 'refuses ambiguous workspace and organization ownership' , ( ) => {
@@ -60,6 +59,6 @@ describe('chat status ownership', () => {
6059 { chatId : 'chat-1' , type : 'created' }
6160 )
6261 ) . toThrow ( 'Invalid organization chat owner' )
63- expect ( publish ) . not . toHaveBeenCalled ( )
62+ expect ( received ) . toEqual ( [ ] )
6463 } )
6564} )
0 commit comments