Files
felhom.eu/hub/internal/store/r581_newest_report_test.go
T

46 lines
1.5 KiB
Go

package store
import "testing"
// R-581: two reports for one customer inside the same second. received_at has second granularity, so
// the dashboard's newest-report read must tie-break on insertion order — one row per customer, and it
// is the SECOND report's health and version. The received_at values are forced equal so the tie is
// certain rather than depending on the test's timing.
func TestGetCustomers_SameSecondReportsOneRowNewestWins(t *testing.T) {
s := langStore(t)
if err := s.SaveReport("tie", []byte(`{"controller_version":"0.1.0","health":{"status":"fail"}}`)); err != nil {
t.Fatal(err)
}
if err := s.SaveReport("tie", []byte(`{"controller_version":"0.2.0","health":{"status":"ok"}}`)); err != nil {
t.Fatal(err)
}
if _, err := s.db.Exec(`UPDATE reports SET received_at = '2026-10-05 12:00:00' WHERE customer_id = 'tie'`); err != nil {
t.Fatal(err)
}
custs, err := s.GetCustomers()
if err != nil {
t.Fatal(err)
}
var got []CustomerSummary
for _, c := range custs {
if c.CustomerID == "tie" {
got = append(got, c)
}
}
if len(got) != 1 {
t.Fatalf("GetCustomers returned %d rows for one customer, want exactly 1", len(got))
}
if got[0].ControllerVersion != "0.2.0" || got[0].HealthStatus != "ok" {
t.Fatalf("newest report lost the tie: version=%q health=%q, want 0.2.0/ok", got[0].ControllerVersion, got[0].HealthStatus)
}
one, err := s.GetCustomer("tie")
if err != nil {
t.Fatal(err)
}
if one.ControllerVersion != "0.2.0" {
t.Fatalf("GetCustomer newest = %q, want 0.2.0", one.ControllerVersion)
}
}