mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-08 08:55:22 +00:00
背景:gpt-5.3-codex-spark 使用独立于 codex 全局(5h/7d)的配额窗口(数据源是 /wham/usage 响应体的 codex_bengalfox,而非 codex 全局用的 x-codex-* 响应头),且 只能挂在已完成 OAuth 授权的 OpenAI 账号下复用其登录态,不能作为独立账号单独接入。 为此新增“链接型影子账号”(spark shadow account):影子账号本身不持有任何凭据, 通过 parent_account_id 指向母账号,凭据/token/代理透传自母账号并共享母账号的刷新 周期,仅在配额维度(quota_dimension=spark)和用量窗口上与母账号完全独立调度、互不 连坐。 实现: - 数据模型:migration 154(+154a)给 accounts 表加 parent_account_id / quota_dimension 列 + 4 条约束(维度合法 / parent⟺非 global 维度一致 / 禁自指 / FK)+ 2 个 CONCURRENTLY 索引(母账号索引 + 每母账号至多一个影子的唯一索引)。 - 创建:POST /api/v1/admin/accounts/:id/shadow(CreateShadow)—— 一母一影(唯一 索引兜底并发竞态),继承母账号 proxy/分组/并发/优先级(显式传参可覆盖),默认 model_mapping 恒等映射到 spark(拒绝非 spark 模型),母账号必须是真实的 OpenAI OAuth 账号(非影子)。 - 凭据透传:resolveCredentialAccount 把影子解析回母账号,GetAccessToken / 请求头 / WS 三条路径统一走此函数;影子自身 Credentials 恒为空(仅允许写 model_mapping), 凭据写入的汇聚点 persistAccountCredentials 对影子早返 no-op,防止误写。 - 调度:parentHealthyForShadow 只看母账号是否仍是 OpenAI OAuth + 凭据/传输是否 可用(active、token 未过期、未处于 401/刷新失败/传输故障导致的临时不可调度冷却), 刻意不看母账号的 global 限流窗口——两条 429 道互不连坐。 - 用量:影子的 codex_5h/7d 走 OpenAIQuotaService.QueryUsage(/wham/usage 的 codex_bengalfox),与母账号走的 WSv2 探测(/responses 头)完全独立的数据源、 刷新节流与 staleness 判定。 - 备份:ExportData 显式排除影子账号(影子不持凭据,通用凭据型导入强制 credentials 非空、无法表达父子链接),按 skipped_shadows 计数提示前端。 - 前端:账号操作菜单新增“创建 Spark 影子”入口,影子行展示回填的母账号信息 (邮箱 / plan / 隐私模式 / 订阅到期 / chatgpt_account_id),批量操作自动跳过 影子账号。 说明:migrations 目录用完整文件名(而非纯数字前缀)标识迁移,故本次新增的 154_account_spark_shadow.sql / 154a_..._notx.sql 与已有的 154_add_ops_system_logs_api_key_id.sql 按序号共存,与目录里 145/151 已有的 先例一致。 测试:新增约 20 个测试文件,覆盖 handler(CreateShadow 校验 / 母账号信息回填)、 repository(影子 round-trip / 一母一影唯一索引 / 迁移 schema)、service(凭据 透传三路径 / 调度母健康门 / 用量窗口来源与刷新节流 / CRS 母账号不变量 / 各类 早返与 fail-closed 场景)及前端组件(账号列表 / 操作菜单 / 用量重置)。 验证(镜像 CI;golangci-lint 首次全量分析耗时过长被跳过,其余全部实测): - gofmt -l:干净 - go build ./... / go vet ./...:通过 - go test ./... -count=1:全绿(全部包 ok,含 internal/service、 internal/repository、migrations) - go test -tags integration ./internal/repository/... ./internal/service/... (真实 Postgres,testcontainers):全绿,含迁移幂等性 (TestMigrationsRunner_IsIdempotent_AndSchemaIsUpToDate)与影子相关全部用例 - pnpm lint:check / pnpm typecheck / pnpm build(真实 vite 构建)/ pnpm vitest run:全绿(124 文件 760 用例) Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
284 lines
10 KiB
Go
284 lines
10 KiB
Go
//go:build unit
|
|
|
|
// 账号服务删除方法的单元测试
|
|
// 测试 AccountService.Delete 方法在各种场景下的行为
|
|
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// accountRepoStub 是 AccountRepository 接口的测试桩实现。
|
|
// 用于隔离测试 AccountService.Delete 方法,避免依赖真实数据库。
|
|
//
|
|
// 设计说明:
|
|
// - exists: 模拟 ExistsByID 返回的存在性结果
|
|
// - existsErr: 模拟 ExistsByID 返回的错误
|
|
// - deleteErr: 模拟 Delete 返回的错误
|
|
// - deletedIDs: 记录被调用删除的账号 ID,用于断言验证
|
|
type accountRepoStub struct {
|
|
exists bool // ExistsByID 的返回值
|
|
existsErr error // ExistsByID 的错误返回值
|
|
deleteErr error // Delete 的错误返回值
|
|
deletedIDs []int64 // 记录已删除的账号 ID 列表
|
|
}
|
|
|
|
// 以下方法在本测试中不应被调用,使用 panic 确保测试失败时能快速定位问题
|
|
|
|
func (s *accountRepoStub) Create(ctx context.Context, account *Account) error {
|
|
panic("unexpected Create call")
|
|
}
|
|
|
|
func (s *accountRepoStub) GetByID(ctx context.Context, id int64) (*Account, error) {
|
|
panic("unexpected GetByID call")
|
|
}
|
|
|
|
func (s *accountRepoStub) GetByIDs(ctx context.Context, ids []int64) ([]*Account, error) {
|
|
panic("unexpected GetByIDs call")
|
|
}
|
|
|
|
// ExistsByID 返回预设的存在性检查结果。
|
|
// 这是 Delete 方法调用的第一个仓储方法,用于验证账号是否存在。
|
|
func (s *accountRepoStub) ExistsByID(ctx context.Context, id int64) (bool, error) {
|
|
return s.exists, s.existsErr
|
|
}
|
|
|
|
func (s *accountRepoStub) GetByCRSAccountID(ctx context.Context, crsAccountID string) (*Account, error) {
|
|
panic("unexpected GetByCRSAccountID call")
|
|
}
|
|
|
|
func (s *accountRepoStub) FindByExtraField(ctx context.Context, key string, value any) ([]Account, error) {
|
|
panic("unexpected FindByExtraField call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ListCRSAccountIDs(ctx context.Context) (map[string]int64, error) {
|
|
panic("unexpected ListCRSAccountIDs call")
|
|
}
|
|
|
|
func (s *accountRepoStub) Update(ctx context.Context, account *Account) error {
|
|
panic("unexpected Update call")
|
|
}
|
|
|
|
// Delete 记录被删除的账号 ID 并返回预设的错误。
|
|
// 通过 deletedIDs 可以验证删除操作是否被正确调用。
|
|
func (s *accountRepoStub) Delete(ctx context.Context, id int64) error {
|
|
s.deletedIDs = append(s.deletedIDs, id)
|
|
return s.deleteErr
|
|
}
|
|
|
|
// 以下是接口要求实现但本测试不关心的方法
|
|
|
|
func (s *accountRepoStub) List(ctx context.Context, params pagination.PaginationParams) ([]Account, *pagination.PaginationResult, error) {
|
|
panic("unexpected List call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ListWithFilters(ctx context.Context, params pagination.PaginationParams, platform, accountType, status, search string, groupID int64, privacyMode string) ([]Account, *pagination.PaginationResult, error) {
|
|
panic("unexpected ListWithFilters call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ListByGroup(ctx context.Context, groupID int64) ([]Account, error) {
|
|
panic("unexpected ListByGroup call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ListActive(ctx context.Context) ([]Account, error) {
|
|
panic("unexpected ListActive call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ListByPlatform(ctx context.Context, platform string) ([]Account, error) {
|
|
panic("unexpected ListByPlatform call")
|
|
}
|
|
|
|
func (s *accountRepoStub) UpdateLastUsed(ctx context.Context, id int64) error {
|
|
panic("unexpected UpdateLastUsed call")
|
|
}
|
|
|
|
func (s *accountRepoStub) BatchUpdateLastUsed(ctx context.Context, updates map[int64]time.Time) error {
|
|
panic("unexpected BatchUpdateLastUsed call")
|
|
}
|
|
|
|
func (s *accountRepoStub) SetError(ctx context.Context, id int64, errorMsg string) error {
|
|
panic("unexpected SetError call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ClearError(ctx context.Context, id int64) error {
|
|
panic("unexpected ClearError call")
|
|
}
|
|
|
|
func (s *accountRepoStub) SetSchedulable(ctx context.Context, id int64, schedulable bool) error {
|
|
panic("unexpected SetSchedulable call")
|
|
}
|
|
|
|
func (s *accountRepoStub) AutoPauseExpiredAccounts(ctx context.Context, now time.Time) (int64, error) {
|
|
panic("unexpected AutoPauseExpiredAccounts call")
|
|
}
|
|
|
|
func (s *accountRepoStub) BindGroups(ctx context.Context, accountID int64, groupIDs []int64) error {
|
|
panic("unexpected BindGroups call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ListSchedulable(ctx context.Context) ([]Account, error) {
|
|
panic("unexpected ListSchedulable call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ListSchedulableByGroupID(ctx context.Context, groupID int64) ([]Account, error) {
|
|
panic("unexpected ListSchedulableByGroupID call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ListSchedulableByPlatform(ctx context.Context, platform string) ([]Account, error) {
|
|
panic("unexpected ListSchedulableByPlatform call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ListSchedulableByGroupIDAndPlatform(ctx context.Context, groupID int64, platform string) ([]Account, error) {
|
|
panic("unexpected ListSchedulableByGroupIDAndPlatform call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ListSchedulableByPlatforms(ctx context.Context, platforms []string) ([]Account, error) {
|
|
panic("unexpected ListSchedulableByPlatforms call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ListSchedulableByGroupIDAndPlatforms(ctx context.Context, groupID int64, platforms []string) ([]Account, error) {
|
|
panic("unexpected ListSchedulableByGroupIDAndPlatforms call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ListSchedulableUngroupedByPlatform(ctx context.Context, platform string) ([]Account, error) {
|
|
panic("unexpected ListSchedulableUngroupedByPlatform call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ListSchedulableUngroupedByPlatforms(ctx context.Context, platforms []string) ([]Account, error) {
|
|
panic("unexpected ListSchedulableUngroupedByPlatforms call")
|
|
}
|
|
|
|
func (s *accountRepoStub) SetRateLimited(ctx context.Context, id int64, resetAt time.Time) error {
|
|
panic("unexpected SetRateLimited call")
|
|
}
|
|
|
|
func (s *accountRepoStub) SetModelRateLimit(ctx context.Context, id int64, scope string, resetAt time.Time, reason ...string) error {
|
|
panic("unexpected SetModelRateLimit call")
|
|
}
|
|
|
|
func (s *accountRepoStub) SetOverloaded(ctx context.Context, id int64, until time.Time) error {
|
|
panic("unexpected SetOverloaded call")
|
|
}
|
|
|
|
func (s *accountRepoStub) SetTempUnschedulable(ctx context.Context, id int64, until time.Time, reason string) error {
|
|
panic("unexpected SetTempUnschedulable call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ClearTempUnschedulable(ctx context.Context, id int64) error {
|
|
panic("unexpected ClearTempUnschedulable call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ClearRateLimit(ctx context.Context, id int64) error {
|
|
panic("unexpected ClearRateLimit call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ClearAntigravityQuotaScopes(ctx context.Context, id int64) error {
|
|
panic("unexpected ClearAntigravityQuotaScopes call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ClearModelRateLimits(ctx context.Context, id int64) error {
|
|
panic("unexpected ClearModelRateLimits call")
|
|
}
|
|
|
|
func (s *accountRepoStub) UpdateSessionWindow(ctx context.Context, id int64, start, end *time.Time, status string) error {
|
|
panic("unexpected UpdateSessionWindow call")
|
|
}
|
|
|
|
func (s *accountRepoStub) UpdateSessionWindowEnd(ctx context.Context, id int64, end time.Time) error {
|
|
panic("unexpected UpdateSessionWindowEnd call")
|
|
}
|
|
|
|
func (s *accountRepoStub) UpdateExtra(ctx context.Context, id int64, updates map[string]any) error {
|
|
panic("unexpected UpdateExtra call")
|
|
}
|
|
|
|
func (s *accountRepoStub) BulkUpdate(ctx context.Context, ids []int64, updates AccountBulkUpdate) (int64, error) {
|
|
panic("unexpected BulkUpdate call")
|
|
}
|
|
|
|
func (s *accountRepoStub) IncrementQuotaUsed(ctx context.Context, id int64, amount float64) error {
|
|
return nil
|
|
}
|
|
|
|
func (s *accountRepoStub) ResetQuotaUsed(ctx context.Context, id int64) error {
|
|
return nil
|
|
}
|
|
|
|
func (s *accountRepoStub) RevertProxyFallback(ctx context.Context, accountID int64) error {
|
|
panic("unexpected RevertProxyFallback call")
|
|
}
|
|
|
|
func (s *accountRepoStub) ListShadowsByParent(ctx context.Context, parentID int64) ([]*Account, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
// TestAccountService_Delete_NotFound 测试删除不存在的账号时返回正确的错误。
|
|
// 预期行为:
|
|
// - ExistsByID 返回 false(账号不存在)
|
|
// - 返回 ErrAccountNotFound 错误
|
|
// - Delete 方法不被调用(deletedIDs 为空)
|
|
func TestAccountService_Delete_NotFound(t *testing.T) {
|
|
repo := &accountRepoStub{exists: false}
|
|
svc := &AccountService{accountRepo: repo}
|
|
|
|
err := svc.Delete(context.Background(), 55)
|
|
require.ErrorIs(t, err, ErrAccountNotFound)
|
|
require.Empty(t, repo.deletedIDs) // 验证删除操作未被调用
|
|
}
|
|
|
|
// TestAccountService_Delete_CheckError 测试存在性检查失败时的错误处理。
|
|
// 预期行为:
|
|
// - ExistsByID 返回数据库错误
|
|
// - 返回包含 "check account" 的错误信息
|
|
// - Delete 方法不被调用
|
|
func TestAccountService_Delete_CheckError(t *testing.T) {
|
|
repo := &accountRepoStub{existsErr: errors.New("db down")}
|
|
svc := &AccountService{accountRepo: repo}
|
|
|
|
err := svc.Delete(context.Background(), 55)
|
|
require.Error(t, err)
|
|
require.ErrorContains(t, err, "check account") // 验证错误信息包含上下文
|
|
require.Empty(t, repo.deletedIDs)
|
|
}
|
|
|
|
// TestAccountService_Delete_DeleteError 测试删除操作失败时的错误处理。
|
|
// 预期行为:
|
|
// - ExistsByID 返回 true(账号存在)
|
|
// - Delete 被调用但返回错误
|
|
// - 返回包含 "delete account" 的错误信息
|
|
// - deletedIDs 记录了尝试删除的 ID
|
|
func TestAccountService_Delete_DeleteError(t *testing.T) {
|
|
repo := &accountRepoStub{
|
|
exists: true,
|
|
deleteErr: errors.New("delete failed"),
|
|
}
|
|
svc := &AccountService{accountRepo: repo}
|
|
|
|
err := svc.Delete(context.Background(), 55)
|
|
require.Error(t, err)
|
|
require.ErrorContains(t, err, "delete account")
|
|
require.Equal(t, []int64{55}, repo.deletedIDs) // 验证删除操作被调用
|
|
}
|
|
|
|
// TestAccountService_Delete_Success 测试删除操作成功的场景。
|
|
// 预期行为:
|
|
// - ExistsByID 返回 true(账号存在)
|
|
// - Delete 成功执行
|
|
// - 返回 nil 错误
|
|
// - deletedIDs 记录了被删除的 ID
|
|
func TestAccountService_Delete_Success(t *testing.T) {
|
|
repo := &accountRepoStub{exists: true}
|
|
svc := &AccountService{accountRepo: repo}
|
|
|
|
err := svc.Delete(context.Background(), 55)
|
|
require.NoError(t, err)
|
|
require.Equal(t, []int64{55}, repo.deletedIDs) // 验证正确的 ID 被删除
|
|
}
|