package store_test import ( "context" "testing" "time" "github.com/wangjia/pangolin/server/internal/devices" "github.com/wangjia/pangolin/server/internal/sessions" ) // ListDevices should derive online (last_seen<3min), client_version, and // last_login (most recent session) per device. func TestSQLite_ListDevices_OnlineAndLastLogin(t *testing.T) { ctx := context.Background() db := openSQLite(t) if _, err := db.Exec( `INSERT INTO users (id, uuid, email, pw_hash, dp_uuid, status) VALUES (1,'u','u@e.com','h','dp','active')`); err != nil { t.Fatalf("seed user: %v", err) } now := time.Now().UTC() old := now.Add(-time.Hour) // device 10: online (last_seen now) + client_version; device 20: offline. db.Exec(`INSERT INTO devices (id, uuid, user_id, name, platform, last_seen, client_version) VALUES (10,'d10',1,'Mac','macos',?,?)`, now, "v1.0.10") db.Exec(`INSERT INTO devices (id, uuid, user_id, name, platform, last_seen) VALUES (20,'d20',1,'PC','windows',?)`, old) // one login session for device 10. db.Exec(`INSERT INTO sessions (user_id, device_id, refresh_jti, created_at) VALUES (1,10,'j',?)`, now) svc := devices.NewService(devices.NewStore(db), nil) svc.SetSessionPort(sessions.NewStore(db)) list, apiErr := svc.ListDevices(ctx, 1) if apiErr != nil { t.Fatalf("ListDevices: %v", apiErr) } if len(list) != 2 { t.Fatalf("want 2 devices, got %d", len(list)) } by := map[string]devices.Device{} for _, d := range list { by[d.UUID] = d } if d := by["d10"]; !d.Online || d.ClientVersion != "v1.0.10" || d.LastLogin == nil { t.Errorf("d10 wrong: online=%v ver=%q lastLogin=%v", d.Online, d.ClientVersion, d.LastLogin) } if d := by["d20"]; d.Online || d.LastLogin != nil { t.Errorf("d20 wrong: online=%v lastLogin=%v", d.Online, d.LastLogin) } }