Skip to content

Commit 26ce04f

Browse files
adjust unit tests
1 parent 4d6d258 commit 26ce04f

File tree

2 files changed

+107
-28
lines changed

2 files changed

+107
-28
lines changed

packages/sdk/tests/test-utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,32 @@ export const mockWorkerpoolOrderbook = {
211211
count: 1,
212212
};
213213

214+
export const mockAppOrderbook = {
215+
orders: [
216+
{
217+
order: {
218+
app: '0xc8c5E295D2BedA01D1fB8DD4d85A1Cb769185a34',
219+
appprice: 0,
220+
volume: 10000000,
221+
tag: '0x0000000000000000000000000000000000000000000000000000000000000003',
222+
datasetrestrict: '0x0000000000000000000000000000000000000000',
223+
workerpoolrestrict: '0x0000000000000000000000000000000000000000',
224+
requesterrestrict: '0x0000000000000000000000000000000000000000',
225+
salt: '0x82107d3b5694d3ab4cd4e5f2057e1bdeb7da359518ccfb15638405c619fa12b0',
226+
sign: '0x0112d6f1b53777a001054daf62f542a5f94679f88885515a2126a6794505d6993e425c3a432b4c2cdbf004f6f0c8c9908493135dedb829f6c958e67daa068dd61c',
227+
},
228+
orderHash:
229+
'0x64208bc3580bbee092c4a4efb26629cf885a2f1e99b6b4d9bd809ea85b58332f',
230+
chainId: 134,
231+
publicationTimestamp: '2025-02-05T14:35:51.271Z',
232+
signer: '0x9cfFa14604A6836E9d6fBAcCc624cfE0bE3Be5B4',
233+
status: 'open',
234+
remaining: 9999961,
235+
},
236+
],
237+
count: 1,
238+
};
239+
214240
export function observableMockComplete() {
215241
const mockObservable: any = {
216242
subscribe: jest.fn(({ complete }) => {

packages/sdk/tests/unit/dataProtectorCore/processProtectedData/processProtectedData.test.ts

Lines changed: 81 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import {
1212
getRandomAddress,
1313
getRequiredFieldMessage,
14+
mockAppOrderbook,
1415
mockWorkerpoolOrderbook,
1516
resolveWithNoOrder,
1617
} from '../../../test-utils.js';
@@ -40,7 +41,7 @@ jest.unstable_mockModule(
4041
jest.unstable_mockModule(
4142
'../../../../src/utils/processProtectedData.models.js',
4243
() => ({
43-
findWorkerpoolOrders: jest.fn(
44+
filterWorkerpoolOrders: jest.fn(
4445
() => mockWorkerpoolOrderbook.orders[0].order
4546
),
4647
checkUserVoucher: jest.fn(),
@@ -184,10 +185,10 @@ describe('processProtectedData', () => {
184185
});
185186
});
186187

187-
describe('When maxPrice is not a positive number', () => {
188+
describe('When DatamaxPrice is not a positive number', () => {
188189
it('should throw a yup ValidationError with the correct message', async () => {
189190
// --- GIVEN
190-
const invalidMaxPrice = -1;
191+
const invalidDataMaxPrice = -1;
191192

192193
await expect(
193194
// --- WHEN
@@ -196,11 +197,55 @@ describe('processProtectedData', () => {
196197
iexec: {},
197198
protectedData: getRandomAddress(),
198199
app: getRandomAddress(),
199-
maxPrice: invalidMaxPrice,
200+
dataMaxPrice: invalidDataMaxPrice,
200201
})
201202
// --- THEN
202203
).rejects.toThrow(
203-
new ValidationError('maxPrice must be greater than or equal to 0')
204+
new ValidationError('dataMaxPrice must be greater than or equal to 0')
205+
);
206+
});
207+
});
208+
209+
describe('When WorkerpoolmaxPrice is not a positive number', () => {
210+
it('should throw a yup ValidationError with the correct message', async () => {
211+
// --- GIVEN
212+
const invalidWorkerpoolMaxPrice = -1;
213+
214+
await expect(
215+
// --- WHEN
216+
processProtectedData({
217+
// @ts-expect-error No need for iexec here
218+
iexec: {},
219+
protectedData: getRandomAddress(),
220+
app: getRandomAddress(),
221+
workerpoolMaxPrice: invalidWorkerpoolMaxPrice,
222+
})
223+
// --- THEN
224+
).rejects.toThrow(
225+
new ValidationError(
226+
'workerpoolMaxPrice must be greater than or equal to 0'
227+
)
228+
);
229+
});
230+
});
231+
232+
describe('When appMaxPrice is not a positive number', () => {
233+
it('should throw a yup ValidationError with the correct message', async () => {
234+
// --- GIVEN
235+
const invalidAppMaxPrice = -1;
236+
237+
await expect(
238+
// --- WHEN
239+
processProtectedData({
240+
// @ts-expect-error No need for iexec here
241+
iexec: {},
242+
protectedData: getRandomAddress(),
243+
app: getRandomAddress(),
244+
appMaxPrice: invalidAppMaxPrice,
245+
})
246+
// --- THEN
247+
).rejects.toThrow(
248+
new ValidationError('appMaxPrice must be greater than or equal to 0')
204249
);
205250
});
206251
});
@@ -344,7 +389,9 @@ describe('processProtectedData', () => {
344389
fetchDatasetOrderbook: jest
345390
.fn<() => Promise<{ orders: []; count: number }>>()
346391
.mockResolvedValue(resolveWithNoOrder()),
347-
fetchAppOrderbook: jest.fn(),
392+
fetchAppOrderbook: jest
393+
.fn<() => Promise<any>>()
394+
.mockResolvedValue(mockAppOrderbook),
348395
fetchWorkerpoolOrderbook: jest
349396
.fn<() => Promise<any>>()
350397
.mockResolvedValue(mockWorkerpoolOrderbook),
@@ -363,7 +410,7 @@ describe('processProtectedData', () => {
363410
).rejects.toThrow(
364411
new WorkflowError({
365412
message: processProtectedDataErrorMessage,
366-
errorCause: Error('No dataset orders found'),
413+
errorCause: Error('No Dataset order found for the desired price'),
367414
})
368415
);
369416
});
@@ -401,7 +448,7 @@ describe('processProtectedData', () => {
401448
).rejects.toThrow(
402449
new WorkflowError({
403450
message: processProtectedDataErrorMessage,
404-
errorCause: Error('No app orders found'),
451+
errorCause: Error('No App order found for the desired price'),
405452
})
406453
);
407454
});
@@ -425,16 +472,17 @@ describe('processProtectedData', () => {
425472
});
426473

427474
// --- THEN
428-
expect(fetchWorkerpoolOrderbookMock).toHaveBeenCalledWith({
429-
workerpool: 'any', // <-- What we want to test
430-
app: expect.any(String),
431-
dataset: expect.any(String),
432-
requester: expect.any(String),
433-
category: 0,
434-
minTag: SCONE_TAG,
435-
maxTag: SCONE_TAG,
436-
isRequesterStrict: expect.any(Boolean),
437-
});
475+
expect(fetchWorkerpoolOrderbookMock).toHaveBeenCalledWith(
476+
expect.objectContaining({
477+
workerpool: 'any', // <-- the core of what you're testing
478+
dataset: expect.any(String),
479+
requester: expect.any(String),
480+
category: 0,
481+
minTag: SCONE_TAG,
482+
maxTag: SCONE_TAG,
483+
isRequesterStrict: expect.any(Boolean),
484+
})
485+
);
438486
});
439487
});
440488

@@ -511,24 +559,29 @@ describe('processProtectedData', () => {
511559
});
512560

513561
// --- THEN
514-
expect(fetchDatasetOrderbookMock).toHaveBeenCalledWith(
515-
expect.any(String),
516-
{
517-
app: expect.any(String),
518-
workerpool: expect.any(String),
519-
requester: validWhitelistAddress.toLowerCase(), // <-- whitelist address instead of user wallet address
520-
}
562+
const calls = fetchDatasetOrderbookMock.mock.calls;
563+
564+
expect(calls).toEqual(
565+
expect.arrayContaining([
566+
[
567+
expect.any(String),
568+
expect.objectContaining({
569+
app: expect.any(String),
570+
requester: validWhitelistAddress.toLowerCase(),
571+
}),
572+
],
573+
])
521574
);
522575
});
523576
});
524577

525578
describe('When there is NO workerpool orders', () => {
526579
it('should throw a WorkflowError with the correct message', async () => {
527-
const { findWorkerpoolOrders } = await import(
580+
const { filterWorkerpoolOrders } = await import(
528581
'../../../../src/utils/processProtectedData.models.js'
529582
);
530583

531-
(findWorkerpoolOrders as jest.Mock).mockReturnValue(null);
584+
(filterWorkerpoolOrders as jest.Mock).mockReturnValue(null);
532585
// --- GIVEN
533586
const iexec = {
534587
wallet: {
@@ -557,7 +610,7 @@ describe('processProtectedData', () => {
557610
).rejects.toThrow(
558611
new WorkflowError({
559612
message: processProtectedDataErrorMessage,
560-
errorCause: Error('No Workerpool order found.'),
613+
errorCause: Error('No Workerpool order found for the desired price'),
561614
})
562615
);
563616
});

0 commit comments

Comments
 (0)