chat_nav_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. package foodgroup
  2. import (
  3. "context"
  4. "errors"
  5. "log/slog"
  6. "testing"
  7. "github.com/mk6i/open-oscar-server/state"
  8. "github.com/mk6i/open-oscar-server/wire"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestChatNavService_CreateRoom(t *testing.T) {
  12. basicChatRoom := state.NewChatRoom("the-chat-room-name", state.NewIdentScreenName("the-screen-name"), state.PrivateExchange)
  13. publicChatRoom := state.NewChatRoom("the-public-chat-room-name", state.NewIdentScreenName("the-screen-name"), state.PublicExchange)
  14. tests := []struct {
  15. name string
  16. chatRoom *state.ChatRoom
  17. instance *state.SessionInstance
  18. inputSNAC wire.SNACMessage
  19. want wire.SNACMessage
  20. mockParams mockParams
  21. wantErr error
  22. fnNewChatRoom func() state.ChatRoom
  23. }{
  24. {
  25. name: "create private room that already exists",
  26. chatRoom: &basicChatRoom,
  27. instance: newTestInstance("the-screen-name"),
  28. inputSNAC: wire.SNACMessage{
  29. Frame: wire.SNACFrame{
  30. RequestID: 1234,
  31. },
  32. Body: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  33. Exchange: basicChatRoom.Exchange(),
  34. Cookie: "create", // actual canned value sent by AIM client
  35. InstanceNumber: basicChatRoom.InstanceNumber(),
  36. DetailLevel: basicChatRoom.DetailLevel(),
  37. TLVBlock: wire.TLVBlock{
  38. TLVList: wire.TLVList{
  39. wire.NewTLVBE(wire.ChatRoomTLVRoomName, basicChatRoom.Name()),
  40. },
  41. },
  42. },
  43. },
  44. want: wire.SNACMessage{
  45. Frame: wire.SNACFrame{
  46. FoodGroup: wire.ChatNav,
  47. SubGroup: wire.ChatNavNavInfo,
  48. RequestID: 1234,
  49. },
  50. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{
  51. TLVRestBlock: wire.TLVRestBlock{
  52. TLVList: wire.TLVList{
  53. wire.NewTLVBE(
  54. wire.ChatNavRequestRoomInfo,
  55. wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  56. Exchange: basicChatRoom.Exchange(),
  57. Cookie: basicChatRoom.Cookie(),
  58. InstanceNumber: basicChatRoom.InstanceNumber(),
  59. DetailLevel: basicChatRoom.DetailLevel(),
  60. TLVBlock: wire.TLVBlock{
  61. TLVList: basicChatRoom.TLVList(),
  62. },
  63. },
  64. ),
  65. },
  66. },
  67. },
  68. },
  69. mockParams: mockParams{
  70. chatRoomRegistryParams: chatRoomRegistryParams{
  71. chatRoomByNameParams: chatRoomByNameParams{
  72. {
  73. exchange: basicChatRoom.Exchange(),
  74. name: basicChatRoom.Name(),
  75. room: basicChatRoom,
  76. },
  77. },
  78. },
  79. },
  80. },
  81. {
  82. name: "create private room that doesn't already exist",
  83. chatRoom: &basicChatRoom,
  84. instance: newTestInstance("the-screen-name"),
  85. inputSNAC: wire.SNACMessage{
  86. Frame: wire.SNACFrame{
  87. RequestID: 1234,
  88. },
  89. Body: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  90. Exchange: basicChatRoom.Exchange(),
  91. Cookie: "create", // actual canned value sent by AIM client
  92. InstanceNumber: basicChatRoom.InstanceNumber(),
  93. DetailLevel: basicChatRoom.DetailLevel(),
  94. TLVBlock: wire.TLVBlock{
  95. TLVList: wire.TLVList{
  96. wire.NewTLVBE(wire.ChatRoomTLVRoomName, basicChatRoom.Name()),
  97. },
  98. },
  99. },
  100. },
  101. want: wire.SNACMessage{
  102. Frame: wire.SNACFrame{
  103. FoodGroup: wire.ChatNav,
  104. SubGroup: wire.ChatNavNavInfo,
  105. RequestID: 1234,
  106. },
  107. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{
  108. TLVRestBlock: wire.TLVRestBlock{
  109. TLVList: wire.TLVList{
  110. wire.NewTLVBE(
  111. wire.ChatNavRequestRoomInfo,
  112. wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  113. Exchange: basicChatRoom.Exchange(),
  114. Cookie: basicChatRoom.Cookie(),
  115. InstanceNumber: basicChatRoom.InstanceNumber(),
  116. DetailLevel: basicChatRoom.DetailLevel(),
  117. TLVBlock: wire.TLVBlock{
  118. TLVList: basicChatRoom.TLVList(),
  119. },
  120. },
  121. ),
  122. },
  123. },
  124. },
  125. },
  126. mockParams: mockParams{
  127. chatRoomRegistryParams: chatRoomRegistryParams{
  128. chatRoomByNameParams: chatRoomByNameParams{
  129. {
  130. exchange: basicChatRoom.Exchange(),
  131. name: basicChatRoom.Name(),
  132. err: state.ErrChatRoomNotFound,
  133. },
  134. },
  135. createChatRoomParams: createChatRoomParams{
  136. {
  137. room: &basicChatRoom,
  138. },
  139. },
  140. },
  141. },
  142. fnNewChatRoom: func() state.ChatRoom {
  143. return basicChatRoom
  144. },
  145. },
  146. {
  147. name: "create public room that already exists",
  148. chatRoom: &publicChatRoom,
  149. instance: newTestInstance("the-screen-name"),
  150. inputSNAC: wire.SNACMessage{
  151. Frame: wire.SNACFrame{
  152. RequestID: 1234,
  153. },
  154. Body: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  155. Exchange: publicChatRoom.Exchange(),
  156. Cookie: "create", // actual canned value sent by AIM client
  157. InstanceNumber: publicChatRoom.InstanceNumber(),
  158. DetailLevel: publicChatRoom.DetailLevel(),
  159. TLVBlock: wire.TLVBlock{
  160. TLVList: wire.TLVList{
  161. wire.NewTLVBE(wire.ChatRoomTLVRoomName, publicChatRoom.Name()),
  162. },
  163. },
  164. },
  165. },
  166. want: wire.SNACMessage{
  167. Frame: wire.SNACFrame{
  168. FoodGroup: wire.ChatNav,
  169. SubGroup: wire.ChatNavNavInfo,
  170. RequestID: 1234,
  171. },
  172. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{
  173. TLVRestBlock: wire.TLVRestBlock{
  174. TLVList: wire.TLVList{
  175. wire.NewTLVBE(
  176. wire.ChatNavRequestRoomInfo,
  177. wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  178. Exchange: publicChatRoom.Exchange(),
  179. Cookie: publicChatRoom.Cookie(),
  180. InstanceNumber: publicChatRoom.InstanceNumber(),
  181. DetailLevel: publicChatRoom.DetailLevel(),
  182. TLVBlock: wire.TLVBlock{
  183. TLVList: publicChatRoom.TLVList(),
  184. },
  185. },
  186. ),
  187. },
  188. },
  189. },
  190. },
  191. mockParams: mockParams{
  192. chatRoomRegistryParams: chatRoomRegistryParams{
  193. chatRoomByNameParams: chatRoomByNameParams{
  194. {
  195. exchange: publicChatRoom.Exchange(),
  196. name: publicChatRoom.Name(),
  197. room: publicChatRoom,
  198. },
  199. },
  200. },
  201. },
  202. },
  203. {
  204. name: "create public room that does not exist",
  205. chatRoom: &publicChatRoom,
  206. instance: newTestInstance("the-screen-name"),
  207. inputSNAC: wire.SNACMessage{
  208. Frame: wire.SNACFrame{
  209. RequestID: 1234,
  210. },
  211. Body: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  212. Exchange: publicChatRoom.Exchange(),
  213. Cookie: "create", // actual canned value sent by AIM client
  214. InstanceNumber: publicChatRoom.InstanceNumber(),
  215. DetailLevel: publicChatRoom.DetailLevel(),
  216. TLVBlock: wire.TLVBlock{
  217. TLVList: wire.TLVList{
  218. wire.NewTLVBE(wire.ChatRoomTLVRoomName, publicChatRoom.Name()),
  219. },
  220. },
  221. },
  222. },
  223. want: wire.SNACMessage{
  224. Frame: wire.SNACFrame{
  225. FoodGroup: wire.ChatNav,
  226. SubGroup: wire.ChatNavErr,
  227. RequestID: 1234,
  228. },
  229. Body: wire.SNACError{
  230. Code: wire.ErrorCodeNoMatch,
  231. },
  232. },
  233. mockParams: mockParams{
  234. chatRoomRegistryParams: chatRoomRegistryParams{
  235. chatRoomByNameParams: chatRoomByNameParams{
  236. {
  237. exchange: publicChatRoom.Exchange(),
  238. name: publicChatRoom.Name(),
  239. err: state.ErrChatRoomNotFound,
  240. },
  241. },
  242. },
  243. },
  244. fnNewChatRoom: func() state.ChatRoom {
  245. return basicChatRoom
  246. },
  247. },
  248. {
  249. name: "create room invalid exchange number",
  250. chatRoom: &basicChatRoom,
  251. instance: newTestInstance("the-screen-name"),
  252. inputSNAC: wire.SNACMessage{
  253. Frame: wire.SNACFrame{
  254. RequestID: 1234,
  255. },
  256. Body: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  257. Exchange: 1337,
  258. Cookie: "create", // actual canned value sent by AIM client
  259. InstanceNumber: basicChatRoom.InstanceNumber(),
  260. DetailLevel: basicChatRoom.DetailLevel(),
  261. TLVBlock: wire.TLVBlock{
  262. TLVList: wire.TLVList{
  263. wire.NewTLVBE(wire.ChatRoomTLVRoomName, basicChatRoom.Name()),
  264. },
  265. },
  266. },
  267. },
  268. want: wire.SNACMessage{
  269. Frame: wire.SNACFrame{
  270. FoodGroup: wire.ChatNav,
  271. SubGroup: wire.ChatNavErr,
  272. RequestID: 1234,
  273. },
  274. Body: wire.SNACError{
  275. Code: wire.ErrorCodeNotSupportedByHost,
  276. },
  277. },
  278. },
  279. {
  280. name: "incoming create room missing name tlv",
  281. chatRoom: &basicChatRoom,
  282. instance: newTestInstance("the-screen-name"),
  283. inputSNAC: wire.SNACMessage{
  284. Frame: wire.SNACFrame{
  285. RequestID: 1234,
  286. },
  287. Body: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  288. Exchange: basicChatRoom.Exchange(),
  289. Cookie: "create", // actual canned value sent by AIM client
  290. InstanceNumber: basicChatRoom.InstanceNumber(),
  291. DetailLevel: basicChatRoom.DetailLevel(),
  292. TLVBlock: wire.TLVBlock{
  293. TLVList: wire.TLVList{}, // intentionally empty for test
  294. },
  295. },
  296. },
  297. want: wire.SNACMessage{},
  298. wantErr: errChatNavRoomNameMissing,
  299. },
  300. {
  301. name: "create private room failed",
  302. chatRoom: &basicChatRoom,
  303. instance: newTestInstance("the-screen-name"),
  304. inputSNAC: wire.SNACMessage{
  305. Frame: wire.SNACFrame{
  306. RequestID: 1234,
  307. },
  308. Body: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  309. Exchange: basicChatRoom.Exchange(),
  310. Cookie: "create", // actual canned value sent by AIM client
  311. InstanceNumber: basicChatRoom.InstanceNumber(),
  312. DetailLevel: basicChatRoom.DetailLevel(),
  313. TLVBlock: wire.TLVBlock{
  314. TLVList: wire.TLVList{
  315. wire.NewTLVBE(wire.ChatRoomTLVRoomName, basicChatRoom.Name()),
  316. },
  317. },
  318. },
  319. },
  320. want: wire.SNACMessage{},
  321. wantErr: errChatNavRoomCreateFailed,
  322. mockParams: mockParams{
  323. chatRoomRegistryParams: chatRoomRegistryParams{
  324. chatRoomByNameParams: chatRoomByNameParams{
  325. {
  326. exchange: basicChatRoom.Exchange(),
  327. name: basicChatRoom.Name(),
  328. err: state.ErrChatRoomNotFound,
  329. },
  330. },
  331. createChatRoomParams: createChatRoomParams{
  332. {
  333. room: &basicChatRoom,
  334. err: errors.New("fake database error"),
  335. },
  336. },
  337. },
  338. },
  339. fnNewChatRoom: func() state.ChatRoom {
  340. return basicChatRoom
  341. },
  342. },
  343. {
  344. name: "create private room failed unknown error",
  345. chatRoom: &basicChatRoom,
  346. instance: newTestInstance("the-screen-name"),
  347. inputSNAC: wire.SNACMessage{
  348. Frame: wire.SNACFrame{
  349. RequestID: 1234,
  350. },
  351. Body: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  352. Exchange: basicChatRoom.Exchange(),
  353. Cookie: "create", // actual canned value sent by AIM client
  354. InstanceNumber: basicChatRoom.InstanceNumber(),
  355. DetailLevel: basicChatRoom.DetailLevel(),
  356. TLVBlock: wire.TLVBlock{
  357. TLVList: wire.TLVList{
  358. wire.NewTLVBE(wire.ChatRoomTLVRoomName, basicChatRoom.Name()),
  359. },
  360. },
  361. },
  362. },
  363. want: wire.SNACMessage{},
  364. wantErr: errChatNavRetrieveFailed,
  365. mockParams: mockParams{
  366. chatRoomRegistryParams: chatRoomRegistryParams{
  367. chatRoomByNameParams: chatRoomByNameParams{
  368. {
  369. exchange: basicChatRoom.Exchange(),
  370. name: basicChatRoom.Name(),
  371. err: errors.New("fake database error"),
  372. },
  373. },
  374. },
  375. },
  376. fnNewChatRoom: func() state.ChatRoom {
  377. return basicChatRoom
  378. },
  379. },
  380. }
  381. for _, tt := range tests {
  382. t.Run(tt.name, func(t *testing.T) {
  383. chatRoomRegistry := newMockChatRoomRegistry(t)
  384. for _, params := range tt.mockParams.chatRoomByNameParams {
  385. chatRoomRegistry.EXPECT().
  386. ChatRoomByName(matchContext(), params.exchange, params.name).
  387. Return(params.room, params.err)
  388. }
  389. for _, params := range tt.mockParams.createChatRoomParams {
  390. chatRoomRegistry.EXPECT().
  391. CreateChatRoom(matchContext(), params.room).
  392. Return(params.err)
  393. }
  394. svc := NewChatNavService(slog.Default(), chatRoomRegistry)
  395. outputSNAC, err := svc.CreateRoom(context.Background(), tt.instance, tt.inputSNAC.Frame, tt.inputSNAC.Body.(wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate))
  396. assert.ErrorIs(t, err, tt.wantErr)
  397. assert.Equal(t, tt.want, outputSNAC)
  398. })
  399. }
  400. }
  401. func TestChatNavService_RequestRoomInfo(t *testing.T) {
  402. privateChatRoom := state.NewChatRoom("the-chat-room", state.NewIdentScreenName("the-user"), state.PrivateExchange)
  403. publicChatRoom := state.NewChatRoom("the-chat-room", state.NewIdentScreenName("the-user"), state.PublicExchange)
  404. tests := []struct {
  405. name string
  406. inputSNAC wire.SNACMessage
  407. want wire.SNACMessage
  408. mockParams mockParams
  409. wantErr error
  410. }{
  411. {
  412. name: "request existing private room info successfully",
  413. inputSNAC: wire.SNACMessage{
  414. Frame: wire.SNACFrame{
  415. RequestID: 1234,
  416. },
  417. Body: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  418. Exchange: state.PrivateExchange,
  419. Cookie: privateChatRoom.Cookie(),
  420. },
  421. },
  422. want: wire.SNACMessage{
  423. Frame: wire.SNACFrame{
  424. FoodGroup: wire.ChatNav,
  425. SubGroup: wire.ChatNavNavInfo,
  426. RequestID: 1234,
  427. },
  428. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{
  429. TLVRestBlock: wire.TLVRestBlock{
  430. TLVList: wire.TLVList{
  431. wire.NewTLVBE(0x04, wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  432. Cookie: privateChatRoom.Cookie(),
  433. DetailLevel: privateChatRoom.DetailLevel(),
  434. Exchange: privateChatRoom.Exchange(),
  435. InstanceNumber: privateChatRoom.InstanceNumber(),
  436. TLVBlock: wire.TLVBlock{
  437. TLVList: privateChatRoom.TLVList(),
  438. },
  439. }),
  440. },
  441. },
  442. },
  443. },
  444. mockParams: mockParams{
  445. chatRoomRegistryParams: chatRoomRegistryParams{
  446. chatRoomByCookieParams: chatRoomByCookieParams{
  447. {
  448. cookie: privateChatRoom.Cookie(),
  449. room: privateChatRoom,
  450. },
  451. },
  452. },
  453. },
  454. },
  455. {
  456. name: "request existing public room info successfully",
  457. inputSNAC: wire.SNACMessage{
  458. Frame: wire.SNACFrame{
  459. RequestID: 1234,
  460. },
  461. Body: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  462. Exchange: publicChatRoom.Exchange(),
  463. Cookie: publicChatRoom.Cookie(),
  464. },
  465. },
  466. want: wire.SNACMessage{
  467. Frame: wire.SNACFrame{
  468. FoodGroup: wire.ChatNav,
  469. SubGroup: wire.ChatNavNavInfo,
  470. RequestID: 1234,
  471. },
  472. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{
  473. TLVRestBlock: wire.TLVRestBlock{
  474. TLVList: wire.TLVList{
  475. wire.NewTLVBE(0x04, wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  476. Cookie: publicChatRoom.Cookie(),
  477. DetailLevel: publicChatRoom.DetailLevel(),
  478. Exchange: publicChatRoom.Exchange(),
  479. InstanceNumber: publicChatRoom.InstanceNumber(),
  480. TLVBlock: wire.TLVBlock{
  481. TLVList: publicChatRoom.TLVList(),
  482. },
  483. }),
  484. },
  485. },
  486. },
  487. },
  488. mockParams: mockParams{
  489. chatRoomRegistryParams: chatRoomRegistryParams{
  490. chatRoomByCookieParams: chatRoomByCookieParams{
  491. {
  492. cookie: publicChatRoom.Cookie(),
  493. room: publicChatRoom,
  494. },
  495. },
  496. },
  497. },
  498. },
  499. {
  500. name: "request room info with invalid exchange number",
  501. inputSNAC: wire.SNACMessage{
  502. Frame: wire.SNACFrame{
  503. RequestID: 1234,
  504. },
  505. Body: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  506. Exchange: 1337,
  507. Cookie: "the-chat-cookie",
  508. },
  509. },
  510. want: wire.SNACMessage{
  511. Frame: wire.SNACFrame{
  512. FoodGroup: wire.ChatNav,
  513. SubGroup: wire.ChatNavErr,
  514. RequestID: 1234,
  515. },
  516. Body: wire.SNACError{
  517. Code: wire.ErrorCodeNotSupportedByHost,
  518. },
  519. },
  520. },
  521. {
  522. name: "request room info on nonexistent room",
  523. inputSNAC: wire.SNACMessage{
  524. Frame: wire.SNACFrame{
  525. RequestID: 1234,
  526. },
  527. Body: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  528. Exchange: state.PrivateExchange,
  529. Cookie: privateChatRoom.Cookie(),
  530. },
  531. },
  532. want: wire.SNACMessage{},
  533. wantErr: errChatNavMismatchedExchange,
  534. mockParams: mockParams{
  535. chatRoomRegistryParams: chatRoomRegistryParams{
  536. chatRoomByCookieParams: chatRoomByCookieParams{
  537. {
  538. cookie: privateChatRoom.Cookie(),
  539. room: publicChatRoom,
  540. },
  541. },
  542. },
  543. },
  544. },
  545. {
  546. name: "request room info with mismatched exchange",
  547. inputSNAC: wire.SNACMessage{
  548. Frame: wire.SNACFrame{
  549. RequestID: 1234,
  550. },
  551. Body: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  552. Exchange: state.PrivateExchange,
  553. Cookie: privateChatRoom.Cookie(),
  554. },
  555. },
  556. want: wire.SNACMessage{},
  557. wantErr: state.ErrChatRoomNotFound,
  558. mockParams: mockParams{
  559. chatRoomRegistryParams: chatRoomRegistryParams{
  560. chatRoomByCookieParams: chatRoomByCookieParams{
  561. {
  562. cookie: privateChatRoom.Cookie(),
  563. err: state.ErrChatRoomNotFound,
  564. },
  565. },
  566. },
  567. },
  568. },
  569. }
  570. for _, tt := range tests {
  571. t.Run(tt.name, func(t *testing.T) {
  572. chatRoomRegistry := newMockChatRoomRegistry(t)
  573. for _, params := range tt.mockParams.chatRoomByCookieParams {
  574. chatRoomRegistry.EXPECT().
  575. ChatRoomByCookie(matchContext(), params.cookie).
  576. Return(params.room, params.err)
  577. }
  578. svc := NewChatNavService(slog.Default(), chatRoomRegistry)
  579. got, err := svc.RequestRoomInfo(context.Background(), tt.inputSNAC.Frame,
  580. tt.inputSNAC.Body.(wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo))
  581. assert.ErrorIs(t, err, tt.wantErr)
  582. if tt.wantErr != nil {
  583. return
  584. }
  585. assert.Equal(t, tt.want, got)
  586. })
  587. }
  588. }
  589. func TestChatNavService_RequestChatRights(t *testing.T) {
  590. svc := NewChatNavService(nil, nil)
  591. have := svc.RequestChatRights(context.Background(), wire.SNACFrame{RequestID: 1234})
  592. want := wire.SNACMessage{
  593. Frame: wire.SNACFrame{
  594. FoodGroup: wire.ChatNav,
  595. SubGroup: wire.ChatNavNavInfo,
  596. RequestID: 1234,
  597. },
  598. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{
  599. TLVRestBlock: wire.TLVRestBlock{
  600. TLVList: wire.TLVList{
  601. wire.NewTLVBE(wire.ChatNavTLVMaxConcurrentRooms, uint8(10)),
  602. wire.NewTLVBE(wire.ChatNavTLVExchangeInfo, wire.SNAC_0x0D_0x09_TLVExchangeInfo{
  603. Identifier: 4,
  604. TLVBlock: wire.TLVBlock{
  605. TLVList: wire.TLVList{
  606. wire.NewTLVBE(wire.ChatRoomTLVMaxConcurrentRooms, uint8(10)),
  607. wire.NewTLVBE(wire.ChatRoomTLVClassPerms, uint16(0x0010)),
  608. wire.NewTLVBE(wire.ChatRoomTLVMaxNameLen, uint16(100)),
  609. wire.NewTLVBE(wire.ChatRoomTLVFlags, uint16(15)),
  610. wire.NewTLVBE(wire.ChatRoomTLVNavCreatePerms, uint8(2)),
  611. wire.NewTLVBE(wire.ChatRoomTLVCharSet1, "us-ascii"),
  612. wire.NewTLVBE(wire.ChatRoomTLVLang1, "en"),
  613. wire.NewTLVBE(wire.ChatRoomTLVCharSet2, "us-ascii"),
  614. wire.NewTLVBE(wire.ChatRoomTLVLang2, "en"),
  615. },
  616. },
  617. }),
  618. wire.NewTLVBE(wire.ChatNavTLVExchangeInfo, wire.SNAC_0x0D_0x09_TLVExchangeInfo{
  619. Identifier: 5,
  620. TLVBlock: wire.TLVBlock{
  621. TLVList: wire.TLVList{
  622. wire.NewTLVBE(wire.ChatRoomTLVMaxConcurrentRooms, uint8(10)),
  623. wire.NewTLVBE(wire.ChatRoomTLVClassPerms, uint16(0x0010)),
  624. wire.NewTLVBE(wire.ChatRoomTLVMaxNameLen, uint16(100)),
  625. wire.NewTLVBE(wire.ChatRoomTLVFlags, uint16(15)),
  626. wire.NewTLVBE(wire.ChatRoomTLVNavCreatePerms, uint8(2)),
  627. wire.NewTLVBE(wire.ChatRoomTLVCharSet1, "us-ascii"),
  628. wire.NewTLVBE(wire.ChatRoomTLVLang1, "en"),
  629. wire.NewTLVBE(wire.ChatRoomTLVCharSet2, "us-ascii"),
  630. wire.NewTLVBE(wire.ChatRoomTLVLang2, "en"),
  631. },
  632. },
  633. }),
  634. },
  635. },
  636. },
  637. }
  638. assert.Equal(t, want, have)
  639. }
  640. func TestChatNavService_ExchangeInfo(t *testing.T) {
  641. tests := []struct {
  642. name string
  643. inputSNAC wire.SNACMessage
  644. want wire.SNACMessage
  645. mockParams mockParams
  646. wantErr error
  647. }{
  648. {
  649. name: "request private exchange info",
  650. inputSNAC: wire.SNACMessage{
  651. Frame: wire.SNACFrame{
  652. RequestID: 1234,
  653. },
  654. Body: wire.SNAC_0x0D_0x03_ChatNavRequestExchangeInfo{
  655. Exchange: state.PrivateExchange,
  656. },
  657. },
  658. want: wire.SNACMessage{
  659. Frame: wire.SNACFrame{
  660. FoodGroup: wire.ChatNav,
  661. SubGroup: wire.ChatNavNavInfo,
  662. RequestID: 1234,
  663. },
  664. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{
  665. TLVRestBlock: wire.TLVRestBlock{
  666. TLVList: wire.TLVList{
  667. wire.NewTLVBE(wire.ChatNavTLVMaxConcurrentRooms, uint8(10)),
  668. wire.NewTLVBE(wire.ChatNavTLVExchangeInfo, wire.SNAC_0x0D_0x09_TLVExchangeInfo{
  669. Identifier: state.PrivateExchange,
  670. TLVBlock: wire.TLVBlock{
  671. TLVList: wire.TLVList{
  672. wire.NewTLVBE(wire.ChatRoomTLVMaxConcurrentRooms, uint8(10)),
  673. wire.NewTLVBE(wire.ChatRoomTLVClassPerms, uint16(0x0010)),
  674. wire.NewTLVBE(wire.ChatRoomTLVMaxNameLen, uint16(100)),
  675. wire.NewTLVBE(wire.ChatRoomTLVFlags, uint16(15)),
  676. wire.NewTLVBE(wire.ChatRoomTLVNavCreatePerms, uint8(2)),
  677. wire.NewTLVBE(wire.ChatRoomTLVCharSet1, "us-ascii"),
  678. wire.NewTLVBE(wire.ChatRoomTLVLang1, "en"),
  679. wire.NewTLVBE(wire.ChatRoomTLVCharSet2, "us-ascii"),
  680. wire.NewTLVBE(wire.ChatRoomTLVLang2, "en"),
  681. },
  682. },
  683. }),
  684. },
  685. },
  686. },
  687. },
  688. },
  689. {
  690. name: "request public exchange info",
  691. inputSNAC: wire.SNACMessage{
  692. Frame: wire.SNACFrame{
  693. RequestID: 1234,
  694. },
  695. Body: wire.SNAC_0x0D_0x03_ChatNavRequestExchangeInfo{
  696. Exchange: state.PublicExchange,
  697. },
  698. },
  699. want: wire.SNACMessage{
  700. Frame: wire.SNACFrame{
  701. FoodGroup: wire.ChatNav,
  702. SubGroup: wire.ChatNavNavInfo,
  703. RequestID: 1234,
  704. },
  705. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{
  706. TLVRestBlock: wire.TLVRestBlock{
  707. TLVList: wire.TLVList{
  708. wire.NewTLVBE(wire.ChatNavTLVMaxConcurrentRooms, uint8(10)),
  709. wire.NewTLVBE(wire.ChatNavTLVExchangeInfo, wire.SNAC_0x0D_0x09_TLVExchangeInfo{
  710. Identifier: state.PublicExchange,
  711. TLVBlock: wire.TLVBlock{
  712. TLVList: wire.TLVList{
  713. wire.NewTLVBE(wire.ChatRoomTLVMaxConcurrentRooms, uint8(10)),
  714. wire.NewTLVBE(wire.ChatRoomTLVClassPerms, uint16(0x0010)),
  715. wire.NewTLVBE(wire.ChatRoomTLVMaxNameLen, uint16(100)),
  716. wire.NewTLVBE(wire.ChatRoomTLVFlags, uint16(15)),
  717. wire.NewTLVBE(wire.ChatRoomTLVNavCreatePerms, uint8(2)),
  718. wire.NewTLVBE(wire.ChatRoomTLVCharSet1, "us-ascii"),
  719. wire.NewTLVBE(wire.ChatRoomTLVLang1, "en"),
  720. wire.NewTLVBE(wire.ChatRoomTLVCharSet2, "us-ascii"),
  721. wire.NewTLVBE(wire.ChatRoomTLVLang2, "en"),
  722. },
  723. },
  724. }),
  725. },
  726. },
  727. },
  728. },
  729. },
  730. {
  731. name: "request invalid exchange info",
  732. inputSNAC: wire.SNACMessage{
  733. Frame: wire.SNACFrame{
  734. RequestID: 1234,
  735. },
  736. Body: wire.SNAC_0x0D_0x03_ChatNavRequestExchangeInfo{
  737. Exchange: 6,
  738. },
  739. },
  740. want: wire.SNACMessage{
  741. Frame: wire.SNACFrame{
  742. FoodGroup: wire.ChatNav,
  743. SubGroup: wire.ChatNavErr,
  744. RequestID: 1234,
  745. },
  746. Body: wire.SNACError{
  747. Code: wire.ErrorCodeNotSupportedByHost,
  748. },
  749. },
  750. },
  751. }
  752. for _, tt := range tests {
  753. t.Run(tt.name, func(t *testing.T) {
  754. svc := NewChatNavService(slog.Default(), nil)
  755. outputSNAC, err := svc.ExchangeInfo(context.Background(), tt.inputSNAC.Frame,
  756. tt.inputSNAC.Body.(wire.SNAC_0x0D_0x03_ChatNavRequestExchangeInfo))
  757. assert.ErrorIs(t, err, tt.wantErr)
  758. if tt.wantErr != nil {
  759. return
  760. }
  761. assert.Equal(t, tt.want, outputSNAC)
  762. })
  763. }
  764. }