pingo_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package main
  2. import (
  3. "pingo"
  4. "strings"
  5. "testing"
  6. "time"
  7. tea "charm.land/bubbletea/v2"
  8. "charm.land/lipgloss/v2"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func spawnMD(hosts []string, width, height, chartHeight int) md {
  12. return md{p: pingo.InitialModel(hosts, width, height, chartHeight)}
  13. }
  14. func Test_md_Init(t *testing.T) {
  15. testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
  16. cmd := testMD.Init()
  17. assert.IsType(t, *new(timingMsg), cmd())
  18. }
  19. func Test_md_Update_handle_windowsize(t *testing.T) {
  20. testMsg := tea.WindowSizeMsg{Width: 80, Height: 80}
  21. testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
  22. assert.Equal(t, 40, testMD.p.Width)
  23. assert.Equal(t, 20, testMD.p.Height)
  24. assert.Equal(t, 0, testMD.viewport.Width())
  25. assert.Equal(t, 0, testMD.viewport.Height())
  26. model, cmd := testMD.Update(testMsg)
  27. assert.Nil(t, cmd)
  28. assert.Equal(t, 80, model.(md).p.Width)
  29. assert.Equal(t, 80-2, model.(md).p.Height)
  30. }
  31. func Test_md_Update_handle_keymsg(t *testing.T) {
  32. testKeyMsg := tea.KeyPressMsg(tea.Key{Text: "ctrl+c"})
  33. // getTestKey := func() tea.Msg { return testKey }
  34. testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
  35. var cmd tea.Cmd
  36. _, cmd = testMD.Update(testKeyMsg)
  37. assert.NotNil(t, cmd)
  38. assert.IsType(t, *new(tea.QuitMsg), cmd())
  39. }
  40. func Test_md_Update_handle_timing(t *testing.T) {
  41. testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
  42. _, cmd := testMD.Update(tea.Msg(timingMsg{}))
  43. assert.NotNil(t, cmd)
  44. assert.IsType(t, *new(tea.BatchMsg), cmd())
  45. batch := []tea.Cmd(cmd().(tea.BatchMsg))
  46. assert.True(t, len(batch) == 2)
  47. for _, cmd := range batch {
  48. if value, ok := cmd().(timingMsg); ok {
  49. assert.WithinDuration(t, time.Now(), time.Time(value), time.Second*1)
  50. }
  51. }
  52. }
  53. func Test_md_View(t *testing.T) {
  54. testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
  55. view := testMD.View()
  56. assert.True(t, strings.Contains(view.Content, testMD.header()))
  57. assert.True(t, strings.Contains(view.Content, testMD.footer()))
  58. assert.True(t, strings.Contains(view.Content, testMD.viewport.GetContent()))
  59. }
  60. func Test_md_header(t *testing.T) {
  61. testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
  62. testData := testMD.header()
  63. assert.IsType(t, *new(string), testData)
  64. assert.True(t, lipgloss.Width(testData) > 1)
  65. assert.True(t, strings.Contains(testData, "pingo"))
  66. }
  67. func Test_md_footer(t *testing.T) {
  68. testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
  69. testData := testMD.footer()
  70. assert.IsType(t, *new(string), testData)
  71. assert.True(t, lipgloss.Width(testData) > 1)
  72. assert.True(t, strings.Contains(testData, "j/k: down/up"), testData)
  73. assert.True(t, strings.Contains(testData, "ctrl+c: quit"), testData)
  74. }
  75. func Test_md_getVerticalMargin(t *testing.T) {
  76. testMD := spawnMD([]string{"127.0.0.1"}, 40, 20, 10)
  77. assert.Equal(t, lipgloss.Height(testMD.header()+testMD.footer()), testMD.getVerticalMargin())
  78. }