bug处理

This commit is contained in:
truewhile
2026-09-05 01:15:11 +08:00
parent a311438aa1
commit 4790f7753e
17 changed files with 748 additions and 150 deletions
+115
View File
@@ -5,7 +5,11 @@ import (
"encoding/json"
"testing"
"go.uber.org/zap"
"github.com/truewhile/MeBox/internal/config"
"github.com/truewhile/MeBox/internal/model"
"github.com/truewhile/MeBox/internal/repository"
)
func TestStrmAccountConfigPreviewOf(t *testing.T) {
@@ -67,6 +71,117 @@ func TestUpdateStrmAccountMergesConfigWithoutClearingSecrets(t *testing.T) {
}
}
func TestDeleteStrmAccountCascadesEmbyMounts(t *testing.T) {
ctx := context.Background()
db := newServiceTestDB(t, &model.StrmAccount{}, &model.EmbyMount{}, &model.StrmSyncPath{})
repos := repository.New(db)
svc := NewStrmService(nil, zap.NewNop(), repos, nil)
createEmbyAcct := func(id, name string) *model.StrmAccount {
acct := &model.StrmAccount{
Base: model.Base{ID: id},
Name: name,
Provider: model.StrmProviderEmbyRemote,
Enabled: true,
}
if err := repos.StrmAccount.Create(ctx, acct); err != nil {
t.Fatalf("create account %s: %v", id, err)
}
return acct
}
createMounts := func(accountID string, viewIDs ...string) {
for _, vid := range viewIDs {
m := &model.EmbyMount{
AccountID: accountID,
RemoteViewID: vid,
RemoteViewName: "库-" + vid,
Enabled: true,
}
if err := repos.EmbyMount.Create(ctx, m); err != nil {
t.Fatalf("create mount %s: %v", vid, err)
}
}
}
gone := createEmbyAcct("acct-gone", "要删除的账号")
createMounts(gone.ID, "view-1", "view-2", "view-3")
keep := createEmbyAcct("acct-keep", "保留的账号")
createMounts(keep.ID, "view-a")
if err := svc.DeleteStrmAccount(ctx, gone.ID); err != nil {
t.Fatalf("DeleteStrmAccount failed: %v", err)
}
remaining, err := repos.StrmAccount.FindByID(ctx, gone.ID)
if err != nil {
t.Fatalf("find account: %v", err)
}
if remaining != nil {
t.Fatalf("account %s should be deleted", gone.ID)
}
goneMounts, err := repos.EmbyMount.ListByAccountID(ctx, gone.ID)
if err != nil {
t.Fatalf("list mounts: %v", err)
}
if len(goneMounts) != 0 {
t.Fatalf("deleted account still has %d mounts (orphans)", len(goneMounts))
}
keepMounts, err := repos.EmbyMount.ListByAccountID(ctx, keep.ID)
if err != nil {
t.Fatalf("list mounts: %v", err)
}
if len(keepMounts) != 1 || keepMounts[0].RemoteViewID != "view-a" {
t.Fatalf("keep account mounts = %#v, want 1 (view-a)", keepMounts)
}
}
func TestCleanupOrphanMountsRemovesStaleRows(t *testing.T) {
ctx := context.Background()
db := newServiceTestDB(t, &model.StrmAccount{}, &model.EmbyMount{})
repos := repository.New(db)
svc := NewEmbyRemoteService(&config.Config{}, zap.NewNop(), repos, nil)
acct := &model.StrmAccount{
Base: model.Base{ID: "acct-1"},
Name: "emby",
Provider: model.StrmProviderEmbyRemote,
Enabled: true,
}
if err := repos.StrmAccount.Create(ctx, acct); err != nil {
t.Fatalf("create account: %v", err)
}
for i, vid := range []string{"v1", "v2", "v-orphan-1", "v-orphan-2"} {
m := &model.EmbyMount{
Base: model.Base{ID: "mount-" + vid},
AccountID: acct.ID,
RemoteViewID: vid,
Enabled: true,
}
if i >= 2 {
// 模拟历史残留:挂载归属不存在的账号
m.AccountID = "no-such-account"
}
if err := repos.EmbyMount.Create(ctx, m); err != nil {
t.Fatalf("create mount %s: %v", vid, err)
}
}
svc.CleanupOrphanMounts(ctx)
left, err := repos.EmbyMount.List(ctx)
if err != nil {
t.Fatalf("list mounts: %v", err)
}
if len(left) != 2 {
t.Fatalf("mounts after cleanup = %d, want 2 (orphans removed)", len(left))
}
for _, m := range left {
if m.AccountID != acct.ID {
t.Fatalf("mount %s still orphan (account %s)", m.ID, m.AccountID)
}
}
}
func mustJSON(v any) string {
data, err := json.Marshal(v)
if err != nil {