Skip to content

Commit

Permalink
tests passing for leases, renewals, regions
Browse files Browse the repository at this point in the history
  • Loading branch information
filvecchiato committed Dec 16, 2024
1 parent 45d7a63 commit 7f8d384
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 9 deletions.
105 changes: 97 additions & 8 deletions src/services/coretime/CoretimeService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { ApiPromise } from '@polkadot/api';
// import { StorageKey } from '@polkadot/types';
import { Hash } from '@polkadot/types/interfaces';

import { kusamaCoretimeMetadata } from '../../test-helpers/metadata/coretimeKusamaMetadata';
// import { coretimeKusamaRegistryV1003003 } from '../../test-helpers/registries/coretimeChainKusamaRegistry';
import { createApiWithAugmentations, TypeFactory } from '../../test-helpers/typeFactory';
import { blockHash22887036 } from '../test-helpers/mock';
import { mockLeases, mockRegions, potentialRenewalsMocks } from '../test-helpers/mock/coretime';
import { blockHash26187139 } from '../test-helpers/mock/mockBlock26187139';
import { mockKusamaCoretimeApiBlock26187139 } from '../test-helpers/mock/mockCoretimeChainApi';
import { mockKusamaApiBlock26187139 } from '../test-helpers/mock/mockKusamaApiBlock26187139';
Expand Down Expand Up @@ -56,7 +61,68 @@ const mockKusamaApi = {
},
} as unknown as ApiPromise;

const renewalsEntries = () => Promise.resolve().then(() => []);
// const mockRenewals = [
// {
// completion: 'Complete',
// core: '7',
// mask: '0xffffffffffffffffffff',
// price: '7767758517',
// task: '2004',
// when: '336168',
// },
// {
// completion: 'Complete',
// core: '8',
// mask: '0xffffffffffffffffffff',
// price: '7769145866',
// task: '2011',
// when: '326088',
// },
// {
// completion: 'Complete',
// core: '11',
// mask: '0xffffffffffffffffffff',
// price: '7767758517',
// task: '2095',
// when: '336168',
// },
// ];
const coretimeApi = createApiWithAugmentations(kusamaCoretimeMetadata);
const coretimeTypeFactory = new TypeFactory(coretimeApi);

const regionsEntries = () =>
Promise.resolve().then(() =>
mockRegions.map((region) => {
const storageEntry = coretimeApi.query.broker.regions;
const key = coretimeTypeFactory.storageKey(region.key, 'PalletBrokerRegionId', storageEntry);
return [
key,
mockKusamaCoretimeApiBlock26187139.registry.createType('Option<PalletBrokerRegionRecord>', region.value),
];
}),
);

const leases = () =>
Promise.resolve().then(() =>
mockLeases.map((lease) => {
return mockKusamaCoretimeApiBlock26187139.registry.createType('PalletBrokerLeaseRecordItem', lease);
}),
);

const potentialRenewalsEntries = () =>
Promise.resolve().then(() =>
potentialRenewalsMocks.map((renewal) => {
const storageEntry = coretimeApi.query.broker.potentialRenewals;
const key = coretimeTypeFactory.storageKey(renewal.key, 'PalletBrokerPotentialRenewalId', storageEntry);
return [
key,
mockKusamaCoretimeApiBlock26187139.registry.createType(
'Option<PalletBrokerPotentialRenewalRecord>',
renewal.value,
),
];
}),
);

const mockCoretimeApi = {
...mockKusamaCoretimeApiBlock26187139,
Expand All @@ -74,10 +140,10 @@ const mockCoretimeApi = {
mockKusamaCoretimeApiBlock26187139.registry.createType('Option<PalletBrokerConfigRecord>', {}),
),
potentialRenewals: {
entries: renewalsEntries,
entries: potentialRenewalsEntries,
},
reservations: () => [],
leases: () => [],
leases: leases,
saleInfo: () => {},
workplan: {
entries: () => [],
Expand All @@ -87,7 +153,7 @@ const mockCoretimeApi = {
entries: () => [],
},
regions: {
entries: () => [],
entries: regionsEntries,
},
},
paras: {
Expand All @@ -110,10 +176,14 @@ describe('CoretimeService', () => {
);
});
it('should return regions', async () => {
// regions and at given block
// test for coretime and relaychain
console.log(await CoretimeServiceAtCoretimeChain.getCoretimeRegions(blockHash26187139));
return;
const regions = await CoretimeServiceAtCoretimeChain.getCoretimeRegions(blockHash26187139);
expect(regions.regions).toHaveLength(2);
expect(regions.at).toHaveProperty('hash');
expect(regions.regions[0]).toHaveProperty('begin');
expect(regions.regions[0]).toHaveProperty('end');
expect(regions.regions[0]).toHaveProperty('core');
expect(regions.regions[0]).toHaveProperty('owner');
expect(regions.regions[0]).toHaveProperty('paid');
});

it('should return empty array if no regions', () => {
Expand All @@ -127,6 +197,15 @@ describe('CoretimeService', () => {
'This endpoint is only available on coretime chains.',
);
});

it('should return leases', async () => {
const leases = await CoretimeServiceAtCoretimeChain.getCoretimeLeases(blockHash26187139);

expect(leases.leases).toHaveLength(2);
expect(leases.at).toHaveProperty('hash');
expect(leases.leases[0]).toHaveProperty('task');
expect(leases.leases[0]).toHaveProperty('until');
});
});

describe('getReservations', () => {
Expand All @@ -143,6 +222,16 @@ describe('CoretimeService', () => {
'This endpoint is only available on coretime chains.',
);
});

it('should return renewals', async () => {
const renewals = await CoretimeServiceAtCoretimeChain.getCoretimeRenewals(blockHash26187139);
expect(renewals.renewals).toHaveLength(2);
expect(renewals.at).toHaveProperty('hash');
expect(renewals.renewals[0]).toHaveProperty('core');
expect(renewals.renewals[0]).toHaveProperty('price');
expect(renewals.renewals[0]).toHaveProperty('task');
expect(renewals.renewals[0]).toHaveProperty('when');
});
});

describe('getInfo', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/services/coretime/CoretimeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class CoretimeService extends AbstractService {
private getAndDecodeRegions = async (api: ApiDecoration<'promise'>, coreId?: number): Promise<TRegionInfo[]> => {
const regions = await api.query.broker.regions.entries();
const regs = regions as unknown as [StorageKey<[PalletBrokerRegionId]>, Option<PalletBrokerRegionRecord>][];

const regionsInfo = regs.map((region) => {
return extractRegionInfo([region[0], region[1]]);
});
Expand Down Expand Up @@ -159,7 +160,6 @@ export class CoretimeService extends AbstractService {
StorageKey<[PalletBrokerPotentialRenewalId]>,
Option<PalletBrokerPotentialRenewalRecord>,
][];

const potentialRenewalsInfo = sortByCore(
renewals.map((renewal) => extractPotentialRenewalInfo(renewal[1], renewal[0])),
);
Expand Down
50 changes: 50 additions & 0 deletions src/services/test-helpers/mock/coretime/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export const mockRegions = [
{
key: '0x4dcb50595177a3177648411a42aca0f53dc63b0b76ffd6f80704a090da6f8719fd9bd0f1540b065ee2e9725610e019d965f904003d00ffffffffffffffffffff',
value: {
end: '331128',
owner: 'DwnzPJAKmdYLMpxDTqG6FLXjRpnris2fB965GJpmvYbamuE',
paid: '9440600837',
},
},
{
key: '0x4dcb50595177a3177648411a42aca0f53dc63b0b76ffd6f80704a090da6f8719fe1ad0b218654aa73d9b2ba13f09ed33c8f904003a00ffffffffffffffffffff',
value: {
end: '321048',
owner: 'Evn32VbNCLXGvEu7WZnQ7jfV7X4dbN62iUnt3k89AJT4oJj',
paid: '8060143865',
},
},
];

export const potentialRenewalsMocks = [
{
key: '0x4dcb50595177a3177648411a42aca0f5689a1593a634a1c1e2cd84ab4db3337ffc046620dd7b3b82380018e60400',
value: {
price: '55066361452',
completion: {
Complete: [{ mask: '0xffffffffffffffffffff', assignment: { Task: '2274' } }],
},
},
},
{
key: '0x4dcb50595177a3177648411a42aca0f5689a1593a634a1c1e2cd84ab4db3337ff68f3245ca31bebd180028210500',
value: {
price: '76754134107',
completion: {
Complete: [{ mask: '0xffffffffffffffffffff', assignment: { Task: '2007' } }],
},
},
},
];

export const mockLeases = [
{
task: '2000',
until: '340200',
},
{
task: '2084',
until: '340200',
},
];

0 comments on commit 7f8d384

Please sign in to comment.