Files
pipelines/internal/tui/templates_test.go
T

84 lines
2.3 KiB
Go

package tui
import (
"context"
"git.a-wan.net/alan/pipelines/examples"
"git.a-wan.net/alan/pipelines/internal/protocol"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi"
"os"
"path/filepath"
"strings"
"testing"
)
func TestTemplateMarkdownPreviewDraftAndKeyboardScope(t *testing.T) {
m := New(context.Background(), "")
m.width = 120
m.height = 36
m.templateOpen = true
m.keys.templates = true
m.templates = examples.Catalog()
m.renderTemplate()
before := m.templateIndex
m = press(m, "n")
if m.templateIndex == before || m.formStep != 0 {
t.Fatal("template n collided with new pipeline")
}
m = press(m, "p")
if m.templateIndex != before {
t.Fatal("previous template failed")
}
view := ansi.Strip(m.View())
if !strings.Contains(view, "Templates") || !strings.Contains(view, "Preview only") && !strings.Contains(view, "SCHEDULED") || strings.Contains(view, "```") {
t.Fatal("not a rendered preview", view)
}
m = press(m, "v")
if !strings.Contains(ansi.Strip(m.View()), "```") {
t.Fatal("raw Markdown not available")
}
m = press(m, "v")
m = press(m, "enter")
if !m.templateNaming {
t.Fatal("template did not ask for a new name")
}
m.field.SetValue("existing")
m.pipelines = []protocol.PipelineSummary{{Name: "existing"}}
updated, cmd := m.updateTemplates(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
if cmd != nil || m.templateError == "" {
t.Fatal("existing name accepted")
}
m = press(m, "esc")
m = press(m, "esc")
if m.templateOpen {
t.Fatal("template gallery did not close")
}
path := filepath.Join(t.TempDir(), "new.md")
os.WriteFile(path, []byte("source"), 0600)
result := readEditedDraft("new", path, "source", false, nil)
result.confirm = true
result.createOnly = true
updated, _ = m.Update(result)
m = updated.(Model)
if !m.confirmOpen || m.pendingDraft == nil || !m.pendingDraft.createOnly {
t.Fatal("new draft submitted without confirmation")
}
}
func TestTemplateRenderingFitsSmallTerminals(t *testing.T) {
for _, width := range []int{1, 12, 40, 80, 120} {
m := New(context.Background(), "")
m.width = width
m.height = 24
m.templates = examples.Catalog()
m.templateOpen = true
m.renderTemplate()
for _, line := range strings.Split(m.View(), "\n") {
if ansi.StringWidth(line) > width {
t.Fatalf("template overflow at %d", width)
}
}
}
}