package service import ( "context" "errors" "strings" "testing" "time" "go.uber.org/zap" "github.com/truewhile/MeBox/internal/model" "github.com/truewhile/MeBox/internal/repository" ) // 任务失败必须通知管理员:否则只会停留在任务队列里等人自己发现。 func TestTaskFailureNotifiesAdmin(t *testing.T) { repos := repository.New(newServiceTestDB(t)) tracker := NewTaskTrackerService(zap.NewNop(), nil) type call struct{ text string } var adminCalls []call tracker.SetFailureNotifier(func(_ context.Context, text string) { adminCalls = append(adminCalls, call{text}) }) h := tracker.Start(TaskKindOrganize, "自动整理", TaskUpdate{}) h.Finish(errors.New("disk full"), TaskUpdate{}) if len(adminCalls) != 1 { t.Fatalf("admin notifications = %d, want 1", len(adminCalls)) } if !strings.Contains(adminCalls[0].text, "自动整理") { t.Fatalf("notification = %q, want it to name the task", adminCalls[0].text) } if !strings.Contains(adminCalls[0].text, "disk full") { t.Fatalf("notification = %q, want it to carry the error", adminCalls[0].text) } _ = repos } // 动态字段须 HTML 转义,避免路径/错误里的 <>& 破坏 parse_mode。 func TestTaskFailureAlertEscapesHTML(t *testing.T) { got := formatTaskFailureAlert(BackgroundTask{ Name: "整理