gitFlow/internal/rules/rules_test.go
dimitar 012fbf8ac5 feat: phase 6 — polish: completions, notifications, themes, rules, docs
Close out the plan with the remaining polish items and a full README.

Shell completions (cmd/gitflow):
- New `completion [bash|zsh|fish|powershell]` command backed by cobra's
  generators, wired into the root command

Desktop notifications (internal/notify):
- Send() dispatches to notify-send (Linux) with an osascript fallback
  (macOS); Windows is a documented no-op for now; missing notifiers are
  silent, never errors
- watch --notify sends a notification listing repositories whose state
  changed since the previous frame

Color themes (internal/presenter):
- ThemeMode (dark/light) with ParseTheme; light uses bright ANSI variants
  (90-97) that stay legible on light backgrounds; threaded through the
  table, compact, and suggestions renderers; new --theme flag validated
  and dumped by config

Custom rules (internal/rules):
- Rule{name, field (ahead|behind|stash|changes), op (==,!=,<,<=,>,>=),
  value, label} with upfront validation in both Validate and Eval
- Config gains a `rules:` section (yaml/env only, no flag), validated at
  load; matches render as a "FLAGS (custom rules)" section via a new
  presenter.Flags renderer, shown in scan and watch frames

Docs:
- readme.md fully rewritten: features, install, usage, examples, flag
  table, status classes, configuration reference, AI agent behavior and
  --ai-execute guardrails, development layout, CI, and future work
- implementation.md gains an Implementation Progress section recording
  every phase branch and the deviations from the original plan

Multi-platform:
- Verified cross-compilation for windows/amd64 and darwin/arm64; the
  notify package is split behind build tags

Testing:
- rules: validation, operator semantics, Eval ordering, invalid-rule
  errors
- presenter: ParseTheme, light-theme bright codes (and absence of
  dark-theme codes), Flags rendering (empty = silent, matches render)
- config: rules loading from file, invalid-rule rejection, bad theme and
  bad provider rejection
- notify: no-op behaviour when no notifier is installed (skipped when one
  is, to avoid firing real notifications)

Verified: go build, go vet, go test -race (10 packages), gofmt clean,
windows/darwin cross-compile, completion generation, rules + light theme
smoke test, watch --notify graceful shutdown (exit 0, no orphans).
2026-08-02 08:52:50 +02:00

85 lines
2.4 KiB
Go

package rules
import (
"testing"
"gitea.oblak.solutions/dimitar/gitFlow/pkg/status"
)
func TestRuleValidate(t *testing.T) {
good := Rule{Name: "stale", Field: "behind", Op: ">=", Value: 10, Label: "stale"}
if err := good.Validate(); err != nil {
t.Errorf("Validate(good): %v", err)
}
bad := []Rule{
{Name: "a", Field: "nope", Op: ">=", Label: "x"},
{Name: "b", Field: "behind", Op: "~", Label: "x"},
{Name: "c", Field: "behind", Op: ">=", Label: ""},
}
for _, r := range bad {
if err := r.Validate(); err == nil {
t.Errorf("Validate(%+v) succeeded, want error", r)
}
}
}
func TestRuleMatches(t *testing.T) {
repo := status.RepoInfo{BehindBy: 12, StashCount: 1, ModifiedFiles: []string{"a"}}
cases := []struct {
rule Rule
want bool
}{
{Rule{Field: "behind", Op: ">=", Value: 10}, true},
{Rule{Field: "behind", Op: ">=", Value: 13}, false},
{Rule{Field: "behind", Op: "==", Value: 12}, true},
{Rule{Field: "stash", Op: ">", Value: 0}, true},
{Rule{Field: "stash", Op: "==", Value: 0}, false},
{Rule{Field: "changes", Op: ">=", Value: 1}, true},
{Rule{Field: "ahead", Op: "==", Value: 0}, true},
{Rule{Field: "changes", Op: "!=", Value: 3}, true},
}
for _, tc := range cases {
got, err := tc.rule.Matches(repo)
if err != nil {
t.Errorf("Matches(%+v): %v", tc.rule, err)
continue
}
if got != tc.want {
t.Errorf("Matches(%+v) = %v, want %v", tc.rule, got, tc.want)
}
}
}
func TestEval(t *testing.T) {
rules := []Rule{
{Name: "stale", Field: "behind", Op: ">=", Value: 10, Label: "stale"},
{Name: "dirty", Field: "changes", Op: ">=", Value: 1, Label: "dirty"},
}
result := status.ScanResult{Repos: []status.RepoInfo{
{Path: "/a", BehindBy: 15},
{Path: "/b", ModifiedFiles: []string{"x"}},
{Path: "/c"},
}}
flags, err := Eval(rules, result)
if err != nil {
t.Fatalf("Eval: %v", err)
}
want := []Flag{{RepoPath: "/a", Label: "stale"}, {RepoPath: "/b", Label: "dirty"}}
if len(flags) != len(want) {
t.Fatalf("Eval returned %d flags, want %d: %+v", len(flags), len(want), flags)
}
for i, f := range flags {
if f != want[i] {
t.Errorf("flag[%d] = %+v, want %+v", i, f, want[i])
}
}
}
func TestEvalInvalidRule(t *testing.T) {
rules := []Rule{{Name: "bad", Field: "nope", Op: ">=", Label: "x"}}
if _, err := Eval(rules, status.ScanResult{}); err == nil {
t.Error("Eval(invalid rule) succeeded, want error")
}
}