109 lines
3.5 KiB
Go
109 lines
3.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 TestRunPageOpensPinnedPipelineWithoutCancellingRun(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
store, err := pipeline.Open(filepath.Join(t.TempDir(), "runs.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if _, err := store.Submit(ctx, "workflow", pipeline.Template([]string{"original"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := store.List(ctx)
|
|
id := items[0].ID
|
|
if _, err := store.Submit(ctx, "workflow", pipeline.Template([]string{"updated"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv := httptest.NewServer(server.Handler(ctx, store))
|
|
defer srv.Close()
|
|
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)
|
|
d, err := store.Get(ctx, id, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runID, _, err := store.CreateRun(ctx, d, "navigation", "/repo", false, nil, map[string]any{"prompt": "task"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run, err := store.GetRun(ctx, runID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m.runOpen = true
|
|
m.keys.runs = true
|
|
m.runDetail = run
|
|
m.requestedRunID = runID
|
|
m.cliRun = true
|
|
m.cliStarted = false
|
|
if !slices.Contains(m.pageCommands(), ":pipeline") {
|
|
t.Fatal("run page is missing pipeline command")
|
|
}
|
|
updated, send := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'P'}})
|
|
m = updated.(Model)
|
|
if send == nil || m.runOpen || !m.detailLoading || m.requestedPipelineID != id || m.requestedVersion != 1 || !m.cliStarted {
|
|
t.Fatal("incorrect run-to-pipeline navigation")
|
|
}
|
|
if _, ok := send().(sentMsg); !ok {
|
|
t.Fatal("pipeline request failed")
|
|
}
|
|
updated, _ = m.Update(read())
|
|
m = updated.(Model)
|
|
if m.detail == nil || m.detail.Version != 1 || m.detail.LatestVersion != 2 || m.detail.Graph.Stages[0].Key != "original" || m.runForm != 0 {
|
|
t.Fatal("navigation did not open the run's exact immutable version", m.detail)
|
|
}
|
|
after, err := store.GetRun(ctx, runID)
|
|
if err != nil || after.Status != run.Status {
|
|
t.Fatal("navigation changed run state", after, err)
|
|
}
|
|
updated, _ = m.Update(receivedMsg(protocol.Message{Type: protocol.Run, RunDetail: run}))
|
|
m = updated.(Model)
|
|
if m.runOpen || m.runDetail != nil || m.detail.Version != 1 {
|
|
t.Fatal("late polling response reopened the run page")
|
|
}
|
|
}
|
|
|
|
func TestRunsListPipelineCommandUsesSelectedFilteredRun(t *testing.T) {
|
|
m := New(context.Background(), "")
|
|
m.runOpen = true
|
|
m.keys.runs = true
|
|
m.runPipelineID = 42
|
|
m.runs = []protocol.RunSummary{{ID: 1, PipelineID: 1, Version: 9}, {ID: 2, PipelineID: 42, Version: 3}, {ID: 3, PipelineID: 42, Version: 4}}
|
|
m.runIndex = 1
|
|
if id, version := m.selectedRunPipeline(); id != 42 || version != 4 {
|
|
t.Fatal("wrong selected run", id, version)
|
|
}
|
|
m.commandInput = newInput()
|
|
m.commandInput.SetValue(":pipeline")
|
|
updated, _ := m.updatePalette(tea.KeyMsg{Type: tea.KeyEnter})
|
|
m = updated.(Model)
|
|
if m.runOpen || m.requestedPipelineID != 42 || m.requestedVersion != 4 {
|
|
t.Fatal("palette did not open selected pipeline")
|
|
}
|
|
if slices.Contains(m.pageCommands(), ":pipeline") {
|
|
t.Fatal("run-only command leaked onto pipeline page")
|
|
}
|
|
}
|