import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'fs';
import path from 'path';
import os from 'os';
import { loadMcpCatalog, loadMcpDefinition, listAllMcpIds } from '../catalog.js';
let tmpDir;
beforeEach(() => {
    tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mcp-catalog-test-'));
});
afterEach(() => {
    fs.rmSync(tmpDir, { recursive: true, force: true });
});
function writeCatalog(content) {
    fs.writeFileSync(path.join(tmpDir, '_catalog.yaml'), content, 'utf-8');
}
function writeDefinition(id, content) {
    fs.writeFileSync(path.join(tmpDir, `${id}.mcp.yaml`), content, 'utf-8');
}
describe('loadMcpCatalog', () => {
    it('loads and parses _catalog.yaml', () => {
        writeCatalog(`
catalog:
  version: "1.0.0"
  categories:
    - name: development
      mcps: [github]
    - name: communication
      mcps: [slack]
`);
        const catalog = loadMcpCatalog(tmpDir);
        expect(catalog.version).toBe('1.0.0');
        expect(catalog.categories).toHaveLength(2);
        expect(catalog.categories[0].name).toBe('development');
        expect(catalog.categories[0].mcps).toEqual(['github']);
    });
    it('returns empty categories when catalog is missing', () => {
        const catalog = loadMcpCatalog(tmpDir);
        expect(catalog.categories).toEqual([]);
    });
});
describe('listAllMcpIds', () => {
    it('flattens all MCP IDs from categories', () => {
        writeCatalog(`
catalog:
  version: "1.0.0"
  categories:
    - name: dev
      mcps: [github, postgresql]
    - name: comm
      mcps: [slack]
`);
        const ids = listAllMcpIds(tmpDir);
        expect(ids).toEqual(['github', 'postgresql', 'slack']);
    });
});
describe('loadMcpDefinition', () => {
    it('loads individual MCP definition by ID', () => {
        writeDefinition('github', `
id: github
name: GitHub
description: PRs and issues
category: development
detection:
  method: npm
  npm:
    package: "@modelcontextprotocol/server-github"
server:
  command: npx
  args: ["-y", "@modelcontextprotocol/server-github"]
  env:
    GITHUB_PERSONAL_ACCESS_TOKEN: "\${GITHUB_PERSONAL_ACCESS_TOKEN}"
auth:
  - key: GITHUB_PERSONAL_ACCESS_TOKEN
    label: "GitHub Token"
    hint: "Create at github.com/settings/tokens"
    required: true
tools:
  - name: list_issues
    description: List issues
relevant_sectors:
  - development
`);
        const def = loadMcpDefinition(tmpDir, 'github');
        expect(def).not.toBeNull();
        expect(def.id).toBe('github');
        expect(def.name).toBe('GitHub');
        expect(def.detection.method).toBe('npm');
        expect(def.auth).toHaveLength(1);
        expect(def.auth[0].key).toBe('GITHUB_PERSONAL_ACCESS_TOKEN');
        expect(def.server.args).toContain('-y');
    });
    it('returns null for unknown MCP ID', () => {
        const def = loadMcpDefinition(tmpDir, 'nonexistent');
        expect(def).toBeNull();
    });
    it('handles malformed YAML gracefully', () => {
        writeDefinition('broken', '{{{{not yaml');
        const def = loadMcpDefinition(tmpDir, 'broken');
        expect(def).toBeNull();
    });
});
