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.

229 lines
7.0 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. "[13:36:59] <\u001FStuff\u001F> Things said.",
  33. "13:36:59", "text", "Stuff", "Things said.",
  34. },
  35. {
  36. "[23:59] <=Scene=> Scenery and such.",
  37. "23:59:00", "scene", "=Scene=", "Scenery and such.",
  38. },
  39. {
  40. "[01:10:11] * =Scene= Scenery and such from the forum or mIRC using my old script.",
  41. "01:10:11", "scene", "=Scene=", "Scenery and such from the forum or mIRC using my old script.",
  42. },
  43. }
  44. for i, row := range table {
  45. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  46. post, err := parsers.MircPost(row.Input, time.Now(), models.Post{})
  47. if err != nil {
  48. t.Fatal("Could not parse post:", err)
  49. }
  50. assert.Equal(t, row.TS, post.Time.Format("15:04:05"), "Timestamps should match.")
  51. assert.Equal(t, row.Kind, post.Kind, "Kinds should match.")
  52. assert.Equal(t, row.Nick, post.Nick, "Kinds should match.")
  53. assert.Equal(t, row.Text, post.Text, "Kinds should match.")
  54. })
  55. }
  56. }
  57. func TestMircPostErrors(t *testing.T) {
  58. table := []struct {
  59. Input string
  60. Err string
  61. }{
  62. {"[12:34] <Stuff> Things said.", ""},
  63. {"[12:34] >Stuff> Things said.", "line is neither action nor text post"},
  64. {"12:34] <Stuff> Things said.", "no timestamp"},
  65. {"* Stuff Things said.", "no timestamp"},
  66. {"", "no timestamp"},
  67. {"[12:34 <Stuff> Things said.", "incomplete timestamp"},
  68. {"[TE:XT] <Stuff> Things said.", "invalid number in timestamp"},
  69. {"[10] <Stuff> Things said.", "invalid timestamp"},
  70. {"[12:34:56:789] <Stuff> Things said.", ""},
  71. {"[12:34:56.789] <Stuff> Things said.", "invalid number in timestamp"},
  72. {"[12:34] <Stuff>", "post is empty"},
  73. {"[12:34] * Stuff", "post is empty"},
  74. {"[12:34] =Scene=", "line is neither action nor text post"},
  75. {"[12:34] <=Scene=>", "post is empty"},
  76. }
  77. for i, row := range table {
  78. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  79. _, err := parsers.MircPost(row.Input, time.Now(), models.Post{})
  80. errProblem := ""
  81. if err != nil {
  82. if e2, ok := err.(*parsers.ParseError); ok {
  83. errProblem = e2.Problem
  84. } else {
  85. errProblem = err.Error()
  86. }
  87. }
  88. assert.Equal(t, row.Err, errProblem, "Error should match")
  89. })
  90. }
  91. }
  92. func TestMircPostNextDay(t *testing.T) {
  93. table := []struct {
  94. Prev time.Time
  95. TS string
  96. Time time.Time
  97. }{
  98. {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "12:01", Time: parseDate(t, "2019-01-12 12:01:00")},
  99. {Prev: parseDate(t, "2019-04-08 23:51:59"), TS: "00:09", Time: parseDate(t, "2019-04-09 00:09:00")},
  100. {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:29:59", Time: parseDate(t, "2019-01-13 11:29:59")},
  101. }
  102. for i, row := range table {
  103. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  104. input := fmt.Sprintf("[%s] * Stuff does things.", row.TS)
  105. post, err := parsers.MircPost(input, row.Prev, models.Post{Time: row.Prev})
  106. if err != nil {
  107. t.Fatal("Could not parse post:", err)
  108. }
  109. assert.Equal(t, row.Time, post.Time)
  110. })
  111. }
  112. }
  113. func TestMircPostDayPerPostBug(t *testing.T) {
  114. startDate := parseDate(t, "2019-07-20 23:25:00")
  115. table := []struct {
  116. TS string
  117. Time time.Time
  118. }{
  119. {TS: "23:25", Time: parseDate(t, "2019-07-20 23:25:00")},
  120. {TS: "23:45", Time: parseDate(t, "2019-07-20 23:45:00")},
  121. {TS: "23:59", Time: parseDate(t, "2019-07-20 23:59:00")},
  122. {TS: "00:00", Time: parseDate(t, "2019-07-21 00:00:00")},
  123. {TS: "00:03", Time: parseDate(t, "2019-07-21 00:03:00")},
  124. {TS: "00:06", Time: parseDate(t, "2019-07-21 00:06:00")},
  125. {TS: "12:00", Time: parseDate(t, "2019-07-21 12:00:00")},
  126. {TS: "22:00", Time: parseDate(t, "2019-07-21 22:00:00")},
  127. {TS: "23:55", Time: parseDate(t, "2019-07-21 23:55:00")},
  128. {TS: "00:05", Time: parseDate(t, "2019-07-22 00:05:00")},
  129. }
  130. prev := startDate
  131. for i, row := range table {
  132. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  133. input := fmt.Sprintf("[%s] * Test post content.", row.TS)
  134. post, err := parsers.MircPost(input, startDate, models.Post{Time: prev})
  135. if err != nil {
  136. t.Fatal("Could not parse post:", err)
  137. }
  138. prev = post.Time
  139. assert.Equal(t, row.Time, post.Time)
  140. })
  141. }
  142. }
  143. func TestMircLog(t *testing.T) {
  144. parsed, err := parsers.MircLog(testLog, parseDate(t, "2018-05-11 00:00:00"), false)
  145. if err != nil {
  146. t.Fatal("ParseLog", err)
  147. }
  148. assert.Equal(t, testLogPosts, parsed.Posts)
  149. assert.Equal(t, parsed.Posts[0].Time, parsed.Log.Date, "Log's date should be the first post's.")
  150. }
  151. func TestMircLogErrors(t *testing.T) {
  152. _, err1 := parsers.MircLog("\n\n\n\n\n \n\t\r\n", time.Time{}, false)
  153. _, err2 := parsers.MircLog("\n\n\n\n\n[14:57]* Stuff \n\t\r\n", time.Time{}, true)
  154. assert.Equal(t, parsers.ErrEmptyLog, err1)
  155. assert.True(t, parsers.IsParseError(err2))
  156. }
  157. func parseDate(t *testing.T, date string) time.Time {
  158. result, err := time.Parse("2006-01-02 15:04:05", date)
  159. if err != nil {
  160. if t != nil {
  161. t.Fatal("Could not parse date", date, err)
  162. } else {
  163. panic("Could not parse date: " + err.Error())
  164. }
  165. }
  166. return result
  167. }
  168. var testLog = strings.Join([]string{
  169. "[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.",
  170. "",
  171. "[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. ",
  172. "",
  173. "[11:",
  174. " [11:27] <Test> Stuff and things.",
  175. }, "\r\n")
  176. var testLogPosts = []*models.Post{
  177. {
  178. ID: "UNASSIGNED",
  179. LogID: "UNASSIGNED",
  180. Time: parseDate(nil, "2018-05-11 11:21:00"),
  181. Kind: "action",
  182. Nick: "Va`ynna_Atana",
  183. Position: 1,
  184. 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.",
  185. },
  186. {
  187. ID: "UNASSIGNED",
  188. LogID: "UNASSIGNED",
  189. Time: parseDate(nil, "2018-05-11 11:21:00"),
  190. Kind: "scene",
  191. Nick: "=Scene=",
  192. Position: 2,
  193. 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.",
  194. },
  195. {
  196. ID: "UNASSIGNED",
  197. LogID: "UNASSIGNED",
  198. Time: parseDate(nil, "2018-05-11 11:27:00"),
  199. Kind: "text",
  200. Nick: "Test",
  201. Position: 3,
  202. Text: "Stuff and things.",
  203. },
  204. }