perf: bound order and product image reads

This commit is contained in:
fatequod
2026-08-27 11:01:20 +02:00
parent faf1c3ea06
commit ec214cb62b
3 changed files with 79 additions and 6 deletions
+52
View File
@@ -0,0 +1,52 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const Module = require('node:module');
const path = require('node:path');
function loadMethod({ onFetch }) {
const methodPath = path.resolve(__dirname, '../methods/product/get_images.js');
const originalLoad = Module._load;
Module._load = function(request, parent, isMain) {
if (request === '../../src/PrestaApi') {
return class PrestashopApi {
async getProductCoverImageUrl(id) { return onFetch(id); }
};
}
if (request === '../../src/utils') return { isLegacyApi: () => false };
if (request === '../../src/module-config') {
return { loadConfig: async () => ({ apiUrl: 'https://shop.test', apiKey: 'key' }), isConfigured: () => true };
}
if (request === '../../src/register-permissions') {
return { PERMISSIONS: { ORDERS_VIEW: 'orders.view' }, requirePermission() {} };
}
return originalLoad.call(this, request, parent, isMain);
};
delete require.cache[methodPath];
try {
return require(methodPath);
} finally {
Module._load = originalLoad;
}
}
test('product image lookup deduplicates IDs and bounds external concurrency', async () => {
let active = 0;
let maxActive = 0;
const calls = [];
const method = loadMethod({
onFetch: async id => {
calls.push(id);
active += 1;
maxActive = Math.max(maxActive, active);
await new Promise(resolve => setImmediate(resolve));
active -= 1;
return `https://img.test/${id}.jpg`;
}
});
const result = await method({ productIds: ['1', '1', '2', '3', '4', '5', '6'] }, {});
assert.deepEqual(calls.sort(), ['1', '2', '3', '4', '5', '6']);
assert.equal(maxActive, 5);
assert.equal(result['6'], 'https://img.test/6.jpg');
});