import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { detectMcp, resolvePlatformBinary, detectExtension } from '../detect.js';
import fs from 'fs';
import path from 'path';
import os from 'os';
function makeDef(overrides) {
    return {
        name: overrides.id,
        description: '',
        category: 'test',
        server: { command: 'npx', args: [], env: {} },
        auth: [],
        tools: [],
        relevant_sectors: [],
        ...overrides,
    };
}
describe('resolvePlatformBinary', () => {
    it('resolves darwin + arm64', () => {
        expect(resolvePlatformBinary('darwin', 'arm64')).toBe('mcp-server-darwin-arm64');
    });
    it('resolves darwin + x64', () => {
        expect(resolvePlatformBinary('darwin', 'x64')).toBe('mcp-server-darwin-x64');
    });
    it('resolves linux + x64', () => {
        expect(resolvePlatformBinary('linux', 'x64')).toBe('mcp-server-linux-x64');
    });
    it('resolves win32 + x64 with .exe', () => {
        expect(resolvePlatformBinary('win32', 'x64')).toBe('mcp-server-win32-x64.exe');
    });
});
describe('detectExtension', () => {
    let tmpDir;
    beforeEach(() => {
        tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ext-detect-test-'));
    });
    afterEach(() => {
        fs.rmSync(tmpDir, { recursive: true, force: true });
    });
    it('returns null when no extension found', () => {
        const result = detectExtension('nonexistent-prefix-', tmpDir);
        expect(result).toBeNull();
    });
    it('finds latest version', () => {
        fs.mkdirSync(path.join(tmpDir, 'highagency.pencildev-0.6.30'));
        fs.mkdirSync(path.join(tmpDir, 'highagency.pencildev-0.6.35'));
        const result = detectExtension('highagency.pencildev-', tmpDir);
        expect(result).not.toBeNull();
        expect(result.version).toBe('0.6.35');
        expect(result.extensionPath).toContain('0.6.35');
    });
});
describe('detectMcp', () => {
    it('npm detection — always returns available', () => {
        const def = makeDef({
            id: 'slack',
            detection: { method: 'npm', npm: { package: '@modelcontextprotocol/server-slack' } },
        });
        const result = detectMcp(def);
        expect(result.status).toBe('available');
        expect(result.detail).toContain('npm');
    });
    it('cli detection — returns unavailable when command not found', () => {
        const def = makeDef({
            id: 'test-cli',
            detection: { method: 'cli', cli: { command: 'nonexistent-binary-xyz', version_flag: '--version' } },
        });
        const result = detectMcp(def);
        expect(result.status).toBe('unavailable');
    });
    it('hybrid detection — falls back to npm when CLI not found', () => {
        const def = makeDef({
            id: 'test-hybrid',
            detection: {
                method: 'hybrid',
                cli: { command: 'nonexistent-binary-xyz', version_flag: '--version' },
                npm: { package: 'some-package' },
            },
        });
        const result = detectMcp(def);
        expect(result.status).toBe('available');
        expect(result.detail).toContain('npm');
    });
});
