fix: address code review findings
This commit is contained in:
@@ -23,9 +23,9 @@ func (f fixedSchedule) Next(t time.Time) time.Time {
|
||||
|
||||
// collectSender records messages and can be told to fail.
|
||||
type collectSender struct {
|
||||
mu sync.Mutex
|
||||
mu sync.Mutex
|
||||
messages []string
|
||||
failErr error
|
||||
failErr error
|
||||
}
|
||||
|
||||
func (s *collectSender) Send(ctx context.Context, message string) error {
|
||||
@@ -79,7 +79,7 @@ func TestNotifyOnce_SendsAndMarksSent(t *testing.T) {
|
||||
|
||||
sender := &collectSender{}
|
||||
cfg := config.TelegramConfig{Enabled: true}
|
||||
n, err := NotifyOnce(context.Background(), db, sender, cfg, "http://ui:8080")
|
||||
n, err := NotifyOnce(context.Background(), db, sender, cfg, "http://ui:8080", 0.85)
|
||||
if err != nil {
|
||||
t.Fatalf("NotifyOnce: %v", err)
|
||||
}
|
||||
@@ -100,6 +100,48 @@ func TestNotifyOnce_SendsAndMarksSent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotifyOnce_OwnedReleaseNotNotified(t *testing.T) {
|
||||
// Regression: a cached external release the user already owns locally must
|
||||
// not be reported as "missing". Before the scanner intersection was added,
|
||||
// NotifyOnce treated every unnotified external release as missing and would
|
||||
// spam the operator with releases they already have in Navidrome.
|
||||
db, err := database.New(":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("New(): %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
seedRelease(t, db, "rgid-owned", "artist-1", false)
|
||||
// The user already has this album locally, with a matching normalized title.
|
||||
if _, err := db.Conn().Exec(
|
||||
"INSERT INTO local_albums (id, artist_id, title) VALUES (?, ?, ?)",
|
||||
"local-1", "artist-1", "Release rgid-owned",
|
||||
); err != nil {
|
||||
t.Fatalf("seed local album: %v", err)
|
||||
}
|
||||
|
||||
sender := &collectSender{}
|
||||
n, err := NotifyOnce(context.Background(), db, sender, config.TelegramConfig{}, "http://ui", 0.85)
|
||||
if err != nil {
|
||||
t.Fatalf("NotifyOnce: %v", err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Fatalf("expected 0 notified (release already owned), got %d", n)
|
||||
}
|
||||
if sender.count() != 0 {
|
||||
t.Fatalf("expected no digest for an owned release, got %d message(s)", sender.count())
|
||||
}
|
||||
// The owned release is genuinely missing per the scanner, so it must remain
|
||||
// un-marked-sent to avoid corrupting notifications_sent state.
|
||||
sent, err := database.IsNotificationSent(db, "rgid-owned")
|
||||
if err != nil {
|
||||
t.Fatalf("IsNotificationSent: %v", err)
|
||||
}
|
||||
if sent {
|
||||
t.Error("owned release should NOT be marked sent")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotifyOnce_EmptyDoesNotSend(t *testing.T) {
|
||||
db, err := database.New(":memory:")
|
||||
if err != nil {
|
||||
@@ -108,7 +150,7 @@ func TestNotifyOnce_EmptyDoesNotSend(t *testing.T) {
|
||||
defer db.Close()
|
||||
|
||||
sender := &collectSender{}
|
||||
n, err := NotifyOnce(context.Background(), db, sender, config.TelegramConfig{}, "http://ui")
|
||||
n, err := NotifyOnce(context.Background(), db, sender, config.TelegramConfig{}, "http://ui", 0.85)
|
||||
if err != nil {
|
||||
t.Fatalf("NotifyOnce: %v", err)
|
||||
}
|
||||
@@ -132,7 +174,7 @@ func TestNotifyOnce_SkipsAlreadySent(t *testing.T) {
|
||||
seedRelease(t, db, "rgid-new", "artist-1", false)
|
||||
|
||||
sender := &collectSender{}
|
||||
n, err := NotifyOnce(context.Background(), db, sender, config.TelegramConfig{}, "http://ui")
|
||||
n, err := NotifyOnce(context.Background(), db, sender, config.TelegramConfig{}, "http://ui", 0.85)
|
||||
if err != nil {
|
||||
t.Fatalf("NotifyOnce: %v", err)
|
||||
}
|
||||
@@ -168,7 +210,7 @@ func TestNotifyOnce_SendErrorNotMarked(t *testing.T) {
|
||||
|
||||
want := errors.New("send boom")
|
||||
sender := &collectSender{failErr: want}
|
||||
_, err = NotifyOnce(context.Background(), db, sender, config.TelegramConfig{}, "http://ui")
|
||||
_, err = NotifyOnce(context.Background(), db, sender, config.TelegramConfig{}, "http://ui", 0.85)
|
||||
if err == nil || !errors.Is(err, want) {
|
||||
t.Fatalf("expected error %v, got %v", want, err)
|
||||
}
|
||||
@@ -189,7 +231,7 @@ func TestNotifyOnce_NilSender(t *testing.T) {
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
if _, err := NotifyOnce(context.Background(), db, nil, config.TelegramConfig{}, "http://ui"); err == nil {
|
||||
if _, err := NotifyOnce(context.Background(), db, nil, config.TelegramConfig{}, "http://ui", 0.85); err == nil {
|
||||
t.Fatal("expected error for nil sender")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user