Skip to content

Commit 4b28dc7

Browse files
authored
fix(execution): add started and done timestamps (#237)
1 parent 22ff7a3 commit 4b28dc7

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

src/core/StatusManager.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ export class StatusManager {
4040
step.execution = {
4141
status: 'PENDING',
4242
process: [],
43+
startedAt: Date.now(),
4344
}
4445
this.updateStepInRoute(step)
4546
}
4647

4748
// Change status to PENDING after resuming from FAILED
4849
if (step.execution.status === 'FAILED') {
50+
step.execution.startedAt = Date.now()
4951
step.execution.status = 'PENDING'
5052
this.updateStepInRoute(step)
5153
}
@@ -69,6 +71,9 @@ export class StatusManager {
6971
throw Error("Can't update empty execution.")
7072
}
7173
step.execution.status = status
74+
if (status === 'DONE') {
75+
step.execution.doneAt = Date.now()
76+
}
7277
if (execution) {
7378
step.execution = {
7479
...step.execution,
@@ -162,9 +167,6 @@ export class StatusManager {
162167
}
163168

164169
switch (status) {
165-
case 'STARTED':
166-
currentProcess.startedAt = Date.now()
167-
break
168170
case 'CANCELLED':
169171
currentProcess.doneAt = Date.now()
170172
break

src/core/StatusManager.unit.spec.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import type { Route } from '@lifi/types'
22
import type { Mock } from 'vitest'
33
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
4-
import { buildRouteObject, buildStepObject } from '../../tests/fixtures.js'
4+
import {
5+
SOME_DATE,
6+
buildRouteObject,
7+
buildStepObject,
8+
} from '../../tests/fixtures.js'
59
import { setupTestEnvironment } from '../../tests/setup.js'
610
import { StatusManager } from './StatusManager.js'
711
import { executionState } from './executionState.js'
@@ -45,6 +49,7 @@ describe('StatusManager', () => {
4549

4650
beforeEach(() => {
4751
updateRouteHookMock = vi.fn()
52+
vi.spyOn(Date, 'now').mockImplementation(() => SOME_DATE)
4853
})
4954

5055
describe('initExecutionObject', () => {
@@ -59,6 +64,7 @@ describe('StatusManager', () => {
5964
execution: {
6065
status: 'PENDING',
6166
process: [],
67+
startedAt: SOME_DATE,
6268
},
6369
})
6470

@@ -83,6 +89,9 @@ describe('StatusManager', () => {
8389
})
8490

8591
describe('updateExecution', () => {
92+
beforeEach(() => {
93+
vi.spyOn(Date, 'now').mockImplementation(() => SOME_DATE + 10)
94+
})
8695
describe('when no execution is defined yet', () => {
8796
beforeEach(() => {
8897
statusManager = initializeStatusManager({ includingExecution: false })

src/core/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ export type Process = {
162162
}
163163

164164
export interface Execution {
165+
startedAt: number
166+
doneAt?: number
165167
status: ExecutionStatus
166168
process: Array<Process>
167169
fromAmount?: string

tests/fixtures.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const SOME_OTHER_TOKEN: Token = {
1414
priceUSD: '',
1515
}
1616

17-
const SOME_DATE = new Date('2021-04-10').getTime()
17+
export const SOME_DATE = new Date('2021-04-10').getTime()
1818

1919
export const buildStepObject = ({
2020
includingExecution = true,
@@ -124,6 +124,8 @@ export const buildStepObject = ({
124124
execution: includingExecution
125125
? {
126126
status: 'PENDING',
127+
startedAt: SOME_DATE,
128+
doneAt: SOME_DATE + 10,
127129
process: [
128130
{
129131
type: 'TOKEN_ALLOWANCE',

0 commit comments

Comments
 (0)