package tui import ( "context" "git.a-wan.net/alan/pipelines/examples" "git.a-wan.net/alan/pipelines/internal/pipeline" 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.KeyRunes, Runes: []rune{'e'}}) 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 TestTemplateGraphPreviewFocusNavigationAndSizes(t *testing.T) { for _, size := range []struct{ w, h int }{{40, 16}, {80, 24}, {120, 36}, {160, 48}} { m := New(context.Background(), "") m.width, m.height = size.w, size.h m.templateOpen = true m.keys.templates = true m.templates = examples.Catalog() for i, item := range m.templates { m.templateIndex = i m.templateGraphFocus = false m.renderTemplate() description, graph := m.templatePaneHeights(size.h - 4) if description <= graph { t.Fatal("description should dominate initial preview", size, item.Key) } if !strings.Contains(ansi.Strip(m.View()), "Template graph") { t.Fatal("graph preview missing", item.Key) } m = press(m, "tab") description, graph = m.templatePaneHeights(size.h - 4) if !m.templateGraphFocus || graph <= description { t.Fatal("Tab did not expand graph", size, item.Key) } parsed, err := pipeline.Parse(item.Source) if err != nil { t.Fatal(err) } view := ansi.Strip(m.View()) if !strings.Contains(view, parsed.Stages[0].Key) { t.Fatal("focused graph does not match selected template", size, item.Key, view) } oldIndex, oldTop := m.templateIndex, m.templateTop m = press(m, "j") m = press(m, "l") if m.templateIndex != oldIndex || m.templateTop != oldTop { t.Fatal("graph panning moved template or description") } m = press(m, "c") if m.templateGraphTop != 0 || m.templateGraphPan != 0 { t.Fatal("graph centering failed") } for _, line := range strings.Split(m.View(), "\n") { if ansi.StringWidth(line) > size.w { t.Fatal("focused preview overflow", size) } } m = press(m, "shift+tab") if m.templateGraphFocus { t.Fatal("reverse Tab introduced another focus target") } } } } 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) } } } }