"use strict";
/**
 * RED-GREEN tests for the discovery CLI runner.
 *
 * @connexum/ai-governance  discovery-cli (W2.1 + push transport binary)
 */
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 discovery_cli_1 = require("./discovery-cli");
const signed_manifest_1 = require("./signed-manifest");
function makeProject(files) {
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'disc-cli-'));
    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;
}
function recordingTransport() {
    const calls = [];
    return {
        calls,
        transport: async (req) => {
            calls.push(req);
            return { status: 201, bodyText: `{"id":"r_${calls.length}"}` };
        },
    };
}
const noopSleep = async (_ms) => undefined;
(0, node_test_1.describe)('discovery CLI runner', () => {
    (0, node_test_1.it)('discovers + signs + pushes manifests in one call', async () => {
        const root = makeProject({
            '.claude/agents/a.md': `---\nname: a\ntools: Bash, Read\n---`,
            '.claude/agents/b.md': `---\nname: b\ntools: WebFetch\n---`,
        });
        const t = recordingTransport();
        const { privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const result = await (0, discovery_cli_1.runDiscoveryCli)({
            rootDir: root,
            orgId: 'org-x',
            privateKey,
            endpoint: 'https://proxy.my-cc.io/api/v1/manifests',
            transport: t.transport,
            sleep: noopSleep,
        });
        assert.equal(result.ok, true);
        assert.equal(result.agentCount, 2);
        assert.equal(result.successCount, 2);
        assert.equal(result.failureCount, 0);
        assert.equal(t.calls.length, 2);
        assert.equal(t.calls[0].headers['x-connexum-org'], 'org-x');
    });
    (0, node_test_1.it)('dryRun: returns manifests with push=null and skips network', async () => {
        const root = makeProject({
            '.claude/agents/a.md': `---\nname: a\ntools: Bash\n---`,
        });
        const t = recordingTransport();
        const { privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const result = await (0, discovery_cli_1.runDiscoveryCli)({
            rootDir: root,
            orgId: 'org-x',
            privateKey,
            endpoint: 'https://proxy.my-cc.io/api/v1/manifests',
            transport: t.transport,
            sleep: noopSleep,
            dryRun: true,
        });
        assert.equal(result.agentCount, 1);
        assert.equal(result.reports[0].push, null);
        assert.equal(t.calls.length, 0);
        assert.equal(result.ok, true);
    });
    (0, node_test_1.it)('failure on one manifest is reported but does not stop other pushes', async () => {
        const root = makeProject({
            '.claude/agents/a.md': `---\nname: a\ntools: Bash\n---`,
            '.claude/agents/b.md': `---\nname: b\ntools: Read\n---`,
            '.claude/agents/c.md': `---\nname: c\ntools: Write\n---`,
        });
        let i = 0;
        const transport = async () => {
            i++;
            if (i === 2)
                return { status: 400, bodyText: '{"error":"bad"}' };
            return { status: 201, bodyText: `{"id":"r_${i}"}` };
        };
        const { privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const result = await (0, discovery_cli_1.runDiscoveryCli)({
            rootDir: root,
            orgId: 'org-x',
            privateKey,
            endpoint: 'https://proxy.my-cc.io/api/v1/manifests',
            transport,
            sleep: noopSleep,
        });
        assert.equal(result.agentCount, 3);
        assert.equal(result.successCount, 2);
        assert.equal(result.failureCount, 1);
        assert.equal(result.ok, false);
    });
    (0, node_test_1.it)('zero agents discovered -> ok=true with 0 counts', async () => {
        const root = makeProject({ 'README.md': '# empty' });
        const t = recordingTransport();
        const { privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const result = await (0, discovery_cli_1.runDiscoveryCli)({
            rootDir: root,
            orgId: 'org-x',
            privateKey,
            endpoint: 'https://proxy.my-cc.io/api/v1/manifests',
            transport: t.transport,
            sleep: noopSleep,
        });
        assert.equal(result.agentCount, 0);
        assert.equal(result.ok, true);
        assert.equal(t.calls.length, 0);
    });
    (0, node_test_1.it)('missing rootDir / orgId / endpoint throws (fail-closed)', async () => {
        const { privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        await assert.rejects((0, discovery_cli_1.runDiscoveryCli)({ rootDir: '', orgId: 'o', privateKey, endpoint: 'https://x' }), /rootDir is required/);
        await assert.rejects((0, discovery_cli_1.runDiscoveryCli)({ rootDir: '/tmp', orgId: '', privateKey, endpoint: 'https://x' }), /orgId is required/);
        await assert.rejects((0, discovery_cli_1.runDiscoveryCli)({ rootDir: '/tmp', orgId: 'o', privateKey, endpoint: '' }), /endpoint is required/);
    });
    (0, node_test_1.it)('formatCliReport produces readable summary text', async () => {
        const root = makeProject({
            '.claude/agents/a.md': `---\nname: a\ntools: Bash\n---`,
        });
        const t = recordingTransport();
        const { privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const result = await (0, discovery_cli_1.runDiscoveryCli)({
            rootDir: root,
            orgId: 'org-x',
            privateKey,
            endpoint: 'https://proxy.my-cc.io/api/v1/manifests',
            transport: t.transport,
            sleep: noopSleep,
        });
        const text = (0, discovery_cli_1.formatCliReport)(result);
        assert.match(text, /Connexum Discovery/);
        assert.match(text, /Found 1 agent/);
        assert.match(text, /\[OK\]/);
        assert.match(text, /receipt=r_1/);
    });
    (0, node_test_1.it)('formatCliReport in dryRun mode shows [DRY-RUN] for every agent', async () => {
        const root = makeProject({
            '.claude/agents/a.md': `---\nname: a\ntools: Bash\n---`,
            '.claude/agents/b.md': `---\nname: b\ntools: Read\n---`,
        });
        const { privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const result = await (0, discovery_cli_1.runDiscoveryCli)({
            rootDir: root,
            orgId: 'org-x',
            privateKey,
            endpoint: 'https://proxy.my-cc.io/api/v1/manifests',
            sleep: noopSleep,
            dryRun: true,
        });
        const text = (0, discovery_cli_1.formatCliReport)(result);
        const matches = text.match(/\[DRY-RUN\]/g) ?? [];
        assert.equal(matches.length, 2);
    });
    (0, node_test_1.it)('respects discovery options (maxDepth)', async () => {
        const root = makeProject({
            'a/b/c/d/agent.md': `---\nname: deep\ntools: Bash\n---`,
        });
        const t = recordingTransport();
        const { privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const shallow = await (0, discovery_cli_1.runDiscoveryCli)({
            rootDir: root,
            orgId: 'org-x',
            privateKey,
            endpoint: 'https://proxy.my-cc.io/api/v1/manifests',
            transport: t.transport,
            sleep: noopSleep,
            discovery: { maxDepth: 2 },
        });
        assert.equal(shallow.agentCount, 0);
        const deep = await (0, discovery_cli_1.runDiscoveryCli)({
            rootDir: root,
            orgId: 'org-x',
            privateKey,
            endpoint: 'https://proxy.my-cc.io/api/v1/manifests',
            transport: t.transport,
            sleep: noopSleep,
            discovery: { maxDepth: 10 },
        });
        assert.equal(deep.agentCount, 1);
    });
});
//# sourceMappingURL=discovery-cli.test.js.map