146 lines
4.4 KiB
Go
146 lines
4.4 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) {
|
|
configureTestHarness(t)
|
|
t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir())
|
|
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.HandlerWithOptions(ctx, server.Options{AllowAuthoring: true}, 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")
|
|
}
|
|
var editor tea.Cmd
|
|
updated, editor = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'n'}})
|
|
m = updated.(Model)
|
|
if editor == nil || !m.editorOpen || !m.editorDraft.createOnly {
|
|
t.Fatal("n should offer an editor for a new create-only draft")
|
|
}
|
|
m.editorInput.SetValue("ocd")
|
|
updated, editor = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
|
|
m = updated.(Model)
|
|
if editor != nil || m.editorOpen || m.mutationCommand == nil {
|
|
t.Fatal("new editor choice did not require approval")
|
|
}
|
|
updated, editor = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'y'}})
|
|
m = updated.(Model)
|
|
if editor == nil {
|
|
t.Fatal("approved editor did not launch")
|
|
}
|
|
// Editor lifecycle is smoke-tested separately; feed its completion here.
|
|
source := pipeline.Template([]string{"a", "b"}, true)
|
|
for _, draft := range []string{source, source, "invalid"} {
|
|
updated, send := m.Update(editedMsg{name: "demo", path: "draft.md", source: draft})
|
|
m = updated.(Model)
|
|
if send == nil || m.confirmOpen {
|
|
t.Fatal("editor publication check missing")
|
|
}
|
|
updated, _ = m.Update(send())
|
|
m = updated.(Model)
|
|
if !m.confirmOpen && strings.Contains(m.submitNotice, "Already published") {
|
|
continue
|
|
}
|
|
if !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{"Created pipeline demo: version 1", "Already published", "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.confirmOpen = true
|
|
m.pendingDraft = &editedMsg{name: "demo", source: "source"}
|
|
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, "Create pipeline") || !strings.Contains(view, "demo") {
|
|
t.Fatal("missing form content")
|
|
}
|
|
var top int
|
|
for i, line := range lines {
|
|
if strings.Contains(line, "Create pipeline") {
|
|
top = i
|
|
if !strings.HasPrefix(line, " ") {
|
|
t.Fatalf("dialog not horizontally centered: %q", line)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
if top < 5 {
|
|
t.Fatal("dialog not vertically centered")
|
|
}
|
|
}
|
|
}
|
|
}
|