119 lines
4.1 KiB
Go
119 lines
4.1 KiB
Go
package opencode
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// Schema-derived fixtures, not a captured live provider run:
|
|
// anomalyco/opencode@3fdb13bc262a16b3e8f8ecab4381f619963d9bfd
|
|
// packages/schema/src/session-event.ts (Compaction), and canonical V2 OpenAPI
|
|
// Session.Message.Compaction.{Running,Completed,Failed}.
|
|
func TestNormalizeCompactionIsMessageLessSessionActivity(t *testing.T) {
|
|
state := &ChatStreamState{LastMessageID: "previous_assistant"}
|
|
for _, tc := range []struct{ event, status string }{
|
|
{"started", "running"}, {"ended", "completed"}, {"started", "running"}, {"failed", "failed"},
|
|
} {
|
|
payload := []byte(fmt.Sprintf(`{"type":"session.compaction.%s","data":{"sessionID":"ses_1","reason":"auto"}}`, tc.event))
|
|
frame, ok, err := NormalizeChatEvent(payload, "ses_1", state)
|
|
if err != nil || !ok || frame.Kind != protocol.ChatEventCompaction || frame.Compaction.Status != tc.status || frame.MessageID != "" || frame.Part != nil || frame.Step != nil {
|
|
t.Fatalf("invalid compaction: %+v %v %v", frame, ok, err)
|
|
}
|
|
if (state.Compaction != nil) != (tc.status == "running") {
|
|
t.Fatalf("retained state: %+v", state.Compaction)
|
|
}
|
|
}
|
|
_, ok, err := NormalizeChatEvent([]byte(`{"type":"session.compaction.started","data":{"sessionID":"ses_other","reason":"auto"}}`), "ses_1", state)
|
|
if ok || err != nil || state.Compaction != nil {
|
|
t.Fatal("foreign session contaminated compaction")
|
|
}
|
|
_, ok, err = NormalizeChatEvent([]byte(`{"type":"session.compaction.delta","data":{"sessionID":"ses_1","text":"private summary"}}`), "ses_1", state)
|
|
if ok || err != nil {
|
|
t.Fatal("compaction summary must not become transcript content")
|
|
}
|
|
}
|
|
|
|
func TestCompactionSnapshotAndForegroundCompletion(t *testing.T) {
|
|
events := make(chan string, 8)
|
|
active := make(chan struct{})
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
switch r.URL.Path {
|
|
case "/api/session/ses_1/message":
|
|
fmt.Fprint(w, `{"data":[{"type":"compaction","id":"msg_compact","status":"running","reason":"auto","summary":"private","recent":"private","time":{"created":1}}]}`)
|
|
case "/api/session/active":
|
|
select {
|
|
case <-active:
|
|
fmt.Fprint(w, `{"data":{}}`)
|
|
default:
|
|
fmt.Fprint(w, `{"data":{"ses_1":{}}}`)
|
|
}
|
|
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})
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
frames := make(chan protocol.ChatEventFrame, 8)
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
done <- chat.Stream(ctx, "ses_1", nil, func(f protocol.ChatEventFrame) error { frames <- f; return nil })
|
|
}()
|
|
next := func() protocol.ChatEventFrame {
|
|
t.Helper()
|
|
select {
|
|
case f := <-frames:
|
|
return f
|
|
case <-ctx.Done():
|
|
t.Fatal("missing frame")
|
|
return protocol.ChatEventFrame{}
|
|
}
|
|
}
|
|
if f := next(); f.Compaction == nil || f.Compaction.Status != "running" || f.MessageID != "" {
|
|
t.Fatalf("snapshot: %+v", f)
|
|
}
|
|
events <- `{"type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","finish":"tool-calls"}}`
|
|
if f := next(); f.Step == nil || !f.Step.Intermediate {
|
|
t.Fatalf("provider step: %+v", f)
|
|
}
|
|
state := chat.streamState("ses_1")
|
|
state.mu.Lock()
|
|
running := state.Compaction != nil
|
|
state.mu.Unlock()
|
|
if !running {
|
|
t.Fatal("intermediate provider step cleared compaction")
|
|
}
|
|
close(active)
|
|
if f := next(); f.Step == nil || f.Step.Status != protocol.ChatStepEnded || f.Step.Intermediate {
|
|
t.Fatalf("foreground completion: %+v", f)
|
|
}
|
|
cancel()
|
|
<-done
|
|
state.mu.Lock()
|
|
defer state.mu.Unlock()
|
|
if state.Compaction != nil {
|
|
t.Fatal("stream/foreground completion retained stuck compaction")
|
|
}
|
|
}
|