(t *testing.T)
| 11 | ) |
| 12 | |
| 13 | func TestEventEmitter(t *testing.T) { |
| 14 | t.Run("single-subscription", func(t *testing.T) { |
| 15 | emitter := NewEventEmitter(WithSubscriptionBufSize(10)) |
| 16 | _, ch, stateEvents := emitter.Subscribe() |
| 17 | assert.Empty(t, ch) |
| 18 | assert.Equal(t, []Event{ |
| 19 | { |
| 20 | Type: EventTypeStatusChange, |
| 21 | Payload: StatusChangeBody{Status: AgentStatusRunning}, |
| 22 | }, |
| 23 | { |
| 24 | Type: EventTypeScreenUpdate, |
| 25 | Payload: ScreenUpdateBody{Screen: ""}, |
| 26 | }, |
| 27 | }, stateEvents) |
| 28 | |
| 29 | now := time.Now() |
| 30 | emitter.EmitMessages([]st.ConversationMessage{ |
| 31 | {Id: 1, Message: "Hello, world!", Role: st.ConversationRoleUser, Time: now}, |
| 32 | }) |
| 33 | newEvent := <-ch |
| 34 | assert.Equal(t, Event{ |
| 35 | Type: EventTypeMessageUpdate, |
| 36 | Payload: MessageUpdateBody{Id: 1, Message: "Hello, world!", Role: st.ConversationRoleUser, Time: now}, |
| 37 | }, newEvent) |
| 38 | |
| 39 | emitter.EmitMessages([]st.ConversationMessage{ |
| 40 | {Id: 1, Message: "Hello, world! (updated)", Role: st.ConversationRoleUser, Time: now}, |
| 41 | {Id: 2, Message: "What's up?", Role: st.ConversationRoleAgent, Time: now}, |
| 42 | }) |
| 43 | newEvent = <-ch |
| 44 | assert.Equal(t, Event{ |
| 45 | Type: EventTypeMessageUpdate, |
| 46 | Payload: MessageUpdateBody{Id: 1, Message: "Hello, world! (updated)", Role: st.ConversationRoleUser, Time: now}, |
| 47 | }, newEvent) |
| 48 | |
| 49 | newEvent = <-ch |
| 50 | assert.Equal(t, Event{ |
| 51 | Type: EventTypeMessageUpdate, |
| 52 | Payload: MessageUpdateBody{Id: 2, Message: "What's up?", Role: st.ConversationRoleAgent, Time: now}, |
| 53 | }, newEvent) |
| 54 | |
| 55 | emitter.EmitStatus(st.ConversationStatusStable) |
| 56 | newEvent = <-ch |
| 57 | assert.Equal(t, Event{ |
| 58 | Type: EventTypeStatusChange, |
| 59 | Payload: StatusChangeBody{Status: AgentStatusStable, AgentType: ""}, |
| 60 | }, newEvent) |
| 61 | }) |
| 62 | |
| 63 | t.Run("multiple-subscriptions", func(t *testing.T) { |
| 64 | emitter := NewEventEmitter(WithSubscriptionBufSize(10)) |
| 65 | channels := make([]<-chan Event, 0, 10) |
| 66 | for i := 0; i < 10; i++ { |
| 67 | _, ch, _ := emitter.Subscribe() |
| 68 | channels = append(channels, ch) |
| 69 | } |
| 70 | now := time.Now() |
nothing calls this directly
no test coverage detected