c87842347e
Track an authRevision per local record so a fresh bind cancels in-flight probes, sync server-confirmed health back into the desktop cache via reconcileTrackedAccountRemoteState, include verified_at in the upsert signature, and drop runtime health reports older than the stored verified_at on the server side. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
160 lines
4.1 KiB
Go
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_PrefersAccountPresenceClient(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, activeClientID.String(), *view.ClientID)
|
|
}
|
|
if assert.NotNil(t, view.ClientOnline) {
|
|
assert.True(t, *view.ClientOnline)
|
|
}
|
|
assert.Equal(t, "4181862206", view.PlatformUID)
|
|
assert.Equal(t, &lastSeen, view.ClientLastSeenAt)
|
|
assert.Equal(t, &deviceName, 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)
|
|
}
|