51 lines
2.9 KiB
JavaScript
51 lines
2.9 KiB
JavaScript
'use strict';
|
|
|
|
// Test-only bus: keep existing business fixtures while exercising the real
|
|
// request envelope, discovery and error propagation in src/hook-contract.
|
|
// Transport behavior and actual provider registration are tested separately.
|
|
function withHookFixtures(Core) {
|
|
if (Core.__hookFixtures) return Core;
|
|
Object.defineProperty(Core, '__hookFixtures', { value: true });
|
|
const previousHook = Core.execHook?.bind(Core);
|
|
const identities = new Map();
|
|
Core.getInstanceId ||= () => 'fixture-instance';
|
|
Core.getModuleInfo ||= () => ({ code: 'test/module' });
|
|
Core.execHook = async function(name, request) {
|
|
const contract = name.replace(/\.providers$/, '');
|
|
const match = /^(orders|products|shipping|invoicing|printing|sources)\.(.+)$/.exec(contract);
|
|
if (!match) return previousHook ? previousHook(name, request) : [];
|
|
if (name.endsWith('.providers')) {
|
|
const moduleCode = request?.targetModuleCode || `core/${match[1]}`;
|
|
const instanceId = request?.targetInstanceId || `fixture:${moduleCode.replace('/', ':')}`;
|
|
identities.set(`${contract}:${instanceId}`, moduleCode);
|
|
return [{ protocol: 'orbito.contract/1', moduleCode, instanceId }];
|
|
}
|
|
if (request?.protocol !== 'orbito.contract/1') return previousHook ? previousHook(name, request) : [];
|
|
const instanceId = request.targetInstanceId;
|
|
const moduleCode = identities.get(`${contract}:${instanceId}`);
|
|
try {
|
|
let data;
|
|
if (contract === 'orders.references') {
|
|
const page = `details/${request.params.orderId}`;
|
|
data = {
|
|
link: { module: 'core/orders', instanceId, path: page },
|
|
relation: { module: 'core/orders', object: 'order', id: request.params.orderId },
|
|
navigation: { action: 'navigate', module: 'core/orders', instanceId, page },
|
|
approveFinancialData: { action: 'callMethod', module: 'core/orders', instanceId, method: 'import.approveFinancialData', params: { orderId: request.params.orderId, confirmed: true } },
|
|
};
|
|
} else if (/^(shipping\.carrier|printing\.provider)\./.test(contract)) {
|
|
data = await Core.callInstanceMethod(instanceId, match[2].replace(/^(carrier|provider)\./, ''), request.params);
|
|
} else {
|
|
data = await Core.callModule(moduleCode, match[2].replace(/^issuer\./, ''), request.params);
|
|
}
|
|
return [{ protocol: 'orbito.contract/1', instanceId, ok: true, data }];
|
|
} catch (error) {
|
|
return [{ protocol: 'orbito.contract/1', instanceId, ok: false,
|
|
error: { message: error.message, code: error.code, outcomeUncertain: error.outcomeUncertain, retryable: error.retryable } }];
|
|
}
|
|
};
|
|
return Core;
|
|
}
|
|
|
|
module.exports = { withHookFixtures };
|