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']); });