|
@@ -0,0 +1,257 @@
|
|
|
|
|
+package pingo
|
|
|
|
|
+
|
|
|
|
|
+import (
|
|
|
|
|
+ "strings"
|
|
|
|
|
+ "testing"
|
|
|
|
|
+ "time"
|
|
|
|
|
+
|
|
|
|
|
+ tea "charm.land/bubbletea/v2"
|
|
|
|
|
+ "charm.land/lipgloss/v2"
|
|
|
|
|
+ "github.com/stretchr/testify/assert"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+func Test_InitialModel(t *testing.T) {
|
|
|
|
|
+ addresses := []string{"127.0.0.1", "cantresolvethisever"}
|
|
|
|
|
+ speed := time.Second * 1
|
|
|
|
|
+ chartHeight := 10
|
|
|
|
|
+ model := InitialModel(addresses, speed, chartHeight)
|
|
|
|
|
+
|
|
|
|
|
+ assert.Implements(t, new(tea.Model), model)
|
|
|
|
|
+
|
|
|
|
|
+ assert.True(t, model.viewport.MouseWheelEnabled)
|
|
|
|
|
+ assert.Equal(t, speed, model.UpdateSpeed)
|
|
|
|
|
+ assert.Equal(t, chartHeight, model.ChartHeight)
|
|
|
|
|
+ assert.Equal(t, 2, len(model.Addresses))
|
|
|
|
|
+ for i, address := range model.Addresses {
|
|
|
|
|
+ assert.Equal(t, 80, address.max_results)
|
|
|
|
|
+ assert.Equal(t, addresses[i], address.Address)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// skipped until a testing paradigm can be determined (func has sig)
|
|
|
|
|
+func Test_Model_Init(t *testing.T) {
|
|
|
|
|
+ t.Skip()
|
|
|
|
|
+ addresses := []string{"127.0.0.1", "cantresolvethisever"}
|
|
|
|
|
+ speed := time.Millisecond * 1
|
|
|
|
|
+ chartHeight := 10
|
|
|
|
|
+ model := InitialModel(addresses, speed, chartHeight)
|
|
|
|
|
+ cmd := model.Init()
|
|
|
|
|
+ msg := cmd()
|
|
|
|
|
+ assert.IsType(t, new(tickMsg), msg)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// skipped until a testing paradigm can be determined (viewport has scrolled)
|
|
|
|
|
+func Test_Model_Update_scroll_on_j(t *testing.T) {
|
|
|
|
|
+ t.Skip()
|
|
|
|
|
+ testAddresses := []string{"127.0.0.1", "cantresolvethisever"}
|
|
|
|
|
+ testSpeed := time.Second * 1
|
|
|
|
|
+ testModel := InitialModel(testAddresses, testSpeed, 10)
|
|
|
|
|
+ testScroll := testModel.viewport.ScrollPercent()
|
|
|
|
|
+ testSize := tea.WindowSizeMsg{Width: 20, Height: 200}
|
|
|
|
|
+ testMsg := tea.KeyPressMsg(tea.Key{Text: "j"})
|
|
|
|
|
+
|
|
|
|
|
+ model, _ := testModel.Update(testSize)
|
|
|
|
|
+ modelv2, _ := model.Update(testMsg)
|
|
|
|
|
+ assert.Greater(t, testScroll, modelv2.(Model).viewport.ScrollPercent())
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// skipped until a testing paradigm can be determined (viewport has scrolled)
|
|
|
|
|
+func Test_Model_Update_scroll_on_k(t *testing.T) { t.Skip() }
|
|
|
|
|
+
|
|
|
|
|
+func Test_Model_Update_handle_windowsize(t *testing.T) {
|
|
|
|
|
+ testAddresses := []string{"127.0.0.1", "cantresolvethisever"}
|
|
|
|
|
+ testSpeed := time.Second * 1
|
|
|
|
|
+
|
|
|
|
|
+ testModel := InitialModel(testAddresses, testSpeed, 10)
|
|
|
|
|
+ assert.True(t, testModel.viewport.Height() == 0)
|
|
|
|
|
+
|
|
|
|
|
+ testMsg := tea.KeyPressMsg(tea.Key{Text: "ctrl+c"})
|
|
|
|
|
+ assert.IsType(t, *new(tea.KeyPressMsg), testMsg)
|
|
|
|
|
+ testSize := tea.WindowSizeMsg{Width: 20, Height: 200}
|
|
|
|
|
+ assert.IsType(t, *new(tea.WindowSizeMsg), testSize)
|
|
|
|
|
+
|
|
|
|
|
+ model, cmd := testModel.Update(testSize)
|
|
|
|
|
+ assert.Nil(t, cmd)
|
|
|
|
|
+ assert.Equal(t, 200-2, model.(Model).viewport.Height()) // TODO make margin height accessible to tests
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// skipped until a testing paradigm can be determined (func has sig)
|
|
|
|
|
+func Test_Model_Update_handle_tickMsg(t *testing.T) {}
|
|
|
|
|
+
|
|
|
|
|
+func spawnTestModel(addresses []string, points []float64, width, height, cHeight int) Model {
|
|
|
|
|
+ speed := time.Second * 1
|
|
|
|
|
+ testModel := InitialModel(addresses, speed, cHeight)
|
|
|
|
|
+ for i := range testModel.Addresses {
|
|
|
|
|
+ testModel.Addresses[i].results = points
|
|
|
|
|
+ }
|
|
|
|
|
+ testModel.ModelWidth = width
|
|
|
|
|
+ testModel.ModelHeight = height
|
|
|
|
|
+
|
|
|
|
|
+ return testModel
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func Test_Model_Update_handle_pollMsg(t *testing.T) {
|
|
|
|
|
+ testModel := spawnTestModel(
|
|
|
|
|
+ []string{"127.0.0.1"}, []float64{}, 0, 20, 10)
|
|
|
|
|
+ batch := testModel.Poll()
|
|
|
|
|
+ msg := batch().(pollResultMsg)
|
|
|
|
|
+ assert.IsType(t, *new(pollResultMsg), msg)
|
|
|
|
|
+ assert.Equal(t, 1, len(msg.results))
|
|
|
|
|
+ assert.InDelta(t, 1, msg.results[0], 10)
|
|
|
|
|
+ assert.Nil(t, msg.err)
|
|
|
|
|
+ assert.Equal(t, 0, msg.index)
|
|
|
|
|
+
|
|
|
|
|
+ model, batch := testModel.Update(msg)
|
|
|
|
|
+ assert.IsType(t, *new(tea.Cmd), batch)
|
|
|
|
|
+ assert.Equal(t, msg.results, model.(Model).Addresses[0].results)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func Test_Model_View(t *testing.T) {
|
|
|
|
|
+ testModel := spawnTestModel(
|
|
|
|
|
+ []string{"127.0.0.1", "cantresolvethisever"}, []float64{1.0, 2.0, 3.0}, 0, 20, 10)
|
|
|
|
|
+ result := testModel.View()
|
|
|
|
|
+ assert.IsType(t, *new(tea.View), result)
|
|
|
|
|
+ assert.True(t, strings.Contains(result.Content, "pingo"))
|
|
|
|
|
+ assert.True(t, strings.Contains(result.Content, "j/k: down/up"))
|
|
|
|
|
+ assert.True(t, strings.Contains(result.Content, "ctrl-c"))
|
|
|
|
|
+ assert.True(t, strings.Contains(result.Content, "quit"))
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func Test_Model_Render_init_state(t *testing.T) {
|
|
|
|
|
+ testModel := spawnTestModel(
|
|
|
|
|
+ []string{"127.0.0.1", "cantresolvethisever"}, []float64{}, 0, 20, 10)
|
|
|
|
|
+ result := testModel.Render()
|
|
|
|
|
+ assert.IsType(t, *new(string), result)
|
|
|
|
|
+
|
|
|
|
|
+ assert.True(t, strings.Contains(result, "127.0.0.1"))
|
|
|
|
|
+ assert.True(t, strings.Contains(result, "cantresolvethisever"))
|
|
|
|
|
+ assert.False(t, strings.Contains(result, "pingo"))
|
|
|
|
|
+ assert.False(t, strings.Contains(result, "j/k: up/down"))
|
|
|
|
|
+ for line := range strings.SplitSeq(result, "\n") {
|
|
|
|
|
+ if strings.Contains(line, "127.0.0.1") || strings.Contains(line, "cantresolvethisever") {
|
|
|
|
|
+ assert.True(t, strings.Contains(line, "loading..."))
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func Test_Model_Render_connection_unstable(t *testing.T) {
|
|
|
|
|
+ testModel := spawnTestModel([]string{"doesntmatter"}, []float64{-1}, 200, 20, 10)
|
|
|
|
|
+
|
|
|
|
|
+ result := testModel.Render()
|
|
|
|
|
+ assert.IsType(t, *new(string), result)
|
|
|
|
|
+ assert.True(t, strings.Contains(result, "connection unstable"))
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func Test_Model_Render_has_charts(t *testing.T) {
|
|
|
|
|
+ testModel := spawnTestModel(
|
|
|
|
|
+ []string{"127.0.0.1", "cantresolvethisever"}, make([]float64, 20), 20, 20, 10)
|
|
|
|
|
+ result := testModel.Render()
|
|
|
|
|
+ assert.IsType(t, *new(string), result)
|
|
|
|
|
+
|
|
|
|
|
+ assert.True(t, strings.Contains(result, "127.0.0.1"))
|
|
|
|
|
+ assert.True(t, strings.Contains(result, "cantresolvethisever"))
|
|
|
|
|
+ assert.False(t, strings.Contains(result, "pingo"))
|
|
|
|
|
+ assert.False(t, strings.Contains(result, "j/k: up/down"))
|
|
|
|
|
+ lines := strings.Split(result, "\n")
|
|
|
|
|
+ for i, line := range lines {
|
|
|
|
|
+ if strings.Contains(line, "127.0.0.1") || strings.Contains(line, "cantresolvethisever") {
|
|
|
|
|
+ assert.False(t, strings.Contains(line, "loading..."))
|
|
|
|
|
+ assert.True(t, strings.Contains(lines[i+1], "│")) // assert has chart seperator character
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func Test_Model_Render_has_big_chart(t *testing.T) {
|
|
|
|
|
+ testModel := spawnTestModel(
|
|
|
|
|
+ []string{"127.0.0.1"}, make([]float64, 20), 20, 20, 0)
|
|
|
|
|
+ result := testModel.Render()
|
|
|
|
|
+ assert.IsType(t, *new(string), result)
|
|
|
|
|
+ assert.True(t, strings.Contains(result, "127.0.0.1"))
|
|
|
|
|
+ assert.False(t, strings.Contains(result, "pingo"))
|
|
|
|
|
+ assert.False(t, strings.Contains(result, "j/k: up/down"))
|
|
|
|
|
+
|
|
|
|
|
+ lines := strings.Split(result, "\n")
|
|
|
|
|
+ assert.True(t, strings.Contains(lines[1], "127.0.0.1"), lines[1])
|
|
|
|
|
+ assert.False(t, strings.Contains(lines[1], "loading..."))
|
|
|
|
|
+ assert.True(t, strings.Contains(lines[1+18], "0├"), lines[1+18])
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func Test_Model_Render_has_big_charts(t *testing.T) {
|
|
|
|
|
+ testModel := spawnTestModel(
|
|
|
|
|
+ []string{"127.0.0.1", "cantresolvethisever"}, make([]float64, 20), 20, 20, 0)
|
|
|
|
|
+ result := testModel.Render()
|
|
|
|
|
+ assert.IsType(t, *new(string), result)
|
|
|
|
|
+
|
|
|
|
|
+ assert.True(t, strings.Contains(result, "127.0.0.1"))
|
|
|
|
|
+ assert.True(t, strings.Contains(result, "cantresolvethisever"))
|
|
|
|
|
+ assert.False(t, strings.Contains(result, "pingo"))
|
|
|
|
|
+ assert.False(t, strings.Contains(result, "j/k: up/down"))
|
|
|
|
|
+ lines := strings.Split(result, "\n")
|
|
|
|
|
+ for i, line := range lines {
|
|
|
|
|
+ if strings.Contains(line, "127.0.0.1") || strings.Contains(line, "cantresolvethisever") {
|
|
|
|
|
+ assert.False(t, strings.Contains(line, "loading..."))
|
|
|
|
|
+ assert.True(t, strings.Contains(lines[i+18-5], "0├"), line) // assert bottom of chart is where we expect it to be
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func Test_Model_header_and_footer(t *testing.T) {
|
|
|
|
|
+ addresses := []string{"127.0.0.1", "cantresolvethisever"}
|
|
|
|
|
+ speed := time.Second * 1
|
|
|
|
|
+ chartHeight := 10
|
|
|
|
|
+ testModel := InitialModel(addresses, speed, chartHeight)
|
|
|
|
|
+ result := testModel.header()
|
|
|
|
|
+ assert.IsType(t, *new(string), result)
|
|
|
|
|
+ assert.Equal(t, 1, lipgloss.Height(result))
|
|
|
|
|
+ result = testModel.footer()
|
|
|
|
|
+ assert.IsType(t, *new(string), result)
|
|
|
|
|
+ assert.Equal(t, 1, lipgloss.Height(result))
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func Test_Model_getVerticalMargin(t *testing.T) {
|
|
|
|
|
+ addresses := []string{"127.0.0.1", "cantresolvethisever"}
|
|
|
|
|
+ speed := time.Second * 1
|
|
|
|
|
+ chartHeight := 10
|
|
|
|
|
+ testModel := InitialModel(addresses, speed, chartHeight)
|
|
|
|
|
+ testModel.ModelWidth = 20
|
|
|
|
|
+ testModel.ModelHeight = 20
|
|
|
|
|
+ result := testModel.getVerticalMargin()
|
|
|
|
|
+ assert.IsType(t, *new(int), result)
|
|
|
|
|
+ assert.Equal(t, 2, result)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// skipped until a testing paradigm can be determined (func has sig)
|
|
|
|
|
+func Test_Model_cmd_Tick(t *testing.T) {
|
|
|
|
|
+ t.Skip()
|
|
|
|
|
+ addresses := []string{"127.0.0.1", "cantresolvethisever"}
|
|
|
|
|
+ speed := time.Second * 1
|
|
|
|
|
+ chartHeight := 10
|
|
|
|
|
+ model := InitialModel(addresses, speed, chartHeight)
|
|
|
|
|
+ cmd := model.Tick()
|
|
|
|
|
+ assert.Nil(t, cmd)
|
|
|
|
|
+}
|
|
|
|
|
+func Test_Model_cmd_Poll(t *testing.T) {
|
|
|
|
|
+ addresses := []string{"127.0.0.1", "cantresolvethisever"}
|
|
|
|
|
+ speed := time.Second * 1
|
|
|
|
|
+ chartHeight := 10
|
|
|
|
|
+ model := InitialModel(addresses, speed, chartHeight)
|
|
|
|
|
+ batch := model.Poll()
|
|
|
|
|
+ msgs := []tea.Cmd(batch().(tea.BatchMsg)) // type assert for legibility
|
|
|
|
|
+ for i, cmd := range msgs {
|
|
|
|
|
+ msg := cmd()
|
|
|
|
|
+ assert.IsType(t, *new(pollResultMsg), msg)
|
|
|
|
|
+ result := msg.(pollResultMsg)
|
|
|
|
|
+ switch i {
|
|
|
|
|
+ case 0:
|
|
|
|
|
+ assert.Equal(t, 1, len(result.results))
|
|
|
|
|
+ assert.InDelta(t, 1, result.results[0], 10)
|
|
|
|
|
+ assert.Nil(t, result.err)
|
|
|
|
|
+ assert.Equal(t, 0, result.index)
|
|
|
|
|
+ case 1:
|
|
|
|
|
+ assert.Equal(t, 1, len(result.results))
|
|
|
|
|
+ assert.Equal(t, float64(-1), result.results[0])
|
|
|
|
|
+ assert.NotNil(t, result.err.Error())
|
|
|
|
|
+ assert.Equal(t, 1, result.index)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|