/**
 * Version Manager Tests
 * Run with: node versionManager.test.js
 */

const versionManager = require('./versionManager');
const assert = require('assert');

console.log('Running version manager tests...\n');

// Test parseVersion
console.log('Test 1: parseVersion');
assert.deepStrictEqual(versionManager.parseVersion('1.2.3'), { major: 1, minor: 2, patch: 3, raw: '1.2.3' });
assert.strictEqual(versionManager.parseVersion('invalid'), null);
assert.strictEqual(versionManager.parseVersion('1.2'), null);
assert.strictEqual(versionManager.parseVersion(''), null);
console.log('✓ parseVersion tests passed\n');

// Test formatVersion
console.log('Test 2: formatVersion');
assert.strictEqual(versionManager.formatVersion(1, 2, 3), '1.2.3');
assert.strictEqual(versionManager.formatVersion(10, 0, 0), '10.0.0');
console.log('✓ formatVersion tests passed\n');

// Test bumpVersion
console.log('Test 3: bumpVersion');
assert.strictEqual(versionManager.bumpVersion('1.2.3', 'patch'), '1.2.4');
assert.strictEqual(versionManager.bumpVersion('1.2.3', 'minor'), '1.3.0');
assert.strictEqual(versionManager.bumpVersion('1.2.3', 'major'), '2.0.0');
assert.strictEqual(versionManager.bumpVersion('1.2.3', 'keep'), '1.2.3');
assert.strictEqual(versionManager.bumpVersion('1.2.3', 'unknown'), '1.2.4'); // defaults to patch
assert.strictEqual(versionManager.bumpVersion('invalid', 'patch'), null);
console.log('✓ bumpVersion tests passed\n');

// Test extractHeaderVersion
console.log('Test 4: extractHeaderVersion');
const headerContent = `<?php
/**
 * Plugin Name: Test Plugin
 * Version: 2.5.1
 * Author: Test
 */`;
assert.strictEqual(versionManager.extractHeaderVersion(headerContent), '2.5.1');

const headerContentNoSpace = `<?php
/**
 * Plugin Name: Test Plugin
 * Version:3.0.0
 */`;
assert.strictEqual(versionManager.extractHeaderVersion(headerContentNoSpace), '3.0.0');

assert.strictEqual(versionManager.extractHeaderVersion('no version here'), null);
console.log('✓ extractHeaderVersion tests passed\n');

// Test extractDefineVersion
console.log('Test 5: extractDefineVersion');
const defineContent = `<?php
define('TEST_PLUGIN_VERSION', '1.2.3');
define("ANOTHER_VERSION", "2.0.0");`;
assert.strictEqual(versionManager.extractDefineVersion(defineContent), '1.2.3');

const defineContentSpaces = `<?php
define( 'MY_VERSION' , '3.4.5' );`;
assert.strictEqual(versionManager.extractDefineVersion(defineContentSpaces), '3.4.5');

assert.strictEqual(versionManager.extractDefineVersion('no version here'), null);
console.log('✓ extractDefineVersion tests passed\n');

// Test extractAllVersions
console.log('Test 6: extractAllVersions');
const allVersionsContent = `<?php
/**
 * Plugin Name: Test
 * Version: 1.2.3
 */
define('PLUGIN_VERSION', '1.2.3');`;
const allVersions = versionManager.extractAllVersions(allVersionsContent);
assert.strictEqual(allVersions.headerVersion, '1.2.3');
assert.strictEqual(allVersions.defineVersion, '1.2.3');
assert.strictEqual(allVersions.detectedVersion, '1.2.3');
console.log('✓ extractAllVersions tests passed\n');

// Test updateVersionInContent
console.log('Test 7: updateVersionInContent');
const originalContent = `<?php
/**
 * Plugin Name: Test Plugin
 * Version: 1.0.0
 * Author: Test
 */
define('PLUGIN_VERSION', '1.0.0');`;

const updatedContent = versionManager.updateVersionInContent(originalContent, '1.0.0', '1.1.0');
assert(updatedContent.includes('Version: 1.1.0'));
assert(updatedContent.includes("define('PLUGIN_VERSION', '1.1.0')"));
assert(!updatedContent.includes('Version: 1.0.0'));
assert(!updatedContent.includes("define('PLUGIN_VERSION', '1.0.0')"));
console.log('✓ updateVersionInContent tests passed\n');

// Test isWordPressPluginFile
console.log('Test 8: isWordPressPluginFile');
assert.strictEqual(versionManager.isWordPressPluginFile(headerContent), true);
assert.strictEqual(versionManager.isWordPressPluginFile('<?php echo "hello";'), false);
assert.strictEqual(versionManager.isWordPressPluginFile(''), false);
console.log('✓ isWordPressPluginFile tests passed\n');

// Test getPluginSlugFromPath
console.log('Test 9: getPluginSlugFromPath');
assert.strictEqual(versionManager.getPluginSlugFromPath('/path/to/my-plugin/my-plugin.php'), 'my-plugin');
assert.strictEqual(versionManager.getPluginSlugFromPath('/path/to/plugin-name.php'), 'plugin-name');
console.log('✓ getPluginSlugFromPath tests passed\n');

console.log('========================================');
console.log('All version manager tests passed! ✓');
console.log('========================================');
