Files
OpenFlare/backend/plugins/domain/message_gateway/service/dispatch_test.go
T
ryan 4f50f6a8f9 feat(core): bind request services and implement registered tasks
Wire plugin services through Bind/InjectFrom and AppContext so HTTP and
workers resolve dependencies after Apply. Register TaskHandler objects
with persisted results, and implement send_email_code, mail:send,
cleanup_inactive_users, and dispatch_bot_msg.
2026-09-02 16:59:00 +08:00

48 lines
1.5 KiB
Go

// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package service_test
import (
"Wavelet/plugins/domain/message_gateway/service"
"context"
"path/filepath"
"testing"
"github.com/glebarez/sqlite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
"Wavelet/plugins/domain/message_gateway/model"
"Wavelet/plugins/domain/message_gateway/repository"
)
type dispatchTestDB struct{ db *gorm.DB }
func (m *dispatchTestDB) GORM() *gorm.DB { return m.db }
func (m *dispatchTestDB) DB(ctx context.Context) *gorm.DB { return m.db.WithContext(ctx) }
func (m *dispatchTestDB) Named(_ string) *gorm.DB { return m.db }
func TestBotDispatchValidatePayload(t *testing.T) {
h := &service.BotDispatchHandler{}
_, err := h.ValidatePayload([]byte(`{}`))
require.Error(t, err)
_, err = h.ValidatePayload([]byte(`{"text":"hello"}`))
require.NoError(t, err)
}
func TestBotDispatchNoChannels(t *testing.T) {
testDB, err := gorm.Open(sqlite.Open(filepath.Join(t.TempDir(), "dispatch.db")), &gorm.Config{})
require.NoError(t, err)
require.NoError(t, testDB.AutoMigrate(&model.MessageChannel{}, &model.MessageBinding{}))
repository.SetDBServiceForTest(&dispatchTestDB{db: testDB})
t.Cleanup(func() { repository.SetDBServiceForTest(nil) })
h := &service.BotDispatchHandler{}
res, err := h.Execute(context.Background(), []byte(`{"text":"hello"}`))
require.NoError(t, err)
require.NotNil(t, res)
assert.Contains(t, res.Message, "成功 0")
}