Files
FFF_vtt_frozen/client/test/tiles.test.ts
T
SirStone 4534ed6426 feat(client): map tiles, grid and pan/zoom (#69)
T6 of spec #63. Tiles are requested only for the viewport plus one tile of
margin, in viewport order, at most 8 at a time (TileScheduler); decoded
textures are cached by URL so a reconnect refetches nothing; a 403/404 is
remembered as denied and never retried. The browser image loader is the hot
path, with one status fetch only to classify an error.

Camera: drag to pan, wheel/pinch to zoom around the pointer/midpoint; players
clamp at 0.5 (ADR-0013) and the GM has no player floor; both stop at 4.0.
Grid is one TilingSprite toggled by #grid-toggle. Camera is local UI state,
not a reducer slice, so hello/reconnect does not move the view.

Tests: viewport/tile unit tests (clamps, margin window, 8-wide queue, denial,
cache) and client/e2e/map.test.mjs (mobile viewport fill gate (a), grid,
pinch, cache across reconnect, GM zoom, 403 no-retry) against the real
server through TLS/h2. Tile-size gate (a) documented in client/README.md.
2026-11-21 01:11:12 +01:00

173 lines
5.3 KiB
TypeScript

// The tile scheduler (#69, spec #63 decision 12): viewport-order requests, a bounded concurrency of
// 8, a decoded-texture cache the tile module keeps across reconnect, no duplicate in-flight fetch,
// and a denied tile (403/404) is remembered so the client never retries it in a loop.
import { describe, expect, it } from 'vitest'
import { TILE_CONCURRENCY, TileScheduler } from '../src/tiles.ts'
import type { TileLoadResult } from '../src/tiles.ts'
interface Harness {
scheduler: TileScheduler
calls: string[]
maxActive: number
settle(url: string, result: TileLoadResult): void
settleAll(result: TileLoadResult): void
idle: number
}
function harness(concurrency = TILE_CONCURRENCY): Harness {
const calls: string[] = []
const pending = new Map<string, (result: TileLoadResult) => void>()
let active = 0
let maxActive = 0
const state = { idle: 0 }
const scheduler = new TileScheduler({
concurrency,
load: url => {
calls.push(url)
active += 1
maxActive = Math.max(maxActive, active)
return new Promise<TileLoadResult>(resolve => {
pending.set(url, result => {
active -= 1
pending.delete(url)
resolve(result)
})
})
},
onIdle: () => {
state.idle += 1
},
})
return {
scheduler,
calls,
get maxActive() {
return maxActive
},
settle(url, result) {
const resolve = pending.get(url)
if (!resolve) throw new Error(`no pending load for ${url}`)
resolve(result)
},
settleAll(result) {
for (const url of [...pending.keys()]) {
pending.get(url)?.(result)
pending.delete(url)
}
},
get idle() {
return state.idle
},
}
}
function urls(count: number): string[] {
return Array.from({ length: count }, (_, index) => `/tiles/1/v1/${index}_0.png`)
}
/** Let every queued promise callback and the next pump round run. */
async function flush(): Promise<void> {
for (let i = 0; i < 20; i++) await Promise.resolve()
}
describe('TileScheduler', () => {
it('starts at most 8 loads at once, in the order asked', async () => {
expect(TILE_CONCURRENCY).toBe(8)
const h = harness()
const list = urls(20)
h.scheduler.setDesired(list)
expect(h.calls).toEqual(list.slice(0, 8))
expect(h.maxActive).toBe(8)
h.settle(list[0] as string, 'ok')
await flush()
expect(h.calls[8]).toBe(list[8])
expect(h.maxActive).toBe(8)
// Each settle opens a slot for the next queued tile; drain every round.
for (let round = 0; round < 4 && h.calls.length < 20; round++) {
h.settleAll('ok')
await flush()
}
expect(h.calls.length).toBe(20)
expect(h.maxActive).toBe(8)
})
it('never asks twice for the same tile', () => {
const h = harness()
const list = urls(4)
h.scheduler.setDesired(list)
h.scheduler.setDesired(list)
expect(h.calls).toEqual(list)
})
it('remembers a denied tile and never retries it', async () => {
const h = harness()
const list = urls(1)
h.scheduler.setDesired(list)
h.settle(list[0] as string, 'denied')
await flush()
expect(h.scheduler.status(list[0] as string)).toBe('denied')
h.scheduler.setDesired(list)
h.scheduler.setDesired(list)
expect(h.calls).toEqual(list)
})
it('reports idle once when every wanted tile is settled', async () => {
const h = harness()
const list = urls(3)
h.scheduler.setDesired(list)
h.settleAll('ok')
await flush()
expect(h.scheduler.isComplete(list)).toBe(true)
expect(h.idle).toBe(1)
})
it('drops queued work that left the viewport (no prefetch beyond the margin)', async () => {
const h = harness()
const list = urls(20)
h.scheduler.setDesired(list)
expect(h.calls.length).toBe(8)
h.scheduler.setDesired([list[0] as string])
h.settleAll('ok')
await flush()
// Only the first 8 were ever started; the 12 queued tiles were cancelled.
expect(h.calls.length).toBe(8)
expect(h.scheduler.status(list[19] as string)).toBeUndefined()
})
it('does not retry a failed tile on the same window, only on a new one', async () => {
const h = harness()
const list = urls(2)
h.scheduler.setDesired([list[0] as string])
h.settle(list[0] as string, 'error')
await flush()
expect(h.scheduler.isComplete([list[0] as string])).toBe(false)
expect(h.calls).toEqual([list[0]])
// Same window: the rAF sync after the failure must not hammer the server.
h.scheduler.setDesired([list[0] as string])
expect(h.calls).toEqual([list[0]])
// A new window that still wants the tile asks once more.
h.scheduler.setDesired(list)
expect(h.calls).toEqual([list[0], list[0], list[1]])
})
it('markLoaded satisfies a tile from the texture cache without a request', () => {
const h = harness()
const list = urls(1)
h.scheduler.markLoaded(list[0] as string)
h.scheduler.setDesired(list)
expect(h.calls).toEqual([])
expect(h.scheduler.isComplete(list)).toBe(true)
})
it('retryFailed makes a transient failure askable again after a reconnect', async () => {
const h = harness()
const list = urls(1)
h.scheduler.setDesired(list)
h.settle(list[0] as string, 'error')
await flush()
h.scheduler.retryFailed()
h.scheduler.setDesired(list)
expect(h.calls).toEqual([list[0], list[0]])
})
})