/**
 * E2E tests for TestSwiftPackageTool
 * Tests running tests for Swift packages
 */
import { describe, test, expect, beforeAll, beforeEach, afterEach, afterAll } from '@jest/globals';
import { execSync } from 'child_process';
import { CallToolResultSchema } from '@modelcontextprotocol/sdk/types';
import { createAndConnectClient, cleanupClientAndTransport } from '../utils/testHelpers.js';
import { TestProjectManager } from '../utils/TestProjectManager.js';
import { TestEnvironmentCleaner } from '../utils/TestEnvironmentCleaner.js';
describe('TestSwiftPackageTool E2E Tests', () => {
    let client;
    let transport;
    let testProjectManager;
    beforeAll(async () => {
        execSync('npm run build', { cwd: process.cwd() });
        testProjectManager = new TestProjectManager();
        testProjectManager.setup();
    }, 120000);
    beforeEach(async () => {
        const setup = await createAndConnectClient();
        client = setup.client;
        transport = setup.transport;
    }, 30000);
    afterEach(async () => {
        TestEnvironmentCleaner.cleanupTestEnvironment();
        await cleanupClientAndTransport(client, transport);
        testProjectManager.cleanup();
    });
    afterAll(() => {
        TestEnvironmentCleaner.cleanupTestEnvironment();
    });
    describe('Running Swift Package Tests with XCTest', () => {
        test('should run all tests in XCTest package', async () => {
            const response = await client.request({
                method: 'tools/call',
                params: {
                    name: 'test_swift_package',
                    arguments: {
                        packagePath: testProjectManager.paths.swiftPackageXCTestDir
                    }
                }
            }, CallToolResultSchema);
            const text = response.content[0].text;
            expect(text).toMatch(/[✅❌] Tests (passed|failed)/);
            expect(text).toContain('Package:');
            expect(text).toMatch(/\d+ passed, \d+ failed/);
            expect(text).toContain('Configuration: Debug');
        }, 60000);
        test('should run tests with Release configuration', async () => {
            const response = await client.request({
                method: 'tools/call',
                params: {
                    name: 'test_swift_package',
                    arguments: {
                        packagePath: testProjectManager.paths.swiftPackageXCTestDir,
                        configuration: 'Release'
                    }
                }
            }, CallToolResultSchema);
            const text = response.content[0].text;
            expect(text).toContain('Configuration: Release');
            expect(text).toMatch(/[✅❌] Tests (passed|failed)/);
        }, 60000);
        test('should filter tests when specified', async () => {
            const response = await client.request({
                method: 'tools/call',
                params: {
                    name: 'test_swift_package',
                    arguments: {
                        packagePath: testProjectManager.paths.swiftPackageXCTestDir,
                        filter: 'TestSwiftPackageXCTestTests'
                    }
                }
            }, CallToolResultSchema);
            const text = response.content[0].text;
            expect(text).toContain('Filter: TestSwiftPackageXCTestTests');
            expect(text).toMatch(/[✅❌] Tests (passed|failed)/);
        }, 60000);
        test('should handle Package.swift path directly', async () => {
            const packageSwiftPath = `${testProjectManager.paths.swiftPackageXCTestDir}/Package.swift`;
            const response = await client.request({
                method: 'tools/call',
                params: {
                    name: 'test_swift_package',
                    arguments: {
                        packagePath: packageSwiftPath
                    }
                }
            }, CallToolResultSchema);
            const text = response.content[0].text;
            expect(text).toMatch(/[✅❌] Tests (passed|failed)/);
            expect(text).toContain('Package:');
        }, 60000);
        test('should handle package not found error', async () => {
            const response = await client.request({
                method: 'tools/call',
                params: {
                    name: 'test_swift_package',
                    arguments: {
                        packagePath: '/nonexistent/package'
                    }
                }
            }, CallToolResultSchema);
            const text = response.content[0].text;
            expect(text).toContain('No Package.swift found');
        }, 30000);
        test('should reject invalid configuration values', async () => {
            const response = await client.request({
                method: 'tools/call',
                params: {
                    name: 'test_swift_package',
                    arguments: {
                        packagePath: testProjectManager.paths.swiftPackageXCTestDir,
                        configuration: 'InvalidConfig'
                    }
                }
            }, CallToolResultSchema);
            const text = response.content[0].text;
            expect(text.toLowerCase()).toContain('validation error');
            expect(text).toMatch(/Debug.*Release/);
        }, 30000);
        test('should properly report failing tests', async () => {
            const response = await client.request({
                method: 'tools/call',
                params: {
                    name: 'test_swift_package',
                    arguments: {
                        packagePath: testProjectManager.paths.swiftPackageXCTestDir,
                        filter: 'LegacyTests.testFailingTest'
                    }
                }
            }, CallToolResultSchema);
            const text = response.content[0].text;
            expect(text).toContain('Filter: LegacyTests.testFailingTest');
            expect(text).toMatch(/❌ Tests failed: 0 passed, 1 failed/);
            expect(text).toContain('Failing tests:');
            // Verify that failing test details are reported with identifier and reason
            expect(text).toContain('TestSwiftPackageXCTestTests.LegacyTests.testFailingTest');
            expect(text).toContain('Test MCP failing test reporting');
        }, 60000);
    });
    describe('Test Output', () => {
        test('should include full test output', async () => {
            const response = await client.request({
                method: 'tools/call',
                params: {
                    name: 'test_swift_package',
                    arguments: {
                        packagePath: testProjectManager.paths.swiftPackageXCTestDir
                    }
                }
            }, CallToolResultSchema);
            const text = response.content[0].text;
            // Verify test summary and multiple failures are shown
            expect(text).toMatch(/❌ Tests failed: 1 passed, 2 failed/);
            expect(text).toContain('Failing tests:');
            expect(text).toContain('TestSwiftPackageXCTestTests.LegacyTests');
        }, 60000);
    });
    describe('Running Swift Package Tests with Swift Testing', () => {
        test('should run all tests in Swift Testing package', async () => {
            const response = await client.request({
                method: 'tools/call',
                params: {
                    name: 'test_swift_package',
                    arguments: {
                        packagePath: testProjectManager.paths.swiftPackageSwiftTestingDir
                    }
                }
            }, CallToolResultSchema);
            const text = response.content[0].text;
            expect(text).toMatch(/[✅❌] Tests (passed|failed)/);
            expect(text).toContain('Package:');
            expect(text).toMatch(/\d+ passed, \d+ failed/);
            expect(text).toContain('Configuration: Debug');
        }, 60000);
        test('should parse Swift Testing output correctly', async () => {
            const response = await client.request({
                method: 'tools/call',
                params: {
                    name: 'test_swift_package',
                    arguments: {
                        packagePath: testProjectManager.paths.swiftPackageSwiftTestingDir,
                        filter: 'testExample' // Run a specific test
                    }
                }
            }, CallToolResultSchema);
            const text = response.content[0].text;
            expect(text).toContain('Filter: testExample');
            expect(text).toMatch(/[✅❌] Tests (passed|failed)/);
            // The parser should correctly identify Swift Testing output
            expect(text).toMatch(/\d+ passed/);
        }, 60000);
        test('should handle Swift Testing with Release configuration', async () => {
            const response = await client.request({
                method: 'tools/call',
                params: {
                    name: 'test_swift_package',
                    arguments: {
                        packagePath: testProjectManager.paths.swiftPackageSwiftTestingDir,
                        configuration: 'Release'
                    }
                }
            }, CallToolResultSchema);
            const text = response.content[0].text;
            expect(text).toContain('Configuration: Release');
            expect(text).toMatch(/[✅❌] Tests (passed|failed)/);
        }, 60000);
        test('should properly report failing Swift Testing tests', async () => {
            const response = await client.request({
                method: 'tools/call',
                params: {
                    name: 'test_swift_package',
                    arguments: {
                        packagePath: testProjectManager.paths.swiftPackageSwiftTestingDir,
                        filter: 'testFailingTest'
                    }
                }
            }, CallToolResultSchema);
            const text = response.content[0].text;
            expect(text).toContain('Filter: testFailingTest');
            expect(text).toMatch(/❌ Tests failed: 0 passed, 1 failed/);
            expect(text).toContain('Failing tests:');
            // Verify that failing test details are reported with identifier and reason
            expect(text).toContain('TestSwiftPackageSwiftTestingTests.ModernTests.testFailingTest()');
            expect(text).toContain('Expectation failed: 1 == 2');
        }, 60000);
    });
});
//# sourceMappingURL=test-swift-package.e2e.test.js.map