| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package state
- import (
- "testing"
- "github.com/mk6i/retro-aim-server/wire"
- "github.com/stretchr/testify/assert"
- )
- func TestChatRegistry_RegisterAndReceive(t *testing.T) {
- type registration struct {
- room ChatRoom
- value any
- }
- tests := []struct {
- name string
- givenRegistered []registration
- lookupCookie string
- wantRegistered registration
- wantErr error
- }{
- {
- name: "chat room and value found",
- givenRegistered: []registration{
- {
- room: ChatRoom{Cookie: "cookie1"},
- value: "value1",
- },
- {
- room: ChatRoom{Cookie: "cookie2"},
- value: "value2",
- },
- },
- lookupCookie: "cookie2",
- wantRegistered: registration{
- room: ChatRoom{Cookie: "cookie2"},
- value: "value2",
- },
- },
- {
- name: "chat room and value not found",
- givenRegistered: []registration{
- {
- room: ChatRoom{Cookie: "cookie1"},
- value: "value1",
- },
- {
- room: ChatRoom{Cookie: "cookie2"},
- value: "value2",
- },
- },
- lookupCookie: "cookie3",
- wantErr: ErrChatRoomNotFound,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- chatRegistry := NewChatRegistry()
- for _, r := range tt.givenRegistered {
- chatRegistry.Register(r.room, r.value)
- }
- room, value, err := chatRegistry.Retrieve(tt.lookupCookie)
- assert.Equal(t, tt.wantRegistered.room, room)
- assert.Equal(t, tt.wantRegistered.value, value)
- assert.ErrorIs(t, err, tt.wantErr)
- })
- }
- }
- func TestChatRegistry_RegisterAndRemove(t *testing.T) {
- type registration struct {
- room ChatRoom
- value any
- }
- tests := []struct {
- name string
- givenRegistered []registration
- removeCookie string
- wantRegistered []registration
- wantErr error
- }{
- {
- name: "chat room and value removed",
- givenRegistered: []registration{
- {
- room: ChatRoom{Cookie: "cookie1"},
- value: "value1",
- },
- {
- room: ChatRoom{Cookie: "cookie2"},
- value: "value2",
- },
- },
- removeCookie: "cookie2",
- wantRegistered: []registration{
- {
- room: ChatRoom{Cookie: "cookie1"},
- value: "value1",
- },
- },
- },
- {
- name: "no chat room and value removed",
- givenRegistered: []registration{
- {
- room: ChatRoom{Cookie: "cookie1"},
- value: "value1",
- },
- {
- room: ChatRoom{Cookie: "cookie2"},
- value: "value2",
- },
- },
- removeCookie: "cookie3",
- wantRegistered: []registration{
- {
- room: ChatRoom{Cookie: "cookie1"},
- value: "value1",
- },
- {
- room: ChatRoom{Cookie: "cookie2"},
- value: "value2",
- },
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- chatRegistry := NewChatRegistry()
- for _, r := range tt.givenRegistered {
- chatRegistry.Register(r.room, r.value)
- }
- chatRegistry.Remove(tt.removeCookie)
- for _, r := range tt.wantRegistered {
- room, value, err := chatRegistry.Retrieve(r.room.Cookie)
- assert.Equal(t, r.room, room)
- assert.Equal(t, r.value, value)
- assert.NoError(t, err)
- }
- })
- }
- }
- func TestChatRoom_TLVList(t *testing.T) {
- room := NewChatRoom()
- room.Name = "chat-room-name"
- have := room.TLVList()
- want := []wire.TLV{
- wire.NewTLV(wire.ChatRoomTLVFlags, uint16(15)),
- wire.NewTLV(wire.ChatRoomTLVCreateTime, uint32(room.CreateTime.Unix())),
- wire.NewTLV(wire.ChatRoomTLVMaxMsgLen, uint16(1024)),
- wire.NewTLV(wire.ChatRoomTLVMaxOccupancy, uint16(100)),
- wire.NewTLV(wire.ChatRoomTLVNavCreatePerms, uint8(2)),
- wire.NewTLV(wire.ChatRoomTLVFullyQualifiedName, room.Name),
- wire.NewTLV(wire.ChatRoomTLVRoomName, room.Name),
- }
- assert.Equal(t, want, have)
- }
|