-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathhandler_live_test.go
More file actions
227 lines (200 loc) · 6.21 KB
/
Copy pathhandler_live_test.go
File metadata and controls
227 lines (200 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package admin
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/labstack/echo/v5"
"gomodel/internal/auditlog"
"gomodel/internal/live"
"gomodel/internal/usage"
)
func TestLiveCursorRejectsInvalidValue(t *testing.T) {
broker := live.NewBroker(live.Config{Enabled: true})
h := NewHandler(nil, nil, WithLiveBroker(broker))
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/admin/live/logs?cursor=bad", nil)
rec := httptest.NewRecorder()
if err := h.LiveLogs(e.NewContext(req, rec)); err != nil {
t.Fatalf("LiveLogs returned error: %v", err)
}
if rec.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest)
}
if !strings.Contains(rec.Body.String(), "invalid cursor") {
t.Fatalf("response body = %q, want invalid cursor error", rec.Body.String())
}
}
func TestLiveTypeFilterProvidedInvalidTokensMatchNothing(t *testing.T) {
if !liveTypeFilter("").matches(live.EventAuditStarted) {
t.Fatal("empty types filter should match audit events")
}
if !liveTypeFilter("audit").matches(live.EventAuditStarted) {
t.Fatal("audit types filter should match audit events")
}
if liveTypeFilter("usage").matches(live.EventAuditStarted) {
t.Fatal("usage types filter matched audit event")
}
if liveTypeFilter("foo").matches(live.EventAuditStarted) {
t.Fatal("invalid provided types filter matched audit event")
}
}
func TestLiveLogsAppliesTypeFilterToReplayEvents(t *testing.T) {
broker := live.NewBroker(live.Config{Enabled: true})
broker.PublishAuditEvent(live.EventAuditStarted, &auditlog.LogEntry{
ID: "audit-1",
RequestID: "req-1",
Timestamp: time.Now(),
})
broker.PublishUsageEvent(live.EventUsageCompleted, &usage.UsageEntry{
ID: "usage-1",
RequestID: "req-1",
Timestamp: time.Now(),
})
body := runLiveLogsWithCanceledContext(t, broker, "/admin/live/logs?types=usage")
if strings.Contains(body, "event: audit.started") {
t.Fatalf("body contains filtered audit event: %s", body)
}
if !strings.Contains(body, "event: usage.completed") {
t.Fatalf("body = %q, want usage replay event", body)
}
body = runLiveLogsWithCanceledContext(t, broker, "/admin/live/logs?types=foo")
if strings.Contains(body, "event: audit.") || strings.Contains(body, "event: usage.") {
t.Fatalf("invalid types filter should match no replay events, got: %s", body)
}
}
func TestLiveLogsWritesResetAndReplayEvents(t *testing.T) {
broker := live.NewBroker(live.Config{Enabled: true, BufferSize: 1, ReplayLimit: 1})
broker.PublishAuditEvent(live.EventAuditStarted, &auditlog.LogEntry{
ID: "audit-1",
RequestID: "req-1",
Timestamp: time.Now(),
Method: http.MethodPost,
})
broker.PublishAuditEvent(live.EventAuditUpdated, &auditlog.LogEntry{
ID: "audit-1",
RequestID: "req-1",
Timestamp: time.Now(),
RequestedModel: "gpt-test",
})
broker.PublishAuditEvent(live.EventAuditUpdated, &auditlog.LogEntry{
ID: "audit-1",
RequestID: "req-1",
Timestamp: time.Now(),
Provider: "openai",
})
body := runLiveLogsWithCanceledContext(t, broker, "/admin/live/logs?cursor=1")
if !strings.Contains(body, "event: reset") {
t.Fatalf("body = %q, want reset event", body)
}
if !strings.Contains(body, "event: audit.updated") {
t.Fatalf("body = %q, want replayed audit event", body)
}
if !strings.Contains(body, `"provider":"openai"`) {
t.Fatalf("body = %q, want latest replay payload", body)
}
}
func TestLiveLogsForwardsEventsAndHeartbeats(t *testing.T) {
broker := live.NewBroker(live.Config{
Enabled: true,
Heartbeat: time.Millisecond,
})
h := NewHandler(nil, nil, WithLiveBroker(broker))
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/admin/live/logs?types=usage", nil)
rec := newLiveSSERecorder()
errCh := make(chan error, 1)
go func() {
errCh <- h.LiveLogs(e.NewContext(req, rec))
}()
waitForLiveOutput(t, rec, func(body string) bool {
return rec.statusCode() == http.StatusOK
})
broker.PublishUsageEvent(live.EventUsageCompleted, &usage.UsageEntry{
ID: "usage-1",
RequestID: "req-1",
Timestamp: time.Now(),
Model: "gpt-test",
})
waitForLiveOutput(t, rec, func(body string) bool {
return strings.Contains(body, "event: heartbeat") &&
strings.Contains(body, "event: usage.completed")
})
broker.Close()
select {
case err := <-errCh:
if err != nil {
t.Fatalf("LiveLogs returned error: %v", err)
}
case <-time.After(time.Second):
t.Fatal("timed out waiting for LiveLogs to exit")
}
}
func runLiveLogsWithCanceledContext(t *testing.T, broker *live.Broker, target string) string {
t.Helper()
h := NewHandler(nil, nil, WithLiveBroker(broker))
e := echo.New()
ctx, cancel := context.WithCancel(context.Background())
cancel()
req := httptest.NewRequest(http.MethodGet, target, nil).WithContext(ctx)
rec := httptest.NewRecorder()
if err := h.LiveLogs(e.NewContext(req, rec)); err != nil {
t.Fatalf("LiveLogs returned error: %v", err)
}
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d; body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
return rec.Body.String()
}
type liveSSERecorder struct {
mu sync.Mutex
header http.Header
body bytes.Buffer
status int
}
func newLiveSSERecorder() *liveSSERecorder {
return &liveSSERecorder{header: http.Header{}}
}
func (r *liveSSERecorder) Header() http.Header {
return r.header
}
func (r *liveSSERecorder) WriteHeader(status int) {
r.mu.Lock()
defer r.mu.Unlock()
r.status = status
}
func (r *liveSSERecorder) Write(p []byte) (int, error) {
r.mu.Lock()
defer r.mu.Unlock()
if r.status == 0 {
r.status = http.StatusOK
}
return r.body.Write(p)
}
func (r *liveSSERecorder) Flush() {}
func (r *liveSSERecorder) bodyString() string {
r.mu.Lock()
defer r.mu.Unlock()
return r.body.String()
}
func (r *liveSSERecorder) statusCode() int {
r.mu.Lock()
defer r.mu.Unlock()
return r.status
}
func waitForLiveOutput(t *testing.T, rec *liveSSERecorder, ready func(string) bool) {
t.Helper()
deadline := time.Now().Add(time.Second)
for time.Now().Before(deadline) {
body := rec.bodyString()
if ready(body) {
return
}
time.Sleep(time.Millisecond)
}
t.Fatalf("timed out waiting for live output; status=%d body=%q", rec.statusCode(), rec.bodyString())
}