const { withHookFixtures } = require('./helpers/hook-fixtures'); const test = require('node:test'); const assert = require('node:assert/strict'); const { STATUS_SYNC_LOCK_TTL_SECONDS, registerStatusSyncHook, syncOrderStatusToPrestashop, } = require('../src/status-sync'); const ORDER_ID = '507f191e810c19729de860e1'; const OLD_STATUS = '507f191e810c19729de860e2'; const CURRENT_STATUS = '507f191e810c19729de860e3'; function dependencies(overrides = {}) { const calls = { setOrderState: [], released: [], locks: [] }; const Core = withHookFixtures({ getLock: async (name, ttl) => { calls.locks.push([name, ttl]); return 'status-lock'; }, releaseLock: async id => { calls.released.push(id); }, getModuleInfo: () => ({ code: 'core/prestashop' }), getInstanceId: () => 'aaaaaaaaaaaaaaaaaaaaaaaa', callModule: async () => ({ data: [{ _id: ORDER_ID, statusId: CURRENT_STATUS, source: { moduleCode: 'core/prestashop', moduleId: 'aaaaaaaaaaaaaaaaaaaaaaaa', sourceId: '21', }, }] }), registerHook: async () => true, }); const deps = { Core, Log: { info() {}, warn() {}, error() {} }, PsOrder: { findOne: async () => ({ psOrderId: 21, orbitoOrderId: ORDER_ID }), update: async () => ({ matchedCount: 1 }), }, loadConfig: async () => ({ apiUrl: 'https://shop.test', apiKey: 'secret', statusMapping: [{ psStateId: 7, orbitoStatusId: CURRENT_STATUS }], }), apiFor: () => ({ setOrderState: async (...args) => { calls.setOrderState.push(args.slice(0, 2)); await args[2]?.beforeWrite(); return { unchanged: false }; }, }), ...overrides, }; return { deps, calls }; } test('syncs the current Orbito status to the mapped PrestaShop state', async () => { const { deps, calls } = dependencies(); const result = await syncOrderStatusToPrestashop({ orderId: ORDER_ID, statusId: OLD_STATUS, }, deps); assert.deepEqual(calls.setOrderState, [[21, 7]]); assert.deepEqual(calls.locks, [[`prestashop-status-sync:${ORDER_ID}`, STATUS_SYNC_LOCK_TTL_SECONDS]]); assert.deepEqual(calls.released, ['status-lock']); assert.equal(result.orbitoStatusId, CURRENT_STATUS); assert.equal(result.synced, true); }); test('different Orbito statuses both synchronize to their shared PrestaShop state', async () => { const { registerContract } = require('../src/hook-contract'); const { readStatusMapping } = require('../src/status-mapper'); const { deps, calls } = dependencies(); let statusId = OLD_STATUS; const hooks = new Map(); registerContract({ getInstanceId: () => 'alternative-orders-instance', getModuleInfo: () => ({ code: 'alternative/orders' }), registerHook: (name, handler) => hooks.set(name, handler), }, 'orders.search', async () => ({ data: [{ _id: ORDER_ID, statusId, source: { moduleCode: 'core/prestashop', moduleId: deps.Core.getInstanceId(), sourceId: '21' }, }] })); deps.Core.execHook = async (name, request) => [await hooks.get(name).call({ caller: { moduleCode: 'core/prestashop', instanceId: deps.Core.getInstanceId() }, }, request)]; deps.loadConfig = async () => ({ apiUrl: 'https://shop.test', apiKey: 'secret', statusMapping: readStatusMapping({ [`mapping.orbito_${OLD_STATUS}`]: '7', [`mapping.orbito_${CURRENT_STATUS}`]: '7', }), }); await syncOrderStatusToPrestashop({ orderId: ORDER_ID }, deps); statusId = CURRENT_STATUS; await syncOrderStatusToPrestashop({ orderId: ORDER_ID }, deps); assert.deepEqual(calls.setOrderState, [[21, 7], [21, 7]]); }); test('ignores status events for an order owned by another PrestaShop instance', async () => { const { deps, calls } = dependencies(); deps.Core.callModule = async () => ({ data: [{ _id: ORDER_ID, statusId: CURRENT_STATUS, source: { moduleCode: 'core/prestashop', moduleId: 'bbbbbbbbbbbbbbbbbbbbbbbb', sourceId: '21', }, }] }); const result = await syncOrderStatusToPrestashop({ orderId: ORDER_ID }, deps); assert.deepEqual(calls.setOrderState, []); assert.equal(result.reason, 'different_source_instance'); assert.deepEqual(calls.locks, []); assert.deepEqual(calls.released, []); }); test('does not write an unmapped Orbito status to PrestaShop', async () => { const { deps, calls } = dependencies({ loadConfig: async () => ({ apiUrl: 'https://shop.test', apiKey: 'secret', statusMapping: [{ psStateId: 2, orbitoStatusId: OLD_STATUS }], }), }); const result = await syncOrderStatusToPrestashop({ orderId: ORDER_ID }, deps); assert.deepEqual(calls.setOrderState, []); assert.equal(result.reason, 'status_not_mapped'); }); test('keeps a concurrent event retryable when the per-order lock is occupied', async () => { const { deps } = dependencies(); deps.Core.getLock = async () => null; await assert.rejects( () => syncOrderStatusToPrestashop({ orderId: ORDER_ID }, deps), /already running/ ); }); test('registers the Orbito order status change hook', async () => { const { deps } = dependencies(); let registration; deps.Core.registerHook = async (name, handler) => { registration = { name, handler }; }; await registerStatusSyncHook(deps); assert.equal(registration.name, 'event:order:status_changed'); assert.equal(typeof registration.handler, 'function'); });