110 lines
2.9 KiB
Go
110 lines
2.9 KiB
Go
package opencode
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// The active endpoint, not finish/tool-calls or a step boundary, owns the
|
|
// foreground drain. Reconnecting after the last event must still settle it.
|
|
func TestChatActivityMultiStepAndReconnect(t *testing.T) {
|
|
var active atomic.Bool
|
|
var invalid atomic.Bool
|
|
active.Store(true)
|
|
events := make(chan string, 8)
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/api/session/active":
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if invalid.Load() {
|
|
fmt.Fprint(w, `{"data":null}`)
|
|
return
|
|
}
|
|
if active.Load() {
|
|
fmt.Fprint(w, `{"data":{"ses_1":{"type":"running"}}}`)
|
|
} else {
|
|
fmt.Fprint(w, `{"data":{}}`)
|
|
}
|
|
case "/api/event":
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
w.WriteHeader(200)
|
|
w.(http.Flusher).Flush()
|
|
for {
|
|
select {
|
|
case <-r.Context().Done():
|
|
return
|
|
case event := <-events:
|
|
fmt.Fprintf(w, "data: %s\n\n", event)
|
|
w.(http.Flusher).Flush()
|
|
}
|
|
}
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
|
frames := make(chan protocol.ChatEventFrame, 32)
|
|
start := func() (context.CancelFunc, <-chan error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
done <- chat.Stream(ctx, "ses_1", nil, func(frame protocol.ChatEventFrame) error { frames <- frame; return nil })
|
|
}()
|
|
return cancel, done
|
|
}
|
|
next := func() protocol.ChatEventFrame {
|
|
t.Helper()
|
|
select {
|
|
case frame := <-frames:
|
|
return frame
|
|
case <-time.After(3 * time.Second):
|
|
t.Fatal("no activity frame")
|
|
return protocol.ChatEventFrame{}
|
|
}
|
|
}
|
|
cancel, done := start()
|
|
defer cancel()
|
|
// Active readback restores Running even with no SSE replay on reconnect.
|
|
if frame := next(); frame.Step == nil || frame.Step.Status != protocol.ChatStepStarted {
|
|
t.Fatalf("active = %+v", frame)
|
|
}
|
|
for i := 0; i < 2; i++ {
|
|
events <- `{"type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","finish":"tool-calls"}}`
|
|
frame := next()
|
|
if frame.Step == nil || !frame.Step.Intermediate {
|
|
t.Fatalf("step must be intermediate: %+v", frame)
|
|
}
|
|
}
|
|
select {
|
|
case frame := <-frames:
|
|
t.Fatalf("premature completion: %+v", frame)
|
|
case <-time.After(650 * time.Millisecond):
|
|
}
|
|
cancel()
|
|
<-done
|
|
active.Store(false)
|
|
invalid.Store(true)
|
|
cancel2, done2 := start()
|
|
defer cancel2()
|
|
select {
|
|
case frame := <-frames:
|
|
t.Fatalf("invalid readback settled turn: %+v", frame)
|
|
case <-time.After(650 * time.Millisecond):
|
|
}
|
|
invalid.Store(false)
|
|
frame := next()
|
|
if frame.Step == nil || frame.Step.Status != protocol.ChatStepEnded || frame.Step.Intermediate {
|
|
t.Fatalf("idle after reconnect = %+v", frame)
|
|
}
|
|
cancel2()
|
|
<-done2
|
|
}
|