Skip to content

Commit a6e460f

Browse files
fix unit tests
1 parent a997c96 commit a6e460f

File tree

2 files changed

+93
-41
lines changed

2 files changed

+93
-41
lines changed

packages/sdk/tests/test-utils.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,37 @@ export const EMPTY_ORDER_BOOK: any = {
180180
};
181181

182182
export function resolveWithNoOrder() {
183-
return jest
184-
.fn<() => Promise<{ orders: []; count: 0 }>>()
185-
.mockResolvedValue(EMPTY_ORDER_BOOK);
183+
return EMPTY_ORDER_BOOK;
186184
}
187185

186+
export const mockWorkerpoolOrderbook = {
187+
orders: [
188+
{
189+
order: {
190+
workerpool: '0x0e7Bc972c99187c191A17f3CaE4A2711a4188c3F',
191+
workerpoolprice: 263157894,
192+
volume: 1000,
193+
tag: '0x0000000000000000000000000000000000000000000000000000000000000003',
194+
category: 0,
195+
trust: 0,
196+
apprestrict: '0x0000000000000000000000000000000000000000',
197+
datasetrestrict: '0x0000000000000000000000000000000000000000',
198+
requesterrestrict: '0xa1C2e8D384520c5D85Ab288598dC53a06db7dB5d',
199+
salt: '0xa6df3aca62cce93b407a5fe2b683e4fc4a5ff36d3e99731e642ad21f9b77e774',
200+
sign: '0xe2d0b978101b54e0bdce2fe08d44543114a01f994eff0f1ec8ec6ff4f0c5ccbf217271cde8b6d73019bec4486d1914a7087253f4bd3e583f1b60bab66f75de1a1c',
201+
},
202+
orderHash:
203+
'0x4dacfe7ed8883f9d3034d3367c7e6d8f5bc2f9434a58b2a60d480948e216f6d8',
204+
chainId: 134,
205+
publicationTimestamp: '2025-02-25T15:10:16.612Z',
206+
signer: '0x0c2e2F5c360cB58dC9A4813fA29656b56b546BF3',
207+
status: 'open',
208+
remaining: 828,
209+
},
210+
],
211+
count: 1,
212+
};
213+
188214
export function observableMockComplete() {
189215
const mockObservable: any = {
190216
subscribe: jest.fn(({ complete }) => {

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

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import {
1212
getRandomAddress,
1313
getRequiredFieldMessage,
14+
mockWorkerpoolOrderbook,
1415
resolveWithNoOrder,
1516
} from '../../../test-utils.js';
1617
import { resolveWithOneAppOrder } from '../../../utils/appOrders.js';
@@ -36,6 +37,16 @@ jest.unstable_mockModule(
3637
})
3738
);
3839

40+
jest.unstable_mockModule(
41+
'../../../../src/utils/processProtectedData.models.js',
42+
() => ({
43+
filterWorkerpoolOrders: jest.fn(
44+
() => mockWorkerpoolOrderbook.orders[0].order
45+
),
46+
checkUserVoucher: jest.fn(),
47+
})
48+
);
49+
3950
describe('processProtectedData', () => {
4051
let testedModule: any;
4152
let processProtectedData: ProcessProtectedData;
@@ -330,9 +341,13 @@ describe('processProtectedData', () => {
330341
.mockResolvedValue(getRandomAddress()),
331342
},
332343
orderbook: {
333-
fetchDatasetOrderbook: resolveWithNoOrder(),
344+
fetchDatasetOrderbook: jest
345+
.fn<() => Promise<{ orders: []; count: number }>>()
346+
.mockResolvedValue(resolveWithNoOrder()),
334347
fetchAppOrderbook: jest.fn(),
335-
fetchWorkerpoolOrderbook: jest.fn(),
348+
fetchWorkerpoolOrderbook: jest
349+
.fn<() => Promise<any>>()
350+
.mockResolvedValue(mockWorkerpoolOrderbook),
336351
},
337352
};
338353

@@ -365,8 +380,12 @@ describe('processProtectedData', () => {
365380
},
366381
orderbook: {
367382
fetchDatasetOrderbook: resolveWithOneDatasetOrder(), // <-- 1 dataset order
368-
fetchAppOrderbook: resolveWithNoOrder(), // <-- NO app order
369-
fetchWorkerpoolOrderbook: jest.fn(),
383+
fetchAppOrderbook: jest
384+
.fn<() => Promise<{ orders: []; count: 0 }>>()
385+
.mockResolvedValue(resolveWithNoOrder()), // <-- NO app order
386+
fetchWorkerpoolOrderbook: jest
387+
.fn<() => Promise<any>>()
388+
.mockResolvedValue(mockWorkerpoolOrderbook),
370389
},
371390
};
372391

@@ -388,40 +407,6 @@ describe('processProtectedData', () => {
388407
});
389408
});
390409

391-
describe('When there is NO workerpool orders', () => {
392-
it('should throw a WorkflowError with the correct message', async () => {
393-
// --- GIVEN
394-
const iexec = {
395-
wallet: {
396-
getAddress: jest
397-
.fn<() => Promise<Address>>()
398-
.mockResolvedValue(getRandomAddress()),
399-
},
400-
orderbook: {
401-
fetchDatasetOrderbook: resolveWithOneDatasetOrder(), // <-- 1 dataset order
402-
fetchAppOrderbook: resolveWithOneAppOrder(), // <-- 1 app order
403-
fetchWorkerpoolOrderbook: resolveWithNoOrder(), // <-- NO workerpool order
404-
},
405-
};
406-
407-
await expect(
408-
// --- WHEN
409-
processProtectedData({
410-
// @ts-expect-error Minimal iexec implementation with only what's necessary for this test
411-
iexec,
412-
protectedData: getRandomAddress(),
413-
app: getRandomAddress(),
414-
})
415-
// --- THEN
416-
).rejects.toThrow(
417-
new WorkflowError({
418-
message: processProtectedDataErrorMessage,
419-
errorCause: Error('No workerpool orders found'),
420-
})
421-
);
422-
});
423-
});
424-
425410
describe('When workerpool is explicitly set to the Zero address', () => {
426411
it("should call fetchWorkerpoolOrderbook with workerpool parameter equals to 'any'", async () => {
427412
// --- GIVEN
@@ -536,4 +521,45 @@ describe('processProtectedData', () => {
536521
);
537522
});
538523
});
524+
525+
describe('When there is NO workerpool orders', () => {
526+
it('should throw a WorkflowError with the correct message', async () => {
527+
const { filterWorkerpoolOrders } = await import(
528+
'../../../../src/utils/processProtectedData.models.js'
529+
);
530+
531+
(filterWorkerpoolOrders as jest.Mock).mockReturnValue(null);
532+
// --- GIVEN
533+
const iexec = {
534+
wallet: {
535+
getAddress: jest
536+
.fn<() => Promise<Address>>()
537+
.mockResolvedValue(getRandomAddress()),
538+
},
539+
orderbook: {
540+
fetchDatasetOrderbook: resolveWithOneDatasetOrder(), // <-- 1 dataset order
541+
fetchAppOrderbook: resolveWithOneAppOrder(), // <-- 1 app order
542+
fetchWorkerpoolOrderbook: jest
543+
.fn<() => Promise<{ orders: []; count: 0 }>>()
544+
.mockResolvedValue(resolveWithNoOrder()), // <-- NO workerpool order
545+
},
546+
};
547+
548+
await expect(
549+
// --- WHEN
550+
processProtectedData({
551+
// @ts-expect-error Minimal iexec implementation with only what's necessary for this test
552+
iexec,
553+
protectedData: getRandomAddress(),
554+
app: getRandomAddress(),
555+
})
556+
// --- THEN
557+
).rejects.toThrow(
558+
new WorkflowError({
559+
message: processProtectedDataErrorMessage,
560+
errorCause: Error('No Workerpool order found.'),
561+
})
562+
);
563+
});
564+
});
539565
});

0 commit comments

Comments
 (0)