|
1 | 1 | package cli |
2 | 2 |
|
3 | | -// |
4 | | -//import ( |
5 | | -// "fmt" |
6 | | -// "testing" |
7 | | -// "time" |
8 | | -// |
9 | | -// st "github.com/coder/agentapi/lib/cli/screentracker" |
10 | | -// "github.com/coder/agentapi/lib/httpapi" |
11 | | -// "github.com/stretchr/testify/assert" |
12 | | -//) |
13 | | -// |
14 | | -//func TestEventEmitter(t *testing.T) { |
15 | | -// t.Run("single-subscription", func(t *testing.T) { |
16 | | -// emitter := httpapi.NewEventEmitter(10) |
17 | | -// _, ch, stateEvents := emitter.Subscribe() |
18 | | -// assert.Empty(t, ch) |
19 | | -// assert.Equal(t, []httpapi.Event{ |
20 | | -// { |
21 | | -// Type: httpapi.EventTypeStatusChange, |
22 | | -// Payload: httpapi.StatusChangeBody{Status: httpapi.AgentStatusRunning}, |
23 | | -// }, |
24 | | -// { |
25 | | -// Type: httpapi.EventTypeScreenUpdate, |
26 | | -// Payload: httpapi.ConversationUpdateBody{Screen: ""}, |
27 | | -// }, |
28 | | -// }, stateEvents) |
29 | | -// |
30 | | -// now := time.Now() |
31 | | -// emitter.UpdateMessagesAndEmitChanges([]st.ConversationMessage{ |
32 | | -// {Id: 1, Message: "Hello, world!", Role: st.ConversationRoleUser, Time: now}, |
33 | | -// }) |
34 | | -// newEvent := <-ch |
35 | | -// assert.Equal(t, httpapi.Event{ |
36 | | -// Type: httpapi.EventTypeMessageUpdate, |
37 | | -// Payload: httpapi.MessageUpdateBody{Id: 1, Message: "Hello, world!", Role: st.ConversationRoleUser, Time: now}, |
38 | | -// }, newEvent) |
39 | | -// |
40 | | -// emitter.UpdateMessagesAndEmitChanges([]st.ConversationMessage{ |
41 | | -// {Id: 1, Message: "Hello, world! (updated)", Role: st.ConversationRoleUser, Time: now}, |
42 | | -// {Id: 2, Message: "What's up?", Role: st.ConversationRoleAgent, Time: now}, |
43 | | -// }) |
44 | | -// newEvent = <-ch |
45 | | -// assert.Equal(t, httpapi.Event{ |
46 | | -// Type: httpapi.EventTypeMessageUpdate, |
47 | | -// Payload: httpapi.MessageUpdateBody{Id: 1, Message: "Hello, world! (updated)", Role: st.ConversationRoleUser, Time: now}, |
48 | | -// }, newEvent) |
49 | | -// |
50 | | -// newEvent = <-ch |
51 | | -// assert.Equal(t, httpapi.Event{ |
52 | | -// Type: httpapi.EventTypeMessageUpdate, |
53 | | -// Payload: httpapi.MessageUpdateBody{Id: 2, Message: "What's up?", Role: st.ConversationRoleAgent, Time: now}, |
54 | | -// }, newEvent) |
55 | | -// |
56 | | -// emitter.UpdateStatusAndEmitChanges(st.ConversationStatusStable) |
57 | | -// newEvent = <-ch |
58 | | -// assert.Equal(t, httpapi.Event{ |
59 | | -// Type: httpapi.EventTypeStatusChange, |
60 | | -// Payload: httpapi.StatusChangeBody{Status: httpapi.AgentStatusStable}, |
61 | | -// }, newEvent) |
62 | | -// }) |
63 | | -// |
64 | | -// t.Run("multiple-subscriptions", func(t *testing.T) { |
65 | | -// emitter := httpapi.NewEventEmitter(10) |
66 | | -// channels := make([]<-chan httpapi.Event, 0, 10) |
67 | | -// for i := 0; i < 10; i++ { |
68 | | -// _, ch, _ := emitter.Subscribe() |
69 | | -// channels = append(channels, ch) |
70 | | -// } |
71 | | -// now := time.Now() |
72 | | -// |
73 | | -// emitter.UpdateMessagesAndEmitChanges([]st.ConversationMessage{ |
74 | | -// {Id: 1, Message: "Hello, world!", Role: st.ConversationRoleUser, Time: now}, |
75 | | -// }) |
76 | | -// for _, ch := range channels { |
77 | | -// newEvent := <-ch |
78 | | -// assert.Equal(t, httpapi.Event{ |
79 | | -// Type: httpapi.EventTypeMessageUpdate, |
80 | | -// Payload: httpapi.MessageUpdateBody{Id: 1, Message: "Hello, world!", Role: st.ConversationRoleUser, Time: now}, |
81 | | -// }, newEvent) |
82 | | -// } |
83 | | -// }) |
84 | | -// |
85 | | -// t.Run("close-channel", func(t *testing.T) { |
86 | | -// emitter := httpapi.NewEventEmitter(1) |
87 | | -// _, ch, _ := emitter.Subscribe() |
88 | | -// for i := range 5 { |
89 | | -// emitter.UpdateMessagesAndEmitChanges([]st.ConversationMessage{ |
90 | | -// {Id: i, Message: fmt.Sprintf("Hello, world! %d", i), Role: st.ConversationRoleUser, Time: time.Now()}, |
91 | | -// }) |
92 | | -// } |
93 | | -// _, ok := <-ch |
94 | | -// assert.True(t, ok) |
95 | | -// select { |
96 | | -// case _, ok := <-ch: |
97 | | -// assert.False(t, ok) |
98 | | -// default: |
99 | | -// t.Fatalf("read should not block") |
100 | | -// } |
101 | | -// }) |
102 | | -//} |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + st "github.com/coder/agentapi/lib/cli/screentracker" |
| 9 | + "github.com/coder/agentapi/lib/types" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestEventEmitter(t *testing.T) { |
| 14 | + t.Run("single-subscription", func(t *testing.T) { |
| 15 | + emitter := NewEventEmitter(10) |
| 16 | + _, ch, stateEvents := emitter.Subscribe() |
| 17 | + assert.Empty(t, ch) |
| 18 | + assert.Equal(t, []Event{ |
| 19 | + { |
| 20 | + Type: EventTypeStatusChange, |
| 21 | + Payload: types.StatusChangeBody{Status: types.AgentStatusRunning}, |
| 22 | + }, |
| 23 | + { |
| 24 | + Type: EventTypeScreenUpdate, |
| 25 | + Payload: types.ScreenUpdateBody{Screen: ""}, |
| 26 | + }, |
| 27 | + }, stateEvents) |
| 28 | + |
| 29 | + now := time.Now() |
| 30 | + emitter.UpdateMessagesAndEmitChanges([]types.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: types.MessageUpdateBody{Id: 1, Message: "Hello, world!", Role: st.ConversationRoleUser, Time: now}, |
| 37 | + }, newEvent) |
| 38 | + |
| 39 | + emitter.UpdateMessagesAndEmitChanges([]types.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: types.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: types.MessageUpdateBody{Id: 2, Message: "What's up?", Role: st.ConversationRoleAgent, Time: now}, |
| 53 | + }, newEvent) |
| 54 | + |
| 55 | + emitter.UpdateStatusAndEmitChanges(st.ConversationStatusStable) |
| 56 | + newEvent = <-ch |
| 57 | + assert.Equal(t, Event{ |
| 58 | + Type: EventTypeStatusChange, |
| 59 | + Payload: types.StatusChangeBody{Status: types.AgentStatusStable}, |
| 60 | + }, newEvent) |
| 61 | + }) |
| 62 | + |
| 63 | + t.Run("multiple-subscriptions", func(t *testing.T) { |
| 64 | + emitter := NewEventEmitter(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() |
| 71 | + |
| 72 | + emitter.UpdateMessagesAndEmitChanges([]types.ConversationMessage{ |
| 73 | + {Id: 1, Message: "Hello, world!", Role: st.ConversationRoleUser, Time: now}, |
| 74 | + }) |
| 75 | + for _, ch := range channels { |
| 76 | + newEvent := <-ch |
| 77 | + assert.Equal(t, Event{ |
| 78 | + Type: EventTypeMessageUpdate, |
| 79 | + Payload: types.MessageUpdateBody{Id: 1, Message: "Hello, world!", Role: st.ConversationRoleUser, Time: now}, |
| 80 | + }, newEvent) |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + t.Run("close-channel", func(t *testing.T) { |
| 85 | + emitter := NewEventEmitter(1) |
| 86 | + _, ch, _ := emitter.Subscribe() |
| 87 | + for i := range 5 { |
| 88 | + emitter.UpdateMessagesAndEmitChanges([]types.ConversationMessage{ |
| 89 | + {Id: i, Message: fmt.Sprintf("Hello, world! %d", i), Role: st.ConversationRoleUser, Time: time.Now()}, |
| 90 | + }) |
| 91 | + } |
| 92 | + _, ok := <-ch |
| 93 | + assert.True(t, ok) |
| 94 | + select { |
| 95 | + case _, ok := <-ch: |
| 96 | + assert.False(t, ok) |
| 97 | + default: |
| 98 | + t.Fatalf("read should not block") |
| 99 | + } |
| 100 | + }) |
| 101 | +} |
0 commit comments