@@ -4,41 +4,56 @@ import (
44 "errors"
55 "testing"
66
7- "github.com/ktr0731/go-fuzzyfinder "
7+ "github.com/alvarorichard/Goanime/internal/tui "
88 "github.com/stretchr/testify/assert"
99 "github.com/stretchr/testify/require"
1010)
1111
1212// swapFindMenu replaces the menu finder seam for the duration of the test.
1313// Tests using it mutate a package global, so they must not run in parallel.
14- func swapFindMenu (t * testing.T , fn func (items []menuItem , itemFunc func ( i int ) string , opts ... fuzzyfinder. Option ) (int , error )) {
14+ func swapFindMenu (t * testing.T , fn func (items []menuItem ) (int , error )) {
1515 t .Helper ()
1616 orig := findMenuFunc
1717 findMenuFunc = fn
1818 t .Cleanup (func () { findMenuFunc = orig })
1919}
2020
2121func TestGetUserInput_MenuErrorDoesNotAutoAdvance (t * testing.T ) {
22- // Regression: on menu failure (non-TTY, Esc, broken terminal) GetUserInput
22+ // Regression: on menu failure (non-TTY, broken terminal) GetUserInput
2323 // returned "n", which made HandleSeries/HandleMovie auto-play the next
2424 // episode forever with zero user input. Failure must quit.
25- swapFindMenu (t , func (_ []menuItem , _ func ( i int ) string , _ ... fuzzyfinder. Option ) (int , error ) {
25+ swapFindMenu (t , func (_ []menuItem ) (int , error ) {
2626 return 0 , errors .New ("failed to open TTY" )
2727 })
2828 assert .Equal (t , "q" , GetUserInput (), "menu failure must map to quit, never to next-episode" )
2929 assert .Equal (t , "q" , GetUserInput (true ), "movie menu failure must map to quit, never to replay" )
3030}
3131
32+ func TestGetUserInput_PickBackMapsToBack (t * testing.T ) {
33+ swapFindMenu (t , func (_ []menuItem ) (int , error ) {
34+ return - 1 , tui .ErrPickBack
35+ })
36+ assert .Equal (t , "back" , GetUserInput ())
37+ assert .Equal (t , "back" , GetUserInput (true ))
38+ }
39+
40+ func TestGetUserInput_PickCancelMapsToQuit (t * testing.T ) {
41+ swapFindMenu (t , func (_ []menuItem ) (int , error ) {
42+ return - 1 , tui .ErrPickCancelled
43+ })
44+ assert .Equal (t , "q" , GetUserInput ())
45+ }
46+
3247func TestGetUserInput_SeriesMenuMapping (t * testing.T ) {
3348 wantLabels := []string {"Next episode" , "Previous episode" , "Select episode" , "Change anime" , "← Back" , "Quit" }
3449 wantValues := []string {"n" , "p" , "e" , "c" , "back" , "q" }
3550
3651 for i , wantValue := range wantValues {
3752 var gotLabels []string
38- swapFindMenu (t , func (items []menuItem , itemFunc func ( i int ) string , _ ... fuzzyfinder. Option ) (int , error ) {
53+ swapFindMenu (t , func (items []menuItem ) (int , error ) {
3954 gotLabels = nil
40- for j := range items {
41- gotLabels = append (gotLabels , itemFunc ( j ) )
55+ for _ , it := range items {
56+ gotLabels = append (gotLabels , it . Label )
4257 }
4358 return i , nil
4459 })
@@ -54,10 +69,10 @@ func TestGetUserInput_MovieMenuMapping(t *testing.T) {
5469
5570 for i , wantValue := range wantValues {
5671 var gotLabels []string
57- swapFindMenu (t , func (items []menuItem , itemFunc func ( i int ) string , _ ... fuzzyfinder. Option ) (int , error ) {
72+ swapFindMenu (t , func (items []menuItem ) (int , error ) {
5873 gotLabels = nil
59- for j := range items {
60- gotLabels = append (gotLabels , itemFunc ( j ) )
74+ for _ , it := range items {
75+ gotLabels = append (gotLabels , it . Label )
6176 }
6277 return i , nil
6378 })
@@ -66,3 +81,10 @@ func TestGetUserInput_MovieMenuMapping(t *testing.T) {
6681 assert .Equal (t , wantValue , got , "label %q must map to %q" , wantLabels [i ], wantValue )
6782 }
6883}
84+
85+ func TestGetUserInput_InvalidIndexQuits (t * testing.T ) {
86+ swapFindMenu (t , func (_ []menuItem ) (int , error ) {
87+ return 99 , nil
88+ })
89+ assert .Equal (t , "q" , GetUserInput ())
90+ }
0 commit comments