Skip to content

Commit

Permalink
test(useGetLastCycleRewardsTimestamps): unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscotobar committed Dec 4, 2024
1 parent 1b00a27 commit 26f1016
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import {
useGetRewardDistributionFinishedLogs,
useGetLastCycleRewardsTimestamps,
RewardDistributionFinishedEventLog,
} from '@/app/collective-rewards/rewards'
import { describe, expect, it, vi } from 'vitest'
import { Cycle } from '@/app/collective-rewards/metrics'
import { DateTime, Duration } from 'luxon'

vi.mock('@/app/collective-rewards/rewards/hooks/useGetRewardDistributionFinishedLogs', () => {
return {
useGetRewardDistributionFinishedLogs: vi.fn(),
}
})

describe('useGetLastCycleRewardsTimestamps', () => {
type Log = RewardDistributionFinishedEventLog[number] & { timeStamp: number }
const startTimestamp = 1733011200 // 2024-12-01 00:00:00
const duration = 1209600 // 14 days
const distributionWindow = 3600 // 1 hour
const cycle: Cycle = {
cycleStart: DateTime.fromSeconds(startTimestamp),
cycleNext: DateTime.fromSeconds(startTimestamp + duration),
cycleDuration: Duration.fromObject({ seconds: duration }),
fistCycleStart: DateTime.fromSeconds(startTimestamp),
endDistributionWindow: DateTime.fromSeconds(distributionWindow),
}
const { cycleStart, cycleDuration } = cycle
const lastCycleStart = cycle.cycleStart.minus({ seconds: cycleDuration.as('seconds') })

it('should return a 0 time difference when there are no distributions', () => {
vi.mocked(useGetRewardDistributionFinishedLogs).mockImplementation(() => {
return {
data: [],
isLoading: false,
error: null,
}
})

const { data } = useGetLastCycleRewardsTimestamps(cycle)

expect(data).toEqual({
fromTimestamp: lastCycleStart.toSeconds(),
toTimestamp: lastCycleStart.toSeconds(),
})
})

it('should return a 0 time difference when there is only one distribution and happened in the previous cycle', () => {
vi.mocked(useGetRewardDistributionFinishedLogs).mockImplementation(() => {
return {
data: [
{
timeStamp: lastCycleStart.toSeconds(),
},
] as Log[],
isLoading: false,
error: null,
}
})

const { data } = useGetLastCycleRewardsTimestamps(cycle)

expect(data).toEqual({
fromTimestamp: lastCycleStart.toSeconds(),
toTimestamp: lastCycleStart.toSeconds(),
})
})

it('should return from the last cycle start to cycle start when there is only one distribution and did not happen in this cycle', () => {
vi.mocked(useGetRewardDistributionFinishedLogs).mockImplementation(() => {
return {
data: [
{
timeStamp: cycleStart.toSeconds(),
},
] as Log[],
isLoading: false,
error: null,
}
})

const { data } = useGetLastCycleRewardsTimestamps(cycle)

expect(data).toEqual({
fromTimestamp: lastCycleStart.toSeconds(),
toTimestamp: cycleStart.toSeconds(),
})
})

it('should return a 0 time difference when there is no distribution in this cycle', () => {
const beforeLastCycleStart = lastCycleStart.minus({ seconds: cycleDuration.as('seconds') })
vi.mocked(useGetRewardDistributionFinishedLogs).mockImplementation(() => {
return {
data: [
{
timeStamp: beforeLastCycleStart.toSeconds(),
},
{
timeStamp: lastCycleStart.toSeconds(),
},
] as Log[],
isLoading: false,
error: null,
}
})

const { data } = useGetLastCycleRewardsTimestamps(cycle)

expect(data).toEqual({
fromTimestamp: lastCycleStart.toSeconds(),
toTimestamp: lastCycleStart.toSeconds(),
})
})

it('should return from the (last cycle start + 1) to cycle start when there are 2 consecutive distributions and one was in this cycle', () => {
vi.mocked(useGetRewardDistributionFinishedLogs).mockImplementation(() => {
return {
data: [
{
timeStamp: lastCycleStart.toSeconds(),
},
{
timeStamp: cycleStart.toSeconds(),
},
] as Log[],
isLoading: false,
error: null,
}
})

const { data } = useGetLastCycleRewardsTimestamps(cycle)

expect(data).toEqual({
fromTimestamp: lastCycleStart.toSeconds() + 1,
toTimestamp: cycleStart.toSeconds(),
})
})

it('should return from the last cycle start to cycle start when there are 2 distributions and one was in this cycle', () => {
const beforeLastCycleStart = lastCycleStart.minus({ seconds: cycleDuration.as('seconds') })
vi.mocked(useGetRewardDistributionFinishedLogs).mockImplementation(() => {
return {
data: [
{
timeStamp: beforeLastCycleStart.toSeconds(),
},
{
timeStamp: cycleStart.toSeconds(),
},
] as Log[],
isLoading: false,
error: null,
}
})

const { data } = useGetLastCycleRewardsTimestamps(cycle)

expect(data).toEqual({
fromTimestamp: lastCycleStart.toSeconds(),
toTimestamp: cycleStart.toSeconds(),
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@/app/collective-rewards/rewards'

type Log = RewardDistributionFinishedEventLog[number] & { timeStamp: number }
export const useGetLastCycleRewardsTimestamps = ({ cycleStart, cycleDuration, cycleNext }: Cycle) => {
export const useGetLastCycleRewardsTimestamps = ({ cycleStart, cycleDuration }: Cycle) => {
const {
data: rewardDistributionFinished,
isLoading: rewardDistributionFinishedLoading,
Expand Down

0 comments on commit 26f1016

Please sign in to comment.