重构:统一应用服务与基础设施适配边界

将账号、认证、管理员、分析、自动化、卡券、聊天、商品、关键词、设置和订单能力迁移到 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_*。
This commit is contained in:
Christ committed 2026-08-16 23:51:06 +08:00
1 parent 19a7d5f924
commit 375e84d663
239 files changed
+23870 -7871

No files matched your search

@@ -0,0 +1,154 @@
// Package defaultreply 定义默认回复配置用例及其消费者侧持久化 Port。
// 本包不依赖 HTTP、数据库模型或具体基础设施实现。
package defaultreply
import (
"context"
"errors"
)
// ErrInvalidUser 表示调用方没有提供有效的本地用户身份。
var ErrInvalidUser = errors.New("默认回复用户身份无效")
// ErrInvalidCookieID 表示调用方没有提供有效的账号标识。
var ErrInvalidCookieID = errors.New("默认回复账号标识无效")
// ErrAccountNotFound 表示目标账号不存在。
var ErrAccountNotFound = errors.New("默认回复账号不存在")
// ErrForbidden 表示目标账号存在但不属于当前用户。
var ErrForbidden = errors.New("无权操作该账号的默认回复")
// ErrConfigNotFound 表示账号存在但尚未保存默认回复配置。
var ErrConfigNotFound = errors.New("默认回复配置不存在")
// Reply 是默认回复配置的应用层模型,不携带数据库或 HTTP 细节。
type Reply struct {
// Enabled 表示是否启用默认回复。
Enabled bool
// ReplyContent 是默认回复使用的文字内容。
ReplyContent string
// ReplyImageURL 是默认回复使用的图片地址。
ReplyImageURL string
// ReplyOnce 表示同一聊天是否只发送一次默认回复。
ReplyOnce bool
}
// Summary 是按用户列出的默认回复配置及其账号标识。
type Summary struct {
// CookieID 是默认回复所属账号的稳定标识。
CookieID string
// Reply 是该账号的默认回复配置。
Reply Reply
}
// AccountOwnership 是账号所有权 Port 返回的非敏感结果。
type AccountOwnership struct {
// OwnerID 是账号所属本地用户标识;账号不存在时 Port 应返回 ErrAccountNotFound。
OwnerID int64
}
// Repository 定义默认回复用例所需的最小持久化能力。
type Repository interface {
// CheckOwnership 查询账号所有者,不读取或解密任何凭证字段。
CheckOwnership(ctx context.Context, userID int64, cookieID string) (AccountOwnership, error)
// Get 读取账号的默认回复配置;未配置时返回 ErrConfigNotFound。
Get(ctx context.Context, cookieID string) (Reply, error)
// Upsert 保存或覆盖账号的默认回复配置。
Upsert(ctx context.Context, cookieID string, reply Reply) error
// ListForUser 查询指定用户全部账号的默认回复配置。
ListForUser(ctx context.Context, userID int64) ([]Summary, error)
// Delete 删除账号的默认回复配置。
Delete(ctx context.Context, cookieID string) error
// ClearRecords 删除账号的默认回复投递记录。
ClearRecords(ctx context.Context, cookieID string) error
}
// Service 编排默认回复的用户归属校验、配置读写和记录清理。
type Service struct {
// repository 保存调用方注入的默认回复持久化 Port。
repository Repository
}
// NewService 创建默认回复应用服务。
func NewService(repository Repository) *Service {
// service 保存调用方提供的持久化 Port,实际调用时会校验其是否为空。
service := &Service{repository: repository}
return service
}
// Get 读取指定用户账号的默认回复配置,并区分账号不存在与尚未配置。
func (s *Service) Get(ctx context.Context, userID int64, cookieID string) (Reply, error) {
// err 表示输入、所有权或持久化查询失败的原因。
if err := s.ensureOwned(ctx, userID, cookieID); err != nil {
return Reply{}, err
}
return s.repository.Get(ctx, cookieID)
}
// Upsert 校验账号归属后保存默认回复配置。
func (s *Service) Upsert(ctx context.Context, userID int64, cookieID string, reply Reply) error {
// err 表示输入、所有权或持久化写入失败的原因。
if err := s.ensureOwned(ctx, userID, cookieID); err != nil {
return err
}
return s.repository.Upsert(ctx, cookieID, reply)
}
// List 查询指定用户拥有的全部默认回复配置。
func (s *Service) List(ctx context.Context, userID int64) ([]Summary, error) {
// err 表示服务装配或用户身份校验失败的原因。
if err := s.validateUser(userID); err != nil {
return nil, err
}
return s.repository.ListForUser(ctx, userID)
}
// Delete 校验账号归属后删除默认回复配置。
func (s *Service) Delete(ctx context.Context, userID int64, cookieID string) error {
// err 表示输入、所有权或持久化删除失败的原因。
if err := s.ensureOwned(ctx, userID, cookieID); err != nil {
return err
}
return s.repository.Delete(ctx, cookieID)
}
// ClearRecords 校验账号归属后清空默认回复投递记录。
func (s *Service) ClearRecords(ctx context.Context, userID int64, cookieID string) error {
// err 表示输入、所有权或持久化清理失败的原因。
if err := s.ensureOwned(ctx, userID, cookieID); err != nil {
return err
}
return s.repository.ClearRecords(ctx, cookieID)
}
// validateUser 检查应用服务依赖和用户身份,避免无效请求进入 Port。
func (s *Service) validateUser(userID int64) error {
if s == nil || s.repository == nil {
return errors.New("默认回复 repository 未初始化")
}
if userID <= 0 {
return ErrInvalidUser
}
return nil
}
// ensureOwned 统一执行账号标识校验和非敏感所有权判断。
func (s *Service) ensureOwned(ctx context.Context, userID int64, cookieID string) error {
// err 表示服务装配或用户身份校验失败的原因。
if err := s.validateUser(userID); err != nil {
return err
}
if cookieID == "" {
return ErrInvalidCookieID
}
// ownership 保存 Port 返回的账号所有者信息,不包含 Cookie 或登录秘密。
ownership, err := s.repository.CheckOwnership(ctx, userID, cookieID)
if err != nil {
return err
}
if ownership.OwnerID != userID {
return ErrForbidden
}
return nil
}
@@ -0,0 +1,180 @@
package defaultreply
import (
"context"
"errors"
"testing"
)
// fakeRepository 是默认回复服务测试使用的最小内存 Port。
type fakeRepository struct {
// ownership 保存模拟的账号所有权结果。
ownership AccountOwnership
// ownershipErr 保存模拟的所有权查询错误。
ownershipErr error
// reply 保存模拟的默认回复配置。
reply Reply
// getErr 保存模拟的默认回复查询错误。
getErr error
// listed 保存模拟的默认回复列表。
listed []Summary
// listErr 保存模拟的默认回复列表查询错误。
listErr error
// mutationErr 保存模拟的写入、删除或清理错误。
mutationErr error
// mutationCookieID 保存最近一次写操作的账号标识。
mutationCookieID string
}
// CheckOwnership 返回测试预设的账号所有权结果。
func (f *fakeRepository) CheckOwnership(context.Context, int64, string) (AccountOwnership, error) {
return f.ownership, f.ownershipErr
}
// Get 返回测试预设的默认回复配置或查询错误。
func (f *fakeRepository) Get(context.Context, string) (Reply, error) {
return f.reply, f.getErr
}
// Upsert 保存最近一次写入的账号标识并返回预设错误。
func (f *fakeRepository) Upsert(_ context.Context, cookieID string, _ Reply) error {
f.mutationCookieID = cookieID
return f.mutationErr
}
// ListForUser 返回测试预设的默认回复列表或查询错误。
func (f *fakeRepository) ListForUser(context.Context, int64) ([]Summary, error) {
return f.listed, f.listErr
}
// Delete 保存最近一次删除的账号标识并返回预设错误。
func (f *fakeRepository) Delete(_ context.Context, cookieID string) error {
f.mutationCookieID = cookieID
return f.mutationErr
}
// ClearRecords 保存最近一次清理的账号标识并返回预设错误。
func (f *fakeRepository) ClearRecords(_ context.Context, cookieID string) error {
f.mutationCookieID = cookieID
return f.mutationErr
}
// TestServiceOwnershipAndConfigErrors 验证服务区分跨用户、账号缺失和配置缺失。
func TestServiceOwnershipAndConfigErrors(t *testing.T) {
// ctx 是本测试所有应用调用使用的非取消上下文。
ctx := context.Background()
// accountMissing 是模拟账号不存在的持久化错误。
accountMissing := errors.New("账号查询失败")
// cases 是覆盖所有权和配置错误语义的表驱动场景。
cases := []struct {
// name 是场景名称。
name string
// repository 是当前场景使用的内存 Port。
repository *fakeRepository
// wantErr 是期望返回的稳定应用错误。
wantErr error
}{
{name: "cross-user", repository: &fakeRepository{ownership: AccountOwnership{OwnerID: 2}}, wantErr: ErrForbidden},
{name: "missing-account", repository: &fakeRepository{ownershipErr: ErrAccountNotFound}, wantErr: ErrAccountNotFound},
{name: "missing-config", repository: &fakeRepository{ownership: AccountOwnership{OwnerID: 1}, getErr: ErrConfigNotFound}, wantErr: ErrConfigNotFound},
{name: "storage-error", repository: &fakeRepository{ownership: AccountOwnership{OwnerID: 1}, getErr: accountMissing}, wantErr: accountMissing},
}
// item 是当前遍历的错误语义场景。
for _, item := range cases {
t.Run(item.name, func(t *testing.T) {
// service 是使用当前场景内存 Port 的应用服务。
service := NewService(item.repository)
// _, err 保存默认回复读取结果及错误。
_, err := service.Get(ctx, 1, "account-1")
if !errors.Is(err, item.wantErr) {
t.Fatalf("错误=%v,期望=%v", err, item.wantErr)
}
})
}
}
// TestServiceMutationsRequireOwnership 验证所有写操作均先执行账号归属校验。
func TestServiceMutationsRequireOwnership(t *testing.T) {
// ctx 是本测试所有应用调用使用的非取消上下文。
ctx := context.Background()
// repository 是账号归属当前用户的内存 Port。
repository := &fakeRepository{ownership: AccountOwnership{OwnerID: 7}}
// service 是使用内存 Port 的默认回复应用服务。
service := NewService(repository)
// reply 是待保存的默认回复应用模型。
reply := Reply{Enabled: true, ReplyContent: "你好", ReplyOnce: true}
// upsertErr 表示保存默认回复的执行结果。
upsertErr := service.Upsert(ctx, 7, "account-7", reply)
if upsertErr != nil || repository.mutationCookieID != "account-7" {
t.Fatalf("保存结果 err=%v cookie=%q", upsertErr, repository.mutationCookieID)
}
// deleteErr 表示删除默认回复的执行结果。
deleteErr := service.Delete(ctx, 7, "account-7")
if deleteErr != nil || repository.mutationCookieID != "account-7" {
t.Fatalf("删除结果 err=%v cookie=%q", deleteErr, repository.mutationCookieID)
}
// clearErr 表示清空投递记录的执行结果。
clearErr := service.ClearRecords(ctx, 7, "account-7")
if clearErr != nil || repository.mutationCookieID != "account-7" {
t.Fatalf("清理结果 err=%v cookie=%q", clearErr, repository.mutationCookieID)
}
// repository.ownership 保存跨用户归属结果,验证写操作不会继续调用 Port。
repository.ownership = AccountOwnership{OwnerID: 9}
// forbiddenErr 表示跨用户保存返回的错误。
forbiddenErr := service.Upsert(ctx, 7, "account-9", reply)
if !errors.Is(forbiddenErr, ErrForbidden) {
t.Fatalf("跨用户保存错误=%v,期望=%v", forbiddenErr, ErrForbidden)
}
}
// TestServiceInputValidation 验证无效服务依赖、用户和账号标识会被拒绝。
func TestServiceInputValidation(t *testing.T) {
// ctx 是本测试使用的非取消上下文。
ctx := context.Background()
// invalidCases 是需要在 Port 前拦截的输入场景。
invalidCases := []struct {
// name 是场景名称。
name string
// service 是待验证的应用服务实例。
service *Service
// userID 是当前场景使用的用户身份。
userID int64
// cookieID 是当前场景使用的账号标识。
cookieID string
// wantErr 是期望的稳定校验错误。
wantErr error
}{
{name: "nil-service", service: NewService(nil), userID: 1, cookieID: "a", wantErr: errors.New("默认回复 repository 未初始化")},
{name: "invalid-user", service: NewService(&fakeRepository{}), userID: 0, cookieID: "a", wantErr: ErrInvalidUser},
{name: "invalid-account", service: NewService(&fakeRepository{}), userID: 1, cookieID: "", wantErr: ErrInvalidCookieID},
}
// item 是当前遍历的输入校验场景。
for _, item := range invalidCases {
// _, err 保存当前场景的读取错误。
_, err := item.service.Get(ctx, item.userID, item.cookieID)
if item.name == "nil-service" {
if err == nil || err.Error() != item.wantErr.Error() {
t.Fatalf("场景=%s 错误=%v", item.name, err)
}
continue
}
if !errors.Is(err, item.wantErr) {
t.Fatalf("场景=%s 错误=%v,期望=%v", item.name, err, item.wantErr)
}
}
}
// TestServiceListReturnsApplicationSummaries 验证列表结果保持应用模型而不是数据库模型。
func TestServiceListReturnsApplicationSummaries(t *testing.T) {
// ctx 是本测试使用的非取消上下文。
ctx := context.Background()
// repository 是返回单条默认回复摘要的内存 Port。
repository := &fakeRepository{ownership: AccountOwnership{OwnerID: 3}, listed: []Summary{{CookieID: "account-3", Reply: Reply{Enabled: true, ReplyContent: "欢迎"}}}}
// result、err 保存列表查询结果及错误。
result, err := NewService(repository).List(ctx, 3)
if err != nil || len(result) != 1 || result[0].Reply.ReplyContent != "欢迎" {
t.Fatalf("列表结果=%+v err=%v", result, err)
}
}
var _ Repository = (*fakeRepository)(nil)