推进P1账号应用浏览器生命周期与自动化隔离

This commit is contained in:
Christ committed 2026-08-15 22:57:10 +08:00
1 parent bcdc163306
commit e53177ccd1
14 files changed
+677 -50

No files matched your search

@@ -0,0 +1,101 @@
// Package account 提供账号资料与登录相关的应用用例及消费者定义的端口。
package account
import (
"context"
"errors"
)
// ErrNotFound 表示资料刷新目标账号不存在。
var ErrNotFound = errors.New("账号不存在")
// ErrForbidden 表示当前用户无权刷新目标账号资料。
var ErrForbidden = errors.New("无权刷新账号资料")
// Summary 是资料刷新用例所需的非敏感账号摘要,不包含 Cookie、密码或加密 metadata。
type Summary struct {
// ID 是平台账号在本地的稳定标识。
ID string
// UserID 是账号所属的本地用户标识。
UserID int64
// Remark 是用户设置的账号备注,用于资料刷新失败时的展示兜底。
Remark string
// Nickname 是本地缓存的平台昵称,用于资料刷新失败时的展示兜底。
Nickname string
// AvatarURL 是本地缓存的平台头像地址,用于资料刷新失败时的展示兜底。
AvatarURL string
}
// ProfileInput 是平台资料刷新端口的输入,携带已通过归属校验的账号摘要。
type ProfileInput struct {
// UserID 是当前发起资料刷新的用户标识。
UserID int64
// AccountID 是待刷新的账号标识。
AccountID string
// Summary 是已确认归属的非敏感账号摘要。
Summary Summary
}
// ProfileResult 是资料刷新用例返回的可展示结果,不包含任何凭证字段。
type ProfileResult struct {
// AccountID 是完成资料刷新的账号标识。
AccountID string
// Nickname 是平台返回或本地兜底的昵称。
Nickname string
// AvatarURL 是平台返回或本地兜底的头像地址。
AvatarURL string
// ErrorMessage 是平台请求或资料保存失败时的可展示错误;非空不代表本地用例失败。
ErrorMessage string
}
// SummaryRepository 定义资料刷新用例读取账号归属和摘要所需的最小端口。
type SummaryRepository interface {
// GetOwnedSummary 按用户和账号联合查询非敏感摘要;不存在或越权时返回错误。
GetOwnedSummary(context.Context, int64, string) (Summary, error)
}
// ProfilePort 定义平台资料刷新及本地资料保存所需的适配端口。
type ProfilePort interface {
// RefreshProfile 调用平台并保存资料;平台业务失败通过 ProfileResult.ErrorMessage 返回。
RefreshProfile(context.Context, ProfileInput) (ProfileResult, error)
}
// ProfileService 编排账号归属校验、平台资料刷新和可展示结果转换。
type ProfileService struct {
// repository 提供不解密凭证的账号摘要查询能力。
repository SummaryRepository
// profilePort 承担平台调用、凭证会话更新和资料持久化适配。
profilePort ProfilePort
}
// NewProfileService 构造资料刷新应用服务并校验必需端口。
func NewProfileService(repository SummaryRepository, profilePort ProfilePort) (*ProfileService, error) {
if repository == nil {
return nil, errors.New("账号资料摘要 repository 未初始化")
}
if profilePort == nil {
return nil, errors.New("账号资料刷新端口未初始化")
}
return &ProfileService{repository: repository, profilePort: profilePort}, nil
}
// RefreshProfile 执行一次按用户归属校验的账号资料刷新用例。
func (s *ProfileService) RefreshProfile(ctx context.Context, userID int64, accountID string) (ProfileResult, error) {
if s == nil || s.repository == nil || s.profilePort == nil {
return ProfileResult{}, errors.New("账号资料刷新服务未初始化")
}
// summary 保存通过所有权查询得到的非敏感账号摘要。
summary, err := s.repository.GetOwnedSummary(ctx, userID, accountID)
if err != nil {
return ProfileResult{}, err
}
// result 保存平台资料刷新端口转换后的可展示结果。
result, err := s.profilePort.RefreshProfile(ctx, ProfileInput{UserID: userID, AccountID: accountID, Summary: summary})
if err != nil {
return ProfileResult{}, err
}
if result.AccountID == "" {
result.AccountID = accountID
}
return result, nil
}
@@ -0,0 +1,119 @@
package account
import (
"context"
"errors"
"testing"
)
// fakeSummaryRepository 是资料刷新应用服务测试用的摘要 repository 替身。
type fakeSummaryRepository struct {
// summary 是待返回的非敏感账号摘要。
summary Summary
// err 是摘要查询需要模拟的错误。
err error
}
// GetOwnedSummary 返回测试预置的摘要或查询错误。
func (r fakeSummaryRepository) GetOwnedSummary(context.Context, int64, string) (Summary, error) {
return r.summary, r.err
}
// fakeProfilePort 是资料刷新应用服务测试用的平台端口替身。
type fakeProfilePort struct {
// result 是待返回的资料刷新结果。
result ProfileResult
// err 是平台资料刷新需要模拟的错误。
err error
// input 保存最近一次收到的端口输入,供测试验证归属摘要传递。
input ProfileInput
}
// RefreshProfile 返回测试预置结果并记录输入。
func (p *fakeProfilePort) RefreshProfile(_ context.Context, input ProfileInput) (ProfileResult, error) {
p.input = input
return p.result, p.err
}
// TestNewProfileServiceRequiresPorts 验证构造函数拒绝缺失依赖。
func TestNewProfileServiceRequiresPorts(t *testing.T) {
// repository 是满足摘要查询能力的测试端口。
repository := fakeSummaryRepository{}
// profilePort 是满足平台资料刷新能力的测试端口。
profilePort := &fakeProfilePort{}
// cases 是构造阶段需要拒绝的缺失依赖场景。
cases := []struct {
// name 是当前构造失败场景名称。
name string
// repository 是当前场景使用的摘要端口。
repository SummaryRepository
// profilePort 是当前场景使用的平台端口。
profilePort ProfilePort
}{
{name: "缺少摘要repository", repository: nil, profilePort: profilePort},
{name: "缺少资料端口", repository: repository, profilePort: nil},
}
// testCase 表示当前正在验证的构造失败场景。
for _, testCase := range cases {
t.Run(testCase.name, func(t *testing.T) {
// err 保存当前场景的构造结果错误。
_, err := NewProfileService(testCase.repository, testCase.profilePort)
if err == nil {
t.Fatal("缺少必需端口时应构造失败")
}
})
}
}
// TestRefreshProfileVerifiesOwnershipAndPassesNonSensitiveSummary 验证成功路径只向端口传递已归属摘要。
func TestRefreshProfileVerifiesOwnershipAndPassesNonSensitiveSummary(t *testing.T) {
// repository 是返回当前用户账号摘要的测试端口。
repository := fakeSummaryRepository{summary: Summary{ID: "acc1", UserID: 7, Nickname: "旧昵称"}}
// profilePort 是返回平台资料结果的测试端口。
profilePort := &fakeProfilePort{result: ProfileResult{Nickname: "新昵称", AvatarURL: "https://img.example/avatar.jpg"}}
// service 是待验证的账号资料应用服务。
service, err := NewProfileService(repository, profilePort)
if err != nil {
t.Fatalf("构造资料服务失败: %v", err)
}
// result 保存资料刷新成功结果。
result, err := service.RefreshProfile(context.Background(), 7, "acc1")
if err != nil {
t.Fatalf("资料刷新失败: %v", err)
}
if result.AccountID != "acc1" || result.Nickname != "新昵称" {
t.Fatalf("资料刷新结果异常: %+v", result)
}
if profilePort.input.UserID != 7 || profilePort.input.Summary.ID != "acc1" {
t.Fatalf("资料端口未收到归属摘要: %+v", profilePort.input)
}
}
// TestRefreshProfilePropagatesOwnershipAndPlatformFailures 验证归属查询和平台端口错误不会被吞掉。
func TestRefreshProfilePropagatesOwnershipAndPlatformFailures(t *testing.T) {
// ownershipErr 是账号不属于当前用户时 repository 返回的错误。
ownershipErr := errors.New("账号不属于当前用户")
// ownershipService 是使用归属错误替身构造的资料服务。
ownershipService, err := NewProfileService(fakeSummaryRepository{err: ownershipErr}, &fakeProfilePort{})
if err != nil {
t.Fatalf("构造归属错误服务失败: %v", err)
}
// ownershipResult、ownershipCallErr 保存归属失败结果。
_, ownershipCallErr := ownershipService.RefreshProfile(context.Background(), 7, "other")
if !errors.Is(ownershipCallErr, ownershipErr) {
t.Fatalf("应保留归属错误,got %v", ownershipCallErr)
}
// platformErr 是平台资料端口返回的故障。
platformErr := errors.New("平台不可用")
// platformService 是使用平台错误替身构造的资料服务。
platformService, err := NewProfileService(fakeSummaryRepository{summary: Summary{ID: "acc1"}}, &fakeProfilePort{err: platformErr})
if err != nil {
t.Fatalf("构造平台错误服务失败: %v", err)
}
// platformResult、platformCallErr 保存平台失败结果。
_, platformCallErr := platformService.RefreshProfile(context.Background(), 7, "acc1")
if !errors.Is(platformCallErr, platformErr) {
t.Fatalf("应保留平台错误,got %v", platformCallErr)
}
}