125 lines
4.5 KiB
Go
125 lines
4.5 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/server"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
func TestRetryShortcutAndPaletteSendServerRetryWithoutReplacingDirectory(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
store, err := pipeline.Open(filepath.Join(t.TempDir(), "retry.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
store.Submit(ctx, "workflow", pipeline.Template([]string{"one"}, true))
|
|
items, _ := store.List(ctx)
|
|
d, _ := store.Get(ctx, items[0].ID, 1)
|
|
srv := httptest.NewServer(server.Handler(ctx, store))
|
|
defer srv.Close()
|
|
id, _, err := store.CreateRun(ctx, d, "failed", filepath.Join(t.TempDir(), "missing"), false, nil, map[string]any{"prompt": "original task"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.BeginStep(ctx, id, 1, "one", map[string]any{"prompt": "original task"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.FinishStep(ctx, id, protocol.RunStep{Sequence: 1, Stage: "one", Status: "failed", Outcome: "error", Target: protocol.FailureEnd, Input: map[string]any{"prompt": "original task"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.FinishRun(ctx, id, "failed", "error", "original failure", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run, err := store.GetRun(ctx, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m := New(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect")
|
|
connected := m.connect()().(connectedMsg)
|
|
defer connected.conn.CloseNow()
|
|
updated, read := m.Update(connected)
|
|
m = updated.(Model)
|
|
updated, read = m.Update(read())
|
|
m = updated.(Model)
|
|
for _, palette := range []bool{false, true} {
|
|
m.runOpen = true
|
|
m.keys.runs = true
|
|
m.runDetail = run
|
|
m.requestedRunID = id
|
|
m.runs = []protocol.RunSummary{run.RunSummary}
|
|
if !slices.Contains(m.pageCommands(), ":retry") {
|
|
t.Fatal("failed run missing retry action")
|
|
}
|
|
var send tea.Cmd
|
|
if palette {
|
|
m.runDetail = nil
|
|
m.requestedRunID = 0
|
|
m.commandInput = newInput()
|
|
m.commandInput.SetValue(":retry")
|
|
updated, send = m.updatePalette(tea.KeyMsg{Type: tea.KeyEnter})
|
|
} else {
|
|
updated, send = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'r'}})
|
|
}
|
|
m = updated.(Model)
|
|
if send != nil || m.actionConfirm == nil || m.actionConfirm.RunID != id || m.runStartRequest != "" {
|
|
t.Fatal("retry must ask before sending")
|
|
}
|
|
updated, send = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'y'}})
|
|
m = updated.(Model)
|
|
if send == nil || m.runStartRequest == "" || m.runDetail != nil || m.requestedRunID != 0 {
|
|
t.Fatal("retry did not start cleanly")
|
|
}
|
|
updated, _ = m.Update(receivedMsg(protocol.Message{Type: protocol.Run, RunDetail: run}))
|
|
m = updated.(Model)
|
|
if m.runDetail != nil {
|
|
t.Fatal("stale poll restored original run during retry")
|
|
}
|
|
if _, ok := send().(sentMsg); !ok {
|
|
t.Fatal("retry request failed")
|
|
}
|
|
updated, read = m.Update(read())
|
|
m = updated.(Model)
|
|
if !strings.Contains(m.runError, "workspace is unavailable") || m.runStartRequest != "" {
|
|
t.Fatal("retry replaced original directory or stuck loading after server error", m.runError)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRetryResponseClearsPendingRequestAndAllowsAnotherFailedAttempt(t *testing.T) {
|
|
m := New(context.Background(), "")
|
|
m.runOpen = true
|
|
m.keys.runs = true
|
|
m.runStartRequest = "run-2"
|
|
run := &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 3, Status: "failed", FinishedAt: "2026-10-09T00:00:00Z"}, RetryOf: 1, Input: map[string]any{"prompt": "task", "pwd": "/project"}}
|
|
updated, _ := m.Update(receivedMsg(protocol.Message{Type: protocol.RunStarted, ID: "run-2", RunDetail: run}))
|
|
m = updated.(Model)
|
|
if m.runDetail != run || m.requestedRunID != 3 || m.runStartRequest != "" || m.selectedFailedRun() != 3 {
|
|
t.Fatal("retry response was not displayed or another retry is blocked")
|
|
}
|
|
if !slices.Contains(m.pageCommands(), ":retry") {
|
|
t.Fatal("failed retry lacks retry command")
|
|
}
|
|
for _, status := range []string{"queued", "running", "succeeded", "canceled"} {
|
|
m.runDetail.Status = status
|
|
if slices.Contains(m.pageCommands(), ":retry") {
|
|
t.Fatal("retry action visible for nonfailed run", status)
|
|
}
|
|
updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'r'}})
|
|
m = updated.(Model)
|
|
if cmd != nil || m.runError != "Select a failed run to retry." {
|
|
t.Fatal("nonfailed run accepted", status)
|
|
}
|
|
}
|
|
}
|