topfans/backend/gateway/service/app_download_service_test.go

111 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/topfans/backend/pkg/models"
)
// fakeRepo 是 appDownloadRepo 的 in-memory fake 实现
type fakeRepo struct {
configs []models.AppDownloadConfig
}
func (f *fakeRepo) FindByType(ctx context.Context, pkgType string) ([]models.AppDownloadConfig, error) {
var result []models.AppDownloadConfig
for _, c := range f.configs {
if c.Type == pkgType {
result = append(result, c)
}
}
return result, nil
}
func (f *fakeRepo) UpsertAll(ctx context.Context, configs []models.AppDownloadConfig) error {
for _, incoming := range configs {
found := false
for i, existing := range f.configs {
if existing.Platform == incoming.Platform && existing.Type == incoming.Type {
f.configs[i].DownloadURL = incoming.DownloadURL
f.configs[i].Version = incoming.Version
f.configs[i].UpdatedAt = incoming.UpdatedAt
found = true
break
}
}
if !found {
f.configs = append(f.configs, incoming)
}
}
return nil
}
func TestGetAllNativeApp_FiltersWgt(t *testing.T) {
svc := &AppDownloadService{
repo: &fakeRepo{configs: []models.AppDownloadConfig{
{Platform: "android", Type: "native_app", DownloadURL: "https://app.apk"},
{Platform: "android", Type: "wgt", DownloadURL: "https://wgt.wgt"},
}},
}
configs, err := svc.GetAllNativeApp(context.Background())
assert.NoError(t, err)
assert.Len(t, configs, 1)
assert.Equal(t, "native_app", configs[0].Type)
}
func TestGetAllNativeApp_Empty(t *testing.T) {
svc := &AppDownloadService{
repo: &fakeRepo{},
}
configs, err := svc.GetAllNativeApp(context.Background())
assert.NoError(t, err)
assert.Len(t, configs, 0)
}
func TestSyncVersion_BothPlatforms(t *testing.T) {
fake := &fakeRepo{}
svc := &AppDownloadService{repo: fake}
err := svc.SyncVersion(context.Background(), &SyncVersionRequest{
Android: &PlatformVersionInfo{URL: "https://a.apk", Version: "1.0.5", Type: "native_app"},
IOS: &PlatformVersionInfo{URL: "https://apps.apple.com/...", Version: "1.0.5", Type: "native_app"},
})
assert.NoError(t, err)
assert.Len(t, fake.configs, 2)
}
func TestSyncVersion_PartialUpdate(t *testing.T) {
fake := &fakeRepo{
configs: []models.AppDownloadConfig{
{Platform: "android", Type: "native_app", DownloadURL: "https://old.apk", Version: "1.0.0"},
},
}
svc := &AppDownloadService{repo: fake}
// 只更新 android不传 ios
err := svc.SyncVersion(context.Background(), &SyncVersionRequest{
Android: &PlatformVersionInfo{URL: "https://new.apk", Version: "2.0.0", Type: "native_app"},
})
assert.NoError(t, err)
assert.Len(t, fake.configs, 1) // 仍然是 1 条
assert.Equal(t, "https://new.apk", fake.configs[0].DownloadURL)
assert.Equal(t, "2.0.0", fake.configs[0].Version)
}
func TestSyncVersion_NilPlatform(t *testing.T) {
fake := &fakeRepo{}
svc := &AppDownloadService{repo: fake}
// 只传 iOSandroid 为 nil
err := svc.SyncVersion(context.Background(), &SyncVersionRequest{
IOS: &PlatformVersionInfo{URL: "https://apps.apple.com/...", Version: "1.0.5", Type: "native_app"},
})
assert.NoError(t, err)
assert.Len(t, fake.configs, 1)
assert.Equal(t, "ios", fake.configs[0].Platform)
}