From 6a4d2a94b948965feed7f611eee87cd8d6d714b3 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Mon, 21 Oct 2024 23:31:37 +0200 Subject: [PATCH] Generalize prewrite test for memory-level Ref: https://github.com/Level/memory-level/pull/10 Category: fix --- test/hooks/prewrite.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/hooks/prewrite.js b/test/hooks/prewrite.js index a7b0efa..d4bfc17 100644 --- a/test/hooks/prewrite.js +++ b/test/hooks/prewrite.js @@ -769,6 +769,7 @@ module.exports = function (test, testCommon) { const db = testCommon.factory() await db.open() + const textDecoder = new TextDecoder() const books = db.sublevel('books', { valueEncoding: 'json' }) const index = db.sublevel('authors', { // Use JSON, which normally doesn't make sense for keys but @@ -779,7 +780,14 @@ module.exports = function (test, testCommon) { db.on('write', (ops) => { // Check that data is written to correct sublevels, specifically // !authors!Hesse~12 rather than !books!!authors!Hesse~12. - t.same(ops.map(x => x.key), ['!books!12', '!authors!"Hesse~12"']) + t.same(ops.map(x => decode(x.key)), ['!books!12', '!authors!"Hesse~12"']) + + // It's unfortunate DX but because the write is made via the sublevel, the + // format of keys depends on the supported encodings of db. For example on + // a MemoryLevel({ storeEncoding: 'buffer' }) the key will be a buffer. + function decode (key) { + return db.keyEncoding('utf8').format === 'utf8' ? key : textDecoder.decode(key) + } }) books.on('write', (ops) => {