feat(cli): add internal/{text,compat} packages and acceptance scaffold (PR-6)

text/      gh-style display-width string helpers (Truncate / FuzzyAgo / Pluralize)
compat/    server-CLI version skew probe (kubectl) + 24h TTL cache (gh internal/update)
acceptance/contract/  helpers_test.go for in-process cobra exec + golden assert
                      (uses iostreams.SetForTest singleton replacement; no t.Parallel)
cmdutil/errors.go: AllCodes() + ClassifyHTTPErrorOutputs() bridge for the
                   contract test suite (no behavior change in error handling)

go-runewidth promoted from indirect to direct (used by text.Truncate for
display-width-aware CJK truncation).

acceptance/contract/helpers_test.go forward-references cmd.NewRootCmd which
is exported in PR-7 — that file does not compile until then by design.
This commit is contained in:
nullkey
2026-05-08 15:44:32 +08:00
committed by lyingbug
parent b7388f10e1
commit 92d78dea72
17 changed files with 733 additions and 1 deletions
+16
View File
@@ -0,0 +1,16 @@
// Package text holds gh-style string helpers for human-readable CLI output.
// All functions are pure (no I/O, no time.Now), making them trivially testable.
package text
import "fmt"
// Pluralize returns "<n> <thing>" or "<n> <thing>s". Mirrors gh
// internal/text/text.go Pluralize signature — simple suffix "s" only;
// irregular forms (person/people) are not supported (add PluralizeIrregular
// when needed, per spec §2.1).
func Pluralize(n int, thing string) string {
if n == 1 {
return fmt.Sprintf("%d %s", n, thing)
}
return fmt.Sprintf("%d %ss", n, thing)
}
+27
View File
@@ -0,0 +1,27 @@
package text_test
import (
"testing"
"github.com/Tencent/WeKnora/cli/internal/text"
)
func TestPluralize(t *testing.T) {
tests := []struct {
n int
thing string
want string
}{
{0, "doc", "0 docs"},
{1, "doc", "1 doc"},
{2, "doc", "2 docs"},
{5, "chunk", "5 chunks"},
{1, "chunk", "1 chunk"},
}
for _, tt := range tests {
got := text.Pluralize(tt.n, tt.thing)
if got != tt.want {
t.Errorf("Pluralize(%d, %q) = %q, want %q", tt.n, tt.thing, got, tt.want)
}
}
}
+39
View File
@@ -0,0 +1,39 @@
package text
import (
"fmt"
"math"
"time"
)
// FuzzyAgo formats t relative to now in the gh house style.
// Mirrors github.com/cli/cli/internal/text/datetime.go FuzzyAgo.
//
// Examples:
//
// FuzzyAgo(now, now-5m) → "about 5 minutes ago"
// FuzzyAgo(now, now-3h) → "about 3 hours ago"
// FuzzyAgo(now, now-2d) → "about 2 days ago"
// FuzzyAgo(now, now+5m) → "about 5 minutes from now"
func FuzzyAgo(now, t time.Time) string {
d := now.Sub(t)
suffix := "ago"
if d < 0 {
d = -d
suffix = "from now"
}
switch {
case d < time.Minute:
return "less than a minute " + suffix
case d < time.Hour:
return fmt.Sprintf("about %s %s", Pluralize(int(math.Round(d.Minutes())), "minute"), suffix)
case d < 24*time.Hour:
return fmt.Sprintf("about %s %s", Pluralize(int(math.Round(d.Hours())), "hour"), suffix)
case d < 30*24*time.Hour:
return fmt.Sprintf("about %s %s", Pluralize(int(math.Round(d.Hours()/24)), "day"), suffix)
case d < 365*24*time.Hour:
return fmt.Sprintf("about %s %s", Pluralize(int(math.Round(d.Hours()/(24*30))), "month"), suffix)
default:
return fmt.Sprintf("about %s %s", Pluralize(int(math.Round(d.Hours()/(24*365))), "year"), suffix)
}
}
+31
View File
@@ -0,0 +1,31 @@
package text_test
import (
"testing"
"time"
"github.com/Tencent/WeKnora/cli/internal/text"
)
func TestFuzzyAgo(t *testing.T) {
now := time.Date(2026, 5, 8, 12, 0, 0, 0, time.UTC)
tests := []struct {
name string
t time.Time
want string
}{
{"5 minutes ago", now.Add(-5 * time.Minute), "about 5 minutes ago"},
{"1 hour ago", now.Add(-1 * time.Hour), "about 1 hour ago"},
{"3 hours ago", now.Add(-3 * time.Hour), "about 3 hours ago"},
{"2 days ago", now.Add(-2 * 24 * time.Hour), "about 2 days ago"},
{"30 days ago", now.Add(-30 * 24 * time.Hour), "about 1 month ago"},
{"5 minutes from now", now.Add(5 * time.Minute), "about 5 minutes from now"},
{"less than 1m ago", now.Add(-30 * time.Second), "less than a minute ago"},
}
for _, tt := range tests {
got := text.FuzzyAgo(now, tt.t)
if got != tt.want {
t.Errorf("FuzzyAgo(%s) = %q, want %q", tt.name, got, tt.want)
}
}
}
+36
View File
@@ -0,0 +1,36 @@
package text
import "github.com/mattn/go-runewidth"
const ellipsis = "…"
// Truncate cuts s to at most maxWidth display columns (CJK / emoji 占 2 列;
// ASCII 占 1 列), appending "…" if truncated. Mirrors gh
// internal/text/text.go Truncate semantics — display width, not rune count.
//
// Edge cases:
//
// maxWidth ≤ 0 → ""
// s 已 ≤ maxWidth → s 原样返回
// ellipsis 占 1 列, 所以 truncate 时 budget = maxWidth - 1
func Truncate(maxWidth int, s string) string {
if maxWidth <= 0 {
return ""
}
if runewidth.StringWidth(s) <= maxWidth {
return s
}
if maxWidth == 1 {
return ellipsis
}
budget := maxWidth - 1 // reserve for ellipsis
w := 0
for i, r := range s {
rw := runewidth.RuneWidth(r)
if w+rw > budget {
return s[:i] + ellipsis
}
w += rw
}
return s + ellipsis
}
+31
View File
@@ -0,0 +1,31 @@
package text_test
import (
"testing"
"github.com/Tencent/WeKnora/cli/internal/text"
)
func TestTruncate(t *testing.T) {
tests := []struct {
name string
maxWidth int
s string
want string
}{
{"no truncate ASCII", 20, "hello", "hello"},
{"truncate ASCII", 5, "hello world", "hell…"},
{"no truncate CJK", 8, "中文测试", "中文测试"},
{"truncate CJK", 5, "中文测试", "中文…"},
{"empty", 5, "", ""},
{"single char fits", 1, "a", "a"},
{"truncate to width 1", 1, "abc", "…"},
{"maxWidth zero", 0, "x", ""},
}
for _, tt := range tests {
got := text.Truncate(tt.maxWidth, tt.s)
if got != tt.want {
t.Errorf("Truncate(%d, %q) = %q, want %q", tt.maxWidth, tt.s, got, tt.want)
}
}
}