gitFlow/pkg/status/status_test.go
dimitar c9ff44298c feat: phase 3 — presentation layer (table, JSON, compact)
Replace the interim renderers with a dedicated presenter package that
formats scan results in three ways.

Presenter (internal/presenter):
- Formatter interface with Present()/For() dispatch on format name:
  table, json, compact
- TableFormatter: aligned tabwriter table with REPOSITORY / BRANCH /
  STATUS / AHEAD-BEHIND / CHANGES / STASH columns, per-repo change
  summaries like "2M 1U", error details inline, and a colored summary
  line ("N repos | N clean | N need attention | N errors")
- JSONFormatter: indented document with scanned_at, parent_dir, repos,
  and an aggregate summary for scripting; statuses render as string
  labels ("modified") instead of raw integers
- CompactFormatter: one line per repo with color-coded status symbols
  (✓ ✗ ↑ ↓ ⇄ ◉ ▢ !) plus branch, ahead/behind, and change counts
- Color handling: auto/always/never modes, TTY detection, and the
  NO_COLOR convention (explicit --color=always still wins); paths have
  $HOME collapsed to "~" in table and compact views

Domain model (pkg/status):
- JSON tags on RepoInfo/ScanResult/Summary for clean field names
- RepoStatus now marshals to its string label and unmarshals from both
  string labels and numeric values, so JSON output round-trips

Configuration (internal/config):
- New --color flag (auto/always/never) validated in Load and included in
  the config dump; keyed as "color" in viper

CLI (cmd/gitflow):
- scan now routes through presenter.Present with the resolved color mode;
  the interim renderers are removed

Testing:
- Table content (columns, change summaries, error text, summary line) and
  absence of escape codes with ColorNever
- ColorAlways emits ANSI codes even under NO_COLOR; auto stays clean on
  non-terminal writers
- JSON decodes back into the domain types (string statuses round-trip)
- Compact symbols and counts; unknown formats rejected
- RepoStatus JSON round trip covers every status

Verified: go build, go vet, go test -race, gofmt clean; manual smoke of
table / compact / forced-color / JSON output against a scratch directory.
2026-08-02 08:41:18 +02:00

121 lines
2.8 KiB
Go

package status
import (
"encoding/json"
"testing"
"time"
)
func TestRepoStatusString(t *testing.T) {
cases := []struct {
s RepoStatus
want string
}{
{StatusUnknown, "unknown"},
{StatusClean, "clean"},
{StatusModified, "modified"},
{StatusAhead, "ahead"},
{StatusBehind, "behind"},
{StatusDiverged, "diverged"},
{StatusDetached, "detached"},
{StatusBare, "bare"},
{StatusError, "error"},
}
for _, tc := range cases {
if got := tc.s.String(); got != tc.want {
t.Errorf("RepoStatus(%d).String() = %q, want %q", tc.s, got, tc.want)
}
}
}
func TestRepoStatusNeedsAttention(t *testing.T) {
cases := []struct {
s RepoStatus
want bool
}{
{StatusUnknown, false},
{StatusClean, false},
{StatusModified, true},
{StatusAhead, true},
{StatusBehind, true},
{StatusDiverged, true},
{StatusDetached, true},
{StatusBare, true},
{StatusError, true},
}
for _, tc := range cases {
if got := tc.s.NeedsAttention(); got != tc.want {
t.Errorf("RepoStatus(%d).NeedsAttention() = %v, want %v", tc.s, got, tc.want)
}
}
}
func TestRepoInfoFileCount(t *testing.T) {
info := RepoInfo{
StagedFiles: []string{"a", "b"},
ModifiedFiles: []string{"c"},
UntrackedFiles: []string{"d", "e", "f"},
}
if got := info.FileCount(); got != 6 {
t.Errorf("FileCount() = %d, want 6", got)
}
}
func TestRepoStatusJSONRoundTrip(t *testing.T) {
all := []RepoStatus{
StatusUnknown, StatusClean, StatusModified, StatusAhead, StatusBehind,
StatusDiverged, StatusDetached, StatusBare, StatusError,
}
for _, s := range all {
b, err := json.Marshal(s)
if err != nil {
t.Fatalf("Marshal(%d): %v", s, err)
}
var got RepoStatus
if err := json.Unmarshal(b, &got); err != nil {
t.Fatalf("Unmarshal(%s): %v", b, err)
}
if got != s {
t.Errorf("round trip of %s = %v", s, got)
}
}
}
func TestScanResultSummary(t *testing.T) {
result := ScanResult{
ScannedAt: time.Now(),
ParentDir: "/tmp",
Repos: []RepoInfo{
{Status: StatusClean},
{Status: StatusModified, StagedFiles: []string{"a"}, ModifiedFiles: []string{"b"}},
{Status: StatusDiverged, UntrackedFiles: []string{"c"}},
{Status: StatusError, Error: "boom"},
{Status: StatusUnknown},
},
}
got := result.Summary()
want := Summary{Total: 5, Clean: 1, Attention: 2, Errored: 1, Staged: 1, Modified: 1, Untracked: 1}
if got != want {
t.Errorf("Summary() = %+v, want %+v", got, want)
}
}
func TestScanResultNeedsAttention(t *testing.T) {
result := ScanResult{
Repos: []RepoInfo{
{Path: "/a", Status: StatusClean},
{Path: "/b", Status: StatusBehind},
{Path: "/c", Status: StatusError},
},
}
got := result.NeedsAttention()
if len(got) != 2 {
t.Fatalf("NeedsAttention() returned %d repos, want 2", len(got))
}
for _, r := range got {
if r.Path == "/a" {
t.Errorf("clean repo /a listed as needing attention")
}
}
}