Files
gitea-tea/internal/app/prompts_test.go
T
bircni 1f4b5f868f refactor(prompts): fold Asker into Prompter and share delete confirmations
FormPrompter is the only production prompter and implements the rich
prompt API, so Ask, AskText and ConfirmDefault become the Prompter
interface and the plain Input/Confirm fallbacks go away.

Confirmations that default to no derive their non-terminal wording
from the prompt, and single deletes share deleteConfirmed for the
prompt, the request and the done line.

Signed-off-by: bircni <bircni@icloud.com>
2026-10-12 10:47:54 +02:00

54 lines
1.9 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package app
import (
"testing"
"gitea.dev/tea/internal/clierr"
"gitea.dev/tea/internal/terminal"
"github.com/stretchr/testify/require"
)
func TestConfirmActionFollowsThePromptPolicy(t *testing.T) {
c := terminal.Confirmation{Prompt: `Delete label "bug" from alice/x?`, NonTTY: `delete label "bug" from alice/x`}
deps, _, _ := testDeps(t)
a, cmd := outputApplication(deps, "")
err := a.confirmAction(cmd, c)
require.Equal(t, clierr.Usage, clierr.ExitCode(err))
require.EqualError(t, err, `confirmation required to delete label "bug" from alice/x; rerun with --yes`)
a.options.yes = true
require.NoError(t, a.confirmAction(cmd, c))
prompt := &scriptedPrompter{}
deps.Prompter = prompt
deps.Streams.InTTY, deps.Streams.ErrTTY = true, true
a, cmd = outputApplication(deps, "")
err = a.confirmAction(cmd, c)
require.ErrorIs(t, err, terminal.ErrCanceled)
require.Equal(t, clierr.Canceled, clierr.ExitCode(err))
prompt.accept = true
require.NoError(t, a.confirmAction(cmd, terminal.Confirmation{Prompt: "Create pull request?", DefaultYes: true}))
require.Equal(t, []string{c.Prompt, "Create pull request?"}, prompt.confirmations)
require.Equal(t, []bool{false, true}, prompt.defaults)
}
func TestRequireInputPromptsWithTheLabel(t *testing.T) {
prompt := &scriptedPrompter{input: "bug"}
deps, _, _ := testDeps(t)
deps.Prompter = prompt
a, cmd := outputApplication(deps, "")
name := ""
_, err := a.requireInput(cmd, &name, "name", "Label name", nil)
require.EqualError(t, err, "missing required input: supply --name")
deps.Streams.InTTY, deps.Streams.ErrTTY = true, true
a, cmd = outputApplication(deps, "")
asked, err := a.requireInput(cmd, &name, "name", "Label name", nil)
require.NoError(t, err)
require.True(t, asked)
require.Equal(t, "bug", name)
require.Equal(t, []string{"Label name"}, prompt.asked)
}