test: share the first-page and clock helpers

writeFirstPage answers paged listings in app and services tests,
testNow replaces the copied clock, withoutANSI the second color
stripper, and the single-entry table test becomes a plain test.

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-12 14:25:38 +02:00
1 parent 83924892ff
commit dc24231ebc
23 files changed
+66 -105

No files matched your search

+18 -27
View File
@@ -151,7 +151,7 @@ func TestMultiItemHumanOutputPagesOnce(t *testing.T) {
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v1/repos/alice/project/milestones":
writeMilestones(w, r, `[{"id":3,"title":"v1"}]`)
writeFirstPage(w, r, `[{"id":3,"title":"v1"}]`)
case "/api/v1/repos/alice/project/issues":
_, _ = w.Write([]byte("[" + issue(1) + "," + issue(2) + "]"))
case "/api/v1/repos/alice/project/issues/1", "/api/v1/repos/alice/project/issues/2":
@@ -223,31 +223,22 @@ func TestListsFitTheTerminalAndSplitWhenPiped(t *testing.T) {
`{"number":5,"title":"支持中文路径 (support CJK paths)","state":"open","user":{"login":""}}]`))
}))
defer server.Close()
for group, want := range map[string][2]string{
"issue": {
"6\tA very long issue title that keeps going and going to see how list output handles wrapping\t\topen\tissue\talice\t\n" +
"5\t支持中文路径 (support CJK paths)\t\topen\tissue\t\t\n",
"Showing 2 of 2 open issues in alice/project\n\n" +
"ID TITLE LABELS AUTHOR UPDATED\n" +
"#6 A very long issue ti… alice\n" +
"#5 支持中文路径 (suppor…\n",
},
} {
t.Run(group, func(t *testing.T) {
deps, out, _ := serverDeps(t, server.URL)
args := []string{group, "list", "-R", "alice/project"}
require.NoError(t, Execute(context.Background(), deps, args))
require.Equal(t, want[0], out.String())
deps, out, _ := serverDeps(t, server.URL)
args := []string{"issue", "list", "-R", "alice/project"}
require.NoError(t, Execute(context.Background(), deps, args))
require.Equal(t, "6\tA very long issue title that keeps going and going to see how list output handles wrapping\t\topen\tissue\talice\t\n"+
"5\t支持中文路径 (support CJK paths)\t\topen\tissue\t\t\n", out.String())
out.Reset()
deps.Streams.OutTTY, deps.Streams.Width = true, 50
deps.Getenv = serverEnv(server.URL, map[string]string{"NO_COLOR": "1"})
require.NoError(t, Execute(context.Background(), deps, args))
require.Equal(t, want[1], stripLinks(out.String()))
for _, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
require.LessOrEqual(t, runewidth.StringWidth(stripLinks(line)), 50, line)
}
})
out.Reset()
deps.Streams.OutTTY, deps.Streams.Width = true, 50
deps.Getenv = serverEnv(server.URL, map[string]string{"NO_COLOR": "1"})
require.NoError(t, Execute(context.Background(), deps, args))
require.Equal(t, "Showing 2 of 2 open issues in alice/project\n\n"+
"ID TITLE LABELS AUTHOR UPDATED\n"+
"#6 A very long issue ti… alice\n"+
"#5 支持中文路径 (suppor…\n", stripLinks(out.String()))
for _, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
require.LessOrEqual(t, runewidth.StringWidth(stripLinks(line)), 50, line)
}
}
@@ -265,7 +256,7 @@ func stripLinks(s string) string {
// outputApplication returns an application over deps and a command writing to its streams.
func outputApplication(deps Dependencies, json string) (*application, *cobra.Command) {
deps.Now = func() time.Time { return time.Date(2026, 10, 11, 20, 0, 0, 0, time.UTC) }
deps.Now = func() time.Time { return testNow }
a := &application{deps: deps, options: options{json: json}}
cmd := &cobra.Command{Use: "list"}
cmd.SetOut(deps.Streams.Out)
@@ -335,7 +326,7 @@ func TestWriteView(t *testing.T) {
view := func(item listed) terminal.View {
return terminal.View{
Title: item.Title, ID: fmt.Sprintf("#%d", item.Number), Badges: []terminal.Cell{{Text: "Open", Color: "green"}},
Meta: "alice opened " + terminal.RelativeTime(time.Date(2026, 10, 11, 20, 0, 0, 0, time.UTC), item.Updated), Body: "Steps", Kind: "issue", URL: "https://example.org/1",
Meta: "alice opened " + terminal.RelativeTime(testNow, item.Updated), Body: "Steps", Kind: "issue", URL: "https://example.org/1",
}
}
item := listed{1, "Login fails", time.Date(2026, 10, 11, 18, 0, 0, 0, time.UTC)}