Files
Starlight_Lancher/apps/app-frontend/src/composables/live-log-reconciler.test.ts
T
Disy ce0f41c973
Axolotl desktop CI / guardrails (push) Has been cancelled
Axolotl desktop CI / desktop (macos-latest) (push) Has been cancelled
Axolotl desktop CI / desktop (ubuntu-latest) (push) Has been cancelled
Axolotl desktop CI / desktop (windows-latest) (push) Has been cancelled
Axolotl desktop CI / website (push) Has been cancelled
Repository checks / typos (push) Has been cancelled
Repository checks / tombi (push) Has been cancelled
Rust checks / shear (push) Has been cancelled
Sync source to CNB / Sync Git ref (push) Has been cancelled
Sync LobeHub models / sync (push) Has been cancelled
fix: 修复实时日志与数据库切换可靠性
2026-09-27 14:18:25 +08:00

71 lines
1.7 KiB
TypeScript

import assert from 'node:assert/strict'
import test from 'node:test'
import {
createLiveLogReconciler,
type LiveLogSnapshot,
} from './live-log-reconciler.ts'
test('merges the authoritative snapshot with queued events without gaps or duplicates', async () => {
let resolveSnapshot!: (snapshot: LiveLogSnapshot) => void
const displayed: number[] = []
const reconciler = createLiveLogReconciler(
() =>
new Promise((resolve) => {
resolveSnapshot = resolve
}),
(snapshot) => {
displayed.splice(
0,
displayed.length,
...snapshot.lines.map((line) => line.sequence),
)
},
(event) => displayed.push(event.last_sequence),
)
const syncing = reconciler.sync()
reconciler.accept({ first_sequence: 3, last_sequence: 3 })
resolveSnapshot({
lines: [
{ sequence: 1, message: 'one' },
{ sequence: 2, message: 'two' },
],
last_sequence: 2,
})
await syncing
assert.deepEqual(displayed, [1, 2, 3])
await reconciler.accept({ first_sequence: 3, last_sequence: 3 })
assert.deepEqual(displayed, [1, 2, 3])
})
test('resynchronizes when an event sequence reveals a missed batch', async () => {
const displayed: number[] = []
let loads = 0
const reconciler = createLiveLogReconciler(
async () => {
loads++
return {
lines: [1, 2, 3, 4].map((sequence) => ({
sequence,
message: String(sequence),
})),
last_sequence: 4,
}
},
(snapshot) => {
displayed.splice(
0,
displayed.length,
...snapshot.lines.map((line) => line.sequence),
)
},
(event) => displayed.push(event.last_sequence),
)
await reconciler.accept({ first_sequence: 4, last_sequence: 4 })
assert.equal(loads, 1)
assert.deepEqual(displayed, [1, 2, 3, 4])
})