import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'fs';
import path from 'path';
import os from 'os';
import { detectPencilExtension, resolvePlatformBinary, buildMcpConfig, } from '../detect.js';
describe('pencil/detect', () => {
    let tmpDir;
    beforeEach(() => {
        tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pencil-detect-'));
    });
    afterEach(() => {
        fs.rmSync(tmpDir, { recursive: true, force: true });
    });
    describe('detectPencilExtension()', () => {
        it('returns null when no extension directories exist', () => {
            const result = detectPencilExtension(tmpDir);
            expect(result).toBeNull();
        });
        it('returns the latest version when multiple versions exist', () => {
            fs.mkdirSync(path.join(tmpDir, 'highagency.pencildev-0.6.33', 'out'), { recursive: true });
            fs.mkdirSync(path.join(tmpDir, 'highagency.pencildev-0.6.35', 'out'), { recursive: true });
            fs.mkdirSync(path.join(tmpDir, 'highagency.pencildev-0.6.34', 'out'), { recursive: true });
            const result = detectPencilExtension(tmpDir);
            expect(result).not.toBeNull();
            expect(result.version).toBe('0.6.35');
            expect(result.extensionPath).toContain('highagency.pencildev-0.6.35');
        });
        it('returns info for a single installed version', () => {
            fs.mkdirSync(path.join(tmpDir, 'highagency.pencildev-0.6.33', 'out'), { recursive: true });
            const result = detectPencilExtension(tmpDir);
            expect(result).not.toBeNull();
            expect(result.version).toBe('0.6.33');
        });
    });
    describe('resolvePlatformBinary()', () => {
        it('returns darwin-arm64 for macOS ARM', () => {
            const binary = resolvePlatformBinary('darwin', 'arm64');
            expect(binary).toBe('mcp-server-darwin-arm64');
        });
        it('returns darwin-x64 for macOS Intel', () => {
            const binary = resolvePlatformBinary('darwin', 'x64');
            expect(binary).toBe('mcp-server-darwin-x64');
        });
        it('returns linux-x64 for Linux', () => {
            const binary = resolvePlatformBinary('linux', 'x64');
            expect(binary).toBe('mcp-server-linux-x64');
        });
        it('returns win32-x64.exe for Windows', () => {
            const binary = resolvePlatformBinary('win32', 'x64');
            expect(binary).toBe('mcp-server-win32-x64.exe');
        });
    });
    describe('buildMcpConfig()', () => {
        it('generates correct config object', () => {
            const info = {
                version: '0.6.35',
                extensionPath: '/Users/test/.vscode/extensions/highagency.pencildev-0.6.35',
            };
            const config = buildMcpConfig(info, 'mcp-server-darwin-arm64');
            expect(config).toEqual({
                mcpServers: {
                    pencil: {
                        command: '/Users/test/.vscode/extensions/highagency.pencildev-0.6.35/out/mcp-server-darwin-arm64',
                        args: ['--app', 'visual_studio_code'],
                        env: {},
                    },
                },
            });
        });
    });
});
