mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
feat(admin): online means a tab in front, and keeps last activity current
Online in the user list meant any open socket, so a tab left in the background showed as online for days while its last activity stood still. The client now sends a presence frame on connect and on every visibility change; only sockets whose tab is visible count as online (one that never sent the frame still counts, so older clients are unchanged). While a tab is in front, the gateway writes its session's last_seen_at at most every five minutes, on the frame and on the heartbeat, so last activity follows the online state.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// vi.unmock must run before the module is imported (tests/setup.ts mocks it globally)
|
||||
vi.unmock('./websocket')
|
||||
|
||||
// FE-WSCORE-001 to FE-WSCORE-016
|
||||
// FE-WSCORE-001 to FE-WSCORE-018
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { http, HttpResponse } from 'msw'
|
||||
import { server } from '../../tests/helpers/msw/server'
|
||||
@@ -135,18 +135,52 @@ describe('websocket > reconnect refetch hook', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('FE-WSCORE-005: with no joined trips onopen sends nothing and skips the refetch', async () => {
|
||||
it('FE-WSCORE-005: with no joined trips onopen sends only its presence and skips the refetch', async () => {
|
||||
const refetch = vi.fn((_tripId: string) => {})
|
||||
setRefetchCallback(refetch)
|
||||
|
||||
const sock = await openSocket()
|
||||
sock.onopen!()
|
||||
|
||||
expect(sock.send).not.toHaveBeenCalled()
|
||||
expect(sock.send.mock.calls.map(([raw]) => JSON.parse(raw).type)).toEqual(['presence'])
|
||||
expect(refetch).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('websocket > presence', () => {
|
||||
function presences(sock: MockWebSocket): unknown[] {
|
||||
return sock.send.mock.calls.map(([raw]) => JSON.parse(raw)).filter(m => m.type === 'presence').map(m => m.visible)
|
||||
}
|
||||
|
||||
function setVisibility(state: 'visible' | 'hidden'): void {
|
||||
Object.defineProperty(document, 'visibilityState', { configurable: true, get: () => state })
|
||||
document.dispatchEvent(new Event('visibilitychange'))
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
delete (document as { visibilityState?: unknown }).visibilityState
|
||||
})
|
||||
|
||||
it('FE-WSCORE-017: tells the server on open and on every change whether the tab is in front', async () => {
|
||||
Object.defineProperty(document, 'visibilityState', { configurable: true, get: () => 'visible' })
|
||||
const sock = await openSocket()
|
||||
sock.onopen!()
|
||||
setVisibility('hidden')
|
||||
setVisibility('visible')
|
||||
|
||||
expect(presences(sock)).toEqual([true, false, true])
|
||||
})
|
||||
|
||||
it('FE-WSCORE-018: stops telling it once logged out', async () => {
|
||||
const sock = await openSocket()
|
||||
sock.onopen!()
|
||||
disconnect()
|
||||
setVisibility('hidden')
|
||||
|
||||
expect(presences(sock)).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('websocket > connection lifecycle', () => {
|
||||
it('FE-WSCORE-006: connect() is a no-op while a socket is still CONNECTING', async () => {
|
||||
const sock = await openSocket()
|
||||
|
||||
@@ -129,6 +129,7 @@ async function connectInternal(_isReconnect = false): Promise<void> {
|
||||
|
||||
socket.onopen = () => {
|
||||
reconnectDelay = 1000
|
||||
sendPresence()
|
||||
activeBooks.forEach(journeyId => {
|
||||
if (socket && socket.readyState === WebSocket.OPEN) {
|
||||
socket.send(JSON.stringify({ type: 'book:join', journeyId }))
|
||||
@@ -173,6 +174,17 @@ async function connectInternal(_isReconnect = false): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells the server whether this tab is in front. Online, in the admin's user
|
||||
* list, means a tab someone is looking at, not one left open in the
|
||||
* background, and while one is in front the server keeps the account's
|
||||
* last activity current.
|
||||
*/
|
||||
function sendPresence(): void {
|
||||
if (!socket || socket.readyState !== WebSocket.OPEN || typeof document === 'undefined') return
|
||||
socket.send(JSON.stringify({ type: 'presence', visible: document.visibilityState === 'visible' }))
|
||||
}
|
||||
|
||||
/**
|
||||
* The network came back or the tab became visible: a socket waiting out its
|
||||
* backoff (up to 30 s) reconnects now. Does nothing while logged out or while
|
||||
@@ -186,6 +198,7 @@ export function reconnectNow(): void {
|
||||
|
||||
export function connect(): void {
|
||||
shouldReconnect = true
|
||||
if (typeof document !== 'undefined') document.addEventListener('visibilitychange', sendPresence)
|
||||
reconnectDelay = 1000
|
||||
if (reconnectTimer) {
|
||||
clearTimeout(reconnectTimer)
|
||||
@@ -196,6 +209,7 @@ export function connect(): void {
|
||||
|
||||
export function disconnect(): void {
|
||||
shouldReconnect = false
|
||||
if (typeof document !== 'undefined') document.removeEventListener('visibilitychange', sendPresence)
|
||||
generation++
|
||||
if (reconnectTimer) {
|
||||
clearTimeout(reconnectTimer)
|
||||
|
||||
Reference in New Issue
Block a user