Files
pipelines/internal/tui/create_test.go
T

142 lines
4.1 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/server"
)
func TestCreateAndSubmit(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()
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)
if !m.loaded || len(m.pipelines) != 0 {
t.Fatal("missing initial pipeline snapshot")
}
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'n'}})
m = updated.(Model)
if m.formStep != 1 {
t.Fatal("n did not open form")
}
m.field.SetValue("demo")
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
m.field.SetValue("a,a")
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
if m.formStep != 2 || m.formError == "" {
t.Fatal("duplicate stage names accepted")
}
m.field.SetValue("a,b")
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
if m.formStep != 3 || len(m.draftStages) != 2 {
t.Fatal("stage form failed")
}
// Editor lifecycle is smoke-tested separately; feed its completion here.
m.formStep = 0
m.keys.form = false
source := pipeline.Template(m.draftStages, true)
for _, draft := range []string{source, source, "invalid"} {
updated, send := m.Update(editedMsg{name: m.draftName, path: "draft.md", source: draft})
m = updated.(Model)
if send != nil || !m.confirmOpen {
t.Fatal("editor completion bypassed explicit approval")
}
updated, send = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
if _, ok := send().(sentMsg); !ok {
t.Fatal("send failed")
}
response := read()
updated, read = m.Update(response)
m = updated.(Model)
}
text := strings.Join(m.lines, "\n")
for _, expected := range []string{"Submitted demo: version 1", "no changes", "unexpected text outside a block"} {
if !strings.Contains(text, expected) {
t.Fatalf("missing %q in %s", expected, text)
}
}
if m.conn == nil {
t.Fatal("validation error disconnected client")
}
if len(m.pipelines) != 1 || m.pipelines[0].Name != "demo" || m.pipelines[0].Stages != 2 {
t.Fatal("home summary not updated after submission")
}
m = press(m, ":")
m = press(m, "refresh")
updated, send := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
if _, ok := send().(sentMsg); !ok {
t.Fatal("refresh failed")
}
updated, _ = m.Update(read())
m = updated.(Model)
if len(m.pipelines) != 1 {
t.Fatal("refresh lost pipeline summaries")
}
}
func TestPopupLayout(t *testing.T) {
for _, size := range []tea.WindowSizeMsg{{Width: 100, Height: 30}, {Width: 40, Height: 8}, {Width: 12, Height: 5}} {
m := New(context.Background(), "ws://localhost/connect")
updated, _ := m.Update(size)
m = updated.(Model)
m.formStep = 1
m.keys.form = true
m.field.SetValue("demo")
view := ansi.Strip(m.View())
lines := strings.Split(view, "\n")
if len(lines) != size.Height {
t.Fatalf("%+v: height=%d", size, len(lines))
}
for _, line := range lines {
if lipgloss.Width(line) > size.Width {
t.Fatalf("%+v: line too wide: %q", size, line)
}
}
if size.Width == 100 {
if !strings.Contains(view, "New pipeline · 1/3") || !strings.Contains(view, "demo") {
t.Fatal("missing form content")
}
var top int
for i, line := range lines {
if strings.Contains(line, "╭") {
top = i
if !strings.HasPrefix(line, " ╭") {
t.Fatalf("dialog not horizontally centered: %q", line)
}
break
}
}
if top < 5 {
t.Fatal("dialog not vertically centered")
}
}
}
}