GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
6.7 KiB

  1. package parsers_test
  2. import (
  3. "fmt"
  4. "git.aiterp.net/rpdata/api/models"
  5. "git.aiterp.net/rpdata/api/services/parsers"
  6. "github.com/stretchr/testify/assert"
  7. "strings"
  8. "testing"
  9. "time"
  10. )
  11. func TestMircPost(t *testing.T) {
  12. table := []struct {
  13. Input string
  14. TS string
  15. Kind string
  16. Nick string
  17. Text string
  18. }{
  19. {
  20. "[12:34] * Stuff does things.",
  21. "12:34:00", "action", "Stuff", "does things.",
  22. },
  23. {
  24. "[12:34] <Stuff> Things said.",
  25. "12:34:00", "text", "Stuff", "Things said.",
  26. },
  27. {
  28. "[13:36:59] <Stuff> Things said.",
  29. "13:36:59", "text", "Stuff", "Things said.",
  30. },
  31. {
  32. "[23:59] <=Scene=> Scenery and such.",
  33. "23:59:00", "scene", "=Scene=", "Scenery and such.",
  34. },
  35. {
  36. "[01:10:11] * =Scene= Scenery and such from the forum or mIRC using my old script.",
  37. "01:10:11", "scene", "=Scene=", "Scenery and such from the forum or mIRC using my old script.",
  38. },
  39. }
  40. for i, row := range table {
  41. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  42. post, err := parsers.MircPost(row.Input, time.Now(), models.Post{})
  43. if err != nil {
  44. t.Fatal("Could not parse post:", err)
  45. }
  46. assert.Equal(t, row.TS, post.Time.Format("15:04:05"), "Timestamps should match.")
  47. assert.Equal(t, row.Kind, post.Kind, "Kinds should match.")
  48. assert.Equal(t, row.Nick, post.Nick, "Kinds should match.")
  49. assert.Equal(t, row.Text, post.Text, "Kinds should match.")
  50. })
  51. }
  52. }
  53. func TestMircPostErrors(t *testing.T) {
  54. table := []struct {
  55. Input string
  56. Err error
  57. }{
  58. {"[12:34] <Stuff> Things said.", nil},
  59. {"[12:34] >Stuff> Things said.", parsers.ErrNotPost},
  60. {"12:34] <Stuff> Things said.", parsers.ErrNotPost},
  61. {"* Stuff Things said.", parsers.ErrNotPost},
  62. {"", parsers.ErrNotPost},
  63. {"[12:34 <Stuff> Things said.", parsers.ErrNotPost},
  64. {"[TE:XT] <Stuff> Things said.", parsers.ErrNotPost},
  65. {"[10] <Stuff> Things said.", parsers.ErrNotPost},
  66. {"[12:34:56:789] <Stuff> Things said.", nil},
  67. {"[12:34:56.789] <Stuff> Things said.", parsers.ErrNotPost},
  68. {"[12:34] <Stuff>", parsers.ErrNotPost},
  69. {"[12:34] * Stuff", parsers.ErrNotPost},
  70. {"[12:34] =Scene=", parsers.ErrNotPost},
  71. {"[12:34] <=Scene=>", parsers.ErrNotPost},
  72. }
  73. for i, row := range table {
  74. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  75. _, err := parsers.MircPost(row.Input, time.Now(), models.Post{})
  76. assert.Equal(t, row.Err, err, "Error should match")
  77. })
  78. }
  79. }
  80. func TestMircPostNextDay(t *testing.T) {
  81. table := []struct {
  82. Prev time.Time
  83. TS string
  84. Time time.Time
  85. }{
  86. {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "12:01", Time: parseDate(t, "2019-01-12 12:01:00")},
  87. {Prev: parseDate(t, "2019-04-08 23:51:59"), TS: "00:09", Time: parseDate(t, "2019-04-09 00:09:00")},
  88. {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:29:59", Time: parseDate(t, "2019-01-13 11:29:59")},
  89. }
  90. for i, row := range table {
  91. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  92. input := fmt.Sprintf("[%s] * Stuff does things.", row.TS)
  93. post, err := parsers.MircPost(input, row.Prev, models.Post{Time: row.Prev})
  94. if err != nil {
  95. t.Fatal("Could not parse post:", err)
  96. }
  97. assert.Equal(t, row.Time, post.Time)
  98. })
  99. }
  100. }
  101. func TestMircPostDayPerPostBug(t *testing.T) {
  102. startDate := parseDate(t, "2019-07-20 23:25:00")
  103. table := []struct {
  104. TS string
  105. Time time.Time
  106. }{
  107. {TS: "23:25", Time: parseDate(t, "2019-07-20 23:25:00")},
  108. {TS: "23:45", Time: parseDate(t, "2019-07-20 23:45:00")},
  109. {TS: "23:59", Time: parseDate(t, "2019-07-20 23:59:00")},
  110. {TS: "00:00", Time: parseDate(t, "2019-07-21 00:00:00")},
  111. {TS: "00:03", Time: parseDate(t, "2019-07-21 00:03:00")},
  112. {TS: "00:06", Time: parseDate(t, "2019-07-21 00:06:00")},
  113. {TS: "12:00", Time: parseDate(t, "2019-07-21 12:00:00")},
  114. {TS: "22:00", Time: parseDate(t, "2019-07-21 22:00:00")},
  115. {TS: "23:55", Time: parseDate(t, "2019-07-21 23:55:00")},
  116. {TS: "00:05", Time: parseDate(t, "2019-07-22 00:05:00")},
  117. }
  118. prev := startDate
  119. for i, row := range table {
  120. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  121. input := fmt.Sprintf("[%s] * Test post content.", row.TS)
  122. post, err := parsers.MircPost(input, startDate, models.Post{Time: prev})
  123. if err != nil {
  124. t.Fatal("Could not parse post:", err)
  125. }
  126. prev = post.Time
  127. assert.Equal(t, row.Time, post.Time)
  128. })
  129. }
  130. }
  131. func TestMircLog(t *testing.T) {
  132. parsed, err := parsers.MircLog(testLog, parseDate(t, "2018-05-11 00:00:00"), false)
  133. if err != nil {
  134. t.Fatal("ParseLog", err)
  135. }
  136. assert.Equal(t, testLogPosts, parsed.Posts)
  137. assert.Equal(t, parsed.Posts[0].Time, parsed.Log.Date, "Log's date should be the first post's.")
  138. }
  139. func TestMircLogErrors(t *testing.T) {
  140. _, err1 := parsers.MircLog("\n\n\n\n\n \n\t\r\n", time.Time{}, false)
  141. _, err2 := parsers.MircLog("\n\n\n\n\n[14:57]* Stuff \n\t\r\n", time.Time{}, true)
  142. assert.Equal(t, parsers.ErrEmptyLog, err1)
  143. assert.Equal(t, parsers.ErrNotPost, err2)
  144. }
  145. func parseDate(t *testing.T, date string) time.Time {
  146. result, err := time.Parse("2006-01-02 15:04:05", date)
  147. if err != nil {
  148. if t != nil {
  149. t.Fatal("Could not parse date", date, err)
  150. } else {
  151. panic("Could not parse date: " + err.Error())
  152. }
  153. }
  154. return result
  155. }
  156. var testLog = strings.Join([]string{
  157. "[11:21] * Va`ynna_Atana returns to the apartment at the end of another long day. She hangs up her jacket and a green scarf next to the door before going straight to the bathroom; she leaves the door open, though. She walked home with Uvena this time, but the two parted ways downstairs.",
  158. "",
  159. "[11:21] <=Scene=> The weather outside is not pleasant in the slightest. The storm is still going on, visibility is reduced and the worst gusts of wind can be felt inside the apartment as a faint shudder. ",
  160. "",
  161. "[11:",
  162. " [11:27] <Test> Stuff and things.",
  163. }, "\r\n")
  164. var testLogPosts = []*models.Post{
  165. {
  166. ID: "UNASSIGNED",
  167. LogID: "UNASSIGNED",
  168. Time: parseDate(nil, "2018-05-11 11:21:00"),
  169. Kind: "action",
  170. Nick: "Va`ynna_Atana",
  171. Position: 1,
  172. Text: "returns to the apartment at the end of another long day. She hangs up her jacket and a green scarf next to the door before going straight to the bathroom; she leaves the door open, though. She walked home with Uvena this time, but the two parted ways downstairs.",
  173. },
  174. {
  175. ID: "UNASSIGNED",
  176. LogID: "UNASSIGNED",
  177. Time: parseDate(nil, "2018-05-11 11:21:00"),
  178. Kind: "scene",
  179. Nick: "=Scene=",
  180. Position: 2,
  181. Text: "The weather outside is not pleasant in the slightest. The storm is still going on, visibility is reduced and the worst gusts of wind can be felt inside the apartment as a faint shudder.",
  182. },
  183. {
  184. ID: "UNASSIGNED",
  185. LogID: "UNASSIGNED",
  186. Time: parseDate(nil, "2018-05-11 11:27:00"),
  187. Kind: "text",
  188. Nick: "Test",
  189. Position: 3,
  190. Text: "Stuff and things.",
  191. },
  192. }