package tui import ( "context" "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" "github.com/muesli/termenv" "os" "path/filepath" "strings" "testing" "time" ) func TestSoftSurfacesAdaptAndKeepCharacterChromeMinimal(t *testing.T) { profile, dark := lipgloss.ColorProfile(), lipgloss.HasDarkBackground() lipgloss.SetColorProfile(termenv.TrueColor) defer func() { lipgloss.SetColorProfile(profile); lipgloss.SetHasDarkBackground(dark) }() var views []string for _, dark := range []bool{true, false} { lipgloss.SetHasDarkBackground(dark) m := New(context.Background(), "ws://localhost/connect") m.width = 120 m.height = 30 m.loaded = true view := m.View() views = append(views, view) if !strings.Contains(view, "48;2;") { t.Fatal("soft surfaces missing") } for _, s := range []string{view, popup(120, 26, 76, []string{"New pipeline with…", "nvim", "OpenCode"})} { plain := ansi.Strip(s) if strings.ContainsAny(plain, "╭╮╰╯│─") { t.Fatal("heavy character chrome remains", plain) } } for _, s := range []string{bar("Title", "3", 40, true), menuOption("OpenCode", true), action("edit", true), stageTab("planner", "planner", true)} { if strings.Contains(s, "[7m") || strings.Contains(s, ";7m") { t.Fatal("neon reverse-video selection remains", s) } } for _, line := range strings.Split(ansi.Strip(view), "\n") { if lipgloss.Width(line) > 120 { t.Fatal("surfaces changed layout width") } } } if views[0] == views[1] { t.Fatal("light and dark surfaces are identical") } } func TestLoadingUsesLocalAnimationAcrossPendingViews(t *testing.T) { m := New(context.Background(), "") m.detailLoading = true before := m.loading("Loading pipeline…") updated, cmd := m.Update(m.runSpinner.Tick()) m = updated.(Model) if cmd == nil || before == m.loading("Loading pipeline…") || m.nextID != 0 || m.statusRequest != "" { t.Fatal("loading did not animate locally") } for _, view := range []string{m.pipelineView(20), m.templatesView(20), strings.Join(m.usageLines(), "\n")} { if !strings.Contains(view, m.runSpinner.View()) { t.Fatal("pending view lacks loading indicator", view) } } } func TestNestedLabelsDoNotPunchHolesInPanelBackground(t *testing.T) { profile := lipgloss.ColorProfile() lipgloss.SetColorProfile(termenv.ANSI) defer lipgloss.SetColorProfile(profile) nested := lipgloss.NewStyle().Bold(true).Foreground(themeAccent).Render("name") + " tail" view := panel(60, 8, []string{bar("Title", "1", 56, true)}, []string{nested}, "hint", true) want := backgroundAt(t, lipgloss.NewStyle().Background(themeSurface).Render("surface"), "surface") if want < 0 || backgroundAt(t, view, "tail") != want || backgroundAt(t, view, "hint") != want { t.Fatal("nested style resets leave holes in soft panel") } } // Optional actual-render artifacts for visual review, without a live server. func TestSoftThemeRenderPreviews(t *testing.T) { dir := os.Getenv("PIPELINES_TUI_PREVIEW_DIR") if dir == "" { t.Skip("set PIPELINES_TUI_PREVIEW_DIR to export actual TUI renders") } if err := os.MkdirAll(dir, 0700); err != nil { t.Fatal(err) } profile, dark := lipgloss.ColorProfile(), lipgloss.HasDarkBackground() lipgloss.SetColorProfile(termenv.TrueColor) defer func() { lipgloss.SetColorProfile(profile); lipgloss.SetHasDarkBackground(dark) }() for _, theme := range []string{"dark", "light"} { lipgloss.SetHasDarkBackground(theme == "dark") m := New(context.Background(), "ws://localhost:8080/connect") m.width = 124 m.height = 34 m.loaded = true m.status = "localhost:8080" stamp := time.Now().UTC() m.pipelines = []protocol.PipelineSummary{{ID: 1, Name: "Repository maintenance", Version: 3, Stages: 4, CreatedAt: stamp.Add(-48 * time.Hour).Format(time.RFC3339), UpdatedAt: stamp.Format(time.RFC3339)}, {ID: 2, Name: "Release notes", Version: 1, Stages: 2, CreatedAt: stamp.Add(-24 * time.Hour).Format(time.RFC3339), UpdatedAt: stamp.Format(time.RFC3339)}} m.runs = []protocol.RunSummary{{ID: 18, Name: "Repository maintenance", Version: 3, Status: "running", CurrentStage: "reviewer", CreatedAt: stamp.Add(-47 * time.Second).Format(time.RFC3339)}, {ID: 17, Name: "Release notes", Version: 1, Status: "succeeded", CreatedAt: stamp.Add(-time.Hour).Format(time.RFC3339), FinishedAt: stamp.Add(-time.Hour + 82*time.Second).Format(time.RFC3339)}} m.usage = &protocol.UsageStats{RunCounts: protocol.RunCounts{Total: 18, Succeeded: 14, Failed: 2, Canceled: 1, Active: 1, AverageSeconds: 82}} for i, n := range []int{0, 1, 0, 2, 1, 0, 0, 3, 0, 1, 2, 1, 2, 2} { m.usage.Days = append(m.usage.Days, protocol.DailyUsage{Date: stamp.AddDate(0, 0, i-13).Format("2006-01-02"), Runs: n}) } views := map[string]string{"home": m.View()} m.editorOpen = true m.editorDraft = &editedMsg{createOnly: true} m.editorInput = newInput() views["new"] = m.View() p := panelModel(t) p.width = 124 p.height = 34 views["pipeline"] = p.View() for name, view := range views { if err := os.WriteFile(filepath.Join(dir, name+"-"+theme+".ansi"), []byte(view), 0600); err != nil { t.Fatal(err) } } } }