refactor(channels): tighten types and error paths per second review
- service: drop groupRepo nil guard (DI must inject), switch SupportedModels to SliceStable to match doc - frontend: reuse user-side DTO types in SupportedModelChip/AvailableChannelsTable instead of duplicating shapes; narrow admin statusLabel param to ChannelStatus - tests: replace nil-groupRepo case with ListAll/ListActive error propagation and BillingModelSource default-backfill coverage
This commit is contained in:
@@ -509,7 +509,7 @@ func (c *Channel) SupportedModels() []SupportedModel {
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(result, func(i, j int) bool {
|
||||
sort.SliceStable(result, func(i, j int) bool {
|
||||
if result[i].Platform != result[j].Platform {
|
||||
return result[i].Platform < result[j].Platform
|
||||
}
|
||||
|
||||
@@ -38,19 +38,17 @@ func (s *ChannelService) ListAvailable(ctx context.Context) ([]AvailableChannel,
|
||||
return nil, fmt.Errorf("list channels: %w", err)
|
||||
}
|
||||
|
||||
groupByID := make(map[int64]AvailableGroupRef)
|
||||
if s.groupRepo != nil {
|
||||
groups, err := s.groupRepo.ListActive(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list active groups: %w", err)
|
||||
}
|
||||
for i := range groups {
|
||||
g := groups[i]
|
||||
groupByID[g.ID] = AvailableGroupRef{
|
||||
ID: g.ID,
|
||||
Name: g.Name,
|
||||
Platform: g.Platform,
|
||||
}
|
||||
groups, err := s.groupRepo.ListActive(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list active groups: %w", err)
|
||||
}
|
||||
groupByID := make(map[int64]AvailableGroupRef, len(groups))
|
||||
for i := range groups {
|
||||
g := groups[i]
|
||||
groupByID[g.ID] = AvailableGroupRef{
|
||||
ID: g.ID,
|
||||
Name: g.Name,
|
||||
Platform: g.Platform,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
|
||||
@@ -12,11 +13,16 @@ import (
|
||||
|
||||
// stubGroupRepoForAvailable 是 ListAvailable 测试用的 GroupRepository stub,
|
||||
// 仅实现 ListActive;其他方法对本测试无关,返回零值即可。
|
||||
// listActiveErr 非 nil 时,ListActive 返回该错误用于错误传播测试。
|
||||
type stubGroupRepoForAvailable struct {
|
||||
activeGroups []Group
|
||||
activeGroups []Group
|
||||
listActiveErr error
|
||||
}
|
||||
|
||||
func (s *stubGroupRepoForAvailable) ListActive(ctx context.Context) ([]Group, error) {
|
||||
if s.listActiveErr != nil {
|
||||
return nil, s.listActiveErr
|
||||
}
|
||||
return s.activeGroups, nil
|
||||
}
|
||||
|
||||
@@ -61,7 +67,7 @@ func (s *stubGroupRepoForAvailable) UpdateSortOrders(ctx context.Context, update
|
||||
}
|
||||
|
||||
// newAvailableChannelService 构造一个 ChannelService,channelRepo.ListAll 返回给定 channels,
|
||||
// groupRepo 由参数决定(可传 nil 测试 nil 分支)。
|
||||
// groupRepo 由参数决定。传入空 stub 表示「活跃分组列表为空」。
|
||||
func newAvailableChannelService(channels []Channel, groupRepo GroupRepository) *ChannelService {
|
||||
repo := &mockChannelRepository{
|
||||
listAllFn: func(ctx context.Context) ([]Channel, error) { return channels, nil },
|
||||
@@ -69,15 +75,15 @@ func newAvailableChannelService(channels []Channel, groupRepo GroupRepository) *
|
||||
return NewChannelService(repo, groupRepo, nil)
|
||||
}
|
||||
|
||||
func TestListAvailable_NilGroupRepo_NoGroupsAttached(t *testing.T) {
|
||||
// groupRepo 为 nil 时不应 panic,且每个渠道的 Groups 应为空切片。
|
||||
func TestListAvailable_EmptyActiveGroups_NoGroupsAttached(t *testing.T) {
|
||||
// 活跃分组列表为空时,渠道的 Groups 应为空切片,不报错。
|
||||
channels := []Channel{{
|
||||
ID: 1,
|
||||
Name: "chA",
|
||||
Status: StatusActive,
|
||||
GroupIDs: []int64{10, 20},
|
||||
}}
|
||||
svc := newAvailableChannelService(channels, nil)
|
||||
svc := newAvailableChannelService(channels, &stubGroupRepoForAvailable{})
|
||||
out, err := svc.ListAvailable(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Len(t, out, 1)
|
||||
@@ -109,7 +115,7 @@ func TestListAvailable_SortedByName(t *testing.T) {
|
||||
{ID: 2, Name: "Alpha"},
|
||||
{ID: 3, Name: "charlie"},
|
||||
}
|
||||
svc := newAvailableChannelService(channels, nil)
|
||||
svc := newAvailableChannelService(channels, &stubGroupRepoForAvailable{})
|
||||
out, err := svc.ListAvailable(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Len(t, out, 3)
|
||||
@@ -117,3 +123,42 @@ func TestListAvailable_SortedByName(t *testing.T) {
|
||||
require.Equal(t, "beta", out[1].Name)
|
||||
require.Equal(t, "charlie", out[2].Name)
|
||||
}
|
||||
|
||||
func TestListAvailable_ListAllErrorPropagates(t *testing.T) {
|
||||
// ListAll 返回错误时 ListAvailable 应直接返回包装后的错误,不再访问 groupRepo。
|
||||
sentinel := errors.New("list-all-boom")
|
||||
repo := &mockChannelRepository{
|
||||
listAllFn: func(ctx context.Context) ([]Channel, error) { return nil, sentinel },
|
||||
}
|
||||
svc := NewChannelService(repo, &stubGroupRepoForAvailable{}, nil)
|
||||
out, err := svc.ListAvailable(context.Background())
|
||||
require.Nil(t, out)
|
||||
require.ErrorIs(t, err, sentinel)
|
||||
}
|
||||
|
||||
func TestListAvailable_ListActiveErrorPropagates(t *testing.T) {
|
||||
// groupRepo.ListActive 返回错误时 ListAvailable 应直接返回包装后的错误。
|
||||
sentinel := errors.New("list-active-boom")
|
||||
svc := newAvailableChannelService(
|
||||
[]Channel{{ID: 1, Name: "chA"}},
|
||||
&stubGroupRepoForAvailable{listActiveErr: sentinel},
|
||||
)
|
||||
out, err := svc.ListAvailable(context.Background())
|
||||
require.Nil(t, out)
|
||||
require.ErrorIs(t, err, sentinel)
|
||||
}
|
||||
|
||||
func TestListAvailable_DefaultsEmptyBillingModelSource(t *testing.T) {
|
||||
// 渠道 BillingModelSource 为空时应回填为 BillingModelSourceChannelMapped,
|
||||
// 显式值应原样保留(由 service 层统一处理,避免各 handler 重复默认逻辑)。
|
||||
channels := []Channel{
|
||||
{ID: 1, Name: "empty", BillingModelSource: ""},
|
||||
{ID: 2, Name: "explicit", BillingModelSource: BillingModelSourceUpstream},
|
||||
}
|
||||
svc := newAvailableChannelService(channels, &stubGroupRepoForAvailable{})
|
||||
out, err := svc.ListAvailable(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Len(t, out, 2)
|
||||
require.Equal(t, BillingModelSourceChannelMapped, out[0].BillingModelSource)
|
||||
require.Equal(t, BillingModelSourceUpstream, out[1].BillingModelSource)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user