Skip to content

Commit

Permalink
Merge branch 'main' into marmaladedocs
Browse files Browse the repository at this point in the history
  • Loading branch information
nillo committed Oct 25, 2023
2 parents 001ebf5 + 334c218 commit d3791a1
Show file tree
Hide file tree
Showing 19 changed files with 537 additions and 16,363 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-moons-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kadena/chainwebjs': none
---

Migrate from jest to vitest
10 changes: 0 additions & 10 deletions packages/libs/chainwebjs/jest.config.js

This file was deleted.

8 changes: 4 additions & 4 deletions packages/libs/chainwebjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"lint:fmt": "prettier . --cache --check",
"lint:pkg": "lint-package",
"lint:src": "eslint src --ext .js,.ts",
"test": "jest"
"test": "vitest"
},
"dependencies": {
"base64url": "^3.0.1",
Expand All @@ -74,12 +74,12 @@
"@microsoft/api-extractor": "^7.38.0",
"@rushstack/eslint-config": "~3.3.0",
"@types/eventsource": "1.1.11",
"@types/jest": "^29.5.3",
"@types/node": "^18.17.14",
"eslint": "^8.45.0",
"jest": "^29.7.0",
"msw": "^1.3.2",
"prettier": "~3.0.3",
"typescript": "5.2.2"
"typescript": "5.2.2",
"vitest": "^0.34.6"
},
"publishConfig": {
"provenance": true
Expand Down
231 changes: 172 additions & 59 deletions packages/libs/chainwebjs/src/tests/blocks.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
jest.mock('cross-fetch', () => {
return {
__esModule: true,
default: jest.fn(),
};
});
import fetch from 'cross-fetch';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import chainweb from '..';
import type { IBlockHeader, IBlockPayload, IPagedResponse } from '../types';
import { config } from './config';
import { blockByHeightCurrentCutMock } from './mocks/blockByHeightCurrentCutMock';
import { blockByHeightPayloadsMock } from './mocks/blockByHeightPayloadsMock';
import { blockRecentsPayloadsMock } from './mocks/blockRecentsPayloadsMock';
import { blockRecentsRecentHeadersMock } from './mocks/blockRecentsRecentHeaders';
import { header } from './mocks/header';
import { makeFetchResponse, mockFetch, urlHelper } from './mokker';
import { rangeHeadersMock } from './mocks/rangeHeaderMock';
import { rangePayloadsMock } from './mocks/rangePayloadsMock';

const server = setupServer();
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

const mockedFunctionFetch = fetch as jest.MockedFunction<typeof fetch>;
mockedFunctionFetch.mockImplementation(
mockFetch as jest.MockedFunction<typeof fetch>,
);
/* ************************************************************************** */
/* Test settings */

jest.setTimeout(25000);
const debug: boolean = false;

/* ************************************************************************** */
Expand All @@ -30,11 +29,6 @@ const logg = (...args: unknown[]): void => {
}
};

beforeEach(() => {
mockedFunctionFetch.mockImplementation(
mockFetch as jest.MockedFunction<typeof fetch>,
);
});
/* ************************************************************************** */
/* Blocks */

Expand All @@ -46,6 +40,37 @@ describe('chainweb.block', () => {
/* By Height */

it('gets the block by height an validates', async () => {
server.resetHandlers(
rest.get(
'https://api.chainweb.com/chainweb/0.0/mainnet01/cut',
(req, res, ctx) => {
return res.once(
ctx.status(200),
ctx.json(blockByHeightCurrentCutMock),
);
},
),
rest.post(
'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/header/branch',
(req, res, ctx) => {
return res.once(
ctx.status(200),
ctx.json({
limit: 1,
items: [header],
next: null,
}),
);
},
),
rest.post(
'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/payload/outputs/batch',
(req, res, ctx) => {
return res.once(ctx.status(200), ctx.json(blockByHeightPayloadsMock));
},
),
);

const r = await chainweb.block.height(
0,
height,
Expand All @@ -63,6 +88,28 @@ describe('chainweb.block', () => {
/* By Block Hash */

it('gets the block by block hash an validates', async () => {
server.resetHandlers(
rest.post(
'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/header/branch',
(req, res, ctx) => {
return res.once(
ctx.status(200),
ctx.json({
limit: 1,
items: [header],
next: null,
}),
);
},
),
rest.post(
'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/payload/outputs/batch',
(req, res, ctx) => {
return res.once(ctx.status(200), ctx.json(blockByHeightPayloadsMock));
},
),
);

const r = await chainweb.block.blockHash(
0,
blockHash,
Expand All @@ -77,48 +124,28 @@ describe('chainweb.block', () => {
});

it('throws on fetching Block by blockhash without payload', async () => {
const localMockFetch = (url: URL | string, init?: RequestInit): unknown => {
const path = urlHelper(url);
switch (path) {
case 'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/payload/outputs/batch':
return makeFetchResponse<IBlockPayload<string[]>[]>(
[] as unknown as IBlockPayload<string[]>[],
server.resetHandlers(
rest.post(
'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/header/branch',
(req, res, ctx) => {
return res.once(
ctx.status(200),
ctx.json({
limit: 1,
items: [header],
next: null,
}),
);
default:
return makeFetchResponse<IPagedResponse<IBlockHeader>>({
limit: 1,
items: [
{
nonce: '299775665679630368',
creationTime: 1617745627822054,
parent: 'XtgUmsnF20vMX4Dx9kN2W8cIXXiLNtDdFZLugMoDjrY',
adjacents: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'15': 'ZoBvuaVZWBOKLaDZfM51A9LaKb5B1f2fW83VLftQa3Y',
// eslint-disable-next-line @typescript-eslint/naming-convention
'10': 'oT8NLW-IZSziaOgI_1AfCdJ18u3epFpGONrkQ_F6w_Y',
// eslint-disable-next-line @typescript-eslint/naming-convention
'5': 'SDj4sXByWVqi9epbPAiz1zqhmBGJrzSY2bPk9-IMaA0',
},
target: '9sLMdbnd1x6vtRGpIw5tKMt_1hgprJS0oQkAAAAAAAA',
payloadHash: '2Skc1JkkBdLPkj5ZoV27nzhR3WjGD-tJiztCFGTaKIQ',
chainId: 0,
weight: 'iFU5b59ACHSOAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
height: 1511601,
chainwebVersion: 'mainnet01',
epochStart: 1617743254198411,
featureFlags: 0,
hash: 'BsxyrIDE0to4Kn9bjdgR_Q7Ha9bYkzd7Yso8r0zrdOc',
},
],
next: null,
} as unknown as IPagedResponse<IBlockHeader>);
}
};

mockedFunctionFetch.mockImplementation(
localMockFetch as jest.MockedFunction<typeof fetch>,
},
),
rest.post(
'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/payload/outputs/batch',
(req, res, ctx) => {
return res.once(ctx.status(200), ctx.json(blockByHeightPayloadsMock));
},
),
);

try {
const r = await chainweb.block.blockHash(
0,
Expand Down Expand Up @@ -154,6 +181,24 @@ describe('chainweb.block', () => {
/* By Range */

it('gets block by range and validates', async () => {
server.resetHandlers(
rest.get(
'https://api.chainweb.com/chainweb/0.0/mainnet01/cut',
(req, res, ctx) =>
res.once(ctx.status(200), ctx.json(blockByHeightCurrentCutMock)),
),
rest.post(
'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/header/branch',
(req, res, ctx) =>
res.once(ctx.status(200), ctx.json(rangeHeadersMock(20010))),
),
rest.post(
'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/payload/outputs/batch',
(req, res, ctx) =>
res.once(ctx.status(200), ctx.json(rangePayloadsMock(20010))),
),
);

const n = 10;
const r = await chainweb.block.range(
0,
Expand Down Expand Up @@ -186,6 +231,35 @@ describe('chainweb.block', () => {
it.each([10, 100, 359, 360, 730])(
'gets recent blocks with limit %p',
async (n) => {
server.resetHandlers(
rest.get(
'https://api.chainweb.com/chainweb/0.0/mainnet01/cut',
(req, res, ctx) =>
res(ctx.status(200), ctx.json(blockByHeightCurrentCutMock)),
),
rest.post(
'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/header/branch',
(req, res, ctx) => {
let m = n;
if (n === 730) {
const limit = req.url.searchParams.get('limit');
if (limit === '730') m = 730360;
if (limit === '370') m = 730720;
if (limit === '10') m = 730730;
}
return res(
ctx.status(200),
ctx.json(blockRecentsRecentHeadersMock(m)),
);
},
),
rest.post(
'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/payload/outputs/batch',
(req, res, ctx) =>
res.once(ctx.status(200), ctx.json(blockRecentsPayloadsMock(n))),
),
);

const cur = (await chainweb.cut.current(config.network, config.host))
.hashes[0].height;
const r = await chainweb.block.recent(
Expand All @@ -211,6 +285,27 @@ describe('chainweb.block', () => {
);

it('recgets recent blocks with low dept', async () => {
server.resetHandlers(
rest.get(
'https://api.chainweb.com/chainweb/0.0/mainnet01/cut',
(req, res, ctx) =>
res.once(ctx.status(200), ctx.json(blockByHeightCurrentCutMock)),
),
rest.post(
'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/header/branch',
(req, res, ctx) =>
res.once(
ctx.status(200),
ctx.json(blockRecentsRecentHeadersMock(10)),
),
),
rest.post(
'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/payload/outputs/batch',
(req, res, ctx) =>
res.once(ctx.status(200), ctx.json(blockRecentsPayloadsMock(10))),
),
);

const r = await chainweb.block.recent(
0,
0,
Expand All @@ -224,6 +319,24 @@ describe('chainweb.block', () => {
});

it('throws when payload is not in sync with headers', async () => {
server.resetHandlers(
rest.get(
'https://api.chainweb.com/chainweb/0.0/mainnet01/cut',
(req, res, ctx) =>
res.once(ctx.status(200), ctx.json(blockByHeightCurrentCutMock)),
),
rest.post(
'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/header/branch',
(req, res, ctx) =>
res.once(ctx.status(200), ctx.json(blockRecentsRecentHeadersMock(9))),
),
rest.post(
'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/0/payload/outputs/batch',
(req, res, ctx) =>
res.once(ctx.status(200), ctx.json(blockRecentsPayloadsMock(9))),
),
);

await expect(async () => {
const r = await chainweb.block.recent(
0,
Expand Down
Loading

0 comments on commit d3791a1

Please sign in to comment.