chat_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. package foodgroup
  2. import (
  3. "context"
  4. "math"
  5. "testing"
  6. "github.com/mk6i/open-oscar-server/state"
  7. "github.com/mk6i/open-oscar-server/wire"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/mock"
  10. )
  11. func TestChatService_ChannelMsgToHost(t *testing.T) {
  12. cases := []struct {
  13. // name is the unit test name
  14. name string
  15. // instance is the session of the user sending the chat message
  16. instance *state.SessionInstance
  17. // inputSNAC is the SNAC sent by the sender client
  18. inputSNAC wire.SNACMessage
  19. // mockParams is the list of params sent to mocks that satisfy this
  20. // method's dependencies
  21. mockParams mockParams
  22. // expectSNACToParticipants is the message the server broadcast to chat
  23. // room participants (except the sender)
  24. expectSNACToParticipants wire.SNACMessage
  25. expectOutput *wire.SNACMessage
  26. // randRollDie generates result of rolling a die
  27. randRollDie func(sides int) int
  28. wantErr error
  29. }{
  30. {
  31. name: "send chat room message, expect acknowledgement to sender client",
  32. instance: newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime,
  33. sessOptChatRoomCookie("the-chat-cookie")),
  34. inputSNAC: wire.SNACMessage{
  35. Frame: wire.SNACFrame{
  36. RequestID: 1234,
  37. },
  38. Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  39. Cookie: 1234,
  40. Channel: 14,
  41. TLVRestBlock: wire.TLVRestBlock{
  42. TLVList: wire.TLVList{
  43. {
  44. Tag: wire.ChatTLVPublicWhisperFlag,
  45. Value: []byte{},
  46. },
  47. {
  48. Tag: wire.ChatTLVEnableReflectionFlag,
  49. Value: []byte{},
  50. },
  51. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  52. TLVList: wire.TLVList{
  53. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  54. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
  55. },
  56. }),
  57. },
  58. },
  59. },
  60. },
  61. mockParams: mockParams{
  62. chatMessageRelayerParams: chatMessageRelayerParams{
  63. chatRelayToAllExceptParams: chatRelayToAllExceptParams{
  64. {
  65. screenName: state.NewIdentScreenName("user_sending_chat_msg"),
  66. cookie: "the-chat-cookie",
  67. message: wire.SNACMessage{
  68. Frame: wire.SNACFrame{
  69. FoodGroup: wire.Chat,
  70. SubGroup: wire.ChatChannelMsgToClient,
  71. },
  72. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  73. Cookie: 1234,
  74. Channel: 14,
  75. TLVRestBlock: wire.TLVRestBlock{
  76. TLVList: wire.TLVList{
  77. wire.NewTLVBE(wire.ChatTLVSenderInformation,
  78. newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime).Session().TLVUserInfo()),
  79. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  80. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  81. TLVList: wire.TLVList{
  82. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  83. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
  84. },
  85. }),
  86. },
  87. },
  88. },
  89. },
  90. },
  91. },
  92. },
  93. },
  94. expectOutput: &wire.SNACMessage{
  95. Frame: wire.SNACFrame{
  96. FoodGroup: wire.Chat,
  97. SubGroup: wire.ChatChannelMsgToClient,
  98. RequestID: 1234,
  99. },
  100. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  101. Cookie: 1234,
  102. Channel: 14,
  103. TLVRestBlock: wire.TLVRestBlock{
  104. TLVList: wire.TLVList{
  105. wire.NewTLVBE(wire.ChatTLVSenderInformation,
  106. newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime).Session().TLVUserInfo()),
  107. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  108. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  109. TLVList: wire.TLVList{
  110. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  111. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
  112. },
  113. }),
  114. },
  115. },
  116. },
  117. },
  118. },
  119. {
  120. name: "send chat whisper",
  121. instance: newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime,
  122. sessOptChatRoomCookie("the-chat-cookie")),
  123. inputSNAC: wire.SNACMessage{
  124. Frame: wire.SNACFrame{
  125. RequestID: 1234,
  126. },
  127. Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  128. Cookie: 1234,
  129. Channel: 14,
  130. TLVRestBlock: wire.TLVRestBlock{
  131. TLVList: wire.TLVList{
  132. wire.NewTLVBE(wire.ChatTLVWhisperToUser, "them"),
  133. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  134. TLVList: wire.TLVList{
  135. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  136. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
  137. },
  138. }),
  139. },
  140. },
  141. },
  142. },
  143. mockParams: mockParams{
  144. chatMessageRelayerParams: chatMessageRelayerParams{
  145. chatRelayToScreenNameParams: chatRelayToScreenNameParams{
  146. {
  147. screenName: state.NewIdentScreenName("them"),
  148. cookie: "the-chat-cookie",
  149. message: wire.SNACMessage{
  150. Frame: wire.SNACFrame{
  151. FoodGroup: wire.Chat,
  152. SubGroup: wire.ChatChannelMsgToClient,
  153. },
  154. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  155. Cookie: 1234,
  156. Channel: 14,
  157. TLVRestBlock: wire.TLVRestBlock{
  158. TLVList: wire.TLVList{
  159. wire.NewTLVBE(wire.ChatTLVSenderInformation,
  160. newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime).Session().TLVUserInfo()),
  161. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  162. TLVList: wire.TLVList{
  163. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  164. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
  165. },
  166. }),
  167. },
  168. },
  169. },
  170. },
  171. },
  172. },
  173. },
  174. },
  175. expectOutput: nil,
  176. },
  177. {
  178. name: "send die roll",
  179. instance: newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime,
  180. sessOptChatRoomCookie("the-chat-cookie")),
  181. inputSNAC: wire.SNACMessage{
  182. Frame: wire.SNACFrame{
  183. RequestID: 1234,
  184. },
  185. Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  186. Cookie: 1234,
  187. Channel: 14,
  188. TLVRestBlock: wire.TLVRestBlock{
  189. TLVList: wire.TLVList{
  190. {
  191. Tag: wire.ChatTLVPublicWhisperFlag,
  192. Value: []byte{},
  193. },
  194. {
  195. Tag: wire.ChatTLVEnableReflectionFlag,
  196. Value: []byte{},
  197. },
  198. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  199. TLVList: wire.TLVList{
  200. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  201. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">//roll-dice3-sides8</FONT></BODY></HTML>"),
  202. },
  203. }),
  204. },
  205. },
  206. },
  207. },
  208. mockParams: mockParams{
  209. chatMessageRelayerParams: chatMessageRelayerParams{
  210. chatRelayToAllExceptParams: chatRelayToAllExceptParams{
  211. {
  212. screenName: state.NewIdentScreenName("user_sending_chat_msg"),
  213. cookie: "the-chat-cookie",
  214. message: wire.SNACMessage{
  215. Frame: wire.SNACFrame{
  216. FoodGroup: wire.Chat,
  217. SubGroup: wire.ChatChannelMsgToClient,
  218. },
  219. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  220. Cookie: 1234,
  221. Channel: 14,
  222. TLVRestBlock: wire.TLVRestBlock{
  223. TLVList: wire.TLVList{
  224. wire.NewTLVBE(wire.ChatTLVSenderInformation, sessOnlineHost.Session().TLVUserInfo()),
  225. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  226. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  227. TLVList: wire.TLVList{
  228. wire.NewTLVBE(wire.ChatTLVMessageInfoEncoding, "us-ascii"),
  229. wire.NewTLVBE(wire.ChatTLVMessageInfoLang, "en"),
  230. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  231. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">user_sending_chat_msg rolled 3 8-sided dice: 2 4 8</FONT></BODY></HTML>"),
  232. },
  233. }),
  234. },
  235. },
  236. },
  237. },
  238. },
  239. },
  240. },
  241. },
  242. expectOutput: &wire.SNACMessage{
  243. Frame: wire.SNACFrame{
  244. FoodGroup: wire.Chat,
  245. SubGroup: wire.ChatChannelMsgToClient,
  246. RequestID: 1234,
  247. },
  248. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  249. Cookie: 1234,
  250. Channel: 14,
  251. TLVRestBlock: wire.TLVRestBlock{
  252. TLVList: wire.TLVList{
  253. wire.NewTLVBE(wire.ChatTLVSenderInformation, sessOnlineHost.Session().TLVUserInfo()),
  254. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  255. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  256. TLVList: wire.TLVList{
  257. wire.NewTLVBE(wire.ChatTLVMessageInfoEncoding, "us-ascii"),
  258. wire.NewTLVBE(wire.ChatTLVMessageInfoLang, "en"),
  259. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  260. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">user_sending_chat_msg rolled 3 8-sided dice: 2 4 8</FONT></BODY></HTML>"),
  261. },
  262. }),
  263. },
  264. },
  265. },
  266. },
  267. randRollDie: func() func(sides int) int {
  268. // return multiples of 2 starting with 2
  269. val := 2
  270. return func(sides int) int {
  271. ret := val
  272. val *= 2
  273. return ret
  274. }
  275. }(),
  276. },
  277. {
  278. name: "send chat room message with macOS client 4.0.9 bug containing bad channel ID, expect message to " +
  279. "client on MIME channel",
  280. instance: newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime,
  281. sessOptChatRoomCookie("the-chat-cookie")),
  282. inputSNAC: wire.SNACMessage{
  283. Frame: wire.SNACFrame{
  284. RequestID: 1234,
  285. },
  286. Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  287. Cookie: 1234,
  288. Channel: math.MaxUint16,
  289. TLVRestBlock: wire.TLVRestBlock{
  290. TLVList: wire.TLVList{
  291. {
  292. Tag: wire.ChatTLVPublicWhisperFlag,
  293. Value: []byte{},
  294. },
  295. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  296. TLVList: wire.TLVList{
  297. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  298. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
  299. },
  300. }),
  301. },
  302. },
  303. },
  304. },
  305. mockParams: mockParams{
  306. chatMessageRelayerParams: chatMessageRelayerParams{
  307. chatRelayToAllExceptParams: chatRelayToAllExceptParams{
  308. {
  309. screenName: state.NewIdentScreenName("user_sending_chat_msg"),
  310. cookie: "the-chat-cookie",
  311. message: wire.SNACMessage{
  312. Frame: wire.SNACFrame{
  313. FoodGroup: wire.Chat,
  314. SubGroup: wire.ChatChannelMsgToClient,
  315. },
  316. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  317. Cookie: 1234,
  318. Channel: wire.ICBMChannelMIME,
  319. TLVRestBlock: wire.TLVRestBlock{
  320. TLVList: wire.TLVList{
  321. wire.NewTLVBE(wire.ChatTLVSenderInformation,
  322. newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime).Session().TLVUserInfo()),
  323. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  324. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  325. TLVList: wire.TLVList{
  326. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  327. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
  328. },
  329. }),
  330. },
  331. },
  332. },
  333. },
  334. },
  335. },
  336. },
  337. },
  338. expectOutput: nil,
  339. },
  340. {
  341. name: "send chat room message, don't expect acknowledgement to sender client",
  342. instance: newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime,
  343. sessOptChatRoomCookie("the-chat-cookie")),
  344. inputSNAC: wire.SNACMessage{
  345. Frame: wire.SNACFrame{
  346. RequestID: 1234,
  347. },
  348. Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  349. Cookie: 1234,
  350. Channel: 14,
  351. TLVRestBlock: wire.TLVRestBlock{
  352. TLVList: wire.TLVList{
  353. {
  354. Tag: wire.ChatTLVPublicWhisperFlag,
  355. Value: []byte{},
  356. },
  357. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  358. TLVList: wire.TLVList{
  359. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  360. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
  361. },
  362. }),
  363. },
  364. },
  365. },
  366. },
  367. mockParams: mockParams{
  368. chatMessageRelayerParams: chatMessageRelayerParams{
  369. chatRelayToAllExceptParams: chatRelayToAllExceptParams{
  370. {
  371. screenName: state.NewIdentScreenName("user_sending_chat_msg"),
  372. cookie: "the-chat-cookie",
  373. message: wire.SNACMessage{
  374. Frame: wire.SNACFrame{
  375. FoodGroup: wire.Chat,
  376. SubGroup: wire.ChatChannelMsgToClient,
  377. },
  378. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  379. Cookie: 1234,
  380. Channel: 14,
  381. TLVRestBlock: wire.TLVRestBlock{
  382. TLVList: wire.TLVList{
  383. wire.NewTLVBE(wire.ChatTLVSenderInformation,
  384. newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime).Session().TLVUserInfo()),
  385. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  386. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  387. TLVList: wire.TLVList{
  388. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  389. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
  390. },
  391. }),
  392. },
  393. },
  394. },
  395. },
  396. },
  397. },
  398. },
  399. },
  400. },
  401. {
  402. name: "send chat room message, expect that TLVs that crash macos client",
  403. instance: newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime,
  404. sessOptChatRoomCookie("the-chat-cookie")),
  405. inputSNAC: wire.SNACMessage{
  406. Frame: wire.SNACFrame{
  407. RequestID: 1234,
  408. },
  409. Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  410. Cookie: 1234,
  411. Channel: 14,
  412. TLVRestBlock: wire.TLVRestBlock{
  413. TLVList: wire.TLVList{
  414. {
  415. Tag: wire.ChatTLVPublicWhisperFlag,
  416. Value: []byte{},
  417. },
  418. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  419. TLVList: wire.TLVList{
  420. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  421. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
  422. wire.NewTLVBE(0x04, "remove me"),
  423. wire.NewTLVBE(0x05, "remove me"),
  424. },
  425. }),
  426. },
  427. },
  428. },
  429. },
  430. mockParams: mockParams{
  431. chatMessageRelayerParams: chatMessageRelayerParams{
  432. chatRelayToAllExceptParams: chatRelayToAllExceptParams{
  433. {
  434. screenName: state.NewIdentScreenName("user_sending_chat_msg"),
  435. cookie: "the-chat-cookie",
  436. message: wire.SNACMessage{
  437. Frame: wire.SNACFrame{
  438. FoodGroup: wire.Chat,
  439. SubGroup: wire.ChatChannelMsgToClient,
  440. },
  441. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  442. Cookie: 1234,
  443. Channel: 14,
  444. TLVRestBlock: wire.TLVRestBlock{
  445. TLVList: wire.TLVList{
  446. wire.NewTLVBE(wire.ChatTLVSenderInformation,
  447. newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime).Session().TLVUserInfo()),
  448. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  449. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  450. TLVList: wire.TLVList{
  451. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  452. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
  453. },
  454. }),
  455. },
  456. },
  457. },
  458. },
  459. },
  460. },
  461. },
  462. },
  463. },
  464. {
  465. name: "fix malformed Kopete encoding ISO 8859 to ISO-8859-1",
  466. instance: newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime,
  467. sessOptChatRoomCookie("the-chat-cookie")),
  468. inputSNAC: wire.SNACMessage{
  469. Frame: wire.SNACFrame{
  470. RequestID: 1234,
  471. },
  472. Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  473. Cookie: 1234,
  474. Channel: 14,
  475. TLVRestBlock: wire.TLVRestBlock{
  476. TLVList: wire.TLVList{
  477. {
  478. Tag: wire.ChatTLVPublicWhisperFlag,
  479. Value: []byte{},
  480. },
  481. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  482. TLVList: wire.TLVList{
  483. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  484. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
  485. wire.NewTLVBE(wire.ChatTLVMessageInfoEncoding, "ISO 8859"),
  486. },
  487. }),
  488. },
  489. },
  490. },
  491. },
  492. mockParams: mockParams{
  493. chatMessageRelayerParams: chatMessageRelayerParams{
  494. chatRelayToAllExceptParams: chatRelayToAllExceptParams{
  495. {
  496. screenName: state.NewIdentScreenName("user_sending_chat_msg"),
  497. cookie: "the-chat-cookie",
  498. message: wire.SNACMessage{
  499. Frame: wire.SNACFrame{
  500. FoodGroup: wire.Chat,
  501. SubGroup: wire.ChatChannelMsgToClient,
  502. },
  503. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  504. Cookie: 1234,
  505. Channel: 14,
  506. TLVRestBlock: wire.TLVRestBlock{
  507. TLVList: wire.TLVList{
  508. wire.NewTLVBE(wire.ChatTLVSenderInformation,
  509. newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime).Session().TLVUserInfo()),
  510. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  511. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  512. TLVList: wire.TLVList{
  513. wire.NewTLVBE(wire.ChatTLVMessageInfoText,
  514. "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
  515. wire.NewTLVBE(wire.ChatTLVMessageInfoEncoding, "ISO-8859-1"),
  516. },
  517. }),
  518. },
  519. },
  520. },
  521. },
  522. },
  523. },
  524. },
  525. },
  526. },
  527. }
  528. for _, tc := range cases {
  529. t.Run(tc.name, func(t *testing.T) {
  530. chatMessageRelayer := newMockChatMessageRelayer(t)
  531. for _, params := range tc.mockParams.chatRelayToAllExceptParams {
  532. chatMessageRelayer.EXPECT().
  533. RelayToAllExcept(mock.Anything, params.cookie, params.screenName, params.message)
  534. }
  535. for _, params := range tc.mockParams.chatRelayToScreenNameParams {
  536. chatMessageRelayer.EXPECT().
  537. RelayToScreenName(mock.Anything, params.cookie, params.screenName, params.message)
  538. }
  539. svc := NewChatService(chatMessageRelayer)
  540. svc.randRollDie = tc.randRollDie
  541. outputSNAC, err := svc.ChannelMsgToHost(context.Background(), tc.instance, tc.inputSNAC.Frame,
  542. tc.inputSNAC.Body.(wire.SNAC_0x0E_0x05_ChatChannelMsgToHost))
  543. assert.ErrorIs(t, err, tc.wantErr)
  544. assert.Equal(t, tc.expectOutput, outputSNAC)
  545. })
  546. }
  547. }
  548. func TestParseDiceCommand(t *testing.T) {
  549. tests := []struct {
  550. input []byte
  551. expectedValid bool
  552. expectedDice int
  553. expectedSides int
  554. }{
  555. {[]byte("//roll-sides999-dice15"), true, 15, 999},
  556. {[]byte("//roll-sides999-dice15 "), true, 15, 999},
  557. {[]byte("//roll-SIDES999-DICE15"), false, 0, 0},
  558. {[]byte("//roll-sides999-sides15"), false, 0, 0},
  559. {[]byte("//roll-sides999-dice15 as I was saying"), false, 0, 0},
  560. {[]byte("i'm gonna roll some dice //roll-sides999-dice15"), false, 0, 0},
  561. {[]byte("//roll-dice15-sides999"), true, 15, 999},
  562. {[]byte("//roll-dice15"), true, 15, 6},
  563. {[]byte("//roll-dice0"), false, 0, 0},
  564. {[]byte("//roll-sides0"), false, 0, 0},
  565. {[]byte("//roll-sides999"), true, 2, 999},
  566. {[]byte("//roll-dice16"), false, 0, 0},
  567. {[]byte("//roll-sides1000"), false, 0, 0},
  568. {[]byte("//roll-dice-5"), false, 0, 0},
  569. {[]byte("//roll-sides-9"), false, 0, 0},
  570. {[]byte("//roll"), true, 2, 6},
  571. {[]byte("invalid input"), false, 0, 0},
  572. }
  573. for _, test := range tests {
  574. t.Run(string(test.input), func(t *testing.T) {
  575. valid, dice, sides := parseDiceCommand(test.input)
  576. if valid != test.expectedValid {
  577. t.Errorf("For input '%s', expected valid = %v, got %v", test.input, test.expectedValid, valid)
  578. }
  579. if dice != test.expectedDice {
  580. t.Errorf("For input '%s', expected dice = %d, got %d", test.input, test.expectedDice, dice)
  581. }
  582. if sides != test.expectedSides {
  583. t.Errorf("For input '%s', expected sides = %d, got %d", test.input, test.expectedSides, sides)
  584. }
  585. })
  586. }
  587. }