126 lines
4.9 KiB
JavaScript
126 lines
4.9 KiB
JavaScript
const assert = require('node:assert/strict');
|
|
const Module = require('node:module');
|
|
const path = require('node:path');
|
|
const test = require('node:test');
|
|
|
|
const registerConfigPath = path.resolve(__dirname, '../src/register-config.js');
|
|
|
|
function loadConfigModule({ states, cachedStates = [] }) {
|
|
let blocks;
|
|
let savedStates;
|
|
const originalLoad = Module._load;
|
|
Module._load = function(request, parent, isMain) {
|
|
if (request === '@orbito/api-lib') {
|
|
return {
|
|
Config: {
|
|
PropertyType: { INFO: 'info', STRING: 'string' },
|
|
DisplayFormat: { URL: 'url', SECRET: 'secret' },
|
|
registerConfigBlocks: async value => { blocks = value; },
|
|
get: async (key, fallback) => ({
|
|
'connection.apiUrl': 'https://shop.test',
|
|
'connection.apiKey': 'secret',
|
|
'connection.versionCode': '8',
|
|
}[key] ?? fallback),
|
|
},
|
|
Log: { info() {}, warn() {} },
|
|
};
|
|
}
|
|
if (request === './PrestaApi') {
|
|
return class PrestashopApi {
|
|
async getStatuses() { return states; }
|
|
};
|
|
}
|
|
if (request === './utils') return { isLegacyApi: () => false };
|
|
if (request === './module-config') {
|
|
return {
|
|
setPsStates: async value => { savedStates = value; },
|
|
loadPsStates: async () => cachedStates,
|
|
};
|
|
}
|
|
if (request === './shop-icon') return { ensureShopIcon: async () => {} };
|
|
return originalLoad.call(this, request, parent, isMain);
|
|
};
|
|
|
|
delete require.cache[registerConfigPath];
|
|
let loaded;
|
|
try {
|
|
loaded = require(registerConfigPath);
|
|
} finally {
|
|
Module._load = originalLoad;
|
|
}
|
|
return {
|
|
...loaded,
|
|
register: async () => {
|
|
await loaded.registerPrestashopConfigBlocks();
|
|
return blocks;
|
|
},
|
|
getSavedStates: () => savedStates,
|
|
};
|
|
}
|
|
|
|
test('opening the mapping block fetches and persists current PrestaShop state names', async () => {
|
|
const module = loadConfigModule({
|
|
cachedStates: [{ id: 2, name: 'Stara nazwa', color: '#000000' }],
|
|
states: [{ id: '2', name: 'Nowa nazwa', color: '#00ff00', deleted: '0' }],
|
|
});
|
|
const blocks = await module.register();
|
|
|
|
const schema = await blocks.mapping.schema();
|
|
|
|
assert.equal(schema.state_2.name.pl, 'Nowa nazwa (PS #2)');
|
|
assert.deepEqual(module.getSavedStates(), [{ id: 2, name: 'Nowa nazwa', color: '#00ff00' }]);
|
|
});
|
|
|
|
test('mapping validation prevents two PrestaShop states from sharing one Orbito status', async () => {
|
|
const module = loadConfigModule({ states: [] });
|
|
const statusId = '507f191e810c19729de860ea';
|
|
const errors = [];
|
|
|
|
module.validateOutboundMapping({
|
|
state_2: statusId,
|
|
state_3: statusId,
|
|
state_4: '507f191e810c19729de860eb',
|
|
}, {
|
|
addError: (field, message) => errors.push({ field, message }),
|
|
});
|
|
|
|
assert.deepEqual(errors.map(error => error.field), ['state_2', 'state_3']);
|
|
});
|
|
|
|
test('connection validates the shop time zone and defaults to Europe/Warsaw', async () => {
|
|
const module = loadConfigModule({ states: [{ id: '2', name: 'New' }] });
|
|
const blocks = await module.register();
|
|
assert.equal(blocks.connection.schema.timeZone.default, 'Europe/Warsaw');
|
|
const errors = [];
|
|
const report = { addError: (field, message) => errors.push({ field, message }) };
|
|
const connection = { apiUrl: 'https://shop.test', apiKey: 'secret' };
|
|
await blocks.connection.validate({ ...connection, timeZone: 'Not/AZone' }, report);
|
|
assert.deepEqual(errors.map(error => error.field), ['timeZone']);
|
|
errors.length = 0;
|
|
for (const timeZone of ['', 'Europe/Warsaw', 'UTC', 'Europe/London']) {
|
|
await blocks.connection.validate({ ...connection, timeZone }, report);
|
|
}
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
|
|
test('config loading applies Warsaw to existing installations and passes a saved shop zone to jobs', async () => {
|
|
let savedTimeZone;
|
|
const originalLoad = Module._load;
|
|
Module._load = function(request, parent, isMain) {
|
|
if (request === '@orbito/api-lib') return { Config: {
|
|
get: async (key, fallback) => key === 'connection.timeZone' ? savedTimeZone ?? fallback : fallback,
|
|
} };
|
|
return originalLoad.call(this, request, parent, isMain);
|
|
};
|
|
let loadConfig;
|
|
try {
|
|
delete require.cache[require.resolve('../src/module-config')];
|
|
({ loadConfig } = require('../src/module-config'));
|
|
} finally { Module._load = originalLoad; }
|
|
assert.equal((await loadConfig()).timeZone, 'Europe/Warsaw');
|
|
savedTimeZone = ' ';
|
|
assert.equal((await loadConfig()).timeZone, 'Europe/Warsaw');
|
|
savedTimeZone = ' Europe/London ';
|
|
assert.equal((await loadConfig()).timeZone, 'Europe/London');
|
|
});
|