Files
DeepSeek-Reasonix/internal/cli/diffview_test.go
T
YHH 4fbbca6a99 refactor(go): adopt modern stdlib idioms and gate them (#7831)
* refactor(go): adopt modern stdlib idioms across kernel and desktop

Mechanical sweep with golangci-lint's modernize/intrange/copyloopvar/
usestdlibvars fixers plus hand-fixes for what they could not rewrite:
slices.Contains, min/max, slices.Backward, WaitGroup.Go, reflect.TypeFor,
maps.Copy, strings.Cut/CutPrefix/SplitSeq, strings.Builder, range-over-int.
Net -549 lines with no behavior change.

Three loops in session.go stay index-based: slices.Backward yields element
copies, so the fixer's rewrite silently dropped writes to DecisionReceipts
and ToolCalls (caught by TestDelivery* and TestDependentSameBatch*). All 49
slices.Backward sites were audited for that shape; only these three write
through the index, and they carry a nolint with the reason.

CaptureAfter's scan loop had an empty body and never ran any logic, so the
fixer could not express it. Drop it and the accessor it was the only caller
of. omitzero is deliberately untouched: turning omitempty into omitzero
changes the JSON written for nested structs, which is a wire-format change
to session files and the extension protocol.

* build(lint): gate the modern-idiom linters

Nothing stopped the codebase from drifting back to pre-1.22 idioms. Both
modules are now clean for modernize, intrange, copyloopvar, and
usestdlibvars, so enable them rather than leaving the sweep as a one-off.

omitzero stays disabled with the reason inline; usetesting, perfsprint, and
errorlint still have residue and land separately.
2026-08-07 15:30:33 +08:00

133 lines
4.1 KiB
Go

package cli
import (
"strings"
"testing"
"github.com/charmbracelet/colorprofile"
"github.com/charmbracelet/x/ansi"
"reasonix/internal/event"
)
func TestDiffBodyDropsHeadersKeepsLineNumbers(t *testing.T) {
d := event.FileDiff{Diff: "--- a/x.go\n+++ b/x.go\n@@ -7 +7 @@\n-old\n+new\n", Added: 1, Removed: 1}
joined := strings.Join(diffBody(d, "x.go", 80, 40), "\n")
if strings.Contains(joined, "--- a/") || strings.Contains(joined, "+++ b/") || strings.Contains(joined, "@@") {
t.Fatalf("file/hunk headers should be dropped, got:\n%s", joined)
}
for _, want := range []string{"old", "new", "7"} {
if !strings.Contains(joined, want) {
t.Fatalf("missing %q (code or line number) in:\n%s", want, joined)
}
}
}
func TestDiffBodyFolds(t *testing.T) {
var b strings.Builder
b.WriteString("--- a/x\n+++ b/x\n@@ -1,8 +1,8 @@\n")
for range 8 {
b.WriteString("+line\n")
}
body := diffBody(event.FileDiff{Diff: b.String()}, "x", 80, 5)
if len(body) != 5 {
t.Fatalf("want 5 rows (4 content + footer), got %d:\n%s", len(body), strings.Join(body, "\n"))
}
// 8 rendered add rows minus the 4 kept = 4 folded.
if !strings.Contains(body[len(body)-1], "4") {
t.Fatalf("footer should report 4 folded lines, got %q", body[len(body)-1])
}
}
func TestDiffBodyNoFoldWhenShort(t *testing.T) {
d := event.FileDiff{Diff: "@@ -1 +1 @@\n+a\n"}
if got := len(diffBody(d, "x", 80, 40)); got != 1 {
t.Fatalf("want 1 unfolded row, got %d", got)
}
}
func TestDiffBlockHeader(t *testing.T) {
d := event.FileDiff{Diff: "@@ -1 +1 @@\n-a\n+b\n", Added: 1, Removed: 1}
block := diffBlock("edit_file", `{"path":"pkg/x.go"}`, d, 80, 40)
if len(block) == 0 || !strings.Contains(block[0], "Update") || !strings.Contains(block[0], "pkg/x.go") {
t.Fatalf("header should name verb + path, got %q", block[0])
}
}
func TestDiffBlockNilWithoutDiff(t *testing.T) {
if diffBlock("write_file", `{"path":"x"}`, event.FileDiff{}, 80, 40) != nil {
t.Fatal("no diff should yield no block")
}
}
func TestDiffPath(t *testing.T) {
if got := diffPath(`{"path":"a/b.go","old_string":"x"}`); got != "a/b.go" {
t.Fatalf("got %q", got)
}
if got := diffPath(`not json`); got != "" {
t.Fatalf("malformed args should yield empty path, got %q", got)
}
}
func TestDiffBarReappliesBackground(t *testing.T) {
defer func(prev colorprofile.Profile) { activeColorProfile = prev }(activeColorProfile)
activeColorProfile = colorprofile.ANSI256
line := diffBar('+', "a + b", "x.go", 40, bgDiffAdd, fgDiffAdd, 12, 3)
// Syntax highlighting emits multiple \033[0m resets; each must re-arm the bar
// background, so the bg sequence appears more than once and the row ends reset.
if strings.Count(line, bgDiffAdd) < 2 {
t.Fatalf("background not re-applied after chroma resets: %q", line)
}
if !strings.HasSuffix(line, ansiReset) {
t.Fatalf("row should end with a reset: %q", line)
}
}
func TestActiveDiffChromaStyleFollowsCLITheme(t *testing.T) {
previous := activeCLITheme
defer func() { activeCLITheme = previous }()
tests := []struct {
name string
theme cliPalette
want string
}{
{name: "dark", theme: cliDarkTheme, want: "github-dark"},
{name: "light", theme: cliLightTheme, want: "github"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
activeCLITheme = tt.theme
if got := activeDiffChromaStyle().Name; got != tt.want {
t.Fatalf("diff syntax style = %q, want %q", got, tt.want)
}
})
}
}
func TestHighlightCodeUpdatesOnThemeSwitch(t *testing.T) {
previousTheme := activeCLITheme
previousProfile := activeColorProfile
defer func() {
activeCLITheme = previousTheme
activeColorProfile = previousProfile
}()
activeColorProfile = colorprofile.ANSI256
code := `const answer = "value"`
activeCLITheme = cliLightTheme
light := highlightCode("example.ts", code)
activeCLITheme = cliDarkTheme
dark := highlightCode("example.ts", code)
if light == dark {
t.Fatalf("light and dark themes produced identical highlighting: %q", light)
}
for name, got := range map[string]string{"light": light, "dark": dark} {
if plain := ansi.Strip(got); plain != code {
t.Fatalf("%s theme changed code text: got %q, want %q", name, plain, code)
}
}
}