"use strict";
/**
 * RED-GREEN tests for F-NEW-PRODUCT-AGENT-DISCOVERY-CLI (W2.1).
 *
 * @connexum/ai-governance  F-NEW-PRODUCT-AGENT-DISCOVERY-CLI
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const node_test_1 = require("node:test");
const assert = __importStar(require("node:assert/strict"));
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
const os = __importStar(require("node:os"));
const agent_discovery_1 = require("./agent-discovery");
function makeProject(files) {
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-disc-'));
    for (const [rel, content] of Object.entries(files)) {
        const full = path.join(dir, rel);
        fs.mkdirSync(path.dirname(full), { recursive: true });
        fs.writeFileSync(full, content);
    }
    return dir;
}
(0, node_test_1.describe)('F-NEW-PRODUCT-AGENT-DISCOVERY-CLI (W2.1)', () => {
    (0, node_test_1.it)('detects Claude Code agent.md with frontmatter tools', () => {
        const root = makeProject({
            '.claude/agents/mailer.md': `---
name: mailer
tools: Read, Write, Bash, WebFetch
---

You are a mailer agent.
`,
        });
        const manifests = (0, agent_discovery_1.discoverAgents)(root);
        assert.equal(manifests.length, 1);
        assert.equal(manifests[0].agentId, 'claude-code:mailer');
        assert.deepEqual(manifests[0].tools, ['Read', 'Write', 'Bash', 'WebFetch']);
        assert.equal(manifests[0].llmProvider, 'anthropic/claude-code');
    });
    (0, node_test_1.it)('detects multiple agent.md files at different depths', () => {
        const root = makeProject({
            '.claude/agents/a.md': `---\nname: a\ntools: Read\n---\nA agent`,
            'subfolder/.claude/agents/b.md': `---\nname: b\ntools: Write, Bash\n---\nB agent`,
        });
        const manifests = (0, agent_discovery_1.discoverAgents)(root);
        assert.equal(manifests.length, 2);
        const ids = manifests.map((m) => m.agentId).sort();
        assert.deepEqual(ids, ['claude-code:a', 'claude-code:b']);
    });
    (0, node_test_1.it)('detects OpenAI Agents SDK new Agent({}) instantiations in TS', () => {
        const root = makeProject({
            'src/agent.ts': `
        import { Agent } from '@openai/agents';
        const myAgent = new Agent({
          name: 'researcher',
          tools: [webSearch, summarize, db_query],
        });
      `,
        });
        const manifests = (0, agent_discovery_1.discoverAgents)(root);
        const oa = manifests.find((m) => m.agentId.startsWith('openai-agents:'));
        assert.ok(oa, 'OpenAI agent detected');
        assert.equal(oa?.name, 'researcher');
        assert.ok(oa?.tools.includes('webSearch'));
        assert.ok(oa?.tools.includes('db_query'));
    });
    (0, node_test_1.it)('detects LangChain Tool / DynamicTool registrations', () => {
        const root = makeProject({
            'src/tools.js': `
        const t1 = new DynamicTool({ name: 'searchTool', description: 'search the web' });
        const t2 = new Tool({ name: 'computeTool', description: 'compute' });
      `,
        });
        const manifests = (0, agent_discovery_1.discoverAgents)(root);
        const lc = manifests.find((m) => m.agentId.startsWith('langchain:'));
        assert.ok(lc, 'LangChain agent detected');
        assert.deepEqual(lc?.tools.sort(), ['computeTool', 'searchTool']);
    });
    (0, node_test_1.it)('detects MCP servers from mcp.json', () => {
        const root = makeProject({
            'mcp.json': JSON.stringify({
                mcpServers: {
                    github: { command: 'mcp-server-github' },
                    postgres: { url: 'https://internal.example/mcp' },
                },
            }, null, 2),
        });
        const manifests = (0, agent_discovery_1.discoverAgents)(root);
        const mcp = manifests.find((m) => m.agentId.startsWith('mcp-config:'));
        assert.ok(mcp, 'MCP agent detected');
        assert.deepEqual(mcp?.tools.sort(), ['mcp__github', 'mcp__postgres']);
        assert.equal(mcp?.mcpServers?.length, 2);
    });
    (0, node_test_1.it)('skips node_modules and other skip-dirs', () => {
        const root = makeProject({
            'node_modules/some-pkg/.claude/agents/should-skip.md': `---\nname: skipme\ntools: x\n---`,
            'src/.claude/agents/real.md': `---\nname: real\ntools: Read\n---`,
        });
        const manifests = (0, agent_discovery_1.discoverAgents)(root);
        assert.equal(manifests.length, 1);
        assert.equal(manifests[0].agentId, 'claude-code:real');
    });
    (0, node_test_1.it)('respects maxDepth option', () => {
        const root = makeProject({
            'a/b/c/d/e/agent.md': `---\nname: deep\ntools: x\n---`,
        });
        const manifests = (0, agent_discovery_1.discoverAgents)(root, { maxDepth: 2 });
        assert.equal(manifests.length, 0, 'agent at depth 5 should be ignored when maxDepth=2');
        const manifestsDeep = (0, agent_discovery_1.discoverAgents)(root, { maxDepth: 10 });
        assert.equal(manifestsDeep.length, 1);
    });
    (0, node_test_1.it)('returns empty array on a directory with no agents', () => {
        const root = makeProject({
            'README.md': '# nothing here',
            'src/index.ts': 'export const x = 1;',
        });
        const manifests = (0, agent_discovery_1.discoverAgents)(root);
        assert.deepEqual(manifests, []);
    });
    (0, node_test_1.it)('throws on missing root directory (fail-closed)', () => {
        assert.throws(() => (0, agent_discovery_1.discoverAgents)('/tmp/this-does-not-exist-' + Date.now()), /does not exist/);
    });
    (0, node_test_1.it)('throws when root is a file, not a directory (fail-closed)', () => {
        const root = makeProject({ 'file.md': 'content' });
        assert.throws(() => (0, agent_discovery_1.discoverAgents)(path.join(root, 'file.md')), /not a directory/);
    });
    (0, node_test_1.it)('manifests are deduplicated by agentId', () => {
        const root = makeProject({
            '.claude/agents/dup.md': `---\nname: dup\ntools: x\n---`,
            'other/.claude/agents/dup.md': `---\nname: dup\ntools: y\n---`,
        });
        const manifests = (0, agent_discovery_1.discoverAgents)(root);
        assert.equal(manifests.length, 1, 'duplicate agentId removed');
    });
    (0, node_test_1.it)('detection output is shape-compatible with autoGenerateCompanyPack', async () => {
        const root = makeProject({
            'src/agent.ts': `new Agent({ name: 'pay-bot', tools: [bash, postgresQuery] })`,
        });
        const [manifest] = (0, agent_discovery_1.discoverAgents)(root);
        const { autoGenerateCompanyPack } = await Promise.resolve().then(() => __importStar(require('./auto-pack-generator.js')));
        const result = autoGenerateCompanyPack(manifest, { acceptAllSuggestedRules: true });
        assert.ok(result.pack.id.length > 0);
        assert.equal(result.acceptedRules.length, result.proposedRules.length);
    });
});
//# sourceMappingURL=agent-discovery.test.js.map