package tui import ( "context" "net/http/httptest" "path/filepath" "strings" "testing" "time" "git.a-wan.net/alan/pipelines/examples" "git.a-wan.net/alan/pipelines/internal/draft" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/server" tea "github.com/charmbracelet/bubbletea" ) func TestTemplateSubmitNamesRetainsAndExplicitlyPublishesSelectedTemplate(t *testing.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(), "templates.db")) if err != nil { t.Fatal(err) } defer store.Close() srv := httptest.NewServer(server.Handler(ctx, store)) defer srv.Close() url := "ws" + strings.TrimPrefix(srv.URL, "http") + "/connect" m := New(ctx, url) connected := m.connect()().(connectedMsg) defer connected.conn.CloseNow() updated, read := m.Update(connected) m = updated.(Model) updated, _ = m.Update(read()) m = updated.(Model) m.templates = examples.Catalog() m.templateOpen, m.keys.templates = true, true m.defaultModel = "fixture/model" for i, item := range m.templates { if item.Key == "review" { m.templateIndex = i } } m.renderTemplate() if !strings.Contains(m.templatesView(30), "Create pipeline") { t.Fatal("missing visible create button") } stale, err := draft.Create("unrelated", "unrelated draft source", url, true, 0) if err != nil { t.Fatal(err) } m.pendingDraft = &editedMsg{name: stale.Name, source: "unrelated draft source", path: stale.Path, createOnly: true} updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}}) m = updated.(Model) if !m.confirmOpen || m.editorOpen || m.pendingDraft.name != m.templates[m.templateIndex].Name || m.draftNameInput.Value() != m.pendingDraft.name || m.pendingDraft.path != "" || !m.pendingDraft.createOnly { t.Fatal("Create button did not open prefilled confirmation without an editor", m.pendingDraft) } expected := pipeline.WithDefaultModel(m.templates[m.templateIndex].Source, m.defaultModel) if m.pendingDraft.source != expected { t.Fatal("selected template/model lost; unrelated draft reused") } updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEsc}) m = updated.(Model) drafts, _ := draft.List() if len(drafts) != 1 || !m.templateOpen || m.confirmOpen { t.Fatal("canceling template naming created a draft or left gallery") } updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyCtrlA}) m = updated.(Model) updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyCtrlK}) m = updated.(Model) updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("Simple review")}) m = updated.(Model) updated, cmd = m.Update(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) items, _ := store.List(ctx) if cmd == nil || len(items) != 0 || !m.confirmOpen || m.pendingDraft.path == "" { t.Fatal("single create confirmation did not send or failed to retain draft") } retained, err := draft.Load(m.pendingDraft.path) if err != nil || retained.Name != "Simple review" || retained.Server != url || !retained.CreateOnly || retained.BaseVersion != 0 { t.Fatal("template draft metadata lost", retained, err) } text, err := draft.Read(retained.Path) if err != nil || text != expected { t.Fatal("template draft source lost", err) } if _, ok := cmd().(sentMsg); !ok { t.Fatal("publication send failed") } updated, _ = m.Update(m.read()()) m = updated.(Model) items, _ = store.List(ctx) if len(items) != 1 || items[0].Name != retained.Name || items[0].Version != 1 || m.confirmOpen { t.Fatal("template submission failed", items, m.submitNotice) } detail, err := store.Get(ctx, items[0].ID, 0) if err != nil || detail.Source != expected { t.Fatal("published wrong template", err) } text, err = draft.Read(stale.Path) if err != nil || text != "unrelated draft source" { t.Fatal("template submission damaged unrelated draft", err) } if _, err := draft.Load(retained.Path); err != nil { t.Fatal("publication deleted retained draft", err) } // Unchanged defaults also work in one confirmation; duplicate names must never update. for attempt := 0; attempt < 2; attempt++ { m.templateOpen, m.keys.templates = true, true updated, _ = m.updateTemplates(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) defaultName := m.templates[m.templateIndex].Name if m.draftNameInput.Value() != defaultName { t.Fatal("template name not prefilled") } updated, send := m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) if send == nil { t.Fatal("default name needed extra confirmation") } send() updated, _ = m.Update(m.read()()) m = updated.(Model) if attempt == 0 && m.confirmOpen { t.Fatal("default-name creation failed", m.submitNotice) } if attempt == 1 && (!m.confirmOpen || !strings.Contains(m.submitNotice, "already exists")) { t.Fatal("duplicate name did not stay editable", m.submitNotice) } } items, _ = store.List(ctx) if len(items) != 2 { t.Fatal("duplicate creation changed pipelines", items) } for _, item := range items { if item.Version != 1 { t.Fatal("template silently updated existing pipeline") } } } func TestPublicationWordingDistinguishesCreateAndUpdate(t *testing.T) { m := New(context.Background(), "") m.width = 120 for _, test := range []struct { createOnly bool base int want string }{{true, 0, "Create pipeline"}, {false, 3, "Update pipeline"}} { m.pendingDraft = &editedMsg{name: "Workflow", createOnly: test.createOnly, baseVersion: test.base} view := m.confirmView(24) if !strings.Contains(view, test.want) || strings.Contains(view, "Submit edited pipeline") || strings.Contains(view, "s submit") { t.Fatal("wrong publication wording", view) } m.confirmOpen = true if !strings.Contains(m.footerContent(), strings.ToLower(test.want)) { t.Fatal("wrong publication footer") } } }