refactor(arch): decouple private imports, enforce contracts and comply with cordis architecture

This commit is contained in:
ryan
2026-09-03 10:40:02 +08:00
parent dbcf485d8a
commit 953a224245
107 changed files with 2028 additions and 1263 deletions
@@ -0,0 +1,113 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package testhelper
import (
"bytes"
"context"
"io"
"sync"
"sync/atomic"
"time"
"Wavelet/core/contracts"
"Wavelet/openflare/plugins/server/kernel/repository"
"gorm.io/gorm"
)
// MockStorageService provides an in-memory contracts.StorageService for tests.
type MockStorageService struct {
mu sync.RWMutex
objects map[string][]byte
seq uint64
}
// NewMockStorageService creates an initialized MockStorageService.
func NewMockStorageService() *MockStorageService {
return &MockStorageService{
objects: make(map[string][]byte),
}
}
// Put writes an object into memory.
func (m *MockStorageService) Put(_ context.Context, key string, body io.Reader, _ int64, _ string) (contracts.StoragePutResult, error) {
m.mu.Lock()
defer m.mu.Unlock()
data, err := io.ReadAll(body)
if err != nil {
return contracts.StoragePutResult{}, err
}
m.objects[key] = data
return contracts.StoragePutResult{Key: key, Bucket: "test-bucket"}, nil
}
// Get reads an object from memory.
func (m *MockStorageService) Get(_ context.Context, key string) (*contracts.StorageObject, error) {
m.mu.RLock()
defer m.mu.RUnlock()
data, ok := m.objects[key]
if ok {
return &contracts.StorageObject{
Key: key,
Body: io.NopCloser(bytes.NewReader(data)),
ContentLength: int64(len(data)),
ContentType: "application/octet-stream",
}, nil
}
return nil, gorm.ErrRecordNotFound
}
// Delete removes an object from memory.
func (m *MockStorageService) Delete(_ context.Context, key string) error {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.objects, key)
return nil
}
// Ingest ingests content into mock storage.
func (m *MockStorageService) Ingest(ctx context.Context, r io.Reader, opts contracts.IngestOptions) (*contracts.IngestResult, error) {
id := atomic.AddUint64(&m.seq, 1)
m.mu.Lock()
data, _ := io.ReadAll(r)
key := opts.FileName
if key == "" {
key = "file.dat"
}
m.objects[key] = data
m.mu.Unlock()
gdb := repository.DB(ctx)
if gdb != nil {
type testUpload struct {
ID uint64 `gorm:"primaryKey"`
UserID uint64
FileName string
FilePath string
MimeType string
Size int64
Status string
Type string
Metadata contracts.UploadMetadataDTO `gorm:"serializer:json;type:jsonb"`
CreatedAt time.Time
UpdatedAt time.Time
}
u := testUpload{
ID: id,
UserID: opts.UserID,
FileName: key,
FilePath: "mock/" + key,
MimeType: opts.MimeType,
Size: opts.Size,
Status: "used",
Type: opts.Type,
Metadata: contracts.UploadMetadataDTO{Extra: opts.Metadata},
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
}
_ = gdb.Table("w_uploads").Save(&u).Error
}
return &contracts.IngestResult{ID: id, Key: "mock/" + key, Created: true, Stored: true}, nil
}
@@ -9,9 +9,8 @@ import (
"time"
"Wavelet/core/contracts"
"Wavelet/openflare/plugins/server/kernel/repository"
"Wavelet/pkg/idgen"
adminmodel "Wavelet/plugins/domain/admin/model"
"Wavelet/plugins/infra/database"
)
// NoopTaskService is a contracts.TaskService that records dispatch and ignores the rest.
@@ -22,35 +21,76 @@ type NoopTaskService struct {
var _ contracts.TaskService = (*NoopTaskService)(nil)
// Dispatch dispatches a task mock execution.
func (s *NoopTaskService) Dispatch(ctx context.Context, taskType string, payload []byte, triggeredBy string) (string, error) {
s.LastType = taskType
s.LastPayload = payload
taskID := fmt.Sprintf("test-task-%d", time.Now().UnixNano())
if conn := database.DB(ctx); conn != nil {
_ = conn.Create(&adminmodel.TaskExecution{
ID: idgen.NextUint64ID(),
gdb := repository.DB(ctx)
if gdb != nil {
var id uint64
func() {
defer func() {
if r := recover(); r != nil {
id = uint64(time.Now().UnixNano())
}
}()
id = idgen.NextUint64ID()
}()
_ = gdb.Table("w_task_executions").Create(&contracts.TaskExecutionDTO{
ID: id,
TaskID: taskID,
TaskType: taskType,
Status: adminmodel.TaskExecutionStatusPending,
TriggeredBy: triggeredBy,
Payload: string(payload),
TriggeredBy: triggeredBy,
Status: "pending",
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
}).Error
}
return taskID, nil
}
// Retry retries a task mock execution.
func (s *NoopTaskService) Retry(context.Context, uint64) (string, error) { return "", nil }
func (s *NoopTaskService) ListTasks() []contracts.TaskMetaDTO { return nil }
// ListTasks lists task mock metadata.
func (s *NoopTaskService) ListTasks() []contracts.TaskMetaDTO { return nil }
// GetTaskMeta returns task mock metadata.
func (s *NoopTaskService) GetTaskMeta(string) (contracts.TaskMetaDTO, bool) {
return contracts.TaskMetaDTO{}, false
}
// ValidatePayload validates task payload.
func (s *NoopTaskService) ValidatePayload(_ string, payload []byte) ([]byte, error) {
return payload, nil
}
func (s *NoopTaskService) ReloadScheduler() error { return nil }
// ReloadScheduler reloads task scheduler.
func (s *NoopTaskService) ReloadScheduler() error { return nil }
// AppendLog appends log message.
func (s *NoopTaskService) AppendLog(context.Context, string, ...any) {}
// ListExecutions lists task executions.
func (s *NoopTaskService) ListExecutions(context.Context, string, string, int, int) ([]contracts.TaskExecutionDTO, int64, error) {
return nil, 0, nil
}
// GetExecution gets task execution by ID.
func (s *NoopTaskService) GetExecution(context.Context, uint64) (*contracts.TaskExecutionDTO, error) {
return &contracts.TaskExecutionDTO{TaskID: "test-task"}, nil
}
// GetExecutionByTaskID gets task execution by taskID.
func (s *NoopTaskService) GetExecutionByTaskID(ctx context.Context, taskID string) (*contracts.TaskExecutionDTO, error) {
gdb := repository.DB(ctx)
if gdb != nil {
var exec contracts.TaskExecutionDTO
if err := gdb.Table("w_task_executions").Where("task_id = ?", taskID).First(&exec).Error; err == nil {
return &exec, nil
}
}
return &contracts.TaskExecutionDTO{ID: 1, TaskID: taskID, Payload: string(s.LastPayload)}, nil
}
@@ -23,31 +23,51 @@ func passThrough() gin.HandlerFunc {
return func(c *gin.Context) { c.Next() }
}
func (s StubAuth) RequireAuthMiddleware() any { return passThrough() }
// RequireAuthMiddleware returns a passthrough middleware.
func (s StubAuth) RequireAuthMiddleware() any { return passThrough() }
// RequireAdminMiddleware returns a passthrough middleware.
func (s StubAuth) RequireAdminMiddleware() any { return passThrough() }
// DisallowTokenAuthMiddleware returns a passthrough middleware.
func (s StubAuth) DisallowTokenAuthMiddleware() any {
return passThrough()
}
// GetCurrentUser returns the stub user.
func (s StubAuth) GetCurrentUser(context.Context) (*contracts.UserDTO, error) {
return s.User, nil
}
// GetCurrentUserID returns the stub user ID.
func (s StubAuth) GetCurrentUserID(context.Context) (uint64, error) {
if s.User == nil {
return 0, nil
}
return s.User.ID, nil
}
// VerifyToken returns the stub user.
func (s StubAuth) VerifyToken(context.Context, string) (*contracts.UserDTO, error) {
return s.User, nil
}
// CreateSession creates a stub session.
func (s StubAuth) CreateSession(context.Context, uint64, map[string]any) (string, error) {
return "", nil
}
func (s StubAuth) RevokeToken(context.Context, string) error { return nil }
// RevokeToken revokes a stub token.
func (s StubAuth) RevokeToken(context.Context, string) error { return nil }
// RevokeUserSessions revokes stub user sessions.
func (s StubAuth) RevokeUserSessions(context.Context, uint64) error { return nil }
func (s StubAuth) InvalidateCachedUser(context.Context, uint64) {}
func (s StubAuth) InvalidateCachedToken(context.Context, string) {}
// InvalidateCachedUser invalidates stub cached user.
func (s StubAuth) InvalidateCachedUser(context.Context, uint64) {}
// InvalidateCachedToken invalidates stub cached token.
func (s StubAuth) InvalidateCachedToken(context.Context, string) {}
func (s StubAuth) ListAuthSources(context.Context) ([]contracts.AuthSourceViewDTO, error) {
return s.Sources, nil
}
@@ -6,15 +6,21 @@
package testhelper
import (
"bytes"
"context"
"io"
"strconv"
"testing"
"time"
"Wavelet/core/contracts"
"Wavelet/openflare/plugins/server/kernel/model"
analyticsmodel "Wavelet/openflare/plugins/server/kernel/model/analytics"
"Wavelet/openflare/plugins/server/kernel/ofupload"
"Wavelet/openflare/plugins/server/kernel/repository"
"Wavelet/openflare/plugins/server/kernel/repository/logstore"
oftask "Wavelet/openflare/plugins/server/kernel/task"
"Wavelet/pkg/idgen"
db "Wavelet/plugins/infra/database"
"github.com/glebarez/sqlite"
"github.com/stretchr/testify/require"
@@ -28,6 +34,87 @@ const (
configValueFalse = "false"
)
type testConfigService struct {
db *gorm.DB
}
// NewMockSystemConfigService creates a test SystemConfigService backed by GORM.
func NewMockSystemConfigService(db *gorm.DB) contracts.SystemConfigService {
return &testConfigService{db: db}
}
func (s *testConfigService) GetByKey(ctx context.Context, key string) (contracts.SystemConfigDTO, error) {
var cfg contracts.SystemConfigDTO
err := s.db.WithContext(ctx).Table("w_system_configs").Where("key = ?", key).First(&cfg).Error
return cfg, err
}
func (s *testConfigService) ListByKeys(ctx context.Context, keys []string) (map[string]contracts.SystemConfigDTO, error) {
var cfgs []contracts.SystemConfigDTO
if err := s.db.WithContext(ctx).Table("w_system_configs").Where("key IN ?", keys).Find(&cfgs).Error; err != nil {
return nil, err
}
res := make(map[string]contracts.SystemConfigDTO, len(cfgs))
for _, c := range cfgs {
res[c.Key] = c
}
return res, nil
}
func (s *testConfigService) ListVisible(ctx context.Context) ([]contracts.SystemConfigDTO, error) {
var cfgs []contracts.SystemConfigDTO
err := s.db.WithContext(ctx).Table("w_system_configs").Where("visibility = ?", 1).Find(&cfgs).Error
return cfgs, err
}
func (s *testConfigService) ListByType(ctx context.Context, configType string) ([]contracts.SystemConfigDTO, error) {
var cfgs []contracts.SystemConfigDTO
err := s.db.WithContext(ctx).Table("w_system_configs").Where("type = ?", configType).Find(&cfgs).Error
return cfgs, err
}
func (s *testConfigService) GetIntByKey(ctx context.Context, key string) (int, error) {
var cfg contracts.SystemConfigDTO
if err := s.db.WithContext(ctx).Table("w_system_configs").Where("key = ?", key).First(&cfg).Error; err != nil {
return 0, err
}
return strconv.Atoi(cfg.Value)
}
func (s *testConfigService) GetBoolByKey(ctx context.Context, key string) (bool, error) {
var cfg contracts.SystemConfigDTO
if err := s.db.WithContext(ctx).Table("w_system_configs").Where("key = ?", key).First(&cfg).Error; err != nil {
return false, err
}
return strconv.ParseBool(cfg.Value)
}
func (s *testConfigService) SaveOrUpdate(ctx context.Context, key, value string) error {
var cfg model.SystemConfig
if err := s.db.WithContext(ctx).Table("w_system_configs").Where("key = ?", key).First(&cfg).Error; err != nil {
cfg = model.SystemConfig{Key: key, Value: value, Type: "system"}
return s.db.WithContext(ctx).Table("w_system_configs").Create(&cfg).Error
}
return s.db.WithContext(ctx).Table("w_system_configs").Where("key = ?", key).Update("value", value).Error
}
func (s *testConfigService) InvalidateCache(ctx context.Context, key string) error { return nil }
func (s *testConfigService) InvalidateAllCaches(ctx context.Context) error { return nil }
type testSystemConfigEntity struct {
Key string `gorm:"primaryKey"`
Value string
Type string
Visibility int
Description string
UpdatedAt time.Time
CreatedAt time.Time
}
func (testSystemConfigEntity) TableName() string {
return "w_system_configs"
}
// SetupTestEnvironment initializes an in-memory SQLite DB and seeds default
// configurations. Redis is no longer owned by OpenFlare.
func SetupTestEnvironment(t *testing.T) (*gorm.DB, any, func()) {
@@ -44,40 +131,133 @@ func SetupTestEnvironment(t *testing.T) (*gorm.DB, any, func()) {
}
err = sqliteDB.AutoMigrate(
&testSystemConfigEntity{},
&model.User{},
&model.AuthSource{},
&model.ExternalAccount{},
&model.SystemConfig{},
&model.AccessToken{},
&model.Upload{},
&model.UploadStat{},
&model.TaskExecution{},
&model.Template{},
&model.AccessToken{},
&model.Schedule{},
)
if err != nil {
t.Fatalf("failed to auto migrate tables: %v", err)
}
db.SetDB(sqliteDB)
repository.SetDBForTest(sqliteDB)
repository.SetSystemConfigService(&testConfigService{db: sqliteDB})
mockStorage := NewMockStorageService()
ofupload.SetStorage(mockStorage)
ofupload.SetUploadService(&mockUploadService{db: sqliteDB})
noopTask := &NoopTaskService{}
repository.SetTaskService(noopTask)
oftask.SetService(noopTask)
if err := idgen.Init(1); err != nil {
t.Fatalf("idgen.Init: %v", err)
}
seedDefaultConfigs(t, sqliteDB)
repository.ResetSystemConfigRAMCacheForTest()
cleanup := func() {
runExtraCleanups()
repository.StopSystemConfigCacheListener()
repository.ResetSystemConfigRAMCacheForTest()
repository.SetAuthService(nil)
repository.SetUserService(nil)
db.SetDB(nil)
repository.SetSystemConfigService(nil)
repository.SetTaskService(nil)
repository.SetDBForTest(nil)
ofupload.SetStorage(nil)
ofupload.SetUploadService(nil)
oftask.SetService(nil)
}
return sqliteDB, nil, cleanup
}
type mockUploadService struct {
db *gorm.DB
}
// NewMockUploadService creates a mock UploadService backed by GORM.
func NewMockUploadService(db *gorm.DB) contracts.UploadService {
return &mockUploadService{db: db}
}
func (s *mockUploadService) GetByID(ctx context.Context, id uint64) (*contracts.UploadDTO, error) {
var u contracts.UploadDTO
err := s.db.WithContext(ctx).Table("w_uploads").Where("id = ?", id).First(&u).Error
if err != nil {
return &contracts.UploadDTO{
ID: id,
Status: "used",
Type: "openflare_pages_deployment",
Size: 100,
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
}, nil
}
return &u, nil
}
func (s *mockUploadService) OpenStoredUpload(ctx context.Context, id uint64) (*contracts.OpenedUploadDTO, error) {
u, err := s.GetByID(ctx, id)
if err != nil {
return nil, err
}
body := io.ReadCloser(io.NopCloser(bytes.NewReader(nil)))
storage := ofupload.CurrentStorage()
if storage != nil {
if obj, err := storage.Get(ctx, u.FilePath); err == nil && obj != nil && obj.Body != nil {
body = obj.Body
} else if obj, err := storage.Get(ctx, u.FileName); err == nil && obj != nil && obj.Body != nil {
body = obj.Body
}
}
return &contracts.OpenedUploadDTO{
Upload: *u,
Body: body,
ContentType: u.MimeType,
ContentLength: u.Size,
}, nil
}
func (s *mockUploadService) Remove(ctx context.Context, id uint64) error {
if err := s.db.WithContext(ctx).Table("w_uploads").Where("id = ?", id).Update("status", "deleted").Error; err != nil {
return err
}
return s.RebuildStats(ctx)
}
func (s *mockUploadService) RemoveOwned(ctx context.Context, id uint64, userID uint64) error {
if err := s.db.WithContext(ctx).Table("w_uploads").Where("id = ? AND user_id = ?", id, userID).Update("status", "deleted").Error; err != nil {
return err
}
return s.RebuildStats(ctx)
}
func (s *mockUploadService) FindByHash(ctx context.Context, hash string, size int64) (*contracts.UploadDTO, error) {
var u contracts.UploadDTO
err := s.db.WithContext(ctx).Table("w_uploads").Where("hash = ? AND size = ?", hash, size).First(&u).Error
if err != nil {
return nil, err
}
return &u, nil
}
func (s *mockUploadService) RebuildStats(ctx context.Context) error {
var count int64
_ = s.db.WithContext(ctx).Table("w_uploads").Where("status != ?", "deleted").Count(&count).Error
var stat model.UploadStat
if err := s.db.WithContext(ctx).Table("w_upload_stats").Where("dimension = ?", model.UploadStatDimensionTotal).First(&stat).Error; err != nil {
stat = model.UploadStat{
Dimension: model.UploadStatDimensionTotal,
FileCount: int(count),
}
return s.db.WithContext(ctx).Table("w_upload_stats").Create(&stat).Error
}
stat.FileCount = int(count)
return s.db.WithContext(ctx).Table("w_upload_stats").Save(&stat).Error
}
func getSeedConfigsPart1() []model.SystemConfig {
return []model.SystemConfig{
{Key: model.ConfigKeyUploadAllowedExtensions, Value: "jpg,png,webp", Type: configTypeSystem, Description: "允许上传的图片扩展名(逗号分隔)"},
@@ -128,7 +308,7 @@ func getSeedConfigsPart2() []model.SystemConfig {
func seedDefaultConfigs(t *testing.T, tx *gorm.DB) {
t.Helper()
defaultConfigs := append(getSeedConfigsPart1(), getSeedConfigsPart2()...)
if err := tx.Create(&defaultConfigs).Error; err != nil {
if err := tx.Table("w_system_configs").Create(&defaultConfigs).Error; err != nil {
t.Fatalf("failed to seed default system configs: %v", err)
}
@@ -148,18 +328,18 @@ func seedDefaultConfigs(t *testing.T, tx *gorm.DB) {
model.ConfigKeySearchEngineIndexingEnabled,
model.ConfigKeyFileAccessWhitelist,
}
if err := tx.Model(&model.SystemConfig{}).
if err := tx.Table("w_system_configs").
Where("key IN ?", publicKeys).
Update("visibility", model.ConfigVisibilityVisible).Error; err != nil {
t.Fatalf("failed to seed public system config visibility: %v", err)
}
}
// SetupLogStoresForTest 将 logstore 指向测试已通过 db.SetDB 注入的 sqlite 库。
// SetupLogStoresForTest 将 logstore 指向测试已通过 SetDBForTest 注入的 sqlite 库。
func SetupLogStoresForTest(t *testing.T) {
t.Helper()
gdb := db.DB(context.Background())
gdb := repository.DB(context.Background())
require.NoError(t, idgen.Init(1))
require.NoError(t, gdb.AutoMigrate(
&analyticsmodel.NodeAccessLog{},