test(notification): complete platform values and read watch output as NDJSON

A table test covers the completions of alias, config, auth and notification commands, including notification IDs from the inbox. The integration test reads tea notification watch --json as one object per line, and notification feedback, watch lines and completions name a subject the same way.

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-11 23:50:46 +02:00
1 parent 6c124fc998
commit 9e74ec54ad
3 files changed
+45 -11

No files matched your search

+32
View File
@@ -5,6 +5,7 @@ package app
import (
"context"
"net/http"
"os"
"path/filepath"
"strings"
@@ -13,6 +14,7 @@ import (
"gitea.dev/tea/internal/clierr"
"gitea.dev/tea/internal/settings"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)
@@ -263,3 +265,33 @@ func TestAliasDeleteAndList(t *testing.T) {
require.NoError(t, err)
require.Empty(t, snapshot.Preferences.Aliases)
}
func TestPlatformCommandsComplete(t *testing.T) {
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "/api/v1/notifications", r.URL.Path)
_, _ = w.Write([]byte(notificationThreads))
}))
defer server.Close()
deps, out, _ := testDeps(t)
configureAccounts(t, deps, server.URL, "alice", "alice", "bob")
require.NoError(t, Execute(context.Background(), deps, []string{"alias", "set", "il", "issue list"}))
for _, tc := range []struct {
args []string
want []string
}{
{[]string{"alias", "delete", ""}, []string{"il\tissue list"}},
{[]string{"config", "get", "hyp"}, []string{"hyperlinks"}},
{[]string{"config", "set", "hyperlinks", ""}, []string{"auto", "always", "never"}},
{[]string{"config", "set", "markdown", ""}, []string{"auto", "raw"}},
{[]string{"auth", "switch", ""}, []string{"alice\t" + server.URL + " (active)", "bob\t" + server.URL}},
{[]string{"auth", "logout", "b"}, []string{"bob\t" + server.URL}},
{[]string{"auth", "login", "--credential-store", ""}, []string{"keyring", "file"}},
{[]string{"auth", "login", "--scopes", "read:repository,write:re"}, []string{"read:repository,write:repository"}},
{[]string{"notification", "list", "--type", ""}, []string{"issue", "pull", "commit", "repository"}},
{[]string{"notification", "view", ""}, []string{"12\talice/project#17 Improve greeting", "3\talice/project#8 Improve error messages"}},
} {
candidates, directive := complete(t, deps, out, tc.args...)
require.Equal(t, tc.want, candidates, tc.args)
require.NotZero(t, directive&cobra.ShellCompDirectiveNoFileComp, tc.args)
}
}
+3 -2
View File
@@ -293,8 +293,9 @@ func TestGiteaMetadataLifecycle(t *testing.T) {
require.False(t, notification.Pinned)
require.NoError(t, json.Unmarshal(scoped("notification", "mark-unread", notificationID), &notification))
require.True(t, notification.Unread)
require.NoError(t, json.Unmarshal(scoped("notification", "watch", "--timeout", "500ms", "--interval", "100ms"), &notifications))
require.NotEmpty(t, notifications)
watched := json.NewDecoder(bytes.NewReader(scoped("notification", "watch", "--timeout", "500ms", "--interval", "100ms")))
require.NoError(t, watched.Decode(&notification), "watch prints one object per line")
require.Equal(t, repo.FullName, notification.Repository)
scoped("notification", "mark-read", "--all")
require.Equal(t, "[]\n", string(scoped("notification", "list")))
var release services.Release
+10 -9
View File
@@ -356,11 +356,15 @@ func notificationStatus(item services.Notification) string {
// notificationLabel names a notification in feedback: "notification 28 (alice/project#17)".
func notificationLabel(item services.Notification) string {
subject := item.Repository
if item.Number != 0 {
subject += "#" + strconv.FormatInt(item.Number, 10)
return fmt.Sprintf("notification %d (%s)", item.ID, subjectRef(item))
}
// subjectRef names the subject of a notification by its repository and number: "alice/project#17".
func subjectRef(item services.Notification) string {
if item.Number == 0 {
return item.Repository
}
return fmt.Sprintf("notification %d (%s)", item.ID, strings.TrimSpace(subject))
return item.Repository + "#" + strconv.FormatInt(item.Number, 10)
}
// notificationKinds names the subject types in a view footer.
@@ -399,10 +403,7 @@ func watchLine(item services.Notification, d terminal.Display) string {
if when.IsZero() {
when = d.Now
}
subject := item.Repository
if item.Number != 0 {
subject += "#" + strconv.FormatInt(item.Number, 10)
}
subject := subjectRef(item)
details := strings.ToLower(item.Type)
if item.State != "" {
details += " · " + terminal.Colorize(stateColors[item.State], item.State, d.Color)
@@ -415,7 +416,7 @@ func (a *application) completeNotifications() cobra.CompletionFunc {
return a.apiCompletion(false, func(ctx context.Context, t target, _ string) ([]cobra.Completion, error) {
page, err := services.Notifications{Client: t.client, Repository: t.Repo}.List(ctx, services.NotificationOptions{Limit: completionLimit, Page: 1, Statuses: []string{"unread", "pinned", "read"}})
return describe(page.Items, func(item services.Notification) (string, string) {
return strconv.FormatInt(item.ID, 10), strings.TrimPrefix(notificationLabel(item)+" "+item.Title, "notification "+strconv.FormatInt(item.ID, 10)+" ")
return strconv.FormatInt(item.ID, 10), subjectRef(item) + " " + item.Title
}), err
})
}