Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
"lint-staged": "^13.2.2",
"c8": "^8.0.0",
"typedoc": "^0.24.8",
"dotenv": "^16.1.4"
"dotenv": "^16.1.4",
"vitest": "^0.32.2"
},
"dependencies": {
"libp2p": "^0.45.9",
Expand All @@ -116,7 +117,7 @@
"scripts": {
"clean": "rm -rf ./lib",
"build": "npm run clean && tsc -p ./tsconfig.build.json",
"test": "mocha -t 60000 --extension spec.ts",
"test": "vitest --run test",
"semantic-release": "semantic-release",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix && prettier --write .",
Expand Down
1 change: 1 addition & 0 deletions test/client.requestManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { nowSec } from '../src/utils/time.js';
import { randomSalt } from '@windingtree/contracts';
import { memoryStorage } from '../src/storage/index.js';
import { ClientRequestsManager } from '../src/client/requestsManager.js';
import { describe, it, beforeEach } from 'vitest';

const expTime = (sec: number): bigint => BigInt(nowSec() + sec);

Expand Down
3 changes: 2 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { expect } from './setup.js';
import { beforeAll, describe, it } from 'vitest';
import { createLogger } from '../src/utils/logger.js';

const logger = createLogger('test');

describe('SDK', () => {
before(() => {
beforeAll(() => {
logger.trace('before');
});

Expand Down
76 changes: 39 additions & 37 deletions test/node.requestManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import './setup.js';
import { expect } from 'chai';
import { stringify } from 'viem';
import { NodeRequestManager } from '../src/index.js';
import { describe, it, beforeEach } from 'vitest';

describe('Node.NodeRequestManager', () => {
const defaultNoncePeriod = 1;
Expand Down Expand Up @@ -109,43 +110,44 @@ describe('Node.NodeRequestManager', () => {
expect(nodeRequestManager['cache'].get('1')?.data.nonce).to.equal(2);
});

it('should not add request if data is not valid JSON', (done) => {
const requestTopic = 'testTopic';
const data = 'invalid JSON';
const sizeBefore = nodeRequestManager['cache'].size;

nodeRequestManager.addEventListener(
'error',
() => {
expect(nodeRequestManager['cache'].size).to.eq(sizeBefore);
done();
},
{ once: true },
);

nodeRequestManager.add(requestTopic, data);
});

it('should emit request event after nonce period', (done) => {
const requestTopic = 'testTopic';
const data = stringify({
id: '1',
nonce: 1,
expire: Math.floor(Date.now() / 1000) + 20,
});

nodeRequestManager.addEventListener(
'request',
(event) => {
expect(event.detail.topic).to.equal(requestTopic);
expect(event.detail.data).to.deep.equal(JSON.parse(data));
done();
},
{ once: true },
);

nodeRequestManager.add(requestTopic, data);
});
it('should not add request if data is not valid JSON', () =>
new Promise((done) => {
const requestTopic = 'testTopic';
const data = 'invalid JSON';
const sizeBefore = nodeRequestManager['cache'].size;
nodeRequestManager.addEventListener(
'error',
() => {
expect(nodeRequestManager['cache'].size).to.eq(sizeBefore);
done(true);
},
{ once: true },
);

nodeRequestManager.add(requestTopic, data);
}));

it('should emit request event after nonce period', () =>
new Promise((done) => {
const requestTopic = 'testTopic';
const data = stringify({
id: '1',
nonce: 1,
expire: Math.floor(Date.now() / 1000) + 20,
});

nodeRequestManager.addEventListener(
'request',
(event) => {
expect(event.detail.topic).to.equal(requestTopic);
expect(event.detail.data).to.deep.equal(JSON.parse(data));
done(true);
},
{ once: true },
);

nodeRequestManager.add(requestTopic, data);
}));
});

describe('#prune', () => {
Expand Down
9 changes: 3 additions & 6 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import 'mocha';
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';

import {
GenericQuery,
GenericOfferOptions,
Expand All @@ -12,8 +10,7 @@ import {
import { Hash, HDAccount, TypedDataDomain } from 'viem';
import { buildRequest, buildOffer } from '../src/shared/messages.js';
import { randomSalt } from '@windingtree/contracts';

chai.use(chaiAsPromised);
import { expect } from 'vitest';

process.on('unhandledRejection', (error) => {
console.log('Unhandled rejection detected:', error);
Expand Down Expand Up @@ -49,7 +46,7 @@ export interface CustomOfferOptions extends GenericOfferOptions {
checkOut: bigint;
}

export const createRequest = (
export const createRequest = async (
topic: string,
expire: bigint | string = BigInt(1),
) =>
Expand Down
24 changes: 13 additions & 11 deletions test/shared.messages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { supplierId as spId } from '../src/utils/uid.js';
import { randomSalt } from '@windingtree/contracts';
import { RequestData, OfferData } from '../src/shared/types.js';
import { buildOffer, verifyOffer } from '../src/shared/messages.js';
import { describe, it, beforeAll } from 'vitest';

describe('Shared.messages', () => {
const topic = 'test';
Expand All @@ -26,14 +27,14 @@ describe('Shared.messages', () => {

let request: RequestData<CustomQuery>;

before(async () => {
beforeAll(async () => {
request = await createRequest(topic);
});

describe('#buildRequest', () => {
it('should build a request', async () => {
await expect(createRequest(topic)).to.not.rejected;
await expect(createRequest(topic, '1h')).to.not.rejected;
await expect(createRequest(topic)).resolves.toBeTypeOf('object');
await expect(createRequest(topic, '1h')).resolves.toBeTypeOf('object');
});
});

Expand All @@ -46,15 +47,16 @@ describe('Shared.messages', () => {
}
await expect(
createOffer(request, BigInt(1), typedDomain, supplierId, signer),
).to.not.rejected;
await expect(createOffer(request, '30s', typedDomain, supplierId, signer))
.to.not.rejected;
).resolves.toBeTypeOf('object');
await expect(
createOffer(request, '30s', typedDomain, supplierId, signer),
).resolves.toBeTypeOf('object');
});

describe('Offer restoration', () => {
let offer: OfferData<CustomQuery, CustomOfferOptions>;

before(async () => {
beforeAll(async () => {
offer = await createOffer(
request,
BigInt(1),
Expand Down Expand Up @@ -98,7 +100,7 @@ describe('Shared.messages', () => {
transferable: offer.payload.transferable,
idOverride: offer.id,
}),
).to.rejectedWith(
).rejects.toThrow(
'Either account or signatureOverride must be provided with options',
);
});
Expand All @@ -108,7 +110,7 @@ describe('Shared.messages', () => {
describe('#verifyOffer', () => {
let offer: OfferData<CustomQuery, CustomOfferOptions>;

before(async () => {
beforeAll(async () => {
offer = await createOffer(
request,
BigInt(1),
Expand All @@ -126,7 +128,7 @@ describe('Shared.messages', () => {
address: unknownSigner.address,
offer,
}),
).to.rejectedWith(`Invalid offer signer ${unknownSigner.address}`);
).rejects.toThrow(`Invalid offer signer ${unknownSigner.address}`);
});

it('should verify an offer', async () => {
Expand All @@ -136,7 +138,7 @@ describe('Shared.messages', () => {
address: signer.address,
offer,
}),
).to.not.rejected;
).resolves.toBeUndefined();
});
});
});
Loading