package bot_all import ( "context" "testing" "go.uber.org/mock/gomock" "git.3crabs.ru/VLADIMIR/butler/internal/modules/messenger" messenger_mocks "git.3crabs.ru/VLADIMIR/butler/internal/modules/messenger/mocks" "git.3crabs.ru/VLADIMIR/butler/internal/modules/storage" storage_mocks "git.3crabs.ru/VLADIMIR/butler/internal/modules/storage/mocks" ) func Test_botAll_Process(t *testing.T) { t.Parallel() tests := []struct { name string messenger func(ctrl *gomock.Controller) messenger.IMessenger storage func(ctrl *gomock.Controller) storage.IStorage msg *messenger.Message wantErr bool }{ { name: "hello message", messenger: func(ctrl *gomock.Controller) messenger.IMessenger { m := messenger_mocks.NewMockIMessenger(ctrl) m.EXPECT().SendMessage(gomock.Any(), gomock.Any()).Times(0) return m }, storage: func(ctrl *gomock.Controller) storage.IStorage { m := storage_mocks.NewMockIStorage(ctrl) m.EXPECT().UpsertUser(gomock.Any(), storage.User{ChatID: "123", UserID: "username"}).Times(1) m.EXPECT().GetAllUsersByChatID(gomock.Any(), gomock.Any()).Times(0) return m }, msg: &messenger.Message{ ChatID: "123", UserID: "username", Text: "hello", }, wantErr: false, }, { name: "message with all and empty users", messenger: func(ctrl *gomock.Controller) messenger.IMessenger { m := messenger_mocks.NewMockIMessenger(ctrl) m.EXPECT().SendMessage(gomock.Any(), gomock.Any()).Times(0) return m }, storage: func(ctrl *gomock.Controller) storage.IStorage { m := storage_mocks.NewMockIStorage(ctrl) m.EXPECT().UpsertUser(gomock.Any(), storage.User{ChatID: "123", UserID: "username"}).Times(1) m.EXPECT(). GetAllUsersByChatID(gomock.Any(), gomock.Any()). Return([]storage.User{{UserID: "username"}}, nil). Times(1) return m }, msg: &messenger.Message{ ChatID: "123", UserID: "username", Text: "hello @all", }, wantErr: false, }, { name: "message with all and one user", messenger: func(ctrl *gomock.Controller) messenger.IMessenger { m := messenger_mocks.NewMockIMessenger(ctrl) m.EXPECT(). SendMessage( gomock.Any(), &messenger.Message{ ChatID: "123", Text: "username2", }, ). Times(1) return m }, storage: func(ctrl *gomock.Controller) storage.IStorage { m := storage_mocks.NewMockIStorage(ctrl) m.EXPECT().UpsertUser(gomock.Any(), storage.User{ChatID: "123", UserID: "username"}).Times(1) m.EXPECT(). GetAllUsersByChatID(gomock.Any(), gomock.Any()). Return([]storage.User{ {UserID: "username"}, {UserID: "username2"}, }, nil). Times(1) return m }, msg: &messenger.Message{ ChatID: "123", UserID: "username", Text: "hello @all", }, wantErr: false, }, { name: "message with all and one user", messenger: func(ctrl *gomock.Controller) messenger.IMessenger { m := messenger_mocks.NewMockIMessenger(ctrl) m.EXPECT(). SendMessage( gomock.Any(), &messenger.Message{ ChatID: "123", Text: "username2 username3", }, ). Times(1) return m }, storage: func(ctrl *gomock.Controller) storage.IStorage { m := storage_mocks.NewMockIStorage(ctrl) m.EXPECT().UpsertUser(gomock.Any(), storage.User{ChatID: "123", UserID: "username"}).Times(1) m.EXPECT(). GetAllUsersByChatID(gomock.Any(), gomock.Any()). Return([]storage.User{ {UserID: "username"}, {UserID: "username2"}, {UserID: "username3"}, }, nil). Times(1) return m }, msg: &messenger.Message{ ChatID: "123", UserID: "username", Text: "hello @all", }, wantErr: false, }, } for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() ctx := context.Background() ctrl := gomock.NewController(t) defer ctrl.Finish() bot := &botAll{ messenger: tt.messenger(ctrl), storage: tt.storage(ctrl), } if err := bot.Process(ctx, tt.msg); (err != nil) != tt.wantErr { t.Errorf("botAll.Process() error = %v, wantErr %v", err, tt.wantErr) } }) } }