Skip to content

Commit

Permalink
test: added RequestTimeService tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarens2 committed Jan 9, 2024
1 parent 4cbfbc4 commit c0cb46b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
12 changes: 6 additions & 6 deletions src/common/genesis-time/genesis-time.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('GenesisTimeService', () => {
jest.resetAllMocks();
});

it(`GenesisTimeService init correctly`, async () => {
it(`inits correctly`, async () => {
jest.spyOn(consensusProvider, 'getGenesis').mockResolvedValue({
data: {
genesis_time: '10000',
Expand All @@ -66,15 +66,15 @@ describe('GenesisTimeService', () => {
expect(result).toBe(10000);
});

it(`GenesisTimeService init expected to fail`, async () => {
it(`expected to fail when genesis time empty`, async () => {
jest.spyOn(consensusProvider, 'getGenesis').mockResolvedValue({
data: {},
});

await expect(moduleRef.init()).rejects.toEqual(new Error('Failed to get genesis time'));
});

it(`GenesisTimeService getCurrentEpoch`, async () => {
it(`get current epoch`, async () => {
jest.spyOn(consensusProvider, 'getGenesis').mockResolvedValue({
data: {
genesis_time: '1606824023',
Expand All @@ -88,7 +88,7 @@ describe('GenesisTimeService', () => {
expect(result).toBe(246253);
});

it(`GenesisTimeService getFrameOfEpoch`, async () => {
it(`getFrameOfEpoch`, async () => {
jest.spyOn(consensusProvider, 'getGenesis').mockResolvedValue({
data: {
genesis_time: '1606824023',
Expand All @@ -104,7 +104,7 @@ describe('GenesisTimeService', () => {
expect(service.getFrameOfEpoch(201600 + 450)).toBe(2);
});

it(`GenesisTimeService time to frame`, async () => {
it(`get frame of epoch`, async () => {
jest.spyOn(consensusProvider, 'getGenesis').mockResolvedValue({
data: {
genesis_time: '1606824023',
Expand All @@ -120,7 +120,7 @@ describe('GenesisTimeService', () => {
expect(service.getFrameOfEpoch(201600 + 450)).toBe(2);
});

it(`GenesisTimeService test timeToWithdrawalFrame`, async () => {
it(`time to withdrawal frame`, async () => {
jest.spyOn(consensusProvider, 'getGenesis').mockResolvedValue({
data: {
genesis_time: '1606824023',
Expand Down
75 changes: 45 additions & 30 deletions src/http/request-time/request-time.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('RequestTimeService', () => {
const currentEpoch = 252025;
const initialEpoch = 201600;
const epochPerFrame = 225;
const lockedSystemTimestamp = 1703601993996; // 2023-12-26T14:46:33.996Z

// mocks
const getFrameOfEpochMock = (epoch) => {
Expand All @@ -48,7 +49,7 @@ describe('RequestTimeService', () => {

beforeAll(() => {
jest.useFakeTimers();
jest.setSystemTime(new Date(1703601993996));
jest.setSystemTime(new Date(lockedSystemTimestamp));
});

afterAll(() => {
Expand Down Expand Up @@ -121,27 +122,8 @@ describe('RequestTimeService', () => {
contractConfig = moduleRef.get<ContractConfigStorageService>(ContractConfigStorageService);
genesisTimeService = moduleRef.get<GenesisTimeService>(GenesisTimeService);
validatorsStorage = moduleRef.get<ValidatorsStorageService>(ValidatorsStorageService);
});

afterEach(async () => {
await moduleRef.close();
jest.resetAllMocks();
});

it(`calculates frame by rewards only`, () => {
jest.spyOn(contractConfig, 'getInitialEpoch').mockReturnValue(initialEpoch);
jest.spyOn(contractConfig, 'getEpochsPerFrame').mockReturnValue(epochPerFrame);
jest.spyOn(genesisTimeService, 'getCurrentEpoch').mockReturnValue(currentEpoch);
jest.spyOn(genesisTimeService, 'getFrameOfEpoch').mockImplementation(getFrameOfEpochMock);
jest.spyOn(rewardsStorage, 'getRewardsPerFrame').mockReturnValue(rewardsPerFrame);

const expectedResult = 3;
const result = service.calculateFrameByRewardsOnly(BigNumber.from(rewardsPerFrame).mul(expectedResult));

expect(result).toBe(getFrameOfEpochMock(currentEpoch) + expectedResult + 1);
});

it(`calculates withdrawal time by some type`, async () => {
// mocks
jest.spyOn(contractConfig, 'getInitialEpoch').mockReturnValue(initialEpoch);
jest.spyOn(contractConfig, 'getEpochsPerFrame').mockReturnValue(epochPerFrame);
jest.spyOn(contractConfig, 'getMaxValidatorExitRequestsPerReport').mockReturnValue(600);
Expand All @@ -153,45 +135,78 @@ describe('RequestTimeService', () => {
jest.spyOn(genesisTimeService, 'timeToWithdrawalFrame').mockImplementation(timeToWithdrawalFrameMock);
jest.spyOn(rewardsStorage, 'getRewardsPerFrame').mockReturnValue(rewardsPerFrame);
jest.spyOn(validatorsStorage, 'getTotal').mockReturnValue(10000);
});

afterEach(async () => {
await moduleRef.close();
jest.resetAllMocks();
});

it(`calculates frame by rewards only`, () => {
const countFrames = 3;
const expectedResult = getFrameOfEpochMock(currentEpoch) + countFrames + 1;
const result = service.calculateFrameByRewardsOnly(BigNumber.from(rewardsPerFrame).mul(countFrames));

expect(result).toBe(expectedResult);
});

it(`calculates withdrawal type buffer`, async () => {
const result1 = await service.calculateWithdrawalTimeV2({
unfinalized: BigNumber.from('1007748958196602737132'),
buffer: BigNumber.from('1007748958196602737137'),
vaultsBalance: BigNumber.from('0'),
requestTimestamp: 1703592941739,
requestTimestamp: lockedSystemTimestamp,
latestEpoch: '312321',
});

expect(result1.type).toBe(RequestTimeCalculationType.buffer);
});

const result2 = await service.calculateWithdrawalTimeV2({
it(`calculates withdrawal type requestTimestampMargin`, async () => {
const result = await service.calculateWithdrawalTimeV2({
unfinalized: BigNumber.from('1007748958196602737132'),
buffer: BigNumber.from('1007748958196602737137'),
vaultsBalance: BigNumber.from('0'),
requestTimestamp: 1703687441739,
latestEpoch: '312321',
});

expect(result2.type).toBe(RequestTimeCalculationType.requestTimestampMargin);
expect(result.type).toBe(RequestTimeCalculationType.requestTimestampMargin);
});

const result3 = await service.calculateWithdrawalTimeV2({
it(`calculates withdrawal type vaultsBalance`, async () => {
const result = await service.calculateWithdrawalTimeV2({
unfinalized: BigNumber.from('1007748958196602737138'),
buffer: BigNumber.from('1007748958196602737137'),
vaultsBalance: BigNumber.from('2'),
requestTimestamp: 1703592941739,
requestTimestamp: lockedSystemTimestamp,
latestEpoch: '312321',
});

expect(result3.type).toBe(RequestTimeCalculationType.vaultsBalance);
expect(result.type).toBe(RequestTimeCalculationType.vaultsBalance);
});

const result4 = await service.calculateWithdrawalTimeV2({
it(`calculates withdrawal type rewardsOnly`, async () => {
const result = await service.calculateWithdrawalTimeV2({
unfinalized: BigNumber.from('1007748958196602737138'),
buffer: BigNumber.from('1007748958196602737137'),
vaultsBalance: BigNumber.from('0'),
requestTimestamp: 1703592941739,
requestTimestamp: lockedSystemTimestamp,
latestEpoch: '312321',
});

expect(result.type).toBe(RequestTimeCalculationType.rewardsOnly);
});

it(`calculates withdrawal type exit validators`, async () => {
const result = await service.calculateWithdrawalTimeV2({
unfinalized: BigNumber.from('10000007748958196602737138'),
buffer: BigNumber.from('0'),
vaultsBalance: BigNumber.from('0'),
requestTimestamp: lockedSystemTimestamp,
latestEpoch: '312321',
});

expect(result4.type).toBe(RequestTimeCalculationType.rewardsOnly);
expect(result.type).toBe(RequestTimeCalculationType.exitValidators);
});
});

0 comments on commit c0cb46b

Please sign in to comment.