260 lines
8.7 KiB
Go
260 lines
8.7 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/lipgloss"
|
|
"github.com/charmbracelet/x/ansi"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/server"
|
|
)
|
|
|
|
func TestSearchVersionsAndEditSubmission(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
store, err := pipeline.Open(filepath.Join(t.TempDir(), "pipelines.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
source := pipeline.Template([]string{"implementer", "reviewer"}, true)
|
|
source = strings.Replace(source, "success -> #success", "bad_implementation -> implementer\nsuccess -> #success", 1)
|
|
if _, err := store.Submit(ctx, "demo", source); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
v2 := strings.Replace(source, "Describe the task.", "Version two task.", 1)
|
|
if _, err := store.Submit(ctx, "demo", v2); 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)
|
|
m = press(m, "/")
|
|
m = press(m, "dmo")
|
|
if !m.searchOpen || !m.paletteOpen || len(m.searchMatches()) != 1 {
|
|
t.Fatal("omnisearch failed")
|
|
}
|
|
updated, send := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
|
|
m = updated.(Model)
|
|
if !m.detailLoading {
|
|
t.Fatal("missing loading state")
|
|
}
|
|
if _, ok := send().(sentMsg); !ok {
|
|
t.Fatal("get failed")
|
|
}
|
|
updated, read = m.Update(read())
|
|
m = updated.(Model)
|
|
if m.detail == nil || m.detail.Version != 2 || !strings.Contains(m.View(), "Graph") || !strings.Contains(strings.Join(m.graphLines(), "\n"), "bad_implementation") {
|
|
t.Fatal("missing pipeline graph/detail")
|
|
}
|
|
m = press(m, "v")
|
|
m = press(m, "j")
|
|
updated, send = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
|
|
m = updated.(Model)
|
|
send()
|
|
updated, read = m.Update(read())
|
|
m = updated.(Model)
|
|
if m.detail.Version != 1 || m.detail.Source != source || m.detail.LatestVersion != 2 {
|
|
t.Fatal("historic version not loaded")
|
|
}
|
|
newSource := strings.Replace(source, "Describe the task.", "Edited from version one.", 1)
|
|
updated, send = m.Update(editedMsg{name: "demo", path: "draft.md", source: newSource, confirm: true})
|
|
m = updated.(Model)
|
|
if send != nil || !m.confirmOpen {
|
|
t.Fatal("edited pipeline should require explicit submission")
|
|
}
|
|
items, err := store.List(ctx)
|
|
if err != nil || items[0].Version != 2 {
|
|
t.Fatal("edit submitted prematurely")
|
|
}
|
|
updated, send = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}})
|
|
m = updated.(Model)
|
|
if _, ok := send().(sentMsg); !ok {
|
|
t.Fatal("submit send failed")
|
|
}
|
|
updated, read = m.Update(read())
|
|
m = updated.(Model)
|
|
if m.detail.Version != 3 || m.detail.Source != newSource || m.confirmOpen || m.pendingDraft != nil || m.submitID != "" {
|
|
t.Fatal("submitted version not loaded")
|
|
}
|
|
original, err := store.Get(ctx, m.detail.ID, 1)
|
|
if err != nil || original.Source != source {
|
|
t.Fatal("historic version mutated")
|
|
}
|
|
for _, draft := range []string{newSource, "invalid"} {
|
|
updated, _ = m.Update(editedMsg{name: "demo", path: "draft.md", source: draft, confirm: true})
|
|
m = updated.(Model)
|
|
updated, send = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
|
|
m = updated.(Model)
|
|
send()
|
|
updated, read = m.Update(read())
|
|
m = updated.(Model)
|
|
if !m.confirmOpen || m.pendingDraft == nil || m.submitID != "" || m.submitNotice == "" {
|
|
t.Fatal("rejection should retain draft and display diagnostics")
|
|
}
|
|
}
|
|
m = press(m, "esc")
|
|
m = press(m, ":")
|
|
m = press(m, "submit")
|
|
m = press(m, "enter")
|
|
if !m.confirmOpen {
|
|
t.Fatal(":submit did not reopen retained draft")
|
|
}
|
|
m = press(m, "esc")
|
|
m = press(m, "esc")
|
|
if m.keys.detail || m.detail != nil {
|
|
t.Fatal("Escape did not return home")
|
|
}
|
|
}
|
|
|
|
func TestPipelineModalDimensions(t *testing.T) {
|
|
graph, err := pipeline.Parse(pipeline.Template([]string{"a", "b"}, true))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, size := range []tea.WindowSizeMsg{{Width: 100, Height: 30}, {Width: 40, Height: 10}, {Width: 12, Height: 5}, {Width: 1, Height: 1}} {
|
|
for _, mode := range []string{"pipeline", "search", "versions", "confirm"} {
|
|
m := New(context.Background(), "ws://localhost/connect")
|
|
updated, _ := m.Update(size)
|
|
m = updated.(Model)
|
|
m.keys.detail = true
|
|
m.detail = &protocol.PipelineDetail{PipelineSummary: protocol.PipelineSummary{ID: 1, Name: "demo", Version: 1, Stages: 2}, LatestVersion: 1, Graph: graph, Versions: []protocol.VersionSummary{{Version: 1, Stages: 2}}}
|
|
m.pipelines = []protocol.PipelineSummary{m.detail.PipelineSummary}
|
|
switch mode {
|
|
case "search":
|
|
m = press(m, "/")
|
|
case "versions":
|
|
m = press(m, "v")
|
|
case "confirm":
|
|
m.confirmOpen = true
|
|
m.keys.confirm = true
|
|
m.pendingDraft = &editedMsg{name: "demo"}
|
|
}
|
|
lines := strings.Split(ansi.Strip(m.View()), "\n")
|
|
if len(lines) != size.Height {
|
|
t.Fatalf("%s %+v: height=%d", mode, size, len(lines))
|
|
}
|
|
for _, line := range lines {
|
|
if lipgloss.Width(line) > size.Width {
|
|
t.Fatalf("%s %+v: too wide: %q", mode, size, line)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStaleDetailResponseIgnored(t *testing.T) {
|
|
m := New(context.Background(), "ws://localhost/connect")
|
|
m.detailRequest = "2"
|
|
updated, _ := m.Update(receivedMsg{Type: protocol.Pipeline, ID: "1", Detail: &protocol.PipelineDetail{PipelineSummary: protocol.PipelineSummary{Name: "stale"}}})
|
|
m = updated.(Model)
|
|
if m.detail != nil {
|
|
t.Fatal("stale response replaced current selection")
|
|
}
|
|
m = m.goHome()
|
|
updated, _ = m.Update(receivedMsg{Type: protocol.Pipeline, ID: "2", Detail: &protocol.PipelineDetail{}})
|
|
if updated.(Model).detail != nil {
|
|
t.Fatal("late response reopened pipeline after Escape")
|
|
}
|
|
}
|
|
|
|
func TestDisconnectedVersionSelectionQueuesLoad(t *testing.T) {
|
|
m := panelModel(t)
|
|
m = press(m, "v")
|
|
m = press(m, "j")
|
|
updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
|
|
m = updated.(Model)
|
|
if !m.detailLoading || cmd == nil {
|
|
t.Fatal("disconnected version selection silently leaves the latest version displayed")
|
|
}
|
|
if m.requestedVersion != 1 || !strings.Contains(m.View(), "Loading version 1") {
|
|
t.Fatal("requested historical version must be visible while loading")
|
|
}
|
|
}
|
|
|
|
func TestReconnectLoadsRequestedHistoricalVersion(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
store, err := pipeline.Open(filepath.Join(t.TempDir(), "db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
v1 := pipeline.Template([]string{"worker"}, true)
|
|
for _, source := range []string{v1, strings.Replace(v1, "Describe the task.", "NEW PROMPT", 1)} {
|
|
if _, err := store.Submit(ctx, "demo", source); 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)
|
|
updated, read := m.Update(connected)
|
|
m = updated.(Model)
|
|
updated, _ = m.Update(read())
|
|
m = updated.(Model)
|
|
detail, err := store.Get(ctx, 1, 2)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m.detail = detail
|
|
m.keys.detail = true
|
|
m.requestedPipelineID = 1
|
|
m.requestedVersion = 2
|
|
connected.conn.CloseNow()
|
|
updated, _ = m.Update(m.read()())
|
|
m = updated.(Model)
|
|
if m.conn != nil || !m.reconnecting {
|
|
t.Fatal("connection loss must schedule reconnection")
|
|
}
|
|
// Request history while the reconnect timer is pending.
|
|
updated, _ = m.requestPipeline(1, 1)
|
|
m = updated.(Model)
|
|
connected = m.connect()().(connectedMsg)
|
|
defer connected.conn.CloseNow()
|
|
updated, batch := m.Update(connected)
|
|
m = updated.(Model)
|
|
commands := batch().(tea.BatchMsg)
|
|
if len(commands) != 2 {
|
|
t.Fatal("reconnect must read and resend the selected version")
|
|
}
|
|
if _, ok := commands[1]().(sentMsg); !ok {
|
|
t.Fatal("retry send failed")
|
|
}
|
|
updated, read = m.Update(commands[0]())
|
|
m = updated.(Model) // Initial list.
|
|
updated, _ = m.Update(read())
|
|
m = updated.(Model)
|
|
if m.detailLoading || m.detail.Version != 1 || m.detail.Source != v1 || strings.Contains(m.View(), "LATEST") {
|
|
t.Fatal("reconnect silently loaded current instead of requested history")
|
|
}
|
|
}
|
|
|
|
func TestWrongVersionResponseShowsError(t *testing.T) {
|
|
m := panelModel(t)
|
|
m.detailRequest = "requested"
|
|
m.requestedPipelineID = 1
|
|
m.requestedVersion = 1
|
|
m.detailLoading = true
|
|
updated, _ := m.Update(receivedMsg{Type: protocol.Pipeline, ID: "requested", Detail: m.detail})
|
|
m = updated.(Model)
|
|
if m.detailError == "" || m.detailLoading {
|
|
t.Fatal("wrong version response must not silently replace selection")
|
|
}
|
|
}
|