test: add deterministic browse cursor helpers

This commit is contained in:
Jonathan Finley 2026-03-13 21:20:20 -04:00
parent 59c7c0a7c2
commit 36170d0fcb
2 changed files with 65 additions and 0 deletions

View file

@ -0,0 +1,22 @@
function encodeBrowseCursor(payload) {
return Buffer.from(JSON.stringify(payload)).toString('base64url')
}
function decodeBrowseCursor(cursor) {
const decoded = JSON.parse(Buffer.from(cursor, 'base64url').toString('utf8'))
if (!Array.isArray(decoded.keys) || !Array.isArray(decoded.values) || decoded.keys.length !== decoded.values.length) {
throw new Error('Cursor is missing the full ordered key set')
}
if (!decoded.keys.length || decoded.keys[decoded.keys.length - 1] !== 'id') {
throw new Error('Cursor is missing the tie-breaker value')
}
return decoded
}
module.exports = {
encodeBrowseCursor,
decodeBrowseCursor
}