pingo_test.go 2.8 KB

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