2020-04-16 15:18:35 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
import Vuex from 'vuex';
|
|
|
|
|
|
|
|
import { makeNodeModule } from '@/app/store/modules/node';
|
2020-06-01 15:42:40 +01:00
|
|
|
import { makePayoutModule, PAYOUT_ACTIONS, PAYOUT_MUTATIONS } from '@/app/store/modules/payout';
|
2020-06-05 10:42:13 +01:00
|
|
|
import {
|
2020-07-01 17:34:01 +01:00
|
|
|
EstimatedPayout,
|
2020-06-05 10:42:13 +01:00
|
|
|
HeldHistory,
|
|
|
|
HeldHistoryMonthlyBreakdownItem,
|
|
|
|
HeldInfo,
|
|
|
|
PayoutInfoRange,
|
|
|
|
PayoutPeriod,
|
2020-07-01 17:34:01 +01:00
|
|
|
PreviousMonthEstimatedPayout,
|
2020-06-05 10:42:13 +01:00
|
|
|
TotalPayoutInfo,
|
|
|
|
} from '@/app/types/payout';
|
2020-06-01 15:42:40 +01:00
|
|
|
import { getHeldPercentage, getMonthsBeforeNow } from '@/app/utils/payout';
|
2020-04-16 15:18:35 +01:00
|
|
|
import { PayoutHttpApi } from '@/storagenode/api/payout';
|
|
|
|
import { SNOApi } from '@/storagenode/api/storagenode';
|
|
|
|
import { createLocalVue } from '@vue/test-utils';
|
|
|
|
|
|
|
|
const Vue = createLocalVue();
|
|
|
|
const payoutApi = new PayoutHttpApi();
|
|
|
|
const payoutModule = makePayoutModule(payoutApi);
|
|
|
|
|
|
|
|
const nodeApi = new SNOApi();
|
|
|
|
const nodeModule = makeNodeModule(nodeApi);
|
|
|
|
|
|
|
|
Vue.use(Vuex);
|
|
|
|
|
|
|
|
const store = new Vuex.Store({ modules: { payoutModule, node: nodeModule } });
|
|
|
|
|
|
|
|
const state = store.state as any;
|
|
|
|
|
2020-06-01 15:42:40 +01:00
|
|
|
describe('mutations', (): void => {
|
2020-04-16 15:18:35 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
createLocalVue().use(Vuex);
|
|
|
|
});
|
|
|
|
|
2020-06-01 15:42:40 +01:00
|
|
|
it('sets held information', (): void => {
|
2020-04-16 15:18:35 +01:00
|
|
|
const heldInfo = new HeldInfo(13, 12, 11);
|
|
|
|
|
|
|
|
store.commit(PAYOUT_MUTATIONS.SET_HELD_INFO, heldInfo);
|
|
|
|
|
|
|
|
expect(state.payoutModule.heldInfo.usageAtRest).toBe(13);
|
|
|
|
expect(state.payoutModule.heldInfo.usageGet).toBe(12);
|
|
|
|
expect(state.payoutModule.heldInfo.usagePut).toBe(11);
|
|
|
|
});
|
|
|
|
|
2020-06-01 15:42:40 +01:00
|
|
|
it('sets total payout information', (): void => {
|
2020-05-20 18:10:15 +01:00
|
|
|
const totalInfo = new TotalPayoutInfo(50, 100, 22);
|
2020-04-16 15:18:35 +01:00
|
|
|
|
|
|
|
store.commit(PAYOUT_MUTATIONS.SET_TOTAL, totalInfo);
|
|
|
|
|
|
|
|
expect(state.payoutModule.totalHeldAmount).toBe(50);
|
|
|
|
expect(state.payoutModule.totalEarnings).toBe(100);
|
2020-05-20 18:10:15 +01:00
|
|
|
expect(state.payoutModule.currentMonthEarnings).toBe(22);
|
2020-04-16 15:18:35 +01:00
|
|
|
});
|
|
|
|
|
2020-06-01 15:42:40 +01:00
|
|
|
it('sets period range', (): void => {
|
2020-04-16 15:18:35 +01:00
|
|
|
const range = new PayoutInfoRange(new PayoutPeriod(2019, 2), new PayoutPeriod(2020, 3));
|
|
|
|
|
|
|
|
store.commit(PAYOUT_MUTATIONS.SET_RANGE, range);
|
|
|
|
|
|
|
|
if (!state.payoutModule.periodRange.start) {
|
|
|
|
fail('periodRange.start is null');
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(state.payoutModule.periodRange.start.period).toBe('2019-03');
|
|
|
|
expect(state.payoutModule.periodRange.end.period).toBe('2020-04');
|
|
|
|
});
|
|
|
|
|
2020-06-01 15:42:40 +01:00
|
|
|
it('sets held percentage', (): void => {
|
2020-04-16 15:18:35 +01:00
|
|
|
const expectedHeldPercentage = 75;
|
|
|
|
|
|
|
|
store.commit(PAYOUT_MUTATIONS.SET_HELD_PERCENT, expectedHeldPercentage);
|
|
|
|
|
|
|
|
expect(state.payoutModule.heldPercentage).toBe(expectedHeldPercentage);
|
|
|
|
});
|
2020-06-05 10:42:13 +01:00
|
|
|
|
|
|
|
it('sets held history', (): void => {
|
|
|
|
const testHeldHistory = new HeldHistory([
|
|
|
|
new HeldHistoryMonthlyBreakdownItem('1', 'name1', 1, 50000, 0, 0, 0),
|
|
|
|
new HeldHistoryMonthlyBreakdownItem('2', 'name2', 5, 50000, 422280, 0, 0),
|
|
|
|
new HeldHistoryMonthlyBreakdownItem('3', 'name3', 6, 50000, 7333880, 7852235, 0),
|
|
|
|
]);
|
|
|
|
|
|
|
|
store.commit(PAYOUT_MUTATIONS.SET_HELD_HISTORY, testHeldHistory);
|
|
|
|
|
|
|
|
expect(state.payoutModule.heldHistory.monthlyBreakdown.length).toBe(testHeldHistory.monthlyBreakdown.length);
|
|
|
|
expect(state.payoutModule.heldHistory.monthlyBreakdown[1].satelliteName).toBe(testHeldHistory.monthlyBreakdown[1].satelliteName);
|
|
|
|
});
|
2020-05-27 19:41:51 +01:00
|
|
|
|
2020-07-01 17:34:01 +01:00
|
|
|
it('sets estimated payout information', (): void => {
|
|
|
|
const estimatedPayout = new EstimatedPayout(
|
|
|
|
new PreviousMonthEstimatedPayout(
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
5,
|
|
|
|
6,
|
|
|
|
7,
|
|
|
|
8,
|
|
|
|
9,
|
|
|
|
),
|
|
|
|
new PreviousMonthEstimatedPayout(
|
|
|
|
10,
|
|
|
|
11,
|
|
|
|
12,
|
|
|
|
13,
|
|
|
|
14,
|
|
|
|
15,
|
|
|
|
16,
|
|
|
|
17,
|
|
|
|
18,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
store.commit(PAYOUT_MUTATIONS.SET_ESTIMATION, estimatedPayout);
|
|
|
|
|
|
|
|
expect(state.payoutModule.estimation.currentMonth.payout).toBe(8);
|
|
|
|
expect(state.payoutModule.estimation.previousMonth.heldRate).toBe(16);
|
|
|
|
});
|
|
|
|
|
2020-05-27 19:41:51 +01:00
|
|
|
it('sets available periods', (): void => {
|
|
|
|
const firstExpectedPeriod = '2020-04';
|
|
|
|
const secondExpectedPeriod = '1999-11';
|
|
|
|
const incomingDataSet = [firstExpectedPeriod, secondExpectedPeriod];
|
|
|
|
|
|
|
|
store.commit(PAYOUT_MUTATIONS.SET_PERIODS, incomingDataSet);
|
|
|
|
|
|
|
|
expect(state.payoutModule.payoutPeriods.length).toBe(2);
|
|
|
|
expect(state.payoutModule.payoutPeriods[0]).toBe(firstExpectedPeriod);
|
|
|
|
expect(state.payoutModule.payoutPeriods[1]).toBe(secondExpectedPeriod);
|
|
|
|
});
|
2020-04-16 15:18:35 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('actions', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
2020-06-01 15:42:40 +01:00
|
|
|
it('success get held info by month', async (): Promise<void> => {
|
2020-04-16 15:18:35 +01:00
|
|
|
jest.spyOn(payoutApi, 'getHeldInfoByMonth').mockReturnValue(
|
|
|
|
Promise.resolve(new HeldInfo(1, 2 , 3, 4, 5)),
|
|
|
|
);
|
|
|
|
|
|
|
|
const range = new PayoutInfoRange(null, new PayoutPeriod(2020, 3));
|
|
|
|
|
|
|
|
store.commit(PAYOUT_MUTATIONS.SET_RANGE, range);
|
|
|
|
|
|
|
|
await store.dispatch(PAYOUT_ACTIONS.GET_HELD_INFO);
|
|
|
|
|
|
|
|
expect(state.payoutModule.heldInfo.usagePut).toBe(3);
|
|
|
|
expect(state.payoutModule.heldInfo.held).toBe(0);
|
|
|
|
expect(state.payoutModule.heldPercentage).toBe(getHeldPercentage(new Date()));
|
|
|
|
});
|
|
|
|
|
2020-06-01 15:42:40 +01:00
|
|
|
it('get held info by month throws an error when api call fails', async (): Promise<void> => {
|
2020-04-16 15:18:35 +01:00
|
|
|
jest.spyOn(payoutApi, 'getHeldInfoByMonth').mockImplementation(() => { throw new Error(); });
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(PAYOUT_ACTIONS.GET_HELD_INFO);
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
|
|
|
expect(state.payoutModule.heldInfo.usagePut).toBe(3);
|
|
|
|
expect(state.payoutModule.heldInfo.held).toBe(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-01 15:42:40 +01:00
|
|
|
it('success get held info by period', async (): Promise<void> => {
|
2020-04-16 15:18:35 +01:00
|
|
|
jest.spyOn(payoutApi, 'getHeldInfoByPeriod').mockReturnValue(
|
|
|
|
Promise.resolve(new HeldInfo(1, 2 , 3, 4, 5)),
|
|
|
|
);
|
|
|
|
|
|
|
|
const range = new PayoutInfoRange(new PayoutPeriod(2019, 2), new PayoutPeriod(2020, 3));
|
|
|
|
|
|
|
|
store.commit(PAYOUT_MUTATIONS.SET_RANGE, range);
|
|
|
|
|
|
|
|
await store.dispatch(PAYOUT_ACTIONS.GET_HELD_INFO);
|
|
|
|
|
|
|
|
expect(state.payoutModule.heldInfo.usagePut).toBe(3);
|
|
|
|
expect(state.payoutModule.heldInfo.held).toBe(0);
|
|
|
|
});
|
|
|
|
|
2020-06-01 15:42:40 +01:00
|
|
|
it('get held info by period throws an error when api call fails', async (): Promise<void> => {
|
2020-04-16 15:18:35 +01:00
|
|
|
jest.spyOn(payoutApi, 'getHeldInfoByPeriod').mockImplementation(() => { throw new Error(); });
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(PAYOUT_ACTIONS.GET_HELD_INFO);
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
|
|
|
expect(state.payoutModule.heldInfo.usagePut).toBe(3);
|
|
|
|
expect(state.payoutModule.heldInfo.held).toBe(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-01 15:42:40 +01:00
|
|
|
it('success get total', async (): Promise<void> => {
|
2020-04-16 15:18:35 +01:00
|
|
|
jest.spyOn(payoutApi, 'getTotal').mockReturnValue(
|
2020-05-20 18:10:15 +01:00
|
|
|
Promise.resolve(new TotalPayoutInfo(10, 20, 5)),
|
2020-04-16 15:18:35 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
await store.dispatch(PAYOUT_ACTIONS.GET_TOTAL);
|
|
|
|
|
|
|
|
expect(state.payoutModule.totalHeldAmount).toBe(10);
|
2020-05-20 18:10:15 +01:00
|
|
|
expect(state.payoutModule.totalEarnings).toBe(20);
|
|
|
|
expect(state.payoutModule.currentMonthEarnings).toBe(0);
|
2020-04-16 15:18:35 +01:00
|
|
|
});
|
|
|
|
|
2020-06-01 15:42:40 +01:00
|
|
|
it('get total throws an error when api call fails', async (): Promise<void> => {
|
2020-04-16 15:18:35 +01:00
|
|
|
jest.spyOn(payoutApi, 'getTotal').mockImplementation(() => { throw new Error(); });
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(PAYOUT_ACTIONS.GET_TOTAL);
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
|
|
|
expect(state.payoutModule.totalHeldAmount).toBe(10);
|
2020-05-20 18:10:15 +01:00
|
|
|
expect(state.payoutModule.totalEarnings).toBe(20);
|
|
|
|
expect(state.payoutModule.currentMonthEarnings).toBe(0);
|
2020-04-16 15:18:35 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-27 19:41:51 +01:00
|
|
|
it('success fetches available periods', async (): Promise<void> => {
|
|
|
|
const firstExpectedPeriod = '2020-04';
|
|
|
|
const secondExpectedPeriod = '1999-11';
|
|
|
|
|
|
|
|
jest.spyOn(payoutApi, 'getPayoutPeriods').mockReturnValue(
|
|
|
|
Promise.resolve([
|
|
|
|
PayoutPeriod.fromString(firstExpectedPeriod),
|
|
|
|
PayoutPeriod.fromString(secondExpectedPeriod),
|
|
|
|
]),
|
2020-04-16 15:18:35 +01:00
|
|
|
);
|
|
|
|
|
2020-05-27 19:41:51 +01:00
|
|
|
await store.dispatch(PAYOUT_ACTIONS.GET_PERIODS);
|
|
|
|
|
|
|
|
expect(state.payoutModule.payoutPeriods.length).toBe(2);
|
|
|
|
expect(state.payoutModule.payoutPeriods[0].period).toBe(firstExpectedPeriod);
|
|
|
|
expect(state.payoutModule.payoutPeriods[1].period).toBe(secondExpectedPeriod);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('get available periods throws an error when api call fails', async () => {
|
|
|
|
jest.spyOn(payoutApi, 'getPayoutPeriods').mockImplementation(() => { throw new Error(); });
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(PAYOUT_ACTIONS.GET_PERIODS);
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
|
|
|
expect(state.payoutModule.payoutPeriods.length).toBe(2);
|
|
|
|
}
|
2020-04-16 15:18:35 +01:00
|
|
|
});
|
2020-06-05 10:42:13 +01:00
|
|
|
|
|
|
|
it('success get held history', async (): Promise<void> => {
|
|
|
|
jest.spyOn(payoutApi, 'getHeldHistory').mockReturnValue(
|
|
|
|
Promise.resolve(new HeldHistory([
|
|
|
|
new HeldHistoryMonthlyBreakdownItem('1', 'name1', 1, 50000, 0, 0, 0),
|
|
|
|
new HeldHistoryMonthlyBreakdownItem('2', 'name2', 5, 50000, 422280, 0, 0),
|
|
|
|
new HeldHistoryMonthlyBreakdownItem('3', 'name3', 6, 50000, 7333880, 7852235, 0),
|
|
|
|
])),
|
|
|
|
);
|
|
|
|
|
|
|
|
await store.dispatch(PAYOUT_ACTIONS.GET_HELD_HISTORY);
|
|
|
|
|
|
|
|
expect(state.payoutModule.heldHistory.monthlyBreakdown.length).toBe(3);
|
|
|
|
expect(state.payoutModule.heldHistory.monthlyBreakdown[1].satelliteName).toBe('name2');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('get total throws an error when api call fails', async (): Promise<void> => {
|
|
|
|
jest.spyOn(payoutApi, 'getHeldHistory').mockImplementation(() => { throw new Error(); });
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(PAYOUT_ACTIONS.GET_HELD_HISTORY);
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
|
|
|
expect(state.payoutModule.heldHistory.monthlyBreakdown.length).toBe(3);
|
|
|
|
expect(state.payoutModule.heldHistory.monthlyBreakdown[1].satelliteName).toBe('name2');
|
|
|
|
}
|
|
|
|
});
|
2020-07-01 17:34:01 +01:00
|
|
|
|
|
|
|
it('get estimated payout information throws an error when api call fails', async (): Promise<void> => {
|
|
|
|
jest.spyOn(payoutApi, 'getEstimatedInfo').mockImplementation(() => { throw new Error(); });
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(PAYOUT_ACTIONS.GET_ESTIMATION);
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
|
|
|
expect(state.payoutModule.estimation.currentMonth.held).toBe(9);
|
|
|
|
expect(state.payoutModule.estimation.previousMonth.diskSpace).toBe(14);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('success get estimated payout information', async (): Promise<void> => {
|
|
|
|
jest.spyOn(payoutApi, 'getEstimatedInfo').mockReturnValue(
|
|
|
|
Promise.resolve(new EstimatedPayout(
|
|
|
|
new PreviousMonthEstimatedPayout(
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
300,
|
|
|
|
4,
|
|
|
|
5,
|
|
|
|
6,
|
|
|
|
7,
|
|
|
|
8,
|
|
|
|
9,
|
|
|
|
),
|
|
|
|
new PreviousMonthEstimatedPayout(
|
|
|
|
10,
|
|
|
|
11,
|
|
|
|
12,
|
|
|
|
13,
|
|
|
|
700,
|
|
|
|
15,
|
|
|
|
16,
|
|
|
|
17,
|
|
|
|
18,
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
await store.dispatch(PAYOUT_ACTIONS.GET_ESTIMATION);
|
|
|
|
|
|
|
|
expect(state.payoutModule.estimation.currentMonth.egressRepairAudit).toBe(300);
|
|
|
|
expect(state.payoutModule.estimation.previousMonth.diskSpace).toBe(700);
|
|
|
|
});
|
2020-04-16 15:18:35 +01:00
|
|
|
});
|
|
|
|
|
2020-06-01 15:42:40 +01:00
|
|
|
describe('utils functions', (): void => {
|
|
|
|
const _Date = Date;
|
|
|
|
|
|
|
|
// TODO: investigate reset mocks in config
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
global.Date = _Date;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('get correct held percentage', (): void => {
|
|
|
|
const testDates: Date[] = [
|
|
|
|
new Date(Date.UTC(2020, 0, 30)),
|
|
|
|
new Date(Date.UTC(2019, 10, 29)),
|
|
|
|
new Date(Date.UTC(2019, 7, 24)),
|
|
|
|
new Date(Date.UTC(2018, 1, 24)),
|
|
|
|
];
|
2020-04-16 15:18:35 +01:00
|
|
|
const expectedHeldPercentages: number[] = [75, 50, 25, 0];
|
|
|
|
|
2020-06-01 15:42:40 +01:00
|
|
|
const mockedDate = new Date(1580522290000); // Sat Feb 01 2020
|
|
|
|
global.Date = jest.fn(() => mockedDate);
|
|
|
|
|
|
|
|
for (let i = 0; i < testDates.length; i++) {
|
|
|
|
const heldPercentage = getHeldPercentage(testDates[i]);
|
2020-04-16 15:18:35 +01:00
|
|
|
|
|
|
|
expect(heldPercentage).toBe(expectedHeldPercentages[i]);
|
|
|
|
}
|
|
|
|
});
|
2020-06-01 15:42:40 +01:00
|
|
|
|
|
|
|
it('get correct months difference', (): void => {
|
|
|
|
const testDates: Date[] = [
|
|
|
|
new Date(Date.UTC(2020, 0, 30)),
|
|
|
|
new Date(Date.UTC(2019, 10, 29)),
|
|
|
|
new Date(Date.UTC(2019, 7, 24)),
|
|
|
|
new Date(Date.UTC(2018, 1, 24)),
|
|
|
|
];
|
|
|
|
const expectedMonthsCount: number[] = [2, 4, 7, 25];
|
|
|
|
|
|
|
|
const mockedDate = new Date(1580522290000); // Sat Feb 01 2020
|
|
|
|
global.Date = jest.fn(() => mockedDate);
|
|
|
|
|
|
|
|
for (let i = 0; i < testDates.length; i++) {
|
|
|
|
const heldPercentage = getMonthsBeforeNow(testDates[i]);
|
|
|
|
|
|
|
|
expect(heldPercentage).toBe(expectedMonthsCount[i]);
|
|
|
|
}
|
|
|
|
});
|
2020-04-16 15:18:35 +01:00
|
|
|
});
|