package store import ( "context" "testing" "time" ) func newTestDevice(t *testing.T) (*Store, *Device) { t.Helper() st := testStore(t) dev, err := st.CreateDevice(t.Context(), "Harry's phone", "Harry", PlatformAndroid, "sam") if err != nil { t.Fatalf("CreateDevice: %v", err) } return st, dev } func TestSetPresenceAuditsOnlyTransitions(t *testing.T) { st, dev := newTestDevice(t) ctx := context.Background() // The first "online" flips the flag, so it is audited. changed, err := st.SetPresence(ctx, dev.ID, true, time.Now(), "device") if err != nil { t.Fatalf("SetPresence: %v", err) } if !changed { t.Error("changed = false on the first online report, want true") } // A repeat report only refreshes last_seen. changed, err = st.SetPresence(ctx, dev.ID, true, time.Now().Add(time.Minute), "device") if err != nil { t.Fatalf("SetPresence: %v", err) } if changed { t.Error("changed = true on a repeat report, want false") } // The will payload flips it back. changed, err = st.SetPresence(ctx, dev.ID, false, time.Now(), "device") if err != nil { t.Fatalf("SetPresence: %v", err) } if !changed { t.Error("changed = false when the device went offline, want true") } got, err := st.GetDevice(ctx, dev.ID) if err != nil { t.Fatalf("GetDevice: %v", err) } if got.Online { t.Error("Online = true after the will, want false") } if got.LastSeen == nil { t.Fatal("LastSeen is nil, want the report time") } audit, err := st.ListAudit(ctx, 50) if err != nil { t.Fatalf("ListAudit: %v", err) } var online, offline int for _, e := range audit { switch e.Action { case "device.online": online++ case "device.offline": offline++ } } if online != 1 || offline != 1 { t.Errorf("audit has %d online and %d offline entries, want 1 and 1", online, offline) } } func TestInsertTelemetryReplacesTheDailyTotal(t *testing.T) { st, dev := newTestDevice(t) ctx := context.Background() const day = "2026-10-13" n, err := st.InsertTelemetry(ctx, dev.ID, day, []TelemetryEntry{ {App: "com.spotify.music", Seconds: 600}, {Domain: "youtube.com", Seconds: 120}, }) if err != nil { t.Fatalf("InsertTelemetry: %v", err) } if n != 2 { t.Errorf("rows = %d, want 2", n) } // Devices report rolling totals, so a repeat report replaces the row. if _, err := st.InsertTelemetry(ctx, dev.ID, day, []TelemetryEntry{ {App: "com.spotify.music", Seconds: 900}, }); err != nil { t.Fatalf("InsertTelemetry repeat: %v", err) } rows, err := st.TelemetryForDay(ctx, dev.ID, day) if err != nil { t.Fatalf("TelemetryForDay: %v", err) } if len(rows) != 2 { t.Fatalf("rows = %+v, want 2 (a replace, not an insert)", rows) } if rows[0].App != "com.spotify.music" || rows[0].Seconds != 900 { t.Errorf("rows[0] = %+v, want the replaced total 900", rows[0]) } if rows[1].Seconds != 120 { t.Errorf("rows[1] = %+v", rows[1]) } // Another day is a different row. if _, err := st.InsertTelemetry(ctx, dev.ID, "2026-10-14", []TelemetryEntry{ {App: "com.spotify.music", Seconds: 60}, }); err != nil { t.Fatalf("InsertTelemetry next day: %v", err) } rows, err = st.TelemetryForDay(ctx, dev.ID, day) if err != nil { t.Fatalf("TelemetryForDay: %v", err) } if len(rows) != 2 || rows[0].Seconds != 900 { t.Errorf("rows for %s = %+v, want the day isolated", day, rows) } // A negative total is dropped, and an empty report is a no-op. if n, err := st.InsertTelemetry(ctx, dev.ID, day, []TelemetryEntry{{App: "x", Seconds: -5}}); err != nil || n != 0 { t.Errorf("negative telemetry = (%d, %v), want (0, nil)", n, err) } if n, err := st.InsertTelemetry(ctx, dev.ID, day, nil); err != nil || n != 0 { t.Errorf("empty telemetry = (%d, %v), want (0, nil)", n, err) } } func TestDeviceListsAndTopicLookup(t *testing.T) { st, dev := newTestDevice(t) ctx := context.Background() // The Phase 6 editors do not exist yet, so seed the tables directly. seed := []struct { table string kind string value string }{ {"allowlist", "app", "com.spotify.music"}, {"allowlist", "site", "school.example.edu"}, {"blocklist", "app", "com.roblox.client"}, } for _, s := range seed { if _, err := st.DB().ExecContext(ctx, `INSERT INTO `+s.table+` (device_id, kind, value) VALUES (?, ?, ?)`, dev.ID, s.kind, s.value); err != nil { t.Fatalf("seed %s: %v", s.table, err) } } // A NULL device_id applies to every device, and duplicates collapse. if _, err := st.DB().ExecContext(ctx, `INSERT INTO allowlist (device_id, kind, value) VALUES (NULL, 'app', 'com.spotify.music')`); err != nil { t.Fatalf("seed global allow: %v", err) } allow, block, err := st.DeviceLists(ctx, dev.ID) if err != nil { t.Fatalf("DeviceLists: %v", err) } if len(allow) != 2 || allow[0] != "com.spotify.music" || allow[1] != "school.example.edu" { t.Errorf("allow = %v", allow) } if len(block) != 1 || block[0] != "com.roblox.client" { t.Errorf("block = %v", block) } found, err := st.DeviceByConfigTopic(ctx, dev.Topic) if err != nil { t.Fatalf("DeviceByConfigTopic: %v", err) } if found.ID != dev.ID { t.Errorf("DeviceByConfigTopic = %d, want %d", found.ID, dev.ID) } if _, err := st.DeviceByConfigTopic(ctx, "restrict/nope_99/config"); err != ErrNotFound { t.Errorf("unknown topic error = %v, want ErrNotFound", err) } } func TestTelemetryRollupAndWeekQueries(t *testing.T) { st, dev := newTestDevice(t) ctx := context.Background() // Three days of usage. Apps and domains are reported for the same day on // purpose, to prove the rollup keeps them apart instead of double-counting. days := []struct { day string app string appSecs int domain string webSecs int }{ {"2026-10-12", "com.spotify.music", 600, "youtube.com", 120}, {"2026-10-13", "com.spotify.music", 900, "youtube.com", 300}, {"2026-10-13", "com.roblox.client", 1800, "roblox.com", 1800}, {"2026-10-14", "com.roblox.client", 3600, "roblox.com", 3600}, } for _, d := range days { if _, err := st.InsertTelemetry(ctx, dev.ID, d.day, []TelemetryEntry{ {App: d.app, Seconds: d.appSecs}, {Domain: d.domain, Seconds: d.webSecs}, }); err != nil { t.Fatalf("InsertTelemetry %s: %v", d.day, err) } } usage, err := st.UsageRange(ctx, dev.ID, "2026-10-12", "2026-10-14") if err != nil { t.Fatalf("UsageRange: %v", err) } if len(usage) != 3 { t.Fatalf("usage rows = %d, want 3", len(usage)) } if usage[0].Day != "2026-10-12" || usage[0].AppSeconds != 600 || usage[0].DomainSeconds != 120 { t.Errorf("day 1 = %+v", usage[0]) } if usage[1].AppSeconds != 2700 || usage[1].DomainSeconds != 2100 { t.Errorf("day 2 = %+v, want apps 2700 and domains 2100 (no cross-counting)", usage[1]) } if usage[2].AppSeconds != 3600 || usage[2].DomainSeconds != 3600 { t.Errorf("day 3 = %+v", usage[2]) } // A repeated report replaces the day's total, and the rollup follows. if _, err := st.InsertTelemetry(ctx, dev.ID, "2026-10-13", []TelemetryEntry{ {App: "com.roblox.client", Seconds: 200}, }); err != nil { t.Fatalf("InsertTelemetry replace: %v", err) } usage, _ = st.UsageRange(ctx, dev.ID, "2026-10-13", "2026-10-13") if len(usage) != 1 || usage[0].AppSeconds != 1100 { t.Errorf("day 2 after replace = %+v, want apps 1100 (spotify 900 + roblox 200)", usage) } // Top lists are ordered by total seconds. topApps, err := st.TopUsage(ctx, dev.ID, "2026-10-12", "2026-10-14", "app", 10) if err != nil { t.Fatalf("TopUsage app: %v", err) } if len(topApps) != 2 || topApps[0].Name != "com.roblox.client" || topApps[0].Seconds != 3800 { t.Errorf("top apps = %+v, want roblox first with 3800", topApps) } topDomains, err := st.TopUsage(ctx, dev.ID, "2026-10-12", "2026-10-14", "domain", 10) if err != nil { t.Fatalf("TopUsage domain: %v", err) } if len(topDomains) != 2 || topDomains[0].Name != "roblox.com" || topDomains[0].Seconds != 5400 { t.Errorf("top domains = %+v", topDomains) } // An unknown kind and a range with no data are both empty, not errors. if got, err := st.TopUsage(ctx, dev.ID, "2026-10-12", "2026-10-14", "nonsense", 10); err != nil || len(got) != 0 { t.Errorf("TopUsage nonsense = (%+v, %v), want empty", got, err) } if got, err := st.UsageRange(ctx, dev.ID, "2020-01-01", "2020-01-07"); err != nil || len(got) != 0 { t.Errorf("UsageRange empty = (%+v, %v), want empty", got, err) } } func TestRebuildRollupsAndCapAudit(t *testing.T) { st, dev := newTestDevice(t) ctx := context.Background() // Simulate pre-Phase-7 data: telemetry written before the rollup existed. if _, err := st.DB().ExecContext(ctx, `INSERT INTO telemetry (device_id, app, domain, seconds, day) VALUES (?, '', 'example.com', 300, '2026-10-13')`, dev.ID); err != nil { t.Fatalf("seed telemetry: %v", err) } var count int if err := st.DB().QueryRowContext(ctx, `SELECT COUNT(*) FROM telemetry_daily`).Scan(&count); err != nil { t.Fatalf("count rollups: %v", err) } if count != 0 { t.Fatalf("telemetry_daily has %d rows before rebuild, want 0", count) } n, err := st.RebuildRollups(ctx) if err != nil { t.Fatalf("RebuildRollups: %v", err) } if n != 1 { t.Errorf("RebuildRollups = %d rows, want 1", n) } usage, err := st.UsageRange(ctx, dev.ID, "2026-10-13", "2026-10-13") if err != nil || len(usage) != 1 || usage[0].DomainSeconds != 300 { t.Fatalf("usage after rebuild = (%+v, %v)", usage, err) } // The audit cap keeps the newest rows and never edits the rest. for i := 0; i < 25; i++ { if err := st.Audit(ctx, "sam", "test", nil, "row"); err != nil { t.Fatalf("Audit: %v", err) } } removed, err := st.CapAudit(ctx, 10) if err != nil { t.Fatalf("CapAudit: %v", err) } if removed == 0 { t.Error("CapAudit removed nothing, want the oldest rows gone") } if err := st.DB().QueryRowContext(ctx, `SELECT COUNT(*) FROM audit`).Scan(&count); err != nil { t.Fatalf("count audit: %v", err) } if count != 10 { t.Errorf("audit rows = %d, want 10", count) } // A no-op cap leaves the log alone. if n, err := st.CapAudit(ctx, 100); err != nil || n != 0 { t.Errorf("CapAudit(100) = (%d, %v), want (0, nil)", n, err) } }