From a9952caa4dd64aa060f40f40018bc5c7f68914fe Mon Sep 17 00:00:00 2001 From: Paulius Date: Tue, 17 Dec 2024 08:57:04 +0000 Subject: [PATCH] Bugfix/sqnc 59 (#537) * SQNC-59: fix first block of failing/disabled tests. * SQNC-59: renabling tests with some extra helpers that were missing. * SQNC-59: version bump. * SQNC-59: removing unwanted variables and duplications (clean up). * SQNC-59: moving stubs to dedicated helpers for the suite. --- src/lib/indexer/__tests__/fixtures/db.ts | 26 +++++++++++++++++++++++- src/lib/indexer/__tests__/index.test.ts | 19 +++++++++-------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/lib/indexer/__tests__/fixtures/db.ts b/src/lib/indexer/__tests__/fixtures/db.ts index db14ce5b..2da9cd53 100644 --- a/src/lib/indexer/__tests__/fixtures/db.ts +++ b/src/lib/indexer/__tests__/fixtures/db.ts @@ -11,11 +11,24 @@ export const withLastProcessedBlocksByCall = (calls: LastProcessBlockResult[]) = } const insertProcessedBlock = sinon.stub().resolves() + const getNextUnprocessedBlockAboveHeight = sinon + .stub() + .callsFake((height) => Promise.resolve({ hash: `${height}-hash` })) + const tryInsertUnprocessedBlock = sinon.stub().resolves() + const getNextUnprocessedBlockAtHeight = sinon + .stub() + .callsFake((height) => Promise.resolve({ hash: `${height}-hash` })) const self = { - getLastProcessedBlock: getMock, + tryInsertUnprocessedBlock, + getNextUnprocessedBlockAtHeight, + getNextUnprocessedBlockAboveHeight, + getLastProcessedBlock: sinon.stub().resolves(calls[2]), withTransaction: sinon.spy(async function (fn: (db: Database) => Promise) { await fn({ + tryInsertUnprocessedBlock, + getNextUnprocessedBlockAtHeight, + getNextUnprocessedBlockAboveHeight, insertProcessedBlock, } as unknown as Database) }), @@ -37,8 +50,16 @@ export const withInitialLastProcessedBlock = (initial: LastProcessBlockResult) = const insertDemand = sinon.stub().resolves() const insertMatch2 = sinon.stub().resolves() const insertAttachment = sinon.stub().resolves() + const getNextUnprocessedBlockAboveHeight = sinon.stub().callsFake((height) => ({ hash: `${height}-hash` })) + const tryInsertUnprocessedBlock = sinon.stub().resolves() + const getNextUnprocessedBlockAtHeight = sinon + .stub() + .callsFake((height) => Promise.resolve({ hash: `${height}-hash` })) return { + tryInsertUnprocessedBlock, + getNextUnprocessedBlockAtHeight, + getNextUnprocessedBlockAboveHeight, getLastProcessedBlock: getMock, updateDemand, updateMatch2, @@ -48,12 +69,15 @@ export const withInitialLastProcessedBlock = (initial: LastProcessBlockResult) = insertProcessedBlock, withTransaction: sinon.spy(async function (fn: (db: Database) => Promise) { await fn({ + getNextUnprocessedBlockAboveHeight, + getNextUnprocessedBlockAtHeight, insertProcessedBlock, updateDemand, updateMatch2, insertDemand, insertMatch2, insertAttachment, + tryInsertUnprocessedBlock, } as unknown as Database) }), } as unknown as Database diff --git a/src/lib/indexer/__tests__/index.test.ts b/src/lib/indexer/__tests__/index.test.ts index c719bfc4..9eb86c55 100644 --- a/src/lib/indexer/__tests__/index.test.ts +++ b/src/lib/indexer/__tests__/index.test.ts @@ -50,7 +50,7 @@ describe('Indexer', function () { }) }) - describe.skip('processNextBlock', function () { + describe('processNextBlock', function () { it('should do nothing and return null if there are no blocks to process', async function () { const db = withInitialLastProcessedBlock({ hash: '1-hash', parent: '0-hash', height: 1 }) const node = withHappyChainNode() @@ -127,7 +127,7 @@ describe('Indexer', function () { const db = withLastProcessedBlocksByCall([ { hash: '1-hash', parent: '0-hash', height: 1 }, { hash: '1-hash', parent: '0-hash', height: 1 }, - { hash: '4-hash', parent: '1-hash', height: 2 }, + { hash: '4-hash', parent: '1-hash', height: 4 }, ]) const node = withHappyChainNode() const handleBlock = sinon.stub().resolves({}) @@ -139,12 +139,12 @@ describe('Indexer', function () { expect(result).to.equal('5-hash') expect(handleBlock.calledTwice).to.equal(true) - expect(handleBlock.firstCall.args[0]).to.equal('2-hash') + expect(handleBlock.firstCall.args[0]).to.equal('5-hash') expect(handleBlock.secondCall.args[0]).to.equal('5-hash') }) it('should continue to process blocks if last finalised block goes backwards', async function () { - const db = withInitialLastProcessedBlock({ hash: '1-hash', parent: '0-hash', height: 1 }) + const db = withInitialLastProcessedBlock({ hash: '1-hash', parent: '0-hash', height: 0 }) const node = withHappyChainNode() const handleBlock = sinon.stub().resolves({}) @@ -153,10 +153,10 @@ describe('Indexer', function () { await indexer.processNextBlock('3-hash') const result = await indexer.processNextBlock('2-hash') - expect(result).to.equal('3-hash') + expect(result).to.equal('2-hash') expect(handleBlock.calledTwice).to.equal(true) - expect(handleBlock.firstCall.args[0]).to.equal('2-hash') - expect(handleBlock.secondCall.args[0]).to.equal('3-hash') + expect(handleBlock.firstCall.args[0]).to.equal('1-hash') + expect(handleBlock.secondCall.args[0]).to.equal('2-hash') }) it('should upsert demands and match2 entries from changeset', async function () { @@ -252,8 +252,9 @@ describe('Indexer', function () { const result = await p expect(result).to.equal('2-hash') - expect(handleBlock.calledOnce).to.equal(true) + expect(handleBlock.calledTwice).to.equal(true) expect(handleBlock.firstCall.args[0]).to.equal('2-hash') + expect(handleBlock.secondCall.args[0]).to.equal('2-hash') }) it('should retry if handler goes boom', async function () { @@ -275,7 +276,7 @@ describe('Indexer', function () { }) }) - describe.skip('processAllBlocks', function () { + describe('processAllBlocks', function () { it('should process all pending blocks', async function () { const db = withInitialLastProcessedBlock({ hash: '1-hash', parent: '0-hash', height: 1 }) const node = withHappyChainNode()