fix: refresh emby sessions in realtime

This commit is contained in:
ShukeBta
2026-06-25 02:32:32 +08:00
parent 29f5df673c
commit baa4a38b0a
12 changed files with 441 additions and 27 deletions
+45
View File
@@ -77,6 +77,26 @@ func TestActivityRefreshKeepsPlaybackState(t *testing.T) {
}
}
func TestLogoutKeepsRealtimeLastActivityWithoutOnlineSession(t *testing.T) {
tracker := NewSessionTrackerService(zap.NewNop())
now := time.Date(2026, 6, 21, 12, 30, 0, 0, time.UTC)
tracker.now = func() time.Time { return now }
old := now.Add(-8 * time.Hour)
users := []model.User{{Base: model.Base{ID: "u1"}, Username: "viewer", LastLoginAt: &old}}
tracker.RecordActivity(t.Context(), "u1", "viewer", "dev-1", "iPhone", "Infuse", "10.0.0.8")
now = now.Add(time.Minute)
tracker.Logout(t.Context(), "u1", "dev-1", "10.0.0.8")
tracker.ApplyToUsers(t.Context(), users)
if users[0].LastLoginAt == nil || !users[0].LastLoginAt.Equal(now) {
t.Fatalf("last_login_at = %v, want logout activity %v", users[0].LastLoginAt, now)
}
if users[0].RealtimeOnline || users[0].RealtimeDeviceCount != 0 {
t.Fatalf("logged-out user should keep last activity but no online devices, online=%v devices=%d", users[0].RealtimeOnline, users[0].RealtimeDeviceCount)
}
}
func TestBotDevicesIncludesRealtimeSessionOnlyDevices(t *testing.T) {
repos, bot := newBotTestService(t)
user := model.User{Base: model.Base{ID: "u1"}, Username: "viewer", PasswordHash: "x", Role: "user", IsActive: true}
@@ -107,6 +127,31 @@ func TestBotDevicesIncludesRealtimeSessionOnlyDevices(t *testing.T) {
}
}
func TestBotUserInfoUsesRealtimeLastLogin(t *testing.T) {
repos, bot := newBotTestService(t)
now := time.Date(2026, 6, 21, 13, 45, 0, 0, time.UTC)
old := now.Add(-6 * time.Hour)
user := model.User{Base: model.Base{ID: "u1"}, Username: "viewer", PasswordHash: "x", Role: "user", IsActive: true, LastLoginAt: &old}
if err := repos.User.Create(t.Context(), &user); err != nil {
t.Fatal(err)
}
tracker := NewSessionTrackerService(zap.NewNop())
tracker.now = func() time.Time { return now }
tracker.RecordActivity(t.Context(), user.ID, user.Username, "dev-1", "Apple TV", "Yamby", "10.0.0.8")
device := NewDeviceService(zap.NewNop(), repos)
device.SetSessionTracker(tracker)
bot.SetDeviceService(device)
reply := bot.cmdMgoUserInfo(t.Context(), []string{"viewer"})
if !strings.Contains(reply.Text, "最后登录:<b>2026-06-21 13:45</b>") {
t.Fatalf("reply should use realtime last login, got %q", reply.Text)
}
if !strings.Contains(reply.Text, "设备:<b>1</b>") {
t.Fatalf("reply should count realtime device, got %q", reply.Text)
}
}
func TestRealtimeRecentLoginProtectsCleanupCandidate(t *testing.T) {
repos := newSessionTrackerTestRepos(t)
now := time.Date(2026, 6, 21, 12, 0, 0, 0, time.UTC)