Skip to content

Commit 7a739dc

Browse files
committed
test: 💍 Added tests for DealsDb class
1 parent 7a45a74 commit 7a739dc

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

test/node.db.deals.spec.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
import {
3+
describe,
4+
expect,
5+
it,
6+
beforeAll,
7+
createRequest,
8+
createOffer,
9+
} from './setup.js';
10+
import { HDAccount, Hash } from 'viem';
11+
import { mnemonicToAccount } from 'viem/accounts';
12+
import { randomSalt } from '@windingtree/contracts';
13+
import { supplierId as spId } from '../src/utils/uid.js';
14+
import { generateMnemonic } from '../src/utils/wallet.js';
15+
import { Storage } from '../src/storage/abstract.js';
16+
import { MemoryStorage, createInitializer } from '../src/storage/memory.js';
17+
import { DealRecord } from '../src/shared/types.js';
18+
import { DealsDb, DealsDbOptions } from '../src/node/db/deals.js';
19+
import { DealStatus } from '../src/index.js';
20+
21+
const buildRandomDeal = async (
22+
signer: HDAccount,
23+
supplierId: Hash,
24+
): Promise<DealRecord> => {
25+
const typedDomain = {
26+
chainId: 1,
27+
name: 'Test',
28+
version: '1',
29+
contract: signer.address,
30+
};
31+
const request = await createRequest('test', '100s');
32+
const offer = await createOffer(
33+
request,
34+
'200s',
35+
typedDomain,
36+
supplierId,
37+
signer,
38+
);
39+
40+
return {
41+
chainId: typedDomain.chainId,
42+
created: BigInt(Math.round(Date.now() / 1000)),
43+
offer,
44+
retailerId: 'test',
45+
buyer: '0x0',
46+
price: BigInt(1),
47+
asset: '0x0',
48+
status: DealStatus.Claimed,
49+
};
50+
};
51+
52+
describe('DealsDb', () => {
53+
const signer = mnemonicToAccount(generateMnemonic());
54+
const supplierId = spId(randomSalt(), signer.address);
55+
let dealsDbOptions: DealsDbOptions;
56+
let storage: Storage;
57+
let dealsDb: DealsDb;
58+
59+
beforeAll(async () => {
60+
storage = await createInitializer({
61+
scope: 'deals',
62+
})();
63+
dealsDbOptions = {
64+
storage,
65+
prefix: 'test',
66+
};
67+
dealsDb = new DealsDb(dealsDbOptions);
68+
});
69+
70+
describe('#constructor', () => {
71+
it('should correctly initialize with valid parameters', () => {
72+
expect(dealsDb).toBeDefined();
73+
expect(dealsDb.prefix).toEqual(`${dealsDbOptions.prefix}_api_deals_`);
74+
expect(dealsDb.storage).toEqual(storage);
75+
});
76+
});
77+
78+
describe('#set', () => {
79+
it('set deal record to the storage', async () => {
80+
const deal = await buildRandomDeal(signer, supplierId);
81+
await dealsDb.set(deal);
82+
const result = await dealsDb.storage.get<DealRecord>(
83+
`test_api_deals_${deal.offer.id}`,
84+
);
85+
expect(result).toEqual(deal);
86+
});
87+
});
88+
89+
describe('#get', () => {
90+
it('get deal record from the storage', async () => {
91+
const deal = await buildRandomDeal(signer, supplierId);
92+
await dealsDb.set(deal);
93+
const result = await dealsDb.get(deal.offer.id);
94+
expect(result).toStrictEqual(deal);
95+
});
96+
97+
it('should throw an error when the deal is not found', async () => {
98+
const unknownId = '0xNonexistentId';
99+
await expect(dealsDb.get(unknownId)).rejects.toThrow(
100+
`Deal ${unknownId} not found`,
101+
);
102+
});
103+
});
104+
105+
describe('#getAll', () => {
106+
let records: DealRecord[];
107+
108+
beforeAll(async () => {
109+
await (dealsDb.storage as MemoryStorage).reset();
110+
records = await Promise.all(
111+
Array(10)
112+
.fill('')
113+
.map(async (_) => buildRandomDeal(signer, supplierId)),
114+
);
115+
await Promise.all(records.map((r) => dealsDb.set(r)));
116+
});
117+
118+
it('get all deal records from the storage', async () => {
119+
const pagination = { start: 0, skip: 10 };
120+
const deals = await dealsDb.getAll(pagination);
121+
expect(deals.length).toEqual(pagination.skip);
122+
deals.forEach((d) => {
123+
const record = records.find((r) => r.offer.id === d.offer.id);
124+
expect(record).to.be.deep.eq(d);
125+
});
126+
});
127+
128+
it('should return an empty array when no deals are found', async () => {
129+
const deals = await dealsDb.getAll({ start: 100, skip: 10 });
130+
expect(deals.length).toBe(0);
131+
});
132+
133+
it('should return all remaining deals when the skip count exceeds available deals', async () => {
134+
const deals = await dealsDb.getAll({ start: 5, skip: 10 });
135+
expect(deals.length).toBe(5);
136+
});
137+
});
138+
});

0 commit comments

Comments
 (0)