package command import ( "errors" "testing" "github.com/mattermost/mattermost/server/public/model" "github.com/mattermost/mattermost/server/public/plugin/plugintest" "github.com/mattermost/mattermost/server/public/pluginapi" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "github.com/fmartingr/mattermost-plugin-cleanup-channel/server/autocleanup" ) type mockAutoCleanupStore struct { configs map[string]*autocleanup.Config } func (m *mockAutoCleanupStore) Get(channelID string) (*autocleanup.Config, error) { config, ok := m.configs[channelID] if !ok { return nil, nil } return config, nil } func (m *mockAutoCleanupStore) Set(channelID string, offsetDays int) error { if offsetDays <= 0 { return errors.New("invalid offset") } m.configs[channelID] = &autocleanup.Config{ ChannelID: channelID, OffsetDays: offsetDays, } return nil } func (m *mockAutoCleanupStore) Delete(channelID string) error { delete(m.configs, channelID) return nil } func newTestHandler(store AutoCleanupStore) *Handler { api := &plugintest.API{} api.On("HasPermissionTo", mock.Anything, model.PermissionManageSystem).Return(true) return &Handler{ client: pluginapi.NewClient(api, &plugintest.Driver{}), autoCleanupStore: store, } } func TestHandleUnknownCommand(t *testing.T) { handler := newTestHandler(&mockAutoCleanupStore{configs: map[string]*autocleanup.Config{}}) handler.cleanup = func(userID, channelID string) (int, error) { return 0, nil } response, err := handler.Handle(&model.CommandArgs{ Command: "/unknown", }) require.NoError(t, err) require.Equal(t, model.CommandResponseTypeEphemeral, response.ResponseType) require.Contains(t, response.Text, "Unknown command") } func TestHandleAutoCleanupSetOffset(t *testing.T) { store := &mockAutoCleanupStore{configs: map[string]*autocleanup.Config{}} handler := newTestHandler(store) response, err := handler.Handle(&model.CommandArgs{ Command: "/auto-cleanup-channel 30", ChannelId: "channel-1", UserId: "admin", }) require.NoError(t, err) require.Contains(t, response.Text, "Automatic cleanup enabled") require.NotNil(t, store.configs["channel-1"]) require.Equal(t, 30, store.configs["channel-1"].OffsetDays) } func TestHandleAutoCleanupDisable(t *testing.T) { store := &mockAutoCleanupStore{ configs: map[string]*autocleanup.Config{ "channel-1": {ChannelID: "channel-1", OffsetDays: 30}, }, } handler := newTestHandler(store) response, err := handler.Handle(&model.CommandArgs{ Command: "/auto-cleanup-channel off", ChannelId: "channel-1", }) require.NoError(t, err) require.Contains(t, response.Text, "disabled") require.Nil(t, store.configs["channel-1"]) } func TestHandleAutoCleanupShowsStatus(t *testing.T) { handler := newTestHandler(&mockAutoCleanupStore{ configs: map[string]*autocleanup.Config{ "channel-1": {ChannelID: "channel-1", OffsetDays: 14}, }, }) response, err := handler.Handle(&model.CommandArgs{ Command: "/auto-cleanup-channel", ChannelId: "channel-1", }) require.NoError(t, err) require.Contains(t, response.Text, "14 day(s)") } func TestHandleAutoCleanupInvalidDays(t *testing.T) { handler := newTestHandler(&mockAutoCleanupStore{configs: map[string]*autocleanup.Config{}}) response, err := handler.Handle(&model.CommandArgs{ Command: "/auto-cleanup-channel abc", ChannelId: "channel-1", }) require.NoError(t, err) require.Contains(t, response.Text, "Usage:") }