mattermost-plugin-cleanup-c.../server/command/command_test.go
Felipe M. 85e2a38f17
Some checks failed
ci / test (push) Successful in 3m7s
ci / lint (push) Failing after 2m50s
ci / build (push) Successful in 4m21s
release / release (push) Successful in 5m43s
Add automatic channel cleanup with unified modal UI.
Per-channel retention config, daily scheduled cleanup, slash command and REST API support, plus a single Channel cleanup menu that opens a plugin modal for configure, run-now, and disable actions.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-28 18:35:24 +02:00

124 lines
3.4 KiB
Go

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:")
}