1
0
mirror of https://github.com/antonmedv/fx synced 2024-07-08 20:05:46 +00:00
fx/main_test.go

120 lines
2.8 KiB
Go
Raw Normal View History

2023-09-12 11:52:29 +00:00
package main
import (
"bytes"
"io"
"os"
"testing"
"time"
2023-09-15 07:56:11 +00:00
"github.com/charmbracelet/bubbles/textinput"
2023-09-12 11:52:29 +00:00
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/exp/teatest"
"github.com/muesli/termenv"
"github.com/stretchr/testify/require"
2024-03-21 19:48:09 +00:00
"github.com/antonmedv/fx/internal/jsonx"
"github.com/antonmedv/fx/internal/theme"
2023-09-12 11:52:29 +00:00
)
func init() {
2023-09-15 07:56:11 +00:00
lipgloss.SetColorProfile(termenv.ANSI)
2023-09-12 11:52:29 +00:00
}
2023-09-15 07:56:11 +00:00
func prepare(t *testing.T) *teatest.TestModel {
2023-09-12 11:52:29 +00:00
file, err := os.Open("testdata/example.json")
require.NoError(t, err)
json, err := io.ReadAll(file)
require.NoError(t, err)
2024-03-21 19:48:09 +00:00
head, err := jsonx.Parse(json)
2023-09-12 11:52:29 +00:00
require.NoError(t, err)
m := &model{
2023-09-15 07:56:11 +00:00
top: head,
head: head,
wrap: true,
showCursor: true,
digInput: textinput.New(),
searchInput: textinput.New(),
search: newSearch(),
2023-09-12 11:52:29 +00:00
}
tm := teatest.NewTestModel(
t, m,
2023-09-15 07:56:11 +00:00
teatest.WithInitialTermSize(80, 40),
2023-09-12 11:52:29 +00:00
)
2023-09-15 07:56:11 +00:00
return tm
}
2023-09-12 11:52:29 +00:00
2023-09-15 07:56:11 +00:00
func read(t *testing.T, tm *teatest.TestModel) []byte {
var out []byte
2023-09-12 11:52:29 +00:00
teatest.WaitFor(t,
tm.Output(),
func(b []byte) bool {
2023-09-15 07:56:11 +00:00
out = b
return bytes.Contains(b, []byte("{"))
2023-09-12 11:52:29 +00:00
},
teatest.WithCheckInterval(time.Millisecond*100),
teatest.WithDuration(time.Second),
)
2023-09-15 07:56:11 +00:00
return out
}
func TestOutput(t *testing.T) {
tm := prepare(t)
teatest.RequireEqualOutput(t, read(t, tm))
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
func TestNavigation(t *testing.T) {
tm := prepare(t)
tm.Send(tea.KeyMsg{Type: tea.KeyDown})
tm.Send(tea.KeyMsg{Type: tea.KeyDown})
tm.Send(tea.KeyMsg{Type: tea.KeyDown})
teatest.RequireEqualOutput(t, read(t, tm))
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
func TestDig(t *testing.T) {
tm := prepare(t)
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(".")})
2023-09-19 11:45:25 +00:00
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("year")})
2023-09-15 07:56:11 +00:00
tm.Send(tea.KeyMsg{Type: tea.KeyEnter})
teatest.RequireEqualOutput(t, read(t, tm))
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
func TestCollapseRecursive(t *testing.T) {
tm := prepare(t)
tm.Send(tea.KeyMsg{Type: tea.KeyShiftLeft})
teatest.RequireEqualOutput(t, read(t, tm))
2023-09-12 11:52:29 +00:00
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
func TestCollapseRecursiveWithSizes(t *testing.T) {
2024-03-21 19:48:09 +00:00
theme.ShowSizes = true
defer func() { theme.ShowSizes = true }()
tm := prepare(t)
tm.Send(tea.KeyMsg{Type: tea.KeyShiftLeft})
teatest.RequireEqualOutput(t, read(t, tm))
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}