72 lines
2.4 KiB
Go
72 lines
2.4 KiB
Go
package codex
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
func TestCommandReadbackOutputAndOwnership(t *testing.T) {
|
|
var mu sync.Mutex
|
|
status, output := "inProgress", "hello"
|
|
calls := 0
|
|
chat := modelTestChat(t, func(method string, p json.RawMessage) any {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
calls++
|
|
if method != "thread/read" {
|
|
t.Errorf("unexpected method: %s", method)
|
|
}
|
|
return map[string]any{"thread": map[string]any{"id": fixtureThread, "turns": []any{map[string]any{"items": []any{map[string]any{"id": "cmd", "type": "commandExecution", "command": "printf hello", "status": status, "aggregatedOutput": output}}}}}}
|
|
})
|
|
shells, err := chat.Shells(context.Background(), fixtureThread)
|
|
if err != nil || len(shells) != 1 || shells[0].Status != protocol.ChatShellRunning {
|
|
t.Fatalf("shells=%+v err=%v", shells, err)
|
|
}
|
|
q := protocol.ChatShellOutputQuery{SessionID: fixtureThread, ShellID: shells[0].ID, Limit: 3}
|
|
page, err := chat.ShellOutput(context.Background(), q)
|
|
if err != nil || page.Output != "hel" || !page.Truncated || page.Size != 5 {
|
|
t.Fatalf("output=%+v err=%v", page, err)
|
|
}
|
|
mu.Lock()
|
|
before := calls
|
|
mu.Unlock()
|
|
foreign := q
|
|
foreign.SessionID = "foreign"
|
|
if _, err := chat.ShellOutput(context.Background(), foreign); err == nil {
|
|
t.Fatal("cross-thread output accepted")
|
|
}
|
|
mu.Lock()
|
|
if calls != before {
|
|
t.Error("foreign query reached upstream")
|
|
}
|
|
status = "completed"
|
|
output = strings.Repeat("x", commandOutputCap+100)
|
|
mu.Unlock()
|
|
shells, err = chat.Shells(context.Background(), fixtureThread)
|
|
if err != nil || len(shells) != 0 {
|
|
t.Fatalf("completed command stayed running: %+v %v", shells, err)
|
|
}
|
|
q.Limit = protocol.ChatShellOutputLimit
|
|
page, err = chat.ShellOutput(context.Background(), q)
|
|
if err != nil || len(page.Output) != q.Limit || !page.Truncated || page.Size != commandOutputCap+100 {
|
|
t.Fatalf("bounded output=%+v err=%v", page, err)
|
|
}
|
|
}
|
|
|
|
func TestLiveCommandExitCannotBeReopenedByOldSnapshot(t *testing.T) {
|
|
c := &Chat{}
|
|
c.recordCommand(fixtureThread, item{ID: "cmd", Command: "printf test", Status: "completed"}, true)
|
|
c.recordCommand(fixtureThread, item{ID: "cmd", Command: "printf test", Status: "inProgress"}, false)
|
|
c.mu.Lock()
|
|
snapshot := c.commands[fixtureThread][codexShellID(fixtureThread, "cmd")]
|
|
c.mu.Unlock()
|
|
if snapshot.shell.Status != protocol.ChatShellExited {
|
|
t.Fatal("old readback reopened completed command")
|
|
}
|
|
}
|