Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: local provider scheduled command adapter unit test #1361

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@boostercloud/framework-core",
"comment": "Added tests for local scheduled commands",
"type": "patch"
}
],
"packageName": "@boostercloud/framework-core"
}
261 changes: 125 additions & 136 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ export function configureScheduler(config: BoosterConfig, userProject: any): voi
}

function createCronExpression(scheduledCommandMetadata: ScheduledCommandMetadata): string {
const { minute = '*', hour = '*', day = '*', month = '*', weekDay = '*' } = scheduledCommandMetadata.scheduledOn
return `${minute} ${hour} ${day} ${month} ${weekDay}`
const {
second = '*',
minute = '*',
hour = '*',
day = '*',
month = '*',
weekDay = '*',
} = scheduledCommandMetadata.scheduledOn
return `${second} ${minute} ${hour} ${day} ${month} ${weekDay}`
}

function buildScheduledCommandInfo(config: BoosterConfig, scheduledCommandName: string): ScheduledCommandInfo {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { BoosterConfig } from '@boostercloud/framework-types'
import { restore, SinonStubbedInstance, createStubInstance, replace, fake } from 'sinon'
import { configureScheduler } from '../src/scheduler'
import { expect } from './expect'
import { describe } from 'mocha'
import { random } from 'faker'
import * as scheduler from 'node-schedule'

const rewire = require('rewire')
const schedule = rewire('../src/scheduler')
const createCronExpression = schedule.__get__('createCronExpression')
const buildScheduledCommandInfo = schedule.__get__('buildScheduledCommandInfo')
interface ScheduledCommandInfo {
typeName: string
}
declare class UserProject {
constructor()
boosterTriggerScheduledCommand: (rawRequest: unknown) => void
}

describe('Local Scheduler', () => {
let mockUserProjectStub: SinonStubbedInstance<UserProject>
let mockConfig: BoosterConfig
let mockScheduledCommandName: string

beforeEach(() => {
class CheckCart {
public static async handle(): Promise<void> {
console.log('handle')
}
}
class UserProject {
boosterTriggerScheduledCommand(rawRequest: unknown): void {
console.log('rawRequest: ', rawRequest)
}
}

mockScheduledCommandName = random.word()

mockUserProjectStub = createStubInstance(UserProject)

mockConfig = buildConfig()
mockConfig.scheduledCommandHandlers[mockScheduledCommandName] = {
class: CheckCart,
scheduledOn: {},
}
})

afterEach(() => {
restore()
})

describe('buildScheduledCommandInfo', () => {
it('should return expected scheduled command info', async () => {
const result = await buildScheduledCommandInfo(mockConfig, mockScheduledCommandName)
expect(result).to.be.deep.equal({
name: mockScheduledCommandName,
metadata: mockConfig.scheduledCommandHandlers[mockScheduledCommandName],
})
})
})

describe('createCronExpression', () => {
it('should return default cron expression', async () => {
const result = await createCronExpression({ scheduledOn: {} })
expect(result).to.be.equal('* * * * * *')
})

it('should return expected cron expression', async () => {
const result = await createCronExpression({
scheduledOn: {
minute: '30',
hour: '14',
weekDay: '0',
},
})
expect(result).to.be.equal('* 30 14 * * 0')
})
})

describe('configureScheduler', () => {
it('should call scedule job', async () => {
const fakeScheduleJob = fake((name: string, cronExpression: string, scheduledFunction: () => void) => {
scheduledFunction()
})
const fakeTriggerScheduleCommand = fake((command: ScheduledCommandInfo) => {})
replace(scheduler, 'scheduleJob', fakeScheduleJob)
replace(mockUserProjectStub, 'boosterTriggerScheduledCommand', fakeTriggerScheduleCommand as any)

configureScheduler(mockConfig, mockUserProjectStub)

expect(scheduler.scheduleJob).to.have.been.calledWith(mockScheduledCommandName, '* * * * * *')
expect(mockUserProjectStub.boosterTriggerScheduledCommand).to.have.been.calledWith({
typeName: mockScheduledCommandName,
})
})
})

function buildConfig(): BoosterConfig {
return new BoosterConfig('test')
}
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BoosterConfig, ScheduledCommandEnvelope, UUID } from '@boostercloud/framework-types'
import { getLogger } from '@boostercloud/framework-common-helpers'

interface LocalScheduleCommandEnvelope {
export interface LocalScheduleCommandEnvelope {
typeName: string
}

Expand Down
11 changes: 11 additions & 0 deletions packages/framework-provider-local/test/helpers/scheduled-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { random } from 'faker'

interface LocalScheduleCommandEnvelope {
typeName: string
}

export function createMockLocalScheduleCommandEnvelope(): Partial<LocalScheduleCommandEnvelope> {
return {
typeName: random.word(),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { SinonStub, stub, replace, restore, fake } from 'sinon'
import { rawScheduledInputToEnvelope, LocalScheduleCommandEnvelope } from '../../src/library/scheduled-adapter'
import { createMockLocalScheduleCommandEnvelope } from '../helpers/scheduled-helper'
import { BoosterConfig, UUID } from '@boostercloud/framework-types'
import { expect } from '../expect'
import { random } from 'faker'

describe('Local provider scheduled-adapter', () => {
describe('rawScheduledInputToEnvelope', () => {
let config: BoosterConfig

let mockScheduledEnvelope: Partial<LocalScheduleCommandEnvelope>
let mockEmptyScheduledEnvelop: Partial<unknown>
let mockUuid: string

let generateStub: SinonStub

beforeEach(() => {
config = new BoosterConfig('test')
config.logger = {
debug: fake(),
} as any

mockUuid = random.uuid()

mockScheduledEnvelope = createMockLocalScheduleCommandEnvelope()
mockEmptyScheduledEnvelop = {}

generateStub = stub().returns(mockUuid)

replace(UUID, 'generate', generateStub)
})

afterEach(() => {
restore()
})

it('should call logger.debug', async () => {
await rawScheduledInputToEnvelope(config, mockScheduledEnvelope)

expect(config.logger?.debug).to.have.been.calledOnceWith(
'[Booster]|rawScheduledInputToEnvelope: ',
'Received LocalScheduleCommand request: ',
mockScheduledEnvelope
)
})

it('should thrown an exception for empty typeName', async () => {
await expect(rawScheduledInputToEnvelope(config, mockEmptyScheduledEnvelop)).to.be.rejectedWith(
'typeName is not defined or empty, scheduled command envelope should have the structure {typeName: string }, but you gave {}'
)
})

it('should generate expected envelop', async () => {
const result = await rawScheduledInputToEnvelope(config, mockScheduledEnvelope)
expect(result).to.be.deep.equal({
requestID: mockUuid,
typeName: mockScheduledEnvelope.typeName,
})
})
})
})
1 change: 1 addition & 0 deletions packages/framework-types/src/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface ScheduleInterface {
readonly second?: string
readonly minute?: string
readonly hour?: string
readonly day?: string
Expand Down