mirror of
https://github.com/warmbly/warmbly.git
synced 2026-10-26 00:03:46 +00:00
73 lines
2.7 KiB
Go
73 lines
2.7 KiB
Go
package analytics
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/warmbly/warmbly/internal/models"
|
|
"github.com/warmbly/warmbly/internal/repository"
|
|
)
|
|
|
|
func TestPlacementBuilderRollingReadsTheLookback(t *testing.T) {
|
|
from := time.Date(2026, 9, 10, 0, 0, 0, 0, time.UTC)
|
|
to := from.AddDate(0, 0, 1)
|
|
sender := uuid.New()
|
|
b := newPlacementBuilder(from, to)
|
|
// Before the range: only the rolling rate may see it.
|
|
b.addDelivery(repository.WarmupPlacementDayRow{SenderID: sender, Date: "2026-09-05", Group: "google", Inbox: 18, Spam: 2})
|
|
b.addDelivery(repository.WarmupPlacementDayRow{SenderID: sender, Date: "2026-09-10", Group: "microsoft", Inbox: 4, Tabs: 1, Spam: 5, Rescued: 4})
|
|
// A small host counts in the day, never in the rolling rate.
|
|
b.addDelivery(repository.WarmupPlacementDayRow{SenderID: sender, Date: "2026-09-10", Group: "other", Spam: 10})
|
|
|
|
days := b.days()
|
|
if len(days) != 2 {
|
|
t.Fatalf("got %d days", len(days))
|
|
}
|
|
d := days[0]
|
|
if d.Delivered != 20 || d.Spam != 15 || d.Rescued != 4 || *d.InboxRate != 25 {
|
|
t.Fatalf("day counts: %+v", d.WarmupPlacementCounts)
|
|
}
|
|
if d.RollingInboxRate == nil || *d.RollingInboxRate != 76.67 {
|
|
t.Fatalf("rolling rate over 30 deliveries: %v", d.RollingInboxRate)
|
|
}
|
|
if len(d.Groups) != 2 || d.Groups[0].Group != "microsoft" {
|
|
t.Fatalf("groups: %+v", d.Groups)
|
|
}
|
|
if days[1].Delivered != 0 || days[1].InboxRate != nil {
|
|
t.Fatalf("an empty day has no rate: %+v", days[1])
|
|
}
|
|
|
|
boxes := b.mailboxes(map[uuid.UUID]string{sender: "a@example.com"}, nil)
|
|
if len(boxes) != 1 || boxes[0].Delivered != 20 || boxes[0].Rate.Band != models.WarmupPlacementBandNone {
|
|
t.Fatalf("mailboxes: %+v", boxes)
|
|
}
|
|
}
|
|
|
|
func TestApplyWarmupPlacementCapsHealth(t *testing.T) {
|
|
h := models.AccountHealth{Status: "healthy", Score: 100}
|
|
applyWarmupPlacement(&h, nil)
|
|
if h.Score != 100 {
|
|
t.Fatalf("no reading leaves health alone: %+v", h)
|
|
}
|
|
// Small hosts alone leave no headline, so nothing is held against the mailbox.
|
|
small := models.WarmupPlacementWindow{All: models.WarmupPlacementTally{Inbox: 50, Spam: 50}}.Rate()
|
|
applyWarmupPlacement(&h, &small)
|
|
if h.Score != 100 || h.Status != "healthy" {
|
|
t.Fatalf("a small-host reading moved health: %+v", h)
|
|
}
|
|
major := func(inbox, spam int) models.WarmupPlacementRate {
|
|
return models.WarmupPlacementWindow{Major: models.WarmupPlacementTally{Inbox: inbox, Spam: spam}}.Rate()
|
|
}
|
|
r := major(97, 3)
|
|
applyWarmupPlacement(&h, &r)
|
|
if h.Score != 97 || h.Status != "healthy" || len(h.Issues) != 0 {
|
|
t.Fatalf("97%% caps the score and stays healthy: %+v", h)
|
|
}
|
|
r = major(70, 30)
|
|
applyWarmupPlacement(&h, &r)
|
|
if h.Score != 70 || h.Status != "warning" || len(h.Issues) != 1 {
|
|
t.Fatalf("70%% warns: %+v", h)
|
|
}
|
|
}
|