/**
* Copyright Super iPaaS Integration LLC, an IBM Company 2024
*/
import { spawnSync } from 'node:child_process';
import fs from 'fs';

describe('Functional tests for the build command', () => {
	const localDir = 'test/resources/apim/api-studio-root';
	const outputFile = 'test/resources/apim/build-output.zip';

	afterEach(() => {
		if (fs.existsSync(outputFile)) {
			fs.unlinkSync(outputFile);
		}
	});

	/* to be enabled after inter project dependencies are resolved
	test('should check for successful build with specific projects', () => {
		const result = spawnSync('node', [
			'dist/cli.js', 'apic', 'build', 'ProductProject',
			'--localDir', localDir,
			'--output', outputFile
		], { encoding: 'utf-8' });

		const expectedLogs = [
			'Build completed successfully!',
		];

		expectedLogs.forEach(log => {
			expect(result.stdout.trim()).toContain(log);
		});
	});
	*/

	/* to be enabled after inter project dependencies are resolved
	test('should check for successful build of all projects', () => {
		const result = spawnSync('node', [
			'dist/cli.js', 'apic', 'build', '--all',
			'--localDir', localDir,
			'--output', outputFile
		], { encoding: 'utf-8' });

		const expectedLogs = [
			'Build completed successfully!',
		];

		expectedLogs.forEach(log => {
			expect(result.stdout.trim()).toContain(log);
		});
	}); 
	*/

	test.skip('should build specific asset with valid project and asset names', () => {
		const assetName = 'dev:swagger_petstore_testcli:1.0';
		const result = spawnSync('node', [
			'dist/cli.js', 'build',
			'--localDir', localDir, 'ProductProject',
			'--names', assetName,
			'--output', outputFile
		], { encoding: 'utf-8' });

		const expectedLogs = [
			'Build completed successfully!',
		];

		expectedLogs.forEach(log => {
			expect(result.stdout.trim()).toContain(log);
		});
	});
	test('should check for build failure with missing project names', () => {
		const result = spawnSync('node', [
			'dist/cli.js', 'build',
			'--localDir', localDir,
			'--output', outputFile
		], { encoding: 'utf-8' });

		const expectedLogs = [
			'Invalid project names'
		];

		expectedLogs.forEach(log => {
			expect(result.stdout.trim()).toContain(log);
		});
	});

	test('should check for build failure with missing localDir', () => {
		const result = spawnSync('node', [
			'dist/cli.js', 'build', 'ProductProject,PoliciesProject',
			'--output', outputFile
		], { encoding: 'utf-8' });

		const expectedLogs = [
			'error: required option \'-l, --localDir <localDir>\' not specified'
		];

		expectedLogs.forEach(log => {
			expect(result.stderr.trim()).toContain(log);
		});
	});

	test('should check for error with invalid localDir path', () => {
		const invalidlocalDir = 'hgudt/tyrs/drd/isr';
		const result = spawnSync('node', [
			'dist/cli.js', 'build', '--all',
			'--localDir', invalidlocalDir,
			'--output', outputFile
		], { encoding: 'utf-8' });

		expect(result.stderr.trim()).toContain(
			`The local directory path '${invalidlocalDir}' is either not a directory or does not exist.`
		);
	});
});
