Files

148 lines
4.9 KiB
Go

package codex
import (
"context"
"encoding/json"
"errors"
"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:
}
}
func TestExplicitPromptLoadsOnlyAConfirmedUnloadedThread(t *testing.T) {
var methods []string
c := modelTestChat(t, func(method string, params json.RawMessage) any {
methods = append(methods, method)
if method == "turn/start" {
var p map[string]any
json.Unmarshal(params, &p)
if p["model"] != "fixture-model" || p["clientUserMessageId"] != "msg_same" {
t.Errorf("recovery changed explicit turn identity/model: %s", params)
}
}
if len(methods) == 1 {
return &rpcError{Code: -32602, Message: "thread not found: " + fixtureThread}
}
if method == "thread/resume" {
var p map[string]any
json.Unmarshal(params, &p)
if p["threadId"] != fixtureThread || p["excludeTurns"] != true || len(p) != 2 {
t.Errorf("resume changed configuration: %s", params)
}
return map[string]any{"thread": map[string]any{"id": fixtureThread}}
}
return map[string]any{"turn": map[string]any{"id": "loaded-turn"}}
})
if err := c.PromptWithMessageIDModel(context.Background(), fixtureThread, "explicit user message", "msg_same", "fixture-model"); err != nil {
t.Fatal(err)
}
if len(methods) != 3 || methods[0] != "turn/start" || methods[1] != "thread/resume" || methods[2] != "turn/start" {
t.Fatalf("unsafe recovery sequence: %v", methods)
}
}
func TestUnrelatedPromptRefusalIsNeverRetriedOrResumed(t *testing.T) {
methods := []string{}
c := modelTestChat(t, func(method string, _ json.RawMessage) any {
methods = append(methods, method)
return &rpcError{Code: -32602, Message: "controlled provider rejection"}
})
if err := c.Prompt(context.Background(), fixtureThread, "explicit user message"); err == nil {
t.Fatal("refusal ignored")
}
if len(methods) != 1 || methods[0] != "turn/start" {
t.Fatalf("unsafe retry: %v", methods)
}
}
func TestTransportFailureCannotTriggerAnUnloadedThreadRetry(t *testing.T) {
entered, release := make(chan struct{}), make(chan struct{})
var once sync.Once
c := modelTestChat(t, func(method string, _ json.RawMessage) any {
if method != "turn/start" {
t.Errorf("transport failure retried through %s", method)
}
once.Do(func() { close(entered) })
<-release
return map[string]any{"turn": map[string]any{"id": "possibly-admitted"}}
})
result := make(chan error, 1)
go func() { result <- c.Prompt(context.Background(), fixtureThread, "explicit message") }()
<-entered
// Even a transport failure whose text resembles a provider refusal is not
// evidence that the original turn was never admitted.
c.failPending(errors.New("thread not found: " + fixtureThread))
if err := <-result; err == nil {
t.Fatal("transport failure reported success")
}
close(release)
}