import { Container } from '@n8n/di';

import { ScalingModeConfig } from '../scaling-mode.config';

describe('ScalingModeConfig.workerPool', () => {
	const originalEnv = process.env;

	beforeEach(() => {
		Container.reset();
		vi.clearAllMocks();
	});

	afterEach(() => {
		process.env = originalEnv;
	});

	test('defaults to empty string when N8N_WORKER_POOL_NAME is unset', () => {
		process.env = {};
		const config = Container.get(ScalingModeConfig);
		expect(config.workerPool.name).toBe('');
	});

	test('worker pools are disabled by default', () => {
		process.env = {};
		const config = Container.get(ScalingModeConfig);
		expect(config.workerPool.enabled).toBe(false);
	});

	test('enables worker pools when N8N_WORKER_POOLS_ENABLED is true', () => {
		process.env = { N8N_WORKER_POOLS_ENABLED: 'true' };
		const config = Container.get(ScalingModeConfig);
		expect(config.workerPool.enabled).toBe(true);
	});

	test('accepts a valid label', () => {
		process.env = { N8N_WORKER_POOL_NAME: 'gpu' };
		const config = Container.get(ScalingModeConfig);
		expect(config.workerPool.name).toBe('gpu');
	});

	test('accepts lowercase alphanumeric with hyphens', () => {
		process.env = { N8N_WORKER_POOL_NAME: 'a1-b2-c3' };
		const config = Container.get(ScalingModeConfig);
		expect(config.workerPool.name).toBe('a1-b2-c3');
	});

	test.each([
		['uppercase', 'GPU'],
		['underscore', 'g_pu'],
		['space', 'g pu'],
		['leading hyphen', '-gpu'],
		['colon', 'g:pu'],
		['dot', 'g.pu'],
		['too long', 'a'.repeat(64)],
	])('falls back to default and warns on invalid label (%s)', (_label, value) => {
		const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
		process.env = { N8N_WORKER_POOL_NAME: value };

		const config = Container.get(ScalingModeConfig);

		expect(config.workerPool.name).toBe('');
		expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('N8N_WORKER_POOL_NAME'));

		consoleWarnSpy.mockRestore();
	});
});
