Lock TTL is now passed in seconds, matching what the core lock RPC methods expect and what the 3.x SDK documents as ttlSeconds. The presta-api timeout test no longer drains the event loop: it drives the unref-ed timeout timer through node:test mock timers instead of real ones. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LpoFvfAbnMmkSJFRc867XA
83 lines
3.4 KiB
JavaScript
83 lines
3.4 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const Module = require('node:module');
|
|
const Core = { fetch: null };
|
|
const originalLoad = Module._load;
|
|
Module._load = function(request, parent, isMain) {
|
|
if (request === '@orbito/api-lib') return { Core, Log: { warn() {} } };
|
|
return originalLoad.call(this, request, parent, isMain);
|
|
};
|
|
const PrestashopApi = require('../src/PrestaApi');
|
|
Module._load = originalLoad;
|
|
|
|
function response(status, body) {
|
|
return { status, text: () => typeof body === 'string' ? body : JSON.stringify(body) };
|
|
}
|
|
|
|
test('rejects HTTP errors and retries retryable GET responses', async t => {
|
|
const originalFetch = Core.fetch;
|
|
t.after(() => { Core.fetch = originalFetch; });
|
|
let calls = 0;
|
|
Core.fetch = async () => {
|
|
calls += 1;
|
|
return calls < 3 ? response(503, { error: 'busy' }) : response(200, { orders: [] });
|
|
};
|
|
const api = new PrestashopApi('https://shop.example', 'secret', true, {
|
|
maxGetAttempts: 3, retryBaseMs: 1, timeoutMs: 100,
|
|
});
|
|
|
|
assert.deepEqual(await api.makeRequest('orders', 'GET'), { orders: [] });
|
|
assert.equal(calls, 3);
|
|
});
|
|
|
|
test('does not retry mutating requests', async t => {
|
|
const originalFetch = Core.fetch;
|
|
t.after(() => { Core.fetch = originalFetch; });
|
|
let calls = 0;
|
|
Core.fetch = async () => { calls += 1; return response(500, { error: 'failed' }); };
|
|
const api = new PrestashopApi('https://shop.example', 'secret', true, { retryBaseMs: 1 });
|
|
|
|
await assert.rejects(() => api.makeRequest('orders/1', 'PATCH', { order: { id: 1 } }), /HTTP 500/);
|
|
assert.equal(calls, 1);
|
|
});
|
|
|
|
test('rejects non-retryable 4xx and invalid JSON', async t => {
|
|
const originalFetch = Core.fetch;
|
|
t.after(() => { Core.fetch = originalFetch; });
|
|
const api = new PrestashopApi('https://shop.example', 'secret', true, { retryBaseMs: 1 });
|
|
|
|
Core.fetch = async () => response(401, { error: 'unauthorized' });
|
|
await assert.rejects(() => api.makeRequest('orders', 'GET'), /HTTP 401/);
|
|
|
|
Core.fetch = async () => response(200, '<html>not json</html>');
|
|
await assert.rejects(() => api.makeRequest('orders', 'GET'), /invalid JSON/);
|
|
});
|
|
|
|
test('times out stalled requests', async t => {
|
|
const originalFetch = Core.fetch;
|
|
t.after(() => { Core.fetch = originalFetch; });
|
|
// Timer timeoutu w fetchWithTimeout jest `unref`-owany, a Core.fetch nigdy się tu nie rozwiązuje,
|
|
// więc na prawdziwych timerach pętla zdarzeń pustoszeje i node:test anuluje ten test.
|
|
// Mockowany setTimeout pozwala wysterować timeout ręcznie przez tick().
|
|
t.mock.timers.enable({ apis: ['setTimeout'] });
|
|
Core.fetch = async () => new Promise(() => {});
|
|
const api = new PrestashopApi('https://shop.example', 'secret', true, {
|
|
timeoutMs: 5, maxGetAttempts: 1,
|
|
});
|
|
|
|
const pending = api.makeRequest('orders', 'GET');
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
t.mock.timers.tick(10);
|
|
|
|
await assert.rejects(() => pending, /timed out/);
|
|
});
|
|
|
|
test('preserves the explicitly allowed product partial-success compatibility path', async t => {
|
|
const originalFetch = Core.fetch;
|
|
t.after(() => { Core.fetch = originalFetch; });
|
|
Core.fetch = async () => response(500, { product: { id: 1 } });
|
|
const api = new PrestashopApi('https://shop.example', 'secret', true, { maxGetAttempts: 1 });
|
|
|
|
assert.deepEqual(await api.makeRequest('products/1', 'GET', null, true), { product: { id: 1 } });
|
|
});
|