Files
geo/server/internal/tenant/app/desktop_account_service_test.go
T
root 0823e47d46
Desktop Client Build / Resolve Build Metadata (push) Successful in 43s
Frontend CI / Frontend (push) Successful in 3m46s
Backend CI / Backend (push) Failing after 7m52s
Desktop Client Build / Build Desktop Client (push) Successful in 27m13s
Desktop Client Build / Publish Client Artifacts to NAS (push) Successful in 59s
feat: tighten desktop presence loop and refine admin-web UX
- Shrink desktop presence TTL to 30s and heartbeat to 15s so unexpected
  client exits surface within one TTL even if the explicit offline call
  is missed; keep migration default aligned.
- Trust a known presence miss in resolveDesktopClientOnline instead of
  falling back to the recent-heartbeat heuristic, so a still-cached
  LastSeenAt cannot mask an offline client.
- Release the runtime session before clearing renderer desktop sessions
  on shutdown to avoid leaving stale leases behind.
- Auto-refresh the MediaView account list every 5s and revamp card
  layout (inline platform badge, status tags, UID row, meta box).
- Let the brand-question AI wizard accept multiple seed topics via a
  tag-style input; candidates from each topic are merged and deduped.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 19:36:24 +08:00

172 lines
4.5 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_PresenceMissWithKnownPresenceIsOffline(t *testing.T) {
now := time.Date(2026, 4, 19, 21, 30, 0, 0, time.UTC)
lastSeen := now.Add(-desktopClientPresenceTTL / 2)
online := resolveDesktopClientOnline(&repository.DesktopClient{
ID: uuid.New(),
LastSeenAt: &lastSeen,
}, false, true, now)
assert.False(t, online)
}
func TestResolveDesktopClientOnline_FallsBackToRecentHeartbeatWhenPresenceUnknown(t *testing.T) {
now := time.Date(2026, 4, 19, 21, 30, 0, 0, time.UTC)
lastSeen := now.Add(-desktopClientPresenceTTL / 2)
online := resolveDesktopClientOnline(&repository.DesktopClient{
ID: uuid.New(),
LastSeenAt: &lastSeen,
}, false, false, 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)
}