将账号、认证、管理员、分析、自动化、卡券、聊天、商品、关键词、设置和订单能力迁移到 application 与 adapter,删除 Server 内对应的旧仓储和业务服务实现,并收紧架构检查。 完善批量发布与订单刷新生命周期、凭证写回、账号任务补偿、订单 reconciliation、通知和敏感数据边界;补充正常、失败、取消、并发、越权及多方言回归测试,并修正 MySQL 延迟任务表的 InnoDB 配置。 修复空卡密响应为 null 时前端加载失败的问题,重建嵌入式前端资产;重写重构总计划,清除历史流水并明确阶段 1 至 10 各对应一次完整迭代和一个 PR。 验证:make check;make test-server-race;npm test/typecheck/comments:check/build;Docker SQLite/MySQL 8.4/PostgreSQL 17 TestMultiDB_*。
105 lines
4.8 KiB
Go
105 lines
4.8 KiB
Go
package adapter
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"testing"
|
||
|
||
automationapp "xianyu-go/internal/application/automation"
|
||
"xianyu-go/internal/db"
|
||
)
|
||
|
||
// TestAutomationRepositoryOwnershipMapsNotFound 验证资源缺失只表现为不归属,不泄露数据库模型错误。
|
||
func TestAutomationRepositoryOwnershipMapsNotFound(t *testing.T) {
|
||
// store 是当前适配器测试使用的 SQLite 存储。
|
||
store, cleanup := newAdapterTestStore(t)
|
||
defer cleanup()
|
||
// repository 是绑定 SQLite 存储的自动化规则适配器。
|
||
repository := NewAutomationRepository(store)
|
||
// ctx 是本测试共用的非取消上下文。
|
||
ctx := context.Background()
|
||
// owned、ownedErr 保存不存在商品的归属判断结果。
|
||
owned, ownedErr := repository.OwnsItem(ctx, 1, "cid", "missing-item")
|
||
if owned || ownedErr != nil {
|
||
t.Fatalf("缺失商品应返回 false,nil,owned=%v err=%v", owned, ownedErr)
|
||
}
|
||
// cardInfo、cardErr 保存不存在卡密组的最小应用摘要及错误。
|
||
cardInfo, cardErr := repository.GetCard(ctx, 1, 999999)
|
||
if cardInfo != (automationapp.CardInfo{}) || !errors.Is(cardErr, automationapp.ErrRuleNotFound) {
|
||
t.Fatalf("缺失卡密组应映射为应用未找到,info=%+v err=%v", cardInfo, cardErr)
|
||
}
|
||
}
|
||
|
||
// TestAutomationRepositoryOwnershipPropagatesDatabaseErrors 验证数据库不可用时不会伪装成归属失败。
|
||
func TestAutomationRepositoryOwnershipPropagatesDatabaseErrors(t *testing.T) {
|
||
// store 是当前适配器测试使用的 SQLite 存储。
|
||
store, cleanup := newAdapterTestStore(t)
|
||
defer cleanup()
|
||
// repository 是绑定 SQLite 存储的自动化规则适配器。
|
||
repository := NewAutomationRepository(store)
|
||
// ctx 是数据库关闭后调用适配器的非取消上下文。
|
||
ctx := context.Background()
|
||
// closeErr 表示提前关闭测试数据库时的资源释放错误。
|
||
closeErr := store.DB.Close()
|
||
if closeErr != nil {
|
||
t.Fatal(closeErr)
|
||
}
|
||
// _, itemErr 保存商品归属查询透传的数据库错误。
|
||
_, itemErr := repository.OwnsItem(ctx, 1, "cid", "item-1")
|
||
if itemErr == nil {
|
||
t.Fatal("数据库关闭后商品归属查询应返回错误")
|
||
}
|
||
// _, cardErr 保存卡密组查询透传的数据库错误。
|
||
_, cardErr := repository.GetCard(ctx, 1, 1)
|
||
if cardErr == nil || errors.Is(cardErr, db.ErrNotFound) || errors.Is(cardErr, automationapp.ErrRuleNotFound) {
|
||
t.Fatalf("数据库关闭后卡密查询应保留底层错误,err=%v", cardErr)
|
||
}
|
||
}
|
||
|
||
// TestAutomationRepositoryEnsurePublishRuleIsIdempotent 验证发布自动化规则只创建一次。
|
||
func TestAutomationRepositoryEnsurePublishRuleIsIdempotent(t *testing.T) {
|
||
// store 是当前适配器测试使用的 SQLite 存储。
|
||
store, cleanup := newAdapterTestStore(t)
|
||
defer cleanup()
|
||
// repository 是绑定 SQLite 存储的自动化规则适配器。
|
||
repository := NewAutomationRepository(store)
|
||
// ctx 是本测试共用的非取消上下文。
|
||
ctx := context.Background()
|
||
// input 是代表发布成功后自动发货的规则输入。
|
||
input := automationapp.RuleInput{UserID: 1, CookieID: "cid", ItemID: "item-1", Name: "付款后自动发货 - 商品", TriggerType: automationapp.TriggerOrderPaid, Enabled: true, Priority: 100, ConfigJSON: "{}", Actions: []automationapp.ActionInput{{ActionType: automationapp.ActionConfirmShipment, Enabled: true, SortOrder: 1}}}
|
||
// firstErr 保存第一次幂等准备的错误。
|
||
firstErr := repository.EnsurePublishRule(ctx, input)
|
||
if firstErr != nil {
|
||
t.Fatal(firstErr)
|
||
}
|
||
// secondErr 保存第二次幂等准备的错误。
|
||
secondErr := repository.EnsurePublishRule(ctx, input)
|
||
if secondErr != nil {
|
||
t.Fatal(secondErr)
|
||
}
|
||
// rules、matchErr 保存按发布规则唯一条件查询到的规则。
|
||
rules, matchErr := store.Automation.Match(ctx, input.CookieID, input.ItemID, input.TriggerType)
|
||
if matchErr != nil || len(rules) != 1 {
|
||
t.Fatalf("幂等准备应只保留一条规则,rules=%+v err=%v", rules, matchErr)
|
||
}
|
||
}
|
||
|
||
// TestAutomationRepositoryEnsurePublishRulePropagatesDatabaseError 验证数据库故障不会被发布规则适配器吞掉。
|
||
func TestAutomationRepositoryEnsurePublishRulePropagatesDatabaseError(t *testing.T) {
|
||
// store 是当前适配器测试使用的 SQLite 存储。
|
||
store, cleanup := newAdapterTestStore(t)
|
||
defer cleanup()
|
||
// repository 是绑定 SQLite 存储的自动化规则适配器。
|
||
repository := NewAutomationRepository(store)
|
||
// closeErr 表示提前关闭测试数据库时的资源释放错误。
|
||
closeErr := store.DB.Close()
|
||
if closeErr != nil {
|
||
t.Fatal(closeErr)
|
||
}
|
||
// err 保存关闭数据库后执行幂等准备的错误。
|
||
err := repository.EnsurePublishRule(context.Background(), automationapp.RuleInput{UserID: 1, CookieID: "account-1", ItemID: "item-1", Name: "规则", TriggerType: automationapp.TriggerOrderPaid})
|
||
if err == nil {
|
||
t.Fatal("数据库关闭后应返回发布规则持久化错误")
|
||
}
|
||
}
|