refactor: 将客服后端重构为宿主可嵌入模块
- 注入数据库、运行时配置、统一响应、文件存储和平台 AI 能力,补充业务读写工具与客户快捷操作契约。 - 移除模块内重复的组织、客户、工单、标签、技能、旧工作流、MCP 和迁移实现,将身份权限与业务主体交由宿主管理。 - 使用 libSQL 重构向量存储,并完善图片消息、访客身份、排队调度、企业微信和支持聊天页面。 - 统一 HTTP、DTO 与 WebSocket 的 snake_case 协议,补齐模块初始化、业务动作和公共载荷等回归测试。
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"code.tczkiot.com/wlw/ai-agent/internal/models"
|
||||
"code.tczkiot.com/wlw/ai-agent/internal/pkg/config"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/mlogclub/simple/sqls"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
func TestNewDialector(t *testing.T) {
|
||||
@@ -15,18 +19,15 @@ func TestNewDialector(t *testing.T) {
|
||||
dbType string
|
||||
want string
|
||||
}{
|
||||
{dbType: "sqlite", want: "sqlite"},
|
||||
{dbType: "mysql", want: "mysql"},
|
||||
{dbType: "postgres", want: "postgres"},
|
||||
{dbType: "postgresql", want: "postgres"},
|
||||
{dbType: " PostgreSQL ", want: "postgres"},
|
||||
}
|
||||
|
||||
for _, tt := range cases {
|
||||
tt := tt
|
||||
t.Run(tt.dbType, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
dialector, err := newDialector(config.DBConfig{Type: tt.dbType, DSN: ":memory:"})
|
||||
dialector, err := newDialector(config.DBConfig{Type: tt.dbType, DSN: "postgres-dsn"})
|
||||
if err != nil {
|
||||
t.Fatalf("newDialector() error = %v", err)
|
||||
}
|
||||
@@ -37,69 +38,42 @@ func TestNewDialector(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDialectorRejectsUnsupportedType(t *testing.T) {
|
||||
func TestNewDialectorRejectsRemovedAndUnsupportedTypes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if _, err := newDialector(config.DBConfig{Type: "oracle"}); err == nil {
|
||||
t.Fatal("newDialector() error = nil, want unsupported type error")
|
||||
for _, dbType := range []string{"sqlite", "mysql", "oracle", ""} {
|
||||
if _, err := newDialector(config.DBConfig{Type: dbType}); err == nil {
|
||||
t.Fatalf("newDialector(%q) error = nil, want unsupported type error", dbType)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteFilePath(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
dsn string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "plain relative path",
|
||||
dsn: "./data/app.db",
|
||||
want: "./data/app.db",
|
||||
},
|
||||
{
|
||||
name: "file uri with query",
|
||||
dsn: "file:./data/app.db?_busy_timeout=5000",
|
||||
want: "./data/app.db",
|
||||
},
|
||||
{
|
||||
name: "memory dsn",
|
||||
dsn: "file::memory:?cache=shared",
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "memory alias",
|
||||
dsn: ":memory:",
|
||||
want: "",
|
||||
},
|
||||
func TestUseDatabaseSharesConnectionWithoutChangingHostNaming(t *testing.T) {
|
||||
// SQLite remains only as a lightweight in-memory unit-test fixture. Runtime
|
||||
// database initialization accepts PostgreSQL exclusively.
|
||||
host, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{TablePrefix: "iot_"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("gorm.Open() error = %v", err)
|
||||
}
|
||||
hostTable := host.NamingStrategy.TableName("Conversation")
|
||||
|
||||
for _, tt := range cases {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if got := sqliteFilePath(tt.dsn); got != tt.want {
|
||||
t.Fatalf("sqliteFilePath(%q) = %q, want %q", tt.dsn, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureSQLiteDir(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
baseDir := t.TempDir()
|
||||
dbPath := filepath.Join(baseDir, "nested", "app.db")
|
||||
dsn := "file:" + dbPath + "?_busy_timeout=5000"
|
||||
|
||||
if err := ensureSQLiteDir(dsn); err != nil {
|
||||
t.Fatalf("ensureSQLiteDir() error = %v", err)
|
||||
}
|
||||
|
||||
if info, err := os.Stat(filepath.Dir(dbPath)); err != nil {
|
||||
t.Fatalf("os.Stat() error = %v", err)
|
||||
} else if !info.IsDir() {
|
||||
t.Fatalf("expected %q to be a directory", filepath.Dir(dbPath))
|
||||
if err := UseDatabase(host, "iot_ai_"); err != nil {
|
||||
t.Fatalf("UseDatabase() error = %v", err)
|
||||
}
|
||||
moduleDB := sqls.DB()
|
||||
statement := &gorm.Statement{DB: moduleDB}
|
||||
if err := statement.Parse(&models.Conversation{}); err != nil {
|
||||
t.Fatalf("Statement.Parse() error = %v", err)
|
||||
}
|
||||
if statement.Schema.Table != "iot_ai_conversation" {
|
||||
t.Fatalf("module table=%q want iot_ai_conversation", statement.Schema.Table)
|
||||
}
|
||||
if got := host.NamingStrategy.TableName("Conversation"); got != hostTable {
|
||||
t.Fatalf("host table changed from %q to %q", hostTable, got)
|
||||
}
|
||||
if moduleDB.ConnPool != host.ConnPool {
|
||||
t.Fatal("module database does not share the host connection pool")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user