33 lines
1.0 KiB
Go
33 lines
1.0 KiB
Go
package service
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"kefu-cloud/server/internal/model"
|
|
)
|
|
|
|
func TestVisitorIsOnlineByHeartbeatAndLeave(t *testing.T) {
|
|
now := time.Now()
|
|
recent := now.Add(-20 * time.Second)
|
|
stale := now.Add(-VisitorOnlineTimeout - time.Second)
|
|
left := now.Add(-10 * time.Second)
|
|
|
|
if !VisitorIsOnline(&model.Session{Status: "active", LastSeenAt: &recent}, now) {
|
|
t.Fatal("最近有心跳的进行中会话应在线")
|
|
}
|
|
if VisitorIsOnline(&model.Session{Status: "active", LastSeenAt: &stale}, now) {
|
|
t.Fatal("超过 45 秒无心跳应离线")
|
|
}
|
|
if VisitorIsOnline(&model.Session{Status: "active", LastSeenAt: &recent, VisitorLeftAt: &left}, now) {
|
|
t.Fatal("显式离开后应离线")
|
|
}
|
|
afterReturn := now.Add(-5 * time.Second)
|
|
if !VisitorIsOnline(&model.Session{Status: "active", LastSeenAt: &afterReturn, VisitorLeftAt: &left}, now) {
|
|
t.Fatal("离开后重新心跳应恢复在线")
|
|
}
|
|
if VisitorIsOnline(&model.Session{Status: "ended", LastSeenAt: &recent}, now) {
|
|
t.Fatal("已结束会话不应在线")
|
|
}
|
|
}
|