c42a19f7d3
Make OpenCode authoring responsive and expand the managed skill references. Add conflict-aware import/export, real-provider reference workflows, and tracked V2 generation for reliable budget accounting.
149 lines
4.8 KiB
Go
149 lines
4.8 KiB
Go
package tui
|
||
|
||
import (
|
||
"context"
|
||
"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"
|
||
"github.com/charmbracelet/x/ansi"
|
||
"net/http/httptest"
|
||
"path/filepath"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
)
|
||
|
||
func TestRunStageProgressAnimatesAndStops(t *testing.T) {
|
||
m := runInspectionModel()
|
||
m.runDetail.Status = "running"
|
||
m.runStepIndex = 2
|
||
before := ansi.Strip(m.runsView(26))
|
||
updated, cmd := m.Update(m.runSpinner.Tick())
|
||
m = updated.(Model)
|
||
after := ansi.Strip(m.runsView(26))
|
||
if cmd == nil || before == after {
|
||
t.Fatal("running stage spinner did not animate")
|
||
}
|
||
if !strings.Contains(after, "✓") || !strings.Contains(after, "running") {
|
||
t.Fatal("stage progress missing", after)
|
||
}
|
||
for _, test := range []struct{ status, outcome, target, want string }{
|
||
{"completed", "success", protocol.SuccessEnd, "✓ done"},
|
||
{"completed", "error", protocol.FailureEnd, "✗ failed"},
|
||
{"failed", "error", protocol.FailureEnd, "✗ failed"},
|
||
{"interrupted", "", "", "! interrupted"},
|
||
{"canceled", "", "", "− canceled"},
|
||
} {
|
||
m.runDetail.Steps[1].Status = test.status
|
||
m.runDetail.Steps[1].Outcome = test.outcome
|
||
m.runDetail.Steps[1].Target = test.target
|
||
before := ansi.Strip(strings.Join(m.runSectionLines(100), "\n"))
|
||
updated, _ := m.Update(m.runSpinner.Tick())
|
||
m = updated.(Model)
|
||
after := ansi.Strip(strings.Join(m.runSectionLines(100), "\n"))
|
||
if !strings.Contains(after, test.want) || before != after {
|
||
t.Fatal("finished stage should have static marker", test.want, after)
|
||
}
|
||
}
|
||
m.runDetail.Status = "interrupted"
|
||
m.runDetail.Steps[1].Status = "running"
|
||
if m.stepProgressStatus(m.runDetail.Steps[1]) != "interrupted" {
|
||
t.Fatal("stale terminal run still animates")
|
||
}
|
||
}
|
||
|
||
func TestOpenRunRefreshesWithoutNavigation(t *testing.T) {
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||
defer cancel()
|
||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "live.db"))
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer store.Close()
|
||
handler := server.Handler(ctx, store)
|
||
defer func() { cancel(); server.WaitRuns(handler) }()
|
||
srv := httptest.NewServer(handler)
|
||
defer srv.Close()
|
||
if _, err := store.Submit(ctx, "refresh", pipeline.Template([]string{"first", "second"}, true)); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
items, err := store.List(ctx)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
definition, err := store.Get(ctx, items[0].ID, 1)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
id, _, err := store.CreateRun(ctx, definition, "live-refresh", "workspace", false, nil, map[string]any{"prompt": "task"})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := store.BeginStep(ctx, id, 1, "first", nil); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
m := New(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect")
|
||
m.width = 120
|
||
m.height = 36
|
||
connected := m.connect()()
|
||
updated, _ := m.Update(connected)
|
||
m = updated.(Model)
|
||
if m.conn == nil {
|
||
t.Fatal("connection failed", connected)
|
||
}
|
||
defer m.conn.CloseNow()
|
||
updated, _ = m.Update(m.read()())
|
||
m = updated.(Model)
|
||
m.runOpen = true
|
||
updated, send := m.runRequest(protocol.Message{Type: protocol.GetRun, RunID: id})
|
||
m = updated.(Model)
|
||
send()
|
||
updated, _ = m.Update(m.read()())
|
||
m = updated.(Model)
|
||
if m.runDetail == nil || m.runDetail.Steps[0].Status != "running" {
|
||
t.Fatal("initial run not loaded")
|
||
}
|
||
m.runStepIndex = 1
|
||
m.runDetailFocus = true
|
||
m.runTop = 2
|
||
m.runCollapsed = map[string]bool{"1:1": false}
|
||
refresh := func() {
|
||
t.Helper()
|
||
tick := runPoll()()
|
||
updated, batch := m.Update(tick)
|
||
m = updated.(Model)
|
||
commands, ok := batch().(tea.BatchMsg)
|
||
if !ok || len(commands) != 2 {
|
||
t.Fatal("refresh timer did not reschedule")
|
||
}
|
||
commands[0]()
|
||
updated, _ = m.Update(m.read()())
|
||
m = updated.(Model)
|
||
}
|
||
if err := store.FinishStep(ctx, id, protocol.RunStep{Sequence: 1, Status: "completed", Outcome: "success", Target: "second", Output: map[string]any{"summary": "stage finished"}}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := store.BeginStep(ctx, id, 2, "second", nil); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
refresh()
|
||
if len(m.runDetail.Steps) != 2 || m.runDetail.Steps[0].Status != "completed" || m.runDetail.Steps[1].Status != "running" {
|
||
t.Fatal("open run did not update")
|
||
}
|
||
if m.runStepIndex != 1 || !m.runDetailFocus || m.runTop != 2 || m.runCollapsed["1:1"] {
|
||
t.Fatal("refresh reset inspection state")
|
||
}
|
||
if err := store.FinishStep(ctx, id, protocol.RunStep{Sequence: 2, Status: "failed", Outcome: "error", Target: protocol.FailureEnd}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := store.FinishRun(ctx, id, "failed", "error", "test failure", nil); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
refresh()
|
||
view := ansi.Strip(m.runsView(32))
|
||
if m.runDetail.Status != "failed" || !strings.Contains(view, "✗ failed") || !strings.Contains(view, "✓ done") {
|
||
t.Fatal("terminal update missing", view)
|
||
}
|
||
}
|