package tui import ( "context" "encoding/json" "strings" "testing" "git.a-wan.net/alan/pipelines/internal/draft" "git.a-wan.net/alan/pipelines/internal/protocol" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/x/ansi" ) func keyMessage(value string) tea.KeyMsg { return tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(value)} } func TestRunHasExactlyTwoVisibleFocusTargetsAtEverySize(t *testing.T) { for _, size := range []struct{ width, height int }{{40, 16}, {80, 24}, {120, 36}} { m := runInspectionModel() m.width, m.height = size.width, size.height m.runDetail = nil r := runInspectionModel().runDetail updated, _ := m.Update(receivedMsg(protocol.Message{Type: protocol.Run, RunDetail: r})) m = updated.(Model) if m.runGraphFocus || !m.runDetailFocus { t.Fatal("initial result pane not focused") } for i := 0; i < 8; i++ { m = runKey(m, "tab") if m.runGraphFocus == m.runDetailFocus { t.Fatal("invisible third target", size, i) } view := ansi.Strip(m.runsView(size.height - 4)) if m.runGraphFocus { if !strings.Contains(view, "Run graph") || !strings.ContainsAny(view, "╭╔│║") { t.Fatal("focused graph not visible", size, view) } } else if !strings.Contains(strings.ToLower(view), "result") { t.Fatal("focused result not visible", size, view) } if strings.Contains(view, "▸ Stages") { t.Fatal("third pane survived") } index := m.runStepIndex top := m.runTop m = runKey(m, "j") if m.runStepIndex != index { t.Fatal("scroll key changed the recorded visit") } if !m.runGraphFocus && m.runTop < top { t.Fatal("detail scroll moved backwards") } } m = runKey(m, "shift+tab") if !m.runGraphFocus { t.Fatal("reverse Tab did not toggle graph") } m = runKey(m, "shift+tab") if !m.runDetailFocus || m.runGraphFocus { t.Fatal("reverse Tab introduced third target") } } } func TestResultDocumentRendersProseListsAndNestedValuesLosslessly(t *testing.T) { output := map[string]any{"summary": "## Accomplished\n\nUpdated **layout** and added checks.", "artifacts": []any{"screen.go", "report.md"}, "evidence": map[string]any{"command": "go test ./...", "count": json.Number("9007199254740993"), "passed": true}, "optional": nil} before, _ := json.Marshal(output) text := ansi.Strip(strings.Join(resultDocument(output, 72), "\n")) for _, want := range []string{"Accomplished", "Updated layout", "• screen.go", "• report.md", "Command", "go test ./...", "9007199254740993", "true", "(none)"} { if !strings.Contains(text, want) { t.Fatal("result data lost", want, text) } } if strings.Contains(text, "**layout**") || strings.Contains(text, `"command":`) { t.Fatal("result still looks like raw transport data", text) } after, _ := json.Marshal(output) if string(before) != string(after) { t.Fatal("visualization altered retained data") } m := runInspectionModel() m.runStepIndex = 1 m.runDetail.Steps[0].Output = output if text = ansi.Strip(strings.Join(m.runSectionLines(76), "\n")); strings.Count(text, "Updated layout") != 1 { t.Fatal("result summary duplicated", text) } } func TestActionSafetyTextIsReachableAndControlsStayPinned(t *testing.T) { m := New(context.Background(), "") m.width, m.height = 40, 16 m.actionConfirm = &protocol.Message{Type: protocol.RetryRun, RunID: 1234} var views []string for i := 0; i < 40; i++ { view := ansi.Strip(m.actionConfirmView(12)) views = append(views, strings.Join(strings.Fields(view), " ")) if !strings.Contains(view, "y confirm") || !strings.Contains(view, "Esc cancel") { t.Fatal("safety controls disappeared", view) } updated, _ := m.updateActionConfirm(keyMessage("j")) m = updated.(Model) if m.actionConfirm == nil || m.actionApproved { t.Fatal("reading warning executed action") } } all := strings.Join(views, " ") for _, warning := range []string{"NEW run", "original input", "pinned version", "Previous side effects are not undone", "commands/notifications may repeat"} { if !strings.Contains(all, warning) { t.Fatal("safety warning unreachable", warning, all) } } } func TestActiveContextsNeverAdvertiseBackgroundPageControls(t *testing.T) { for _, test := range []struct { name string setup func(*Model) want string }{ {"run form", func(m *Model) { m.runForm = 1 }, "Ctrl+S run"}, {"name", func(m *Model) { m.confirmOpen = true; m.pendingDraft = &editedMsg{} }, "Enter save name"}, {"editor", func(m *Model) { m.editorOpen = true }, "Enter open"}, {"safety", func(m *Model) { m.actionConfirm = &protocol.Message{} }, "y confirm"}, {"status", func(m *Model) { m.statusOpen = true }, "r refresh"}, {"checking", func(m *Model) { m.editorChecking = true }, "draft retained"}, } { t.Run(test.name, func(t *testing.T) { m := New(context.Background(), "") m.width = 100 test.setup(&m) footer := ansi.Strip(m.footerContent()) if !strings.Contains(footer, test.want) || strings.Contains(footer, "/ search") || strings.Contains(footer, "LOGS") { t.Fatal("wrong context footer", footer) } }) } } func TestTerminalErrorsReplaceLoadingAndKeepRecoveryInstructions(t *testing.T) { m := New(context.Background(), "") m.width = 80 m.templateOpen = true m.templateError = "Server connection failed" view := ansi.Strip(m.templatesView(20)) if strings.Contains(view, "Loading") || !strings.Contains(view, "connection failed") { t.Fatal("template error retained loading", view) } m.runOpen = true m.requestedRunID = 8 m.runError = "Run not found" view = ansi.Strip(m.runsDetailsView(20)) if strings.Contains(view, "Loading") || !strings.Contains(view, "Run not found") { t.Fatal("run error retained loading", view) } } func TestNarrowStatusActuallyScrollsToPhoneConnection(t *testing.T) { m := New(context.Background(), "ws://localhost/connect") m.width, m.height = 40, 16 m.statusOpen = true m.statusError = "Connection unavailable" var views []string for i := 0; i < 60; i++ { views = append(views, ansi.Strip(m.statusView(12))) updated, _ := m.updateStatus(keyMessage("j")) m = updated.(Model) } if !strings.Contains(strings.Join(views, "\n"), "ntfy · phone connection") || m.statusTop == 0 { t.Fatal("phone connection remains unreachable") } } func TestNarrowRunShowsActualResultAndEssentialControls(t *testing.T) { m := runInspectionModel() m.width, m.height = 40, 16 m.runStepIndex = 1 m.runDetail.Steps[0].Output = map[string]any{"summary": "## Accomplished\n\nUpdated the layout.", "artifacts": []any{"screen.go"}} view := ansi.Strip(m.View()) if !strings.Contains(view, "Updated the layout.") { t.Fatal("actual result still below first viewport", view) } footer := ansi.Strip(m.footerContent()) for _, hint := range []string{"Tab graph", "n/p visit", "j/k read", "Esc"} { if !strings.Contains(footer, hint) { t.Fatal("essential run control hidden", hint, footer) } } if strings.Contains(footer, "LOGS") { t.Fatal("log badge displaced controls") } } func TestPipelineStageFocusCollapsesUnusableGraphPreview(t *testing.T) { for _, size := range []struct{ width, height int }{{40, 16}, {80, 24}, {120, 36}} { m := panelModel(t) m.width, m.height = size.width, size.height m.stageFocus = true g := m.pipelineGeometry(size.height - 4) if g.graphHeight > 2 { t.Fatal("inactive graph still shows severed box tops", g) } view := ansi.Strip(m.pipelineView(size.height - 4)) if strings.ContainsAny(view, "╭╔│║") { t.Fatal("clipped graph displaced stage document", view) } if !strings.Contains(view, "Output for implementer") { t.Fatal("focused stage output not visible", view) } } } func TestNarrowOverlayFootersAlwaysKeepEscapeVisible(t *testing.T) { for _, setup := range []func(*Model){func(m *Model) { m.paletteOpen = true; m.searchOpen = true }, func(m *Model) { m.paletteOpen = true }, func(m *Model) { m.editorOpen = true }, func(m *Model) { m.versionsOpen = true }, func(m *Model) { m.runPicker = true }, func(m *Model) { m.runForm = 1 }} { m := New(context.Background(), "") m.width = 40 setup(&m) footer := ansi.Strip(m.footerContent()) if !strings.Contains(footer, "Esc") || strings.Contains(footer, "LOGS") { t.Fatal("overlay escape hidden", footer) } } } func TestDraftDeleteUsesWrappedSafetyTextWithoutDeletingWhileReading(t *testing.T) { m := New(context.Background(), "") m.width, m.height = 40, 16 m.draftOpen = true m.keys.drafts = true m.draftDeletingPath = "/Users/reviewer/Library/Caches/pipelines/retained-draft-with-a-long-name.md" m.draftItems = []draft.Draft{{Name: "Layout review", Path: m.draftDeletingPath}} var views []string for i := 0; i < 35; i++ { view := ansi.Strip(m.draftsView(12)) views = append(views, strings.Join(strings.Fields(view), " ")) if !strings.Contains(view, "y delete") || !strings.Contains(view, "Esc keep") { t.Fatal("draft confirmation controls lost", view) } updated, _ := m.updateDrafts(keyMessage("j")) m = updated.(Model) if m.draftDeletingPath == "" { t.Fatal("reading warning deleted/canceled draft") } } all := strings.Join(views, " ") for _, want := range []string{"Layout review", "Only this draft and its metadata are deleted", "Published pipelines/history stay intact", "File:", "name.md"} { if !strings.Contains(all, want) { t.Fatal("draft disclosure unreachable", want, all) } } footer := ansi.Strip(m.footerContent()) if !strings.Contains(footer, "y delete") || strings.Contains(footer, "e edit") { t.Fatal("wrong delete context", footer) } } func TestLongLogDiagnosticsRemainReachableByHorizontalPan(t *testing.T) { m := New(context.Background(), "") m.width, m.height = 40, 16 m.appendLine(strings.Repeat("context ", 30) + "critical failure reason") m.logOpen = true for i := 0; i < 60; i++ { updated, _ := m.updateLogs(keyMessage("right")) m = updated.(Model) } view := ansi.Strip(m.logsView(12)) if m.logPan == 0 || !strings.Contains(view, "critical failure reason") { t.Fatal("long log reason inaccessible", view) } }