package tui import ( "context" "net/http/httptest" "os" "path/filepath" "strings" "testing" "time" "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 TestRetainedDraftBrowserValidatesAndExplicitlySubmits(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(), "drafts.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" d, err := draft.Create("", pipeline.Template([]string{"worker"}, true), url, true, 0) if err != nil { t.Fatal(err) } m := New(ctx, url) connected := m.connect()().(connectedMsg) defer connected.conn.CloseNow() updated, read := m.Update(connected) m = updated.(Model) updated, read = m.Update(read()) m = updated.(Model) updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'d'}}) m = updated.(Model) if !m.draftOpen || len(m.draftItems) != 1 || m.draftItems[0].Path != d.Path || !strings.Contains(m.View(), "Unnamed draft") { t.Fatal("retained draft not visible") } updated, validate := m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'v'}}) m = updated.(Model) updated, _ = m.Update(validate()) m = updated.(Model) if !strings.Contains(m.draftError, "Validation passed") { t.Fatal(m.draftError) } updated, send := m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}}) m = updated.(Model) items, _ := store.List(ctx) if send != nil || !m.confirmOpen || len(items) != 0 { t.Fatal("draft selection published without confirmation") } if !strings.Contains(m.View(), "Name this pipeline") { t.Fatal("missing name prompt") } updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("CLI-created draft")}) m = updated.(Model) updated, send = m.Update(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) items, _ = store.List(ctx) d.Name = "CLI-created draft" retained, err := draft.Load(d.Path) if err != nil || retained.Name != d.Name || retained.Server != url || !retained.CreateOnly || retained.BaseVersion != 0 || send != nil || len(items) != 0 || !m.confirmOpen { t.Fatal("naming lost metadata or published without confirmation", retained, err) } if source, err := draft.Read(d.Path); err != nil || source != pipeline.Template([]string{"worker"}, true) { t.Fatal("naming changed source", err) } updated, send = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) if send == nil { t.Fatal("confirmation did not submit") } if _, ok := send().(sentMsg); !ok { t.Fatal("send failed") } updated, read = m.Update(read()) m = updated.(Model) items, _ = store.List(ctx) if len(items) != 1 || items[0].Name != d.Name || m.draftOpen || m.confirmOpen { t.Fatal("draft submission failed", items, m.submitNotice) } if _, err := os.Stat(d.Path); err != nil { t.Fatal("submission removed retained draft", err) } updated, _ = m.openDrafts() m = updated.(Model) updated, _ = m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}}) m = updated.(Model) updated, send = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) send() updated, _ = m.Update(read()) m = updated.(Model) if !m.confirmOpen || m.submitNotice == "" { t.Fatal("create-only duplicate guard lost") } if _, err := store.Submit(ctx, d.Name, pipeline.Template([]string{"a", "b"}, true)); err != nil { t.Fatal(err) } stale, err := draft.Create(d.Name, pipeline.Template([]string{"a", "b", "c"}, true), url, false, 1) if err != nil { t.Fatal(err) } m.confirmOpen = false m.keys.confirm = false updated, _ = m.openDrafts() m = updated.(Model) for i, item := range m.draftItems { if item.Path == stale.Path { m.draftIndex = i } } updated, _ = m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}}) m = updated.(Model) updated, send = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) send() updated, _ = m.Update(m.read()()) m = updated.(Model) if !strings.Contains(m.submitNotice, "draft conflict") { t.Fatal("retained edit lost expected-version guard", m.submitNotice) } } func TestDraftNamePromptCancelAndInvalidName(t *testing.T) { t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir()) d, err := draft.Create("", "source", "", true, 0) if err != nil { t.Fatal(err) } m := New(context.Background(), "ws://this-server") updated, _ := m.openDrafts() m = updated.(Model) updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}}) m = updated.(Model) updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) if !m.confirmOpen || m.pendingDraft.name != "" || m.submitNotice == "" { t.Fatal("blank name accepted") } updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(strings.Repeat("é", 101))}) m = updated.(Model) updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) if cmd != nil || m.pendingDraft.name != "" { t.Fatal("oversized byte name accepted") } updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEsc}) m = updated.(Model) retained, err := draft.Load(d.Path) if err != nil || retained.Name != "" || m.confirmOpen || !m.draftOpen { t.Fatal("cancel modified draft", retained, err) } updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}}) m = updated.(Model) updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("new name")}) m = updated.(Model) updated, cmd = m.Update(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) if cmd != nil || m.pendingDraft.name != "new name" { t.Fatal("canceled input leaked into next prompt") } } func TestDraftBrowserAcceptsOtherServerAndKeepsUnnamedDraft(t *testing.T) { t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir()) d, err := draft.Create("wrong", "source", "ws://another-server", true, 0) if err != nil { t.Fatal(err) } m := New(context.Background(), "ws://this-server") updated, _ := m.openDrafts() m = updated.(Model) updated, cmd := m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}}) m = updated.(Model) if cmd != nil || !m.confirmOpen || m.draftError != "" || !m.pendingDraft.createOnly || m.pendingDraft.baseVersion != 0 { t.Fatal("portable draft was blocked or kept origin version scope", d, m.draftError) } m.pendingDraft = &editedMsg{path: d.Path, name: "", source: "source", createOnly: true} m.confirmOpen = true updated, cmd = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) if cmd != nil || !strings.Contains(m.submitNotice, "Name must") || !strings.Contains(m.confirmView(20), "Name this pipeline") { t.Fatal("unnamed draft submitted") } } // TestDraftBrowserNoticeNamesBothServersForForeignDraft covers the stale 8081 // draft case in the TUI: the selected foreign draft is projected onto the // configured server (create-only) and a notice names both endpoints instead of // silently retargeting it. func TestDraftBrowserNoticeNamesBothServersForForeignDraft(t *testing.T) { t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir()) foreign := "ws://127.0.0.1:8081/connect" configured := "ws://127.0.0.1:8080/connect" d, err := draft.Create("wrong", "source", foreign, true, 0) if err != nil { t.Fatal(err) } m := New(context.Background(), configured) updated, _ := m.openDrafts() m = updated.(Model) if !strings.Contains(m.draftNotice, foreign) || !strings.Contains(m.draftNotice, configured) { t.Fatalf("preview notice did not name both servers: %q", m.draftNotice) } // The notice must name both endpoints at any realistic panel width: an // unwrapped line is truncated by fitBlock and loses the configured URL. for _, width := range []int{60, 100} { view := m view.width, view.height = width, 24 rendered := view.View() if !strings.Contains(rendered, foreign) || !strings.Contains(rendered, configured) { t.Fatalf("notice at width %d did not name both servers:\n%s", width, rendered) } } updated, _ = m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}}) m = updated.(Model) if !m.confirmOpen || m.pendingDraft == nil || !m.pendingDraft.createOnly || m.pendingDraft.baseVersion != 0 { t.Fatalf("foreign draft was not projected as create-only: %+v", m.pendingDraft) } if !strings.Contains(m.draftNotice, foreign) || !strings.Contains(m.draftNotice, configured) { t.Fatalf("submit lost the dual-URL notice: %q", m.draftNotice) } _ = d }