1
0
Files
nex/backend/tests/helpers.go
lanyuanxiaoyao 395887667d feat: 实现统一模型 ID 机制
实现统一模型 ID 格式 (provider_id/model_name),支持跨协议模型标识和 Smart Passthrough。

核心变更:
- 新增 pkg/modelid 包:解析、格式化、校验统一模型 ID
- 数据库迁移:models 表使用 UUID 主键 + UNIQUE(provider_id, model_name) 约束
- Repository 层:FindByProviderAndModelName、ListEnabled 方法
- Service 层:联合唯一校验、provider ID 字符集校验
- Conversion 层:ExtractModelName、RewriteRequestModelName/RewriteResponseModelName 方法
- Handler 层:统一模型 ID 路由、Smart Passthrough、Models API 本地聚合
- 新增 error-responses、unified-model-id 规范

测试覆盖:
- 单元测试:modelid、conversion、handler、service、repository
- 集成测试:统一模型 ID 路由、Smart Passthrough 保真性、跨协议转换
- 迁移测试:UUID 主键、UNIQUE 约束、级联删除

OpenSpec:
- 归档 unified-model-id 变更到 archive/2026-04-21-unified-model-id
- 同步 11 个 delta specs 到 main specs
- 新增 error-responses、unified-model-id 规范文件
2026-04-21 18:14:10 +08:00

85 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package tests
import (
"fmt"
"testing"
"time"
"nex/backend/internal/config"
"github.com/stretchr/testify/assert"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// SetupTestDB initializes an in-memory SQLite database with auto-migration.
// Uses :memory: mode with MaxOpenConns(1) to ensure all operations share the
// same connection, avoiding "database is closed" errors from connection pool.
// Enables foreign key constraints for SQLite.
func SetupTestDB(t *testing.T) *gorm.DB {
t.Helper()
db, err := gorm.Open(sqlite.Open(":memory:?_foreign_keys=on"), &gorm.Config{})
assert.NoError(t, err, "failed to open test database")
// 限制为单连接,确保 :memory: 数据库不被连接池丢弃
sqlDB, err := db.DB()
assert.NoError(t, err, "failed to get underlying sql.DB")
sqlDB.SetMaxOpenConns(1)
sqlDB.SetConnMaxLifetime(0)
err = db.AutoMigrate(&config.Provider{}, &config.Model{}, &config.UsageStats{})
assert.NoError(t, err, "failed to auto-migrate test database")
return db
}
// CleanupTestDB closes the database after a brief delay to allow async
// goroutines (e.g. stats recording) to finish.
func CleanupTestDB(t *testing.T, db *gorm.DB) {
t.Helper()
// 等待异步 goroutine如 statsService.Record完成
time.Sleep(50 * time.Millisecond)
sqlDB, err := db.DB()
assert.NoError(t, err, "failed to get underlying sql.DB")
err = sqlDB.Close()
assert.NoError(t, err, "failed to close test database")
}
// CreateTestProvider creates a test provider and returns it.
func CreateTestProvider(t *testing.T, db *gorm.DB, id string) config.Provider {
t.Helper()
provider := config.Provider{
ID: id,
Name: fmt.Sprintf("test-provider-%s", id),
APIKey: fmt.Sprintf("test-api-key-%s", id),
BaseURL: fmt.Sprintf("https://api.test-%s.com", id),
Enabled: true,
}
err := db.Create(&provider).Error
assert.NoError(t, err, "failed to create test provider")
return provider
}
// CreateTestModel creates a test model and returns it.
// Does NOT assert on error - returns the model and error for caller to verify.
func CreateTestModel(t *testing.T, db *gorm.DB, id string, providerID string, modelName string) (config.Model, error) {
t.Helper()
model := config.Model{
ID: id,
ProviderID: providerID,
ModelName: modelName,
Enabled: true,
}
err := db.Create(&model).Error
return model, err
}