mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +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()
|
||||
|
||||
Reference in New Issue
Block a user