Skip to content

Commit

Permalink
adding retry infeasable intents tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StoyanD committed Jan 7, 2025
1 parent 809cfab commit 005fb52
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/intervals/retry-infeasable-intents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class RetryInfeasableIntentsService implements OnApplicationBootstrap {
private readonly ecoConfigService: EcoConfigService,
) {}

onModuleInit() {
async onModuleInit() {
this.intentJobConfig = this.ecoConfigService.getIntervals().retryInfeasableIntents.jobTemplate
.opts as JobsOptions
}
Expand Down
116 changes: 102 additions & 14 deletions src/intervals/tests/retry-infeasable-intents.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest'
import { EcoConfigService } from '../../eco-configs/eco-config.service'
import { Test, TestingModule } from '@nestjs/testing'
import { ProofService } from '../../prover/proof.service'
import { MultichainPublicClientService } from '../../transaction/multichain-public-client.service'
import { RetryInfeasableIntentsService } from '@/intervals/retry-infeasable-intents.service'
import { Queue } from 'bullmq'
import { getModelToken } from '@nestjs/mongoose'
import { Model } from 'mongoose'
import { IntentSourceModel } from '@/intent/schemas/intent-source.schema'
import { BullModule, getQueueToken } from '@nestjs/bullmq'
import { QUEUES } from '@/common/redis/constants'
import { Proofs } from '@/contracts'
import { Hex } from 'viem'

describe('RetryInfeasableIntentsService', () => {
let infeasableService: RetryInfeasableIntentsService
Expand All @@ -26,8 +27,6 @@ describe('RetryInfeasableIntentsService', () => {
const chainMod: TestingModule = await Test.createTestingModule({
providers: [
RetryInfeasableIntentsService,
{ provide: getQueueToken(QUEUES.INTERVAL.queue), useValue: createMock<Queue>() },
{ provide: getQueueToken(QUEUES.SOURCE_INTENT.queue), useValue: createMock<Queue>() },
{
provide: getModelToken(IntentSourceModel.name),
useValue: createMock<Model<IntentSourceModel>>(),
Expand All @@ -44,10 +43,10 @@ describe('RetryInfeasableIntentsService', () => {
}),
],
})
// .overrideProvider(getQueueToken(QUEUES.INTERVAL.queue))
// .useValue(createMock<Queue>())
// .overrideProvider(getQueueToken(QUEUES.SOURCE_INTENT.queue))
// .useValue(createMock<Queue>())
.overrideProvider(getQueueToken(QUEUES.INTERVAL.queue))
.useValue(createMock<Queue>())
.overrideProvider(getQueueToken(QUEUES.SOURCE_INTENT.queue))
.useValue(createMock<Queue>())
.compile()

//turn off the services from logging durring testing
Expand All @@ -58,8 +57,9 @@ describe('RetryInfeasableIntentsService', () => {
intentSourceModel = chainMod.get(getModelToken(IntentSourceModel.name))
intervalQueue = chainMod.get(getQueueToken(QUEUES.INTERVAL.queue))
intentQueue = chainMod.get(getQueueToken(QUEUES.SOURCE_INTENT.queue))
proofService['logger'].debug = mockLogDebug
proofService['logger'].log = mockLogLog

infeasableService['logger'].debug = mockLogDebug
infeasableService['logger'].log = mockLogLog
})

afterEach(async () => {
Expand All @@ -69,11 +69,99 @@ describe('RetryInfeasableIntentsService', () => {
})

describe('on startup', () => {
it('should call loadProofTypes', async () => {
// const mockLoad = jest.fn()
// proofService['loadProofTypes'] = mockLoad
// await proofService.onModuleInit()
// expect(mockLoad).toHaveBeenCalledTimes(1)
const mockInternals = {
retryInfeasableIntents: {
repeatOpts: {
every: 10000,
},
jobTemplate: {
name: 'retry-infeasable-intents',
data: {},
},
},
}
beforeEach(async () => {
ecoConfigService.getIntervals.mockReturnValue(mockInternals as any)
})

it('should set intentJobConfig', async () => {
await infeasableService.onModuleInit()
expect(ecoConfigService.getIntervals).toHaveBeenCalledTimes(1)
})

it('should set upsertJobScheduler', async () => {
await infeasableService.onApplicationBootstrap()
expect(ecoConfigService.getIntervals).toHaveBeenCalledTimes(1)
expect(intervalQueue.upsertJobScheduler).toHaveBeenCalledTimes(1)
expect(intervalQueue.upsertJobScheduler).toHaveBeenCalledWith(
QUEUES.INTERVAL.jobs.RETRY_INFEASABLE_INTENTS,
{ ...mockInternals.retryInfeasableIntents.repeatOpts, immediately: true },
{
...mockInternals.retryInfeasableIntents.jobTemplate,
name: QUEUES.INTERVAL.jobs.retry_infeasable_intents,
},
)
})
})

describe('on retryInfeasableIntents', () => {
let mockGetInfeasableIntents = jest.fn()
const mockModels = [
{ intent: { hash: 'hash1', logIndex: 1 } },
{ intent: { hash: 'hash2', logIndex: 2 } },
]
beforeEach(async () => {
infeasableService['getInfeasableIntents'] = mockGetInfeasableIntents
mockGetInfeasableIntents.mockResolvedValue(mockModels)
})

it('should log models retrieved', async () => {
await infeasableService.retryInfeasableIntents()
expect(mockLogDebug).toHaveBeenCalledTimes(1)
expect(mockLogDebug).toHaveBeenCalledWith({
msg: 'retryInfeasableIntents',
models: mockModels,
})
})

it('should add every model to the queue', async () => {
const addSpy = jest.spyOn(intentQueue, 'add')
await infeasableService.retryInfeasableIntents()
expect(addSpy).toHaveBeenCalledTimes(2)
})
})

describe('getInfeasableIntents', () => {
it('should fetch intents with status INFEASABLE and valid expiration for Hyperlane proofs', async () => {
const minDateHyper = new Date('2022-01-01')
const minDateStorage = new Date('2022-01-02')
const proverHyper: Hex[] = ['0x1a', '0x2a']
const proverStorage: Hex[] = ['0x3b', '0x4b']
const mockGetProofMinimumDate = jest
.spyOn(proofService, 'getProofMinimumDate')
.mockImplementation((proof) => (proof == Proofs.Hyperlane ? minDateHyper : minDateStorage))
const mockGetProvers = jest
.spyOn(proofService, 'getProvers')
.mockImplementation((proof) => (proof == Proofs.Hyperlane ? proverHyper : proverStorage))

await infeasableService['getInfeasableIntents']()

expect(intentSourceModel.find).toHaveBeenCalledWith({
status: 'INFEASABLE',
$or: [
{
'intent.expiration': { $gt: minDateHyper },
'intent.prover': { $in: proverHyper },
},
{
'intent.expiration': { $gt: minDateStorage },
'intent.prover': { $in: proverStorage },
},
],
})

expect(mockGetProofMinimumDate).toHaveBeenCalledTimes(2)
expect(mockGetProvers).toHaveBeenCalledTimes(2)
})
})
})

0 comments on commit 005fb52

Please sign in to comment.