Files
geo/server/internal/tenant/app/desktop_account_service_test.go
T
Xu Liang 680adf7b93
Backend CI / Backend (push) Successful in 14m18s
fix(desktop): scope account access to owning client and extend identity verification
Restrict account tracking, probing, and health reporting to accounts owned
by the current client (client_id match). Extend identity-match verification
to bilibili, juejin, and smzdm so session validity is confirmed locally
rather than deferred to remote health state. Server-side account view now
uses stored client_id as authoritative owner instead of presence data.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-05 22:09:10 +08:00

160 lines
4.1 KiB
Go

package app
import (
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/geo-platform/tenant-api/internal/tenant/repository"
)
func TestResolveDesktopClientOnline_PresenceHitWins(t *testing.T) {
now := time.Date(2026, 4, 19, 21, 30, 0, 0, time.UTC)
online := resolveDesktopClientOnline(&repository.DesktopClient{
ID: uuid.New(),
LastSeenAt: nil,
}, true, true, now)
assert.True(t, online)
}
func TestResolveDesktopClientOnline_FallsBackToRecentHeartbeatWhenPresenceMisses(t *testing.T) {
now := time.Date(2026, 4, 19, 21, 30, 0, 0, time.UTC)
lastSeen := now.Add(-30 * time.Second)
online := resolveDesktopClientOnline(&repository.DesktopClient{
ID: uuid.New(),
LastSeenAt: &lastSeen,
}, false, true, now)
assert.True(t, online)
}
func TestResolveDesktopClientOnline_StaleHeartbeatIsOffline(t *testing.T) {
now := time.Date(2026, 4, 19, 21, 30, 0, 0, time.UTC)
lastSeen := now.Add(-(desktopClientPresenceTTL + time.Second))
online := resolveDesktopClientOnline(&repository.DesktopClient{
ID: uuid.New(),
LastSeenAt: &lastSeen,
}, false, false, now)
assert.False(t, online)
}
func TestBuildDesktopAccountView_StoredClientOwnershipWinsOverPresence(t *testing.T) {
service := &DesktopAccountService{}
accountID := uuid.New()
staleClientID := uuid.New()
activeClientID := uuid.New()
lastSeen := time.Date(2026, 4, 19, 21, 30, 0, 0, time.UTC)
deviceName := "Desktop MacIntel"
view := service.buildDesktopAccountView(
&repository.DesktopAccount{
DesktopID: accountID,
ClientID: &staleClientID,
Platform: "toutiaohao",
PlatformUID: "platform:4181862206",
DisplayName: "ireader",
Health: "live",
},
map[uuid.UUID]*repository.DesktopClient{
activeClientID: {
ID: activeClientID,
LastSeenAt: &lastSeen,
DeviceName: &deviceName,
},
},
map[uuid.UUID]uuid.UUID{
accountID: activeClientID,
},
)
if assert.NotNil(t, view.ClientID) {
assert.Equal(t, staleClientID.String(), *view.ClientID)
}
if assert.NotNil(t, view.ClientOnline) {
assert.False(t, *view.ClientOnline)
}
assert.Equal(t, "4181862206", view.PlatformUID)
assert.Nil(t, view.ClientLastSeenAt)
assert.Nil(t, view.ClientDeviceName)
}
func TestBuildDesktopAccountView_FallsBackToStoredClientWhenNoPresence(t *testing.T) {
service := &DesktopAccountService{}
accountID := uuid.New()
storedClientID := uuid.New()
lastSeen := time.Date(2026, 4, 19, 21, 30, 0, 0, time.UTC)
view := service.buildDesktopAccountView(
&repository.DesktopAccount{
DesktopID: accountID,
ClientID: &storedClientID,
Platform: "toutiaohao",
PlatformUID: "4181862206",
DisplayName: "ireader",
Health: "live",
},
map[uuid.UUID]*repository.DesktopClient{
storedClientID: {
ID: storedClientID,
LastSeenAt: &lastSeen,
},
},
nil,
)
if assert.NotNil(t, view.ClientID) {
assert.Equal(t, storedClientID.String(), *view.ClientID)
}
if assert.NotNil(t, view.ClientOnline) {
assert.False(t, *view.ClientOnline)
}
assert.Equal(t, &lastSeen, view.ClientLastSeenAt)
}
func TestShouldApplyDesktopAccountRuntimeHealth_IgnoresReportOlderThanVerifiedAt(t *testing.T) {
verifiedAt := time.Date(2026, 4, 30, 9, 30, 0, 0, time.UTC)
reportCheckedAt := verifiedAt.Add(-time.Minute)
shouldApply := shouldApplyDesktopAccountRuntimeHealth(
DesktopAccountView{
ID: uuid.NewString(),
Health: "live",
VerifiedAt: &verifiedAt,
},
bufferedDesktopAccountHealthReport{
Health: "expired",
AuthState: "expired",
CheckedAt: reportCheckedAt,
},
)
assert.False(t, shouldApply)
}
func TestShouldApplyDesktopAccountRuntimeHealth_AppliesReportAfterVerifiedAt(t *testing.T) {
verifiedAt := time.Date(2026, 4, 30, 9, 30, 0, 0, time.UTC)
reportCheckedAt := verifiedAt.Add(time.Minute)
shouldApply := shouldApplyDesktopAccountRuntimeHealth(
DesktopAccountView{
ID: uuid.NewString(),
Health: "live",
VerifiedAt: &verifiedAt,
},
bufferedDesktopAccountHealthReport{
Health: "expired",
AuthState: "expired",
CheckedAt: reportCheckedAt,
},
)
assert.True(t, shouldApply)
}