diff --git a/internal/app/config_test.go b/internal/app/config_test.go index ad0c58ca..a33628dc 100644 --- a/internal/app/config_test.go +++ b/internal/app/config_test.go @@ -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) + } +} diff --git a/internal/app/integration_test.go b/internal/app/integration_test.go index f3c7cb85..11ab1650 100644 --- a/internal/app/integration_test.go +++ b/internal/app/integration_test.go @@ -293,8 +293,9 @@ func TestGiteaMetadataLifecycle(t *testing.T) { require.False(t, notification.Pinned) require.NoError(t, json.Unmarshal(scoped("notification", "mark-unread", notificationID), ¬ification)) require.True(t, notification.Unread) - require.NoError(t, json.Unmarshal(scoped("notification", "watch", "--timeout", "500ms", "--interval", "100ms"), ¬ifications)) - require.NotEmpty(t, notifications) + watched := json.NewDecoder(bytes.NewReader(scoped("notification", "watch", "--timeout", "500ms", "--interval", "100ms"))) + require.NoError(t, watched.Decode(¬ification), "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 diff --git a/internal/app/notifications.go b/internal/app/notifications.go index f22e39d0..9e1422bd 100644 --- a/internal/app/notifications.go +++ b/internal/app/notifications.go @@ -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 }) }