//go:build unit package service import ( "context" "errors" "testing" "github.com/stretchr/testify/require" ) type accountRepoStubForBulkUpdate struct { accountRepoStub bulkUpdateErr error bulkUpdateIDs []int64 bindGroupErrByID map[int64]error getByIDsAccounts []*Account getByIDsErr error getByIDsCalled bool getByIDsIDs []int64 getByIDAccounts map[int64]*Account getByIDErrByID map[int64]error getByIDCalled []int64 } func (s *accountRepoStubForBulkUpdate) BulkUpdate(_ context.Context, ids []int64, _ AccountBulkUpdate) (int64, error) { s.bulkUpdateIDs = append([]int64{}, ids...) if s.bulkUpdateErr != nil { return 0, s.bulkUpdateErr } return int64(len(ids)), nil } func (s *accountRepoStubForBulkUpdate) BindGroups(_ context.Context, accountID int64, _ []int64) error { if err, ok := s.bindGroupErrByID[accountID]; ok { return err } return nil } func (s *accountRepoStubForBulkUpdate) GetByIDs(_ context.Context, ids []int64) ([]*Account, error) { s.getByIDsCalled = true s.getByIDsIDs = append([]int64{}, ids...) if s.getByIDsErr != nil { return nil, s.getByIDsErr } return s.getByIDsAccounts, nil } func (s *accountRepoStubForBulkUpdate) GetByID(_ context.Context, id int64) (*Account, error) { s.getByIDCalled = append(s.getByIDCalled, id) if err, ok := s.getByIDErrByID[id]; ok { return nil, err } if account, ok := s.getByIDAccounts[id]; ok { return account, nil } return nil, errors.New("account not found") } // TestAdminService_BulkUpdateAccounts_AllSuccessIDs 验证批量更新成功时返回 success_ids/failed_ids。 func TestAdminService_BulkUpdateAccounts_AllSuccessIDs(t *testing.T) { repo := &accountRepoStubForBulkUpdate{} svc := &adminServiceImpl{accountRepo: repo} schedulable := true input := &BulkUpdateAccountsInput{ AccountIDs: []int64{1, 2, 3}, Schedulable: &schedulable, } result, err := svc.BulkUpdateAccounts(context.Background(), input) require.NoError(t, err) require.Equal(t, 3, result.Success) require.Equal(t, 0, result.Failed) require.ElementsMatch(t, []int64{1, 2, 3}, result.SuccessIDs) require.Empty(t, result.FailedIDs) require.Len(t, result.Results, 3) } // TestAdminService_BulkUpdateAccounts_PartialFailureIDs 验证部分失败时 success_ids/failed_ids 正确。 func TestAdminService_BulkUpdateAccounts_PartialFailureIDs(t *testing.T) { repo := &accountRepoStubForBulkUpdate{ bindGroupErrByID: map[int64]error{ 2: errors.New("bind failed"), }, } svc := &adminServiceImpl{accountRepo: repo} groupIDs := []int64{10} schedulable := false input := &BulkUpdateAccountsInput{ AccountIDs: []int64{1, 2, 3}, GroupIDs: &groupIDs, Schedulable: &schedulable, SkipMixedChannelCheck: true, } result, err := svc.BulkUpdateAccounts(context.Background(), input) require.NoError(t, err) require.Equal(t, 2, result.Success) require.Equal(t, 1, result.Failed) require.ElementsMatch(t, []int64{1, 3}, result.SuccessIDs) require.ElementsMatch(t, []int64{2}, result.FailedIDs) require.Len(t, result.Results, 3) } // TestAdminService_BulkUpdateAccounts_SoraSyncWithoutGroupIDs 验证无分组更新时仍会触发 Sora 同步。 func TestAdminService_BulkUpdateAccounts_SoraSyncWithoutGroupIDs(t *testing.T) { repo := &accountRepoStubForBulkUpdate{ getByIDsAccounts: []*Account{ {ID: 1, Platform: PlatformSora}, }, getByIDAccounts: map[int64]*Account{ 1: {ID: 1, Platform: PlatformSora}, }, } svc := &adminServiceImpl{ accountRepo: repo, soraSyncService: &Sora2APISyncService{}, } schedulable := true input := &BulkUpdateAccountsInput{ AccountIDs: []int64{1}, Schedulable: &schedulable, } result, err := svc.BulkUpdateAccounts(context.Background(), input) require.NoError(t, err) require.Equal(t, 1, result.Success) require.True(t, repo.getByIDsCalled) require.ElementsMatch(t, []int64{1}, repo.getByIDCalled) }