Files
DeepSeek-Reasonix/internal/cli/width_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

64 lines
2.1 KiB
Go

package cli
import (
"strings"
"testing"
)
func TestVisibleWidthGraphemeClusters(t *testing.T) {
cases := []struct {
name string
s string
want int
}{
{"ascii", "abc", 3},
{"cjk", "中文", 4},
{"emoji", "🔥", 2},
// x/ansi counts the VS16 keycap as 2 (emoji presentation). Terminals
// disagree on VS16 width, but the point is consistency: wrapAnsi /
// clampWidth now measure via the same x/ansi, so box rails and wrapping
// agree — which a mixed uniseg(1)/ansi(2) split would break.
{"keycap", "1️⃣", 2},
// The regression that motivated the switch: a ZWJ family is one cluster
// occupying one emoji's width, not the rune-by-rune sum (which was 8).
{"zwj-family", "👨‍👩‍👧‍👦", 2},
{"ansi-stripped", "\x1b[31mab\x1b[0m", 2},
{"mixed", "a中🔥", 5},
}
for _, c := range cases {
if got := visibleWidth(c.s); got != c.want {
t.Errorf("%s: visibleWidth(%q) = %d, want %d", c.name, c.s, got, c.want)
}
}
}
// TestClampWidthHardwrap verifies clampWidth (a wrapper over ansi.Hardwrap) keeps
// every wrapped line within the column budget, hard-breaks CJK at the boundary,
// preserves ANSI escapes as zero width, and leaves in-width lines untouched.
func TestClampWidthHardwrap(t *testing.T) {
// CJK hard-breaks at the column boundary (each is 2 cols); no line over width.
for line := range strings.SplitSeq(clampWidth("中文字", 4), "\n") {
if visibleWidth(line) > 4 {
t.Errorf("cjk line %q exceeds width 4 (got %d)", line, visibleWidth(line))
}
}
// A line already within width is returned byte-for-byte.
if got := clampWidth("ab", 10); got != "ab" {
t.Errorf("in-width line altered: %q", got)
}
// ANSI SGR escapes are zero width, so two visible chars fit a width-2 line.
styled := clampWidth("\x1b[31mab\x1b[0m", 2)
if visibleWidth(styled) > 2 {
t.Errorf("styled line exceeds width 2 (got %d): %q", visibleWidth(styled), styled)
}
// Every wrapped line stays within the column budget.
for line := range strings.SplitSeq(clampWidth(strings.Repeat("中", 10), 6), "\n") {
if visibleWidth(line) > 6 {
t.Errorf("line %q exceeds width 6 (got %d)", line, visibleWidth(line))
}
}
}