重构:统一应用服务与基础设施适配边界
将账号、认证、管理员、分析、自动化、卡券、聊天、商品、关键词、设置和订单能力迁移到 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:
1 parent
19a7d5f924
commit
375e84d663
239 files changed
+23870
-7871
No files matched your search
@@ -77,13 +77,18 @@ type ImageUploader interface {
|
||||
}
|
||||
|
||||
// NewWithSending 创建同时支持历史查询和实时发送的聊天应用服务。
|
||||
func NewWithSending(repository Repository, outgoing OutgoingRepository, senders SenderProvider, uploader ImageUploader) *Service {
|
||||
return &Service{
|
||||
func NewWithSending(repository Repository, outgoing OutgoingRepository, senders SenderProvider, uploader ImageUploader, identity ...IdentityResolver) *Service {
|
||||
// service 保存聊天历史、发送和平台身份能力的统一应用服务。
|
||||
service := &Service{
|
||||
repository: repository,
|
||||
outgoing: outgoing,
|
||||
senders: senders,
|
||||
uploader: uploader,
|
||||
}
|
||||
if len(identity) > 0 {
|
||||
service.identityResolver = identity[0]
|
||||
}
|
||||
return service
|
||||
}
|
||||
|
||||
// SendText 创建并发送一条文字消息,失败时尽力保留本地 failed 状态。
|
||||
|
||||
@@ -5,11 +5,16 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ErrInvalidInput 表示聊天历史查询缺少必要的非敏感标识。
|
||||
var ErrInvalidInput = errors.New("聊天历史查询参数无效")
|
||||
|
||||
// ErrSessionUnavailable 表示会话清理、归属或身份持久化端口未装配。
|
||||
var ErrSessionUnavailable = errors.New("聊天会话服务未启用")
|
||||
|
||||
// Message 是聊天历史用例对外暴露的非敏感消息模型。
|
||||
type Message struct {
|
||||
// ID 是本地消息主键。
|
||||
@@ -60,6 +65,21 @@ type Session struct {
|
||||
UnreadCount int
|
||||
}
|
||||
|
||||
// Identity 是平台身份查询返回的非敏感展示信息。
|
||||
type Identity struct {
|
||||
// BuyerName 是平台返回的买家展示名称。
|
||||
BuyerName string
|
||||
// BuyerAvatar 是平台返回的买家头像地址。
|
||||
BuyerAvatar string
|
||||
}
|
||||
|
||||
// IdentityResolver 定义聊天应用获取平台会话展示身份的最小能力。
|
||||
// 凭证读取、平台请求和凭证刷新均由适配器内部完成,应用层只接收展示字段。
|
||||
type IdentityResolver interface {
|
||||
// Resolve 根据账号和聊天会话查询非敏感的买家展示身份。
|
||||
Resolve(ctx context.Context, accountID, chatID string) (Identity, error)
|
||||
}
|
||||
|
||||
// Page 是聊天历史查询的分页结果。
|
||||
type Page struct {
|
||||
// Messages 是按时间正序排列的当前页消息。
|
||||
@@ -78,6 +98,20 @@ type Repository interface {
|
||||
ListSessions(ctx context.Context, userID int64, accountID string, limit int) ([]Session, error)
|
||||
}
|
||||
|
||||
// SessionRepository 定义会话列表之外的清理、身份写入和账号归属能力。
|
||||
type SessionRepository interface {
|
||||
// Repository 提供聊天消息和会话列表查询能力。
|
||||
Repository
|
||||
// DeleteEmptySessions 删除指定账号中没有有效消息的空会话壳。
|
||||
DeleteEmptySessions(ctx context.Context, accountID string) error
|
||||
// UpdateSessionIdentity 更新会话的买家展示名称和头像。
|
||||
UpdateSessionIdentity(ctx context.Context, accountID, chatID, buyerID, buyerName, buyerAvatar string) error
|
||||
// ExistsOwned 判断账号是否归属于指定用户,只返回存在性,不返回敏感字段。
|
||||
ExistsOwned(ctx context.Context, userID int64, accountID string) (bool, error)
|
||||
// MarkRead 将指定用户拥有的会话未读数归零。
|
||||
MarkRead(ctx context.Context, userID int64, accountID, chatID string) error
|
||||
}
|
||||
|
||||
// Service 编排聊天历史查询,不持有 HTTP 请求或数据库连接。
|
||||
type Service struct {
|
||||
// repository 保存由调用方注入的最小持久化端口。
|
||||
@@ -88,6 +122,8 @@ type Service struct {
|
||||
senders SenderProvider
|
||||
// uploader 保存图片上传的平台适配端口。
|
||||
uploader ImageUploader
|
||||
// identityResolver 保存平台身份查询端口,凭证只在适配器内部短暂存在。
|
||||
identityResolver IdentityResolver
|
||||
}
|
||||
|
||||
// New 创建聊天历史应用服务;空端口会导致构造结果不可用。
|
||||
@@ -95,6 +131,11 @@ func New(repository Repository) *Service {
|
||||
return &Service{repository: repository}
|
||||
}
|
||||
|
||||
// NewWithIdentity 创建支持平台会话身份补全的聊天应用服务。
|
||||
func NewWithIdentity(repository Repository, resolver IdentityResolver) *Service {
|
||||
return &Service{repository: repository, identityResolver: resolver}
|
||||
}
|
||||
|
||||
// ListStoredMessages 查询当前用户有权访问的本地聊天历史。
|
||||
// userID 用于归属过滤,accountID/chatID 定位账号和会话,beforeID 控制向更早消息翻页,limit 控制页大小;
|
||||
// 返回的 Page 只含非敏感消息和会话摘要,底层端口错误原样返回。
|
||||
@@ -123,3 +164,176 @@ func (s *Service) ListStoredMessages(ctx context.Context, userID int64, accountI
|
||||
}
|
||||
return Page{Messages: messages, Session: session, HasMore: len(messages) == limit}, nil
|
||||
}
|
||||
|
||||
// ListSessions 查询当前用户有权访问的账号会话摘要。
|
||||
func (s *Service) ListSessions(ctx context.Context, userID int64, accountID string, limit int) ([]Session, error) {
|
||||
accountID = strings.TrimSpace(accountID)
|
||||
if s == nil || s.repository == nil || userID <= 0 || accountID == "" {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
// sessions 和 err 保存带用户归属的会话摘要及查询错误。
|
||||
sessions, err := s.repository.ListSessions(ctx, userID, accountID, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sessions, nil
|
||||
}
|
||||
|
||||
// FindSession 查询指定账号下的单个会话;找不到时返回零值且不视为错误。
|
||||
func (s *Service) FindSession(ctx context.Context, userID int64, accountID, chatID string) (Session, error) {
|
||||
chatID = strings.TrimSpace(chatID)
|
||||
if chatID == "" {
|
||||
return Session{}, ErrInvalidInput
|
||||
}
|
||||
// sessions 和 err 保存会话摘要列表及查询错误。
|
||||
sessions, err := s.ListSessions(ctx, userID, accountID, 500)
|
||||
if err != nil {
|
||||
return Session{}, err
|
||||
}
|
||||
// session 保存匹配到的会话摘要。
|
||||
var session Session
|
||||
// candidate 表示当前遍历到的会话摘要。
|
||||
for _, candidate := range sessions {
|
||||
if candidate.ChatID == chatID {
|
||||
session = candidate
|
||||
break
|
||||
}
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// CleanupEmptySessions 清理平台分页产生的无效空会话壳。
|
||||
func (s *Service) CleanupEmptySessions(ctx context.Context, accountID string) error {
|
||||
accountID = strings.TrimSpace(accountID)
|
||||
if s == nil || s.repository == nil || accountID == "" {
|
||||
return ErrInvalidInput
|
||||
}
|
||||
// repository 保存支持会话维护操作的窄端口。
|
||||
repository, ok := s.repository.(SessionRepository)
|
||||
if !ok {
|
||||
return ErrSessionUnavailable
|
||||
}
|
||||
return repository.DeleteEmptySessions(ctx, accountID)
|
||||
}
|
||||
|
||||
// OwnsAccount 查询当前用户是否拥有指定账号,不读取或解密账号凭证。
|
||||
func (s *Service) OwnsAccount(ctx context.Context, userID int64, accountID string) (bool, error) {
|
||||
accountID = strings.TrimSpace(accountID)
|
||||
if s == nil || s.repository == nil || userID <= 0 || accountID == "" {
|
||||
return false, ErrInvalidInput
|
||||
}
|
||||
// repository 保存支持账号归属查询的窄端口。
|
||||
repository, ok := s.repository.(SessionRepository)
|
||||
if !ok {
|
||||
return false, ErrSessionUnavailable
|
||||
}
|
||||
return repository.ExistsOwned(ctx, userID, accountID)
|
||||
}
|
||||
|
||||
// MarkRead 将当前用户拥有的指定会话标记为已读;底层端口只接收非敏感标识。
|
||||
func (s *Service) MarkRead(ctx context.Context, userID int64, accountID, chatID string) error {
|
||||
accountID = strings.TrimSpace(accountID)
|
||||
chatID = strings.TrimSpace(chatID)
|
||||
if s == nil || s.repository == nil || userID <= 0 || accountID == "" || chatID == "" {
|
||||
return ErrInvalidInput
|
||||
}
|
||||
// repository 保存支持会话已读更新的窄端口。
|
||||
repository, ok := s.repository.(SessionRepository)
|
||||
if !ok {
|
||||
return ErrSessionUnavailable
|
||||
}
|
||||
return repository.MarkRead(ctx, userID, accountID, chatID)
|
||||
}
|
||||
|
||||
// ResolveSessionIdentity 补全单个会话展示身份并尽力保存到本地。
|
||||
// 平台查询错误会原样返回,但已获得的会话摘要仍会返回给调用方。
|
||||
func (s *Service) ResolveSessionIdentity(ctx context.Context, session Session) (Session, error) {
|
||||
if s == nil || s.repository == nil || strings.TrimSpace(session.AccountID) == "" || strings.TrimSpace(session.ChatID) == "" {
|
||||
return session, ErrInvalidInput
|
||||
}
|
||||
// resolveErr 保存平台身份查询失败,供 HTTP 层决定是否触发会话恢复。
|
||||
var resolveErr error
|
||||
if session.BuyerID != "1400" && s.identityResolver != nil {
|
||||
// identity 和 err 保存平台适配器返回的非敏感身份及调用错误。
|
||||
identity, err := s.identityResolver.Resolve(ctx, session.AccountID, session.ChatID)
|
||||
if err != nil {
|
||||
resolveErr = err
|
||||
} else {
|
||||
// name 是去除空白后的平台买家名称。
|
||||
if name := strings.TrimSpace(identity.BuyerName); name != "" {
|
||||
session.BuyerName = name
|
||||
}
|
||||
// avatar 是去除空白后的平台买家头像地址。
|
||||
if avatar := strings.TrimSpace(identity.BuyerAvatar); avatar != "" {
|
||||
session.BuyerAvatar = avatar
|
||||
}
|
||||
}
|
||||
}
|
||||
// repository 保存会话身份更新所需的窄端口。
|
||||
if repository, ok := s.repository.(SessionRepository); ok {
|
||||
// _ 表示身份缓存更新失败不应覆盖旧 handler 的展示容错语义。
|
||||
_ = repository.UpdateSessionIdentity(ctx, session.AccountID, session.ChatID, session.BuyerID, session.BuyerName, session.BuyerAvatar)
|
||||
}
|
||||
return session, resolveErr
|
||||
}
|
||||
|
||||
// RefreshSessionIdentities 并发补全会话列表身份,保留首个失败以供调用方处理过期会话。
|
||||
func (s *Service) RefreshSessionIdentities(ctx context.Context, accountID string, sessions []Session) ([]Session, error) {
|
||||
accountID = strings.TrimSpace(accountID)
|
||||
if s == nil || s.repository == nil || accountID == "" {
|
||||
return sessions, ErrInvalidInput
|
||||
}
|
||||
if s.identityResolver == nil {
|
||||
return sessions, nil
|
||||
}
|
||||
// result 复制输入列表,避免异步身份补全修改 handler 持有的外部切片。
|
||||
result := append([]Session(nil), sessions...)
|
||||
// jobs 保存待补全的会话下标。
|
||||
jobs := make(chan int)
|
||||
// workers 保存身份补全工作器的完成状态。
|
||||
var workers sync.WaitGroup
|
||||
// once 保证只记录第一个平台查询错误。
|
||||
var once sync.Once
|
||||
// firstErr 保存第一个平台查询错误。
|
||||
var firstErr error
|
||||
// workerCount 是固定的并发度,避免单个账号的联系人数量放大 goroutine 数量。
|
||||
workerCount := 8
|
||||
// worker 表示当前启动的身份补全工作器序号。
|
||||
for worker := 0; worker < workerCount; worker++ {
|
||||
workers.Add(1)
|
||||
go func() {
|
||||
defer workers.Done()
|
||||
// index 表示当前待处理会话在结果切片中的下标。
|
||||
for index := range jobs {
|
||||
// identityCtx 和 cancel 限制单个联系人平台查询的最长时间。
|
||||
identityCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
// updated 和 err 保存身份补全后的会话及平台查询错误。
|
||||
updated, err := s.ResolveSessionIdentity(identityCtx, result[index])
|
||||
cancel()
|
||||
result[index] = updated
|
||||
if err != nil {
|
||||
once.Do(func() { firstErr = err })
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
// queueDone 表示是否因为父上下文取消而提前停止投递。
|
||||
queueDone := false
|
||||
// index 表示当前排队会话在结果切片中的下标。
|
||||
for index := range result {
|
||||
if result[index].BuyerID == "1400" {
|
||||
continue
|
||||
}
|
||||
select {
|
||||
case jobs <- index:
|
||||
case <-ctx.Done():
|
||||
queueDone = true
|
||||
}
|
||||
if queueDone {
|
||||
break
|
||||
}
|
||||
}
|
||||
close(jobs)
|
||||
workers.Wait()
|
||||
return result, firstErr
|
||||
}
|
||||
@@ -20,6 +20,20 @@ type fakeRepository struct {
|
||||
userID int64
|
||||
// allowedUserID 是允许读取测试账号的用户 ID;其他用户模拟归属拒绝。
|
||||
allowedUserID int64
|
||||
// deleteErr 是模拟空会话清理失败的错误。
|
||||
deleteErr error
|
||||
// updateErr 是模拟会话身份缓存写入失败的错误。
|
||||
updateErr error
|
||||
// owned 是模拟账号归属查询结果。
|
||||
owned bool
|
||||
// ownedErr 是模拟账号归属查询失败的错误。
|
||||
ownedErr error
|
||||
// updatedSessions 保存最近写入的会话身份,供断言应用端口调用参数。
|
||||
updatedSessions []Session
|
||||
// markReadErr 表示会话已读状态更新需要返回的错误。
|
||||
markReadErr error
|
||||
// markReadCalls 记录会话已读状态更新次数,避免测试误判未调用端口。
|
||||
markReadCalls int
|
||||
}
|
||||
|
||||
// ListMessages 返回测试消息,并记录用户归属参数。
|
||||
@@ -36,6 +50,54 @@ func (r *fakeRepository) ListSessions(_ context.Context, _ int64, _ string, _ in
|
||||
return r.sessions, r.sessionErr
|
||||
}
|
||||
|
||||
// DeleteEmptySessions 返回预设清理结果并记录端口已被调用。
|
||||
func (r *fakeRepository) DeleteEmptySessions(_ context.Context, _ string) error {
|
||||
return r.deleteErr
|
||||
}
|
||||
|
||||
// UpdateSessionIdentity 记录应用层请求保存的会话身份。
|
||||
func (r *fakeRepository) UpdateSessionIdentity(_ context.Context, accountID, chatID, buyerID, buyerName, buyerAvatar string) error {
|
||||
r.updatedSessions = append(r.updatedSessions, Session{AccountID: accountID, ChatID: chatID, BuyerID: buyerID, BuyerName: buyerName, BuyerAvatar: buyerAvatar})
|
||||
return r.updateErr
|
||||
}
|
||||
|
||||
// ExistsOwned 返回预设账号归属结果。
|
||||
func (r *fakeRepository) ExistsOwned(_ context.Context, _ int64, _ string) (bool, error) {
|
||||
return r.owned, r.ownedErr
|
||||
}
|
||||
|
||||
// MarkRead 记录会话已读更新并返回预设错误。
|
||||
func (r *fakeRepository) MarkRead(_ context.Context, _ int64, _, _ string) error {
|
||||
r.markReadCalls++
|
||||
return r.markReadErr
|
||||
}
|
||||
|
||||
// fakeIdentityResolver 返回预设平台展示身份,不接触真实凭证或网络。
|
||||
type fakeIdentityResolver struct {
|
||||
// identity 是模拟平台返回的展示身份。
|
||||
identity Identity
|
||||
// err 是模拟平台身份查询失败的错误。
|
||||
err error
|
||||
}
|
||||
|
||||
// historyOnlyRepository 仅实现历史读取端口,用于验证可选会话维护能力缺失时的错误。
|
||||
type historyOnlyRepository struct{}
|
||||
|
||||
// ListMessages 返回空历史,满足历史读取端口的测试替身契约。
|
||||
func (historyOnlyRepository) ListMessages(context.Context, int64, string, string, int64, int) ([]Message, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// ListSessions 返回空会话列表,满足历史读取端口的测试替身契约。
|
||||
func (historyOnlyRepository) ListSessions(context.Context, int64, string, int) ([]Session, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Resolve 返回预设的非敏感身份结果。
|
||||
func (r fakeIdentityResolver) Resolve(_ context.Context, _, _ string) (Identity, error) {
|
||||
return r.identity, r.err
|
||||
}
|
||||
|
||||
// TestListStoredMessagesUsesUserScopedPort 验证查询会把用户 ID 传到归属端口并组装分页结果。
|
||||
func TestListStoredMessagesUsesUserScopedPort(t *testing.T) {
|
||||
// repository 是带有一条消息和会话摘要的测试端口。
|
||||
@@ -58,7 +120,7 @@ func TestListStoredMessagesUsesUserScopedPort(t *testing.T) {
|
||||
// TestListStoredMessagesRejectsInvalidIdentity 验证无效用户或账号标识会在访问端口前失败。
|
||||
func TestListStoredMessagesRejectsInvalidIdentity(t *testing.T) {
|
||||
// service 是不应被调用的测试服务。
|
||||
service := New(&fakeRepository{})
|
||||
service := New(historyOnlyRepository{})
|
||||
// testCase 表示当前待验证的无效身份场景。
|
||||
for _, testCase := range []struct {
|
||||
// name 是当前无效输入场景名称。
|
||||
@@ -132,3 +194,91 @@ func TestListStoredMessagesDoesNotCrossUserBoundary(t *testing.T) {
|
||||
t.Fatalf("cross-user error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSessionPortCoversCleanupOwnershipAndIdentity 验证会话维护和身份补全都通过应用端口完成。
|
||||
func TestSessionPortCoversCleanupOwnershipAndIdentity(t *testing.T) {
|
||||
// repository 是记录端口调用的测试仓储。
|
||||
repository := &fakeRepository{owned: true}
|
||||
// service 是带平台身份替身的聊天应用服务。
|
||||
service := NewWithIdentity(repository, fakeIdentityResolver{identity: Identity{BuyerName: "买家新名", BuyerAvatar: "avatar-new"}})
|
||||
// err 表示清理空会话时的应用层错误。
|
||||
if err := service.CleanupEmptySessions(context.Background(), "account-1"); err != nil {
|
||||
t.Fatalf("CleanupEmptySessions() error = %v", err)
|
||||
}
|
||||
// owned 和 err 保存账号归属端口结果。
|
||||
owned, err := service.OwnsAccount(context.Background(), 8, "account-1")
|
||||
if err != nil || !owned {
|
||||
t.Fatalf("OwnsAccount() = %v, %v", owned, err)
|
||||
}
|
||||
// resolved 和 resolveErr 保存身份补全后的会话及平台错误。
|
||||
resolved, resolveErr := service.ResolveSessionIdentity(context.Background(), Session{AccountID: "account-1", ChatID: "chat-1", BuyerID: "buyer-1"})
|
||||
if resolveErr != nil || resolved.BuyerName != "买家新名" || len(repository.updatedSessions) != 1 {
|
||||
t.Fatalf("ResolveSessionIdentity() = %+v, %v; updates=%+v", resolved, resolveErr, repository.updatedSessions)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSessionPortPreservesIdentityErrorAndCachedSession 验证平台身份失败时仍保留会话并返回错误供恢复流程处理。
|
||||
func TestSessionPortPreservesIdentityErrorAndCachedSession(t *testing.T) {
|
||||
// wantErr 是模拟平台会话失效的错误。
|
||||
wantErr := errors.New("session expired")
|
||||
// repository 是接收身份缓存写入的测试仓储。
|
||||
repository := &fakeRepository{}
|
||||
// service 是返回平台错误的聊天应用服务。
|
||||
service := NewWithIdentity(repository, fakeIdentityResolver{err: wantErr})
|
||||
// session 和 err 保存身份失败后的会话及错误。
|
||||
session, err := service.ResolveSessionIdentity(context.Background(), Session{AccountID: "account-1", ChatID: "chat-1", BuyerID: "buyer-1", BuyerName: "旧名称"})
|
||||
if !errors.Is(err, wantErr) || session.BuyerName != "旧名称" || len(repository.updatedSessions) != 1 {
|
||||
t.Fatalf("session=%+v err=%v updates=%+v", session, err, repository.updatedSessions)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRefreshSessionIdentitiesKeepsOfficialSessionAndUpdatesPeers 验证批量身份补全保持闲小蜜会话并更新普通联系人。
|
||||
func TestRefreshSessionIdentitiesKeepsOfficialSessionAndUpdatesPeers(t *testing.T) {
|
||||
// repository 是记录身份缓存写入的测试仓储。
|
||||
repository := &fakeRepository{}
|
||||
// service 是返回统一买家身份的批量补全服务。
|
||||
service := NewWithIdentity(repository, fakeIdentityResolver{identity: Identity{BuyerName: "批量名称", BuyerAvatar: "批量头像"}})
|
||||
// sessions 是包含普通联系人和官方会话的测试列表。
|
||||
sessions := []Session{
|
||||
{AccountID: "account-1", ChatID: "chat-1", BuyerID: "buyer-1"},
|
||||
{AccountID: "account-1", ChatID: "chat-official", BuyerID: "1400", BuyerName: "闲小蜜"},
|
||||
}
|
||||
// refreshed 和 refreshErr 保存批量补全结果及首个错误。
|
||||
refreshed, refreshErr := service.RefreshSessionIdentities(context.Background(), "account-1", sessions)
|
||||
if refreshErr != nil || refreshed[0].BuyerName != "批量名称" || refreshed[1].BuyerName != "闲小蜜" || len(repository.updatedSessions) != 1 {
|
||||
t.Fatalf("refreshed=%+v err=%v updates=%+v", refreshed, refreshErr, repository.updatedSessions)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSessionPortRejectsMissingCapabilities 验证未装配会话维护能力时不会伪装成成功。
|
||||
func TestSessionPortRejectsMissingCapabilities(t *testing.T) {
|
||||
// service 是只实现历史查询的应用服务。
|
||||
service := New(historyOnlyRepository{})
|
||||
// err 保存缺失会话维护端口时的应用错误。
|
||||
err := service.CleanupEmptySessions(context.Background(), "account-1")
|
||||
if !errors.Is(err, ErrSessionUnavailable) {
|
||||
t.Fatalf("CleanupEmptySessions() error = %v, want %v", err, ErrSessionUnavailable)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMarkReadValidatesAndPropagatesPortErrors 验证已读用例的输入校验、能力缺失和底层错误分支。
|
||||
func TestMarkReadValidatesAndPropagatesPortErrors(t *testing.T) {
|
||||
// wantErr 是模拟会话已读写入失败的稳定错误。
|
||||
wantErr := errors.New("mark read failed")
|
||||
// repository 是记录已读调用并返回预设错误的测试端口。
|
||||
repository := &fakeRepository{markReadErr: wantErr}
|
||||
// service 是使用会话维护端口构造的聊天应用服务。
|
||||
service := New(repository)
|
||||
// err 保存有效请求透传的底层错误。
|
||||
if err := service.MarkRead(context.Background(), 7, " account-1 ", " chat-1 "); !errors.Is(err, wantErr) || repository.markReadCalls != 1 {
|
||||
t.Fatalf("MarkRead() error=%v calls=%d", err, repository.markReadCalls)
|
||||
}
|
||||
// invalidErr 保存空会话标识返回的应用输入错误。
|
||||
if invalidErr := service.MarkRead(context.Background(), 7, "account-1", ""); !errors.Is(invalidErr, ErrInvalidInput) {
|
||||
t.Fatalf("invalid MarkRead() error=%v", invalidErr)
|
||||
}
|
||||
// unavailableErr 保存仅实现历史读取端口时的能力缺失错误。
|
||||
if unavailableErr := New(historyOnlyRepository{}).MarkRead(context.Background(), 7, "account-1", "chat-1"); !errors.Is(unavailableErr, ErrSessionUnavailable) {
|
||||
t.Fatalf("unavailable MarkRead() error=%v", unavailableErr)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user