Files
FFF_vtt_frozen/client/test/tiles.test.ts
T
SirStone 9c05bb1f46 fix(client): make data-tiles ready when the pass completes
A transient tile load error is terminal for the current window (retried only on
a new window or a reconnect), but data-tiles treated it as never settled, so one
host hiccup left it at 'loading' forever and every e2e readiness wait hung
(audit.test.mjs timed out under a parallel lane). TileScheduler.isPending() now
reports loading only for queued/in-flight tiles; ready means the pass is done.
Proven by aborting one tile request: the wait timed out before, passes after.
Unit-tested at the scheduler seam.
2026-11-21 18:19:47 +01:00

225 lines
7.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, tilesForCells } 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('is not pending once a transient failure has settled this pass', async () => {
const h = harness()
const list = urls(2)
h.scheduler.setDesired(list)
expect(h.scheduler.isPending(list)).toBe(true) // both in flight
h.settle(list[0] as string, 'error')
h.settle(list[1] as string, 'ok')
await flush()
// The stored error is terminal until a new window or a reconnect: readiness must not wait on it.
expect(h.scheduler.isPending(list)).toBe(false)
expect(h.scheduler.isComplete(list)).toBe(false)
})
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]])
})
})
describe('fog-driven denied retry (#70)', () => {
it('re-asks a denied tile exactly once after its cells became explored', async () => {
const h = harness()
const list = urls(2)
h.scheduler.setDesired(list)
h.settle(list[0] as string, 'denied')
h.settle(list[1] as string, 'ok')
await flush()
expect(h.calls).toEqual(list)
// The fog says cells under the first tile became explored: only that denied tile is stale.
h.scheduler.retryDenied([list[0] as string])
h.scheduler.setDesired(list)
expect(h.calls).toEqual([...list, list[0]])
h.settleAll('ok')
await flush()
// A plain re-sync (no new cells) must not ask again: the denial is still remembered.
h.scheduler.setDesired(list)
h.scheduler.retryDenied([])
h.scheduler.setDesired(list)
expect(h.calls).toEqual([...list, list[0]])
})
it('maps explored cells to the tiles they touch', () => {
const scene = { id: 3, art_version: 7, tile_size: 32 }
const grid = { cols: 4, cell: 32 }
// Cells (1,0) -> tile (1,0), (1,1) -> tile (1,1), (3,1) -> tile (3,1).
expect(tilesForCells(scene, grid, [1, 5, 7])).toEqual([
'/tiles/3/v7/1_0.png',
'/tiles/3/v7/1_1.png',
'/tiles/3/v7/3_1.png',
])
// Two cells in one tile are one url; a smaller cell than a tile still maps correctly.
expect(tilesForCells(scene, { cols: 16, cell: 8 }, [0, 1, 2])).toEqual(['/tiles/3/v7/0_0.png'])
expect(tilesForCells(scene, { cols: 0, cell: 0 }, [1])).toEqual([])
})
})