|
|
@@ -0,0 +1,91 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "pingo"
|
|
|
+ "strings"
|
|
|
+ "testing"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ tea "charm.land/bubbletea/v2"
|
|
|
+ "charm.land/lipgloss/v2"
|
|
|
+ "github.com/stretchr/testify/assert"
|
|
|
+)
|
|
|
+
|
|
|
+func spawnMD(hosts []string, width, height, chartHeight int) md {
|
|
|
+ return md{p: pingo.InitialModel(hosts, width, height, chartHeight)}
|
|
|
+}
|
|
|
+
|
|
|
+func Test_md_Init(t *testing.T) {
|
|
|
+ testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
|
|
|
+ cmd := testMD.Init()
|
|
|
+ assert.IsType(t, *new(timingMsg), cmd())
|
|
|
+}
|
|
|
+
|
|
|
+func Test_md_Update_handle_windowsize(t *testing.T) {
|
|
|
+ testMsg := tea.WindowSizeMsg{Width: 80, Height: 80}
|
|
|
+ testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
|
|
|
+
|
|
|
+ assert.Equal(t, 40, testMD.p.Width)
|
|
|
+ assert.Equal(t, 20, testMD.p.Height)
|
|
|
+ assert.Equal(t, 0, testMD.viewport.Width())
|
|
|
+ assert.Equal(t, 0, testMD.viewport.Height())
|
|
|
+
|
|
|
+ model, cmd := testMD.Update(testMsg)
|
|
|
+ assert.Nil(t, cmd)
|
|
|
+ assert.Equal(t, 80, model.(md).p.Width)
|
|
|
+ assert.Equal(t, 80-2, model.(md).p.Height)
|
|
|
+}
|
|
|
+
|
|
|
+func Test_md_Update_handle_keymsg(t *testing.T) {
|
|
|
+ testKeyMsg := tea.KeyPressMsg(tea.Key{Text: "ctrl+c"})
|
|
|
+ // getTestKey := func() tea.Msg { return testKey }
|
|
|
+ testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
|
|
|
+
|
|
|
+ var cmd tea.Cmd
|
|
|
+ _, cmd = testMD.Update(testKeyMsg)
|
|
|
+ assert.NotNil(t, cmd)
|
|
|
+ assert.IsType(t, *new(tea.QuitMsg), cmd())
|
|
|
+}
|
|
|
+func Test_md_Update_handle_timing(t *testing.T) {
|
|
|
+ testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
|
|
|
+ _, cmd := testMD.Update(tea.Msg(timingMsg{}))
|
|
|
+ assert.NotNil(t, cmd)
|
|
|
+ assert.IsType(t, *new(tea.BatchMsg), cmd())
|
|
|
+
|
|
|
+ batch := []tea.Cmd(cmd().(tea.BatchMsg))
|
|
|
+ assert.True(t, len(batch) == 2)
|
|
|
+ for _, cmd := range batch {
|
|
|
+ if value, ok := cmd().(timingMsg); ok {
|
|
|
+ assert.WithinDuration(t, time.Now(), time.Time(value), time.Second*1)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func Test_md_View(t *testing.T) {
|
|
|
+ testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
|
|
|
+ view := testMD.View()
|
|
|
+
|
|
|
+ assert.True(t, strings.Contains(view.Content, testMD.header()))
|
|
|
+ assert.True(t, strings.Contains(view.Content, testMD.footer()))
|
|
|
+ assert.True(t, strings.Contains(view.Content, testMD.viewport.GetContent()))
|
|
|
+}
|
|
|
+
|
|
|
+func Test_md_header(t *testing.T) {
|
|
|
+ testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
|
|
|
+ testData := testMD.header()
|
|
|
+ assert.IsType(t, *new(string), testData)
|
|
|
+ assert.True(t, lipgloss.Width(testData) > 1)
|
|
|
+ assert.True(t, strings.Contains(testData, "pingo"))
|
|
|
+}
|
|
|
+func Test_md_footer(t *testing.T) {
|
|
|
+ testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
|
|
|
+ testData := testMD.footer()
|
|
|
+ assert.IsType(t, *new(string), testData)
|
|
|
+ assert.True(t, lipgloss.Width(testData) > 1)
|
|
|
+ assert.True(t, strings.Contains(testData, "j/k: down/up"), testData)
|
|
|
+ assert.True(t, strings.Contains(testData, "ctrl+c: quit"), testData)
|
|
|
+}
|
|
|
+func Test_md_getVerticalMargin(t *testing.T) {
|
|
|
+ testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
|
|
|
+ assert.Equal(t, lipgloss.Height(testMD.header()+testMD.footer()), testMD.getVerticalMargin())
|
|
|
+}
|