feat:抢单大厅数据排列优化

This commit is contained in:
2026-10-25 14:41:13 +08:00
parent 9c620d39cb
commit d787d3c364
6 changed files with 124 additions and 5 deletions
@@ -1,4 +1,5 @@
import assert from 'node:assert/strict'
import fs from 'node:fs'
import test from 'node:test'
import { shouldReplayRealtimeEvents } from './realtime-event-service.js'
@@ -20,3 +21,36 @@ test('断线重连(带 Last-Event-ID)才补发事件', () => {
assert.equal(shouldReplayRealtimeEvents(1), true)
assert.equal(shouldReplayRealtimeEvents(3993659), true)
})
test('补发窗口取最新的一段,而不是最旧的一段', () => {
const source = fs.readFileSync(
new URL('../../repositories/realtime-event-repo.ts', import.meta.url),
'utf8',
)
const block = source.slice(
source.indexOf('export async function listRealtimeEventsAfter('),
source.indexOf('\n}', source.indexOf('export async function listRealtimeEventsAfter(')),
)
assert.ok(block.length > 0, 'listRealtimeEventsAfter 片段必须存在')
assert.ok(
/ORDER BY id DESC\s+LIMIT \$2\s+\)\s*replay_window\s+ORDER BY id ASC/s.test(block),
'补发必须「内层 id DESC 取最新 N 条、外层 id ASC 回放」;否则断线久了窗口会停在旧事件上',
)
})
test('补发窗口溢出时改为让客户端整体重拉', () => {
const source = fs.readFileSync(new URL('./realtime-event-service.ts', import.meta.url), 'utf8')
const replayBlock = source.slice(
source.indexOf('async function replayEvents('),
source.indexOf('function mapStoredEvent('),
)
assert.ok(replayBlock.length > 0, 'replayEvents 片段必须存在')
assert.ok(
replayBlock.includes('events.length >= REALTIME_REPLAY_EVENT_LIMIT'),
'补发窗口被拿满时必须判定为溢出,不能继续逐条套用旧快照',
)
assert.ok(
replayBlock.includes("'resync'"),
'溢出时要给客户端发 resync,让列表整体重拉(接口才是权威数据源)',
)
})