mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-17 14:57:27 +00:00
Merge branch 'Wei-Shaw:main' into main
This commit is contained in:
95 files changed
+6388
-179
No files matched your search
@@ -132,6 +132,11 @@ func (r *accountRepository) Create(ctx context.Context, account *service.Account
|
||||
builder.SetSessionWindowStatus(account.SessionWindowStatus)
|
||||
}
|
||||
|
||||
builder.SetQuotaDimension(dbaccount.QuotaDimension(account.QuotaDimensionOrDefault()))
|
||||
if account.ParentAccountID != nil {
|
||||
builder.SetParentAccountID(*account.ParentAccountID)
|
||||
}
|
||||
|
||||
created, err := builder.Save(ctx)
|
||||
if err != nil {
|
||||
return translatePersistenceError(err, service.ErrAccountNotFound, nil)
|
||||
@@ -265,7 +270,11 @@ func (r *accountRepository) GetByCRSAccountID(ctx context.Context, crsAccountID
|
||||
}
|
||||
|
||||
// 使用 sqljson.ValueEQ 生成 JSON 路径过滤,避免手写 SQL 片段导致语法兼容问题。
|
||||
// 排除 spark 影子账号(parent_account_id 非空):影子不持凭据,绝不能被 CRS 当作普通账号
|
||||
// 更新而覆盖 type/credentials/proxy。即便影子 Extra 被误写入 crs_account_id 也不会命中
|
||||
// (外审第7轮 P1)。
|
||||
m, err := r.client.Account.Query().
|
||||
Where(dbaccount.ParentAccountIDIsNil()).
|
||||
Where(func(s *entsql.Selector) {
|
||||
s.Where(sqljson.ValueEQ(dbaccount.FieldExtra, crsAccountID, sqljson.Path("crs_account_id")))
|
||||
}).
|
||||
@@ -288,10 +297,13 @@ func (r *accountRepository) GetByCRSAccountID(ctx context.Context, crsAccountID
|
||||
}
|
||||
|
||||
func (r *accountRepository) ListCRSAccountIDs(ctx context.Context) (map[string]int64, error) {
|
||||
// parent_account_id IS NULL 排除 spark 影子账号:影子不是 CRS 账号,绝不能进 CRS 同步映射
|
||||
// (否则会被当普通账号更新而覆盖 type/credentials/proxy)(外审第7轮 P1)。
|
||||
rows, err := r.sql.QueryContext(ctx, `
|
||||
SELECT id, extra->>'crs_account_id'
|
||||
FROM accounts
|
||||
WHERE deleted_at IS NULL
|
||||
AND parent_account_id IS NULL
|
||||
AND extra->>'crs_account_id' IS NOT NULL
|
||||
AND extra->>'crs_account_id' != ''
|
||||
`)
|
||||
@@ -396,6 +408,9 @@ func (r *accountRepository) Update(ctx context.Context, account *service.Account
|
||||
builder.ClearNotes()
|
||||
}
|
||||
|
||||
builder.SetQuotaDimension(dbaccount.QuotaDimension(account.QuotaDimensionOrDefault()))
|
||||
builder.SetNillableParentAccountID(account.ParentAccountID)
|
||||
|
||||
updated, err := builder.Save(ctx)
|
||||
if err != nil {
|
||||
return translatePersistenceError(err, service.ErrAccountNotFound, nil)
|
||||
@@ -1930,6 +1945,8 @@ func accountEntityToService(m *dbent.Account) *service.Account {
|
||||
SessionWindowStart: m.SessionWindowStart,
|
||||
SessionWindowEnd: m.SessionWindowEnd,
|
||||
SessionWindowStatus: derefString(m.SessionWindowStatus),
|
||||
ParentAccountID: m.ParentAccountID,
|
||||
QuotaDimension: string(m.QuotaDimension),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2222,3 +2239,21 @@ func (r *accountRepository) RevertProxyFallback(ctx context.Context, accountID i
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListShadowsByParent 返回指定父账号的影子账号;当前实现仅查 quota_dimension='spark'(唯一预设)。
|
||||
// 同时过滤 parent_account_id 和 quota_dimension='spark',防止未来其它 linked 维度被误伤。
|
||||
// ⚠️ 新增影子维度时:须更新此函数(或新增维度专用列举),并检查所有调用点(级联删除/一母一影校验/type 守卫),否则会静默漏掉新维度。
|
||||
// 软删除行由 SoftDeleteMixin 拦截器自动排除,无需手写 deleted_at IS NULL。
|
||||
func (r *accountRepository) ListShadowsByParent(ctx context.Context, parentID int64) ([]*service.Account, error) {
|
||||
rows, err := r.client.Account.Query().
|
||||
Where(dbaccount.ParentAccountIDEQ(parentID), dbaccount.QuotaDimensionEQ(dbaccount.QuotaDimensionSpark)).
|
||||
All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]*service.Account, 0, len(rows))
|
||||
for _, m := range rows {
|
||||
out = append(out, accountEntityToService(m))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -1031,6 +1031,45 @@ func (s *AccountRepoSuite) TestGetByCRSAccountID_EmptyString() {
|
||||
s.Require().Nil(got)
|
||||
}
|
||||
|
||||
// TestGetByCRSAccountID_ExcludesSparkShadow 验证外审第7轮 P1:即便 spark 影子的 Extra 被误写入
|
||||
// crs_account_id,CRS 查询也绝不能命中影子(否则会被当普通账号更新而覆盖 type/credentials/proxy)。
|
||||
func (s *AccountRepoSuite) TestGetByCRSAccountID_ExcludesSparkShadow() {
|
||||
crsID := "crs-shadow-only-99"
|
||||
parent := mustCreateAccount(s.T(), s.client, &service.Account{
|
||||
Name: "crs-mother", Platform: service.PlatformOpenAI, Type: service.AccountTypeOAuth,
|
||||
})
|
||||
mustCreateAccount(s.T(), s.client, &service.Account{
|
||||
Name: "crs-shadow", Platform: service.PlatformOpenAI, Type: service.AccountTypeOAuth,
|
||||
ParentAccountID: &parent.ID,
|
||||
QuotaDimension: service.QuotaDimensionSpark,
|
||||
Extra: map[string]any{"crs_account_id": crsID},
|
||||
})
|
||||
|
||||
got, err := s.repo.GetByCRSAccountID(s.ctx, crsID)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Nil(got, "spark 影子即便带 crs_account_id 也不应被 CRS 命中")
|
||||
}
|
||||
|
||||
// TestListCRSAccountIDs_ExcludesSparkShadow 验证外审第7轮 P1:影子的 crs_account_id 不应进入
|
||||
// CRS 同步映射(否则后续 CRS 同步会把影子当普通账号更新)。
|
||||
func (s *AccountRepoSuite) TestListCRSAccountIDs_ExcludesSparkShadow() {
|
||||
parent := mustCreateAccount(s.T(), s.client, &service.Account{
|
||||
Name: "crs-list-mother", Platform: service.PlatformOpenAI, Type: service.AccountTypeOAuth,
|
||||
})
|
||||
shadowCRSID := "crs-list-shadow-77"
|
||||
mustCreateAccount(s.T(), s.client, &service.Account{
|
||||
Name: "crs-list-shadow", Platform: service.PlatformOpenAI, Type: service.AccountTypeOAuth,
|
||||
ParentAccountID: &parent.ID,
|
||||
QuotaDimension: service.QuotaDimensionSpark,
|
||||
Extra: map[string]any{"crs_account_id": shadowCRSID},
|
||||
})
|
||||
|
||||
ids, err := s.repo.ListCRSAccountIDs(s.ctx)
|
||||
s.Require().NoError(err)
|
||||
_, ok := ids[shadowCRSID]
|
||||
s.Require().False(ok, "影子的 crs_account_id 不应进入 CRS 映射")
|
||||
}
|
||||
|
||||
// --- BulkUpdate ---
|
||||
|
||||
func (s *AccountRepoSuite) TestBulkUpdate() {
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
//go:build integration
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
)
|
||||
|
||||
func TestAccountRepoSparkShadowRoundTrip(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tx := testEntTx(t)
|
||||
repo := newAccountRepositoryWithSQL(tx.Client(), tx, nil)
|
||||
|
||||
parent := &service.Account{
|
||||
Name: "parent",
|
||||
Platform: service.PlatformOpenAI,
|
||||
Type: service.AccountTypeOAuth,
|
||||
Status: service.StatusActive,
|
||||
}
|
||||
if err := repo.Create(ctx, parent); err != nil {
|
||||
t.Fatalf("create parent: %v", err)
|
||||
}
|
||||
pid := parent.ID
|
||||
shadow := &service.Account{
|
||||
Name: "shadow",
|
||||
Platform: service.PlatformOpenAI,
|
||||
Type: service.AccountTypeOAuth,
|
||||
Status: service.StatusActive,
|
||||
ParentAccountID: &pid,
|
||||
QuotaDimension: service.QuotaDimensionSpark,
|
||||
}
|
||||
if err := repo.Create(ctx, shadow); err != nil {
|
||||
t.Fatalf("create shadow: %v", err)
|
||||
}
|
||||
got, err := repo.GetByID(ctx, shadow.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get: %v", err)
|
||||
}
|
||||
if got.ParentAccountID == nil || *got.ParentAccountID != pid {
|
||||
t.Fatalf("ParentAccountID round-trip: %v", got.ParentAccountID)
|
||||
}
|
||||
if got.QuotaDimension != service.QuotaDimensionSpark {
|
||||
t.Fatalf("QuotaDimension: %q", got.QuotaDimension)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListShadowsByParent(t *testing.T) {
|
||||
// Schema enforces at most one spark shadow per parent (uq_accounts_spark_shadow_per_parent).
|
||||
// Test strategy: create 2 parents each with 1 spark shadow + 1 unrelated account;
|
||||
// assert ListShadowsByParent(parent1.ID) returns exactly 1 (filtering by both
|
||||
// parent_account_id and quota_dimension='spark', excluding parent2's shadow and unrelated).
|
||||
ctx := context.Background()
|
||||
tx := testEntTx(t)
|
||||
repo := newAccountRepositoryWithSQL(tx.Client(), tx, nil)
|
||||
|
||||
// Create parent1 and its spark shadow
|
||||
parent1 := &service.Account{
|
||||
Name: "list-parent1",
|
||||
Platform: service.PlatformOpenAI,
|
||||
Type: service.AccountTypeOAuth,
|
||||
Status: service.StatusActive,
|
||||
}
|
||||
if err := repo.Create(ctx, parent1); err != nil {
|
||||
t.Fatalf("create parent1: %v", err)
|
||||
}
|
||||
pid1 := parent1.ID
|
||||
|
||||
shadow1 := &service.Account{
|
||||
Name: "shadow1",
|
||||
Platform: service.PlatformOpenAI,
|
||||
Type: service.AccountTypeOAuth,
|
||||
Status: service.StatusActive,
|
||||
ParentAccountID: &pid1,
|
||||
QuotaDimension: service.QuotaDimensionSpark,
|
||||
}
|
||||
if err := repo.Create(ctx, shadow1); err != nil {
|
||||
t.Fatalf("create shadow1: %v", err)
|
||||
}
|
||||
|
||||
// Create parent2 and its spark shadow (must NOT appear in parent1's list)
|
||||
parent2 := &service.Account{
|
||||
Name: "list-parent2",
|
||||
Platform: service.PlatformOpenAI,
|
||||
Type: service.AccountTypeOAuth,
|
||||
Status: service.StatusActive,
|
||||
}
|
||||
if err := repo.Create(ctx, parent2); err != nil {
|
||||
t.Fatalf("create parent2: %v", err)
|
||||
}
|
||||
pid2 := parent2.ID
|
||||
|
||||
shadow2 := &service.Account{
|
||||
Name: "shadow2",
|
||||
Platform: service.PlatformOpenAI,
|
||||
Type: service.AccountTypeOAuth,
|
||||
Status: service.StatusActive,
|
||||
ParentAccountID: &pid2,
|
||||
QuotaDimension: service.QuotaDimensionSpark,
|
||||
}
|
||||
if err := repo.Create(ctx, shadow2); err != nil {
|
||||
t.Fatalf("create shadow2: %v", err)
|
||||
}
|
||||
|
||||
// Create 1 unrelated normal account (no parent, global dimension)
|
||||
unrelated := &service.Account{
|
||||
Name: "unrelated",
|
||||
Platform: service.PlatformOpenAI,
|
||||
Type: service.AccountTypeOAuth,
|
||||
Status: service.StatusActive,
|
||||
}
|
||||
if err := repo.Create(ctx, unrelated); err != nil {
|
||||
t.Fatalf("create unrelated: %v", err)
|
||||
}
|
||||
|
||||
// Assert ListShadowsByParent returns exactly 1 for parent1
|
||||
got, err := repo.ListShadowsByParent(ctx, pid1)
|
||||
if err != nil {
|
||||
t.Fatalf("ListShadowsByParent: %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("expected 1 spark shadow for parent1, got %d", len(got))
|
||||
}
|
||||
acc := got[0]
|
||||
if acc.ParentAccountID == nil || *acc.ParentAccountID != pid1 {
|
||||
t.Errorf("unexpected ParentAccountID: %v", acc.ParentAccountID)
|
||||
}
|
||||
if acc.QuotaDimension != service.QuotaDimensionSpark {
|
||||
t.Errorf("unexpected QuotaDimension: %q", acc.QuotaDimension)
|
||||
}
|
||||
if acc.ID != shadow1.ID {
|
||||
t.Errorf("expected shadow1.ID=%d, got %d", shadow1.ID, acc.ID)
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
dbent "github.com/Wei-Shaw/sub2api/ent"
|
||||
dbaccount "github.com/Wei-Shaw/sub2api/ent/account"
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -233,6 +234,12 @@ func mustCreateAccount(t *testing.T, client *dbent.Client, a *service.Account) *
|
||||
if !a.UpdatedAt.IsZero() {
|
||||
create.SetUpdatedAt(a.UpdatedAt)
|
||||
}
|
||||
if a.ParentAccountID != nil {
|
||||
create.SetParentAccountID(*a.ParentAccountID)
|
||||
}
|
||||
if a.QuotaDimension != "" {
|
||||
create.SetQuotaDimension(dbaccount.QuotaDimension(a.QuotaDimension))
|
||||
}
|
||||
|
||||
created, err := create.Save(ctx)
|
||||
require.NoError(t, err, "create account")
|
||||
|
||||
@@ -449,6 +449,8 @@ func buildSchedulerMetadataAccount(account service.Account) service.Account {
|
||||
SessionWindowStart: account.SessionWindowStart,
|
||||
SessionWindowEnd: account.SessionWindowEnd,
|
||||
SessionWindowStatus: account.SessionWindowStatus,
|
||||
ParentAccountID: account.ParentAccountID,
|
||||
QuotaDimension: account.QuotaDimension,
|
||||
AccountGroups: filterSchedulerAccountGroups(account.AccountGroups),
|
||||
GroupIDs: filterSchedulerGroupIDs(account.GroupIDs, account.AccountGroups),
|
||||
Credentials: filterSchedulerCredentials(account.Credentials),
|
||||
@@ -516,7 +518,7 @@ func filterSchedulerCredentials(credentials map[string]any) map[string]any {
|
||||
if len(credentials) == 0 {
|
||||
return nil
|
||||
}
|
||||
keys := []string{"model_mapping", "api_key", "project_id", "oauth_type"}
|
||||
keys := []string{"model_mapping", "compact_model_mapping", "api_key", "project_id", "oauth_type"}
|
||||
filtered := make(map[string]any)
|
||||
for _, key := range keys {
|
||||
if value, ok := credentials[key]; ok && value != nil {
|
||||
|
||||
@@ -134,3 +134,32 @@ func TestBuildSchedulerMetadataAccount_KeepsModelRateLimits(t *testing.T) {
|
||||
require.Contains(t, limits, "antigravity:gemini")
|
||||
require.Nil(t, got.Extra["unused_large_field"])
|
||||
}
|
||||
|
||||
func TestBuildSchedulerMetadataAccount_KeepsSparkShadowRoutingIdentity(t *testing.T) {
|
||||
parentID := int64(100)
|
||||
account := service.Account{
|
||||
ID: 200,
|
||||
Platform: service.PlatformOpenAI,
|
||||
Type: service.AccountTypeOAuth,
|
||||
ParentAccountID: &parentID,
|
||||
QuotaDimension: service.QuotaDimensionSpark,
|
||||
Credentials: map[string]any{
|
||||
"model_mapping": map[string]any{
|
||||
"gpt-5.3-codex-spark": "gpt-5.3-codex-spark",
|
||||
},
|
||||
"compact_model_mapping": map[string]any{
|
||||
"gpt-5.4": "gpt-5.4-openai-compact",
|
||||
},
|
||||
"access_token": "drop-me",
|
||||
},
|
||||
}
|
||||
|
||||
got := buildSchedulerMetadataAccount(account)
|
||||
|
||||
require.NotNil(t, got.ParentAccountID)
|
||||
require.Equal(t, parentID, *got.ParentAccountID)
|
||||
require.Equal(t, service.QuotaDimensionSpark, got.QuotaDimension)
|
||||
require.Equal(t, map[string]any{"gpt-5.3-codex-spark": "gpt-5.3-codex-spark"}, got.Credentials["model_mapping"])
|
||||
require.Equal(t, map[string]any{"gpt-5.4": "gpt-5.4-openai-compact"}, got.Credentials["compact_model_mapping"])
|
||||
require.Nil(t, got.Credentials["access_token"])
|
||||
}
|
||||
Reference in new issue
Block a user