package tui import ( "context" "git.a-wan.net/alan/pipelines/examples" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/x/ansi" "os" "path/filepath" "strings" "testing" ) func TestTemplateMarkdownPreviewDraftAndKeyboardScope(t *testing.T) { t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir()) 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 { 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") updated, cmd := m.updateTemplates(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) if cmd == nil || !m.editorOpen || !m.editorDraft.createOnly || m.editorDraft.source != m.templates[m.templateIndex].Source { t.Fatal("template should offer an editor without losing the selected source") } m = press(m, "esc") if m.editorOpen || !m.templateOpen { t.Fatal("canceling editor picker should return to template gallery") } 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) } } } }