Skip to content

Commit

Permalink
feat((pin 262) crr dd): [sc-24352] Improve Human Readable ID (#658)
Browse files Browse the repository at this point in the history
* feat: refactor tests and introduce year getter [sc-24352]

* chore: remove logs

* test: fix expects

* fix: correctly pull query from SM config [sc-24352]

* chore: remove log [sc-24352]

* refactor: tidying and revert today [sc-24352]
  • Loading branch information
reecebrend committed May 13, 2024
1 parent 004a97e commit fe8c646
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 17 deletions.
7 changes: 7 additions & 0 deletions lib/plugin/components/services/timestamp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class TimestampService {
},
today () {
return moment().hour(0).minute(0).second(0).millisecond(0)
},
getYear (dateInput = new Date()) {
return moment().format('yyyy')
}
}
} // defaultProvider
Expand All @@ -24,6 +27,10 @@ class TimestampService {
today () {
return this.timeProvider.today()
}

getYear () {
return this.timeProvider.getYear()
}
} // class TimestampService

module.exports = {
Expand Down
13 changes: 11 additions & 2 deletions lib/plugin/components/state-resources/timestamp/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@

module.exports = class Timestamp {
init (resourceConfig, env) {
this.query = resourceConfig.query
this.timestamp = env.bootedServices.timestamp
}

run (event, context) {
context.sendTaskSuccess(this.timestamp.now())
if (this.query === '$TODAY') {
return context.sendTaskSuccess(this.timestamp.today())
}

if (this.query === '$YEAR') {
return context.sendTaskSuccess(this.timestamp.getYear())
}

return context.sendTaskSuccess(this.timestamp.now())
} // run
} // GetRegistryKey
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"Comment": "Blueprint to get timestamp",
"version": "1.0",
"StartAt": "Timestamp",
"States": {
"Timestamp": {
"Type": "Task",
"Resource": "module:timestamp",
"ResourceConfig": {
"query": "$TODAY"
},
"ResultPath": "$.timestamp",
"End": true
}
},
"restrictions": [
{
"roleId": "$authenticated",
"allows": [
"*"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"Comment": "Blueprint to get timestamp",
"version": "1.0",
"StartAt": "Timestamp",
"States": {
"Timestamp": {
"Type": "Task",
"Resource": "module:timestamp",
"ResourceConfig": {
"query": "$YEAR"
},
"ResultPath": "$.timestamp",
"End": true
}
},
"restrictions": [
{
"roleId": "$authenticated",
"allows": [
"*"
]
}
]
}
2 changes: 1 addition & 1 deletion test/list-state-machines-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('list available state machines', () => {
it('available-state-machines state resource', async () => {
const stateMachines = await runAvailableStateMachines()

expect(stateMachines[0].name).to.eql('tymlyTest_timestamp_1_0')
expect(stateMachines[0].name).to.eql('tymlyTest_timestampNow_1_0')
expect(stateMachines[0].description).to.eql('Blueprint to get timestamp')
})

Expand Down
46 changes: 32 additions & 14 deletions test/timestamp-state-resource-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ const expect = require('chai').expect
const tymly = require('../lib')
const moment = require('moment')

const TestTimestamp = moment([2000, 2, 28, 22, 35, 0]).local()
const TestTimestampString = `${TestTimestamp}`
const stateMachines = {
now: 'tymlyTest_timestampNow_1_0',
today: 'tymlyTest_timestampToday_1_0',
year: 'tymlyTest_timestampYear_1_0'
}

const timestampStateMachine = 'tymlyTest_timestamp_1_0'
const todayCheck = moment().hour(0).minute(0).second(0).millisecond(0)
const yearCheck = moment().format('yyyy')

describe('Timestamp state resources', function () {
this.timeout(process.env.TIMEOUT || 5000)
Expand All @@ -31,25 +35,39 @@ describe('Timestamp state resources', function () {
tymlyService = tymlyServices.tymly
statebox = tymlyServices.statebox

tymlyServices.timestamp.timeProvider = {
now () {
return TestTimestamp
}
} // debug provider

done()
}
)
})

it('run the state machine to get a timestamp', async () => {
it('run the state machine to get a timestamp for now', async () => {
const execDesc = await statebox.startExecution(
{},
stateMachines.now,
{ sendResponse: 'COMPLETE' }
)

expect(execDesc.ctx.timestamp).to.not.eql(null)
})

it('run the state machine to get a timestamp for today', async () => {
const execDesc = await statebox.startExecution(
{ query: '$TODAY' },
stateMachines.today,
{ sendResponse: 'COMPLETE' }
)

expect(execDesc.ctx.timestamp.format()).to.eql(todayCheck.format())
})

it('run the state machine to get a timestamp for the year', async () => {
const execDesc = await statebox.startExecution(
{ },
timestampStateMachine,
{ query: '$YEAR' },
stateMachines.year,
{ sendResponse: 'COMPLETE' }
)
expect(execDesc.ctx.timestamp).to.eql(TestTimestamp)
expect(execDesc.ctx.timestamp.toString()).to.eql(TestTimestampString)

expect(execDesc.ctx.timestamp).to.eql(yearCheck)
})

it('shutdown Tymly', async () => {
Expand Down

0 comments on commit fe8c646

Please sign in to comment.