Skip to content

Commit

Permalink
Bugfix/sqnc 59 (#537)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
n3op2 authored Dec 17, 2024
1 parent a6dadd0 commit a9952ca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
26 changes: 25 additions & 1 deletion src/lib/indexer/__tests__/fixtures/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>) {
await fn({
tryInsertUnprocessedBlock,
getNextUnprocessedBlockAtHeight,
getNextUnprocessedBlockAboveHeight,
insertProcessedBlock,
} as unknown as Database)
}),
Expand All @@ -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,
Expand All @@ -48,12 +69,15 @@ export const withInitialLastProcessedBlock = (initial: LastProcessBlockResult) =
insertProcessedBlock,
withTransaction: sinon.spy(async function (fn: (db: Database) => Promise<void>) {
await fn({
getNextUnprocessedBlockAboveHeight,
getNextUnprocessedBlockAtHeight,
insertProcessedBlock,
updateDemand,
updateMatch2,
insertDemand,
insertMatch2,
insertAttachment,
tryInsertUnprocessedBlock,
} as unknown as Database)
}),
} as unknown as Database
Expand Down
19 changes: 10 additions & 9 deletions src/lib/indexer/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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({})
Expand All @@ -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({})

Expand All @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand All @@ -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()
Expand Down

0 comments on commit a9952ca

Please sign in to comment.