Files
pipelines/internal/codex/readback_test.go
T

78 lines
2.3 KiB
Go

package codex
import (
"context"
"encoding/json"
"sync"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestStreamReconcilesAcceptedTurnWithoutNativeNotifications(t *testing.T) {
var mu sync.Mutex
started := false
c := modelTestChat(t, func(method string, _ json.RawMessage) any {
mu.Lock()
defer mu.Unlock()
switch method {
case "turn/start":
started = true
return map[string]any{"turn": map[string]any{"id": "turn-new"}}
case "thread/turns/list":
if !started {
return json.RawMessage(`{"data":[],"nextCursor":null}`)
}
return json.RawMessage(`{"data":[{"id":"turn-new","status":"completed","items":[{"id":"reply","type":"agentMessage","text":"done"}]}],"nextCursor":null}`)
default:
t.Errorf("readback must never resume/configure: %s", method)
return nil
}
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ready, ended := make(chan struct{}), make(chan struct{})
var once sync.Once
sawResync := false
go c.Stream(ctx, fixtureThread, func() { close(ready) }, func(frame protocol.ChatEventFrame) error {
if frame.Kind == protocol.ChatEventResync {
sawResync = true
}
if frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded {
if frame.Step.TurnID != "turn-new" {
t.Error("wrong turn completed")
}
once.Do(func() { close(ended) })
}
return nil
})
<-ready
if err := c.Prompt(ctx, fixtureThread, "tool-free verification"); err != nil {
t.Fatal(err)
}
select {
case <-ended:
if !sawResync {
t.Fatal("completion without authoritative history refresh")
}
case <-time.After(4 * time.Second):
t.Fatal("accepted turn stayed running without native notifications")
}
}
func TestLateCompletionDoesNotClearNewCodexTurn(t *testing.T) {
c := &Chat{turns: map[string]string{fixtureThread: "new"}, subs: map[string]map[chan protocol.ChatEventFrame]struct{}{}}
ch := c.subscribe(fixtureThread)
defer c.unsubscribe(fixtureThread, ch)
c.handleNotification(rpcMessage{Method: "turn/completed", Params: json.RawMessage(`{"threadId":"` + fixtureThread + `","turn":{"id":"old","status":"completed"}}`)})
if c.turns[fixtureThread] != "new" {
t.Fatal("old completion released new turn")
}
select {
case <-ch:
t.Fatal("stale terminal frame emitted")
default:
}
}