chat_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package handler
  2. import (
  3. "bytes"
  4. "log/slog"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/mock"
  8. "github.com/mk6i/retro-aim-server/wire"
  9. )
  10. func TestChatHandler_ChannelMsgToHost_WithReflectedResponse(t *testing.T) {
  11. input := wire.SNACMessage{
  12. Frame: wire.SNACFrame{
  13. FoodGroup: wire.Chat,
  14. SubGroup: wire.ChatChannelMsgToHost,
  15. },
  16. Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  17. Channel: 4,
  18. },
  19. }
  20. output := wire.SNACMessage{
  21. Frame: wire.SNACFrame{
  22. FoodGroup: wire.Chat,
  23. SubGroup: wire.ChatChannelMsgToClient,
  24. },
  25. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  26. Channel: 4,
  27. },
  28. }
  29. svc := newMockChatService(t)
  30. svc.EXPECT().
  31. ChannelMsgToHost(mock.Anything, mock.Anything, input.Frame, input.Body).
  32. Return(&output, nil)
  33. h := NewChatHandler(slog.Default(), svc)
  34. responseWriter := newMockResponseWriter(t)
  35. responseWriter.EXPECT().
  36. SendSNAC(output.Frame, output.Body).
  37. Return(nil)
  38. buf := &bytes.Buffer{}
  39. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  40. assert.NoError(t, h.ChannelMsgToHost(nil, nil, input.Frame, buf, responseWriter))
  41. }
  42. func TestChatHandler_ChannelMsgToHost_WithoutReflectedResponse(t *testing.T) {
  43. input := wire.SNACMessage{
  44. Frame: wire.SNACFrame{
  45. FoodGroup: wire.Chat,
  46. SubGroup: wire.ChatChannelMsgToHost,
  47. },
  48. Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  49. Channel: 4,
  50. },
  51. }
  52. // nil response from handler means the response is not reflected back to
  53. // the caller
  54. var output *wire.SNACMessage
  55. svc := newMockChatService(t)
  56. svc.EXPECT().
  57. ChannelMsgToHost(mock.Anything, mock.Anything, input.Frame, input.Body).
  58. Return(output, nil)
  59. h := NewChatHandler(slog.Default(), svc)
  60. responseWriter := newMockResponseWriter(t) // omit mock handler call
  61. buf := &bytes.Buffer{}
  62. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  63. assert.NoError(t, h.ChannelMsgToHost(nil, nil, input.Frame, buf, responseWriter))
  64. }