import {
	gatewayMqQueueNeedsSolaceNetCheck,
	resolveTenantIdFromMqMessage,
	wrapGatewayMqMessageHandler,
} from '@/core/gateway/rails/gateway-mq-wrap';

describe('gateway-mq-wrap', () => {
	const origPrefix = process.env.SOLACENET_GATEWAY_MQ_QUEUE_PREFIX;

	afterEach(() => {
		process.env.SOLACENET_GATEWAY_MQ_QUEUE_PREFIX = origPrefix;
	});

	it('detects default gateway queue prefix', () => {
		delete process.env.SOLACENET_GATEWAY_MQ_QUEUE_PREFIX;
		expect(gatewayMqQueueNeedsSolaceNetCheck('dbis.gateway.inbound')).toBe(true);
		expect(gatewayMqQueueNeedsSolaceNetCheck('other.queue')).toBe(false);
	});

	it('resolves tenant from message', () => {
		expect(resolveTenantIdFromMqMessage({ tenantId: 't1' })).toBe('t1');
		expect(resolveTenantIdFromMqMessage({ body: { tenantId: 't2' } })).toBe('t2');
	});

	it('wrapGatewayMqMessageHandler passes through non-gateway queues', async () => {
		const handler = jest.fn().mockResolvedValue(undefined);
		const wrapped = wrapGatewayMqMessageHandler('other.queue', handler);
		await wrapped({ x: 1 });
		expect(handler).toHaveBeenCalledWith({ x: 1 });
	});
});
