// Pure unit tests for the premium-MCP discovery loop. Real tmp dir, no mocks —
// the discovery function is fs-bound and a mock would just re-state the schema.
//
// Tests are ephemeral per the sprint workflow.
import test from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { findPremiumMcpDirs } from "../lib/premium-mcp-discover.js";
function makeTree(installDir, files) {
    for (const [rel, contents] of Object.entries(files)) {
        const full = join(installDir, rel);
        mkdirSync(join(full, ".."), { recursive: true });
        writeFileSync(full, contents);
    }
}
test("findPremiumMcpDirs: returns empty when premium-plugins/ is absent", () => {
    const dir = mkdtempSync(join(tmpdir(), "premium-mcp-"));
    try {
        assert.deepEqual(findPremiumMcpDirs(dir), []);
    }
    finally {
        rmSync(dir, { recursive: true, force: true });
    }
});
test("findPremiumMcpDirs: discovers loop MCP under real-agent bundle", () => {
    const dir = mkdtempSync(join(tmpdir(), "premium-mcp-"));
    try {
        makeTree(dir, {
            "premium-plugins/real-agent/plugins/loop/mcp/package.json": "{}",
        });
        assert.deepEqual(findPremiumMcpDirs(dir), [
            join(dir, "premium-plugins/real-agent/plugins/loop/mcp"),
        ]);
    }
    finally {
        rmSync(dir, { recursive: true, force: true });
    }
});
test("findPremiumMcpDirs: ignores plugin dir with no mcp/package.json", () => {
    const dir = mkdtempSync(join(tmpdir(), "premium-mcp-"));
    try {
        makeTree(dir, {
            "premium-plugins/real-agent/plugins/loop/mcp/package.json": "{}",
            "premium-plugins/real-agent/plugins/no-mcp/README.md": "noop",
        });
        assert.deepEqual(findPremiumMcpDirs(dir), [
            join(dir, "premium-plugins/real-agent/plugins/loop/mcp"),
        ]);
    }
    finally {
        rmSync(dir, { recursive: true, force: true });
    }
});
test("findPremiumMcpDirs: covers multiple bundles and plugins by construction", () => {
    const dir = mkdtempSync(join(tmpdir(), "premium-mcp-"));
    try {
        makeTree(dir, {
            "premium-plugins/real-agent/plugins/loop/mcp/package.json": "{}",
            "premium-plugins/real-agent/plugins/other/mcp/package.json": "{}",
            "premium-plugins/another-bundle/plugins/foo/mcp/package.json": "{}",
        });
        assert.deepEqual(findPremiumMcpDirs(dir), [
            join(dir, "premium-plugins/another-bundle/plugins/foo/mcp"),
            join(dir, "premium-plugins/real-agent/plugins/loop/mcp"),
            join(dir, "premium-plugins/real-agent/plugins/other/mcp"),
        ]);
    }
    finally {
        rmSync(dir, { recursive: true, force: true });
    }
});
test("findPremiumMcpDirs: skips bundle missing plugins/ subdir", () => {
    const dir = mkdtempSync(join(tmpdir(), "premium-mcp-"));
    try {
        makeTree(dir, {
            "premium-plugins/empty-bundle/README.md": "noop",
        });
        assert.deepEqual(findPremiumMcpDirs(dir), []);
    }
    finally {
        rmSync(dir, { recursive: true, force: true });
    }
});
test("findPremiumMcpDirs: discovers standalone MCP at premium-plugins/<bundle>/mcp/", () => {
    const dir = mkdtempSync(join(tmpdir(), "premium-mcp-"));
    try {
        makeTree(dir, {
            "premium-plugins/writer-craft/mcp/package.json": "{}",
        });
        assert.deepEqual(findPremiumMcpDirs(dir), [
            join(dir, "premium-plugins/writer-craft/mcp"),
        ]);
    }
    finally {
        rmSync(dir, { recursive: true, force: true });
    }
});
test("findPremiumMcpDirs: standalone + bundle-sub coexist under different bundles", () => {
    const dir = mkdtempSync(join(tmpdir(), "premium-mcp-"));
    try {
        makeTree(dir, {
            "premium-plugins/writer-craft/mcp/package.json": "{}",
            "premium-plugins/real-agent/plugins/loop/mcp/package.json": "{}",
        });
        assert.deepEqual(findPremiumMcpDirs(dir), [
            join(dir, "premium-plugins/real-agent/plugins/loop/mcp"),
            join(dir, "premium-plugins/writer-craft/mcp"),
        ]);
    }
    finally {
        rmSync(dir, { recursive: true, force: true });
    }
});
test("findPremiumMcpDirs: ignores standalone bundle with no mcp/ dir", () => {
    const dir = mkdtempSync(join(tmpdir(), "premium-mcp-"));
    try {
        makeTree(dir, {
            "premium-plugins/teaching/PLUGIN.md": "---\nname: teaching\n---\n",
        });
        assert.deepEqual(findPremiumMcpDirs(dir), []);
    }
    finally {
        rmSync(dir, { recursive: true, force: true });
    }
});
