"use strict";
/**
 * test_profile_toolset_mcp.ts — Stories 5.2 + 5.3 + 5.4
 *
 * Combined regression for ProfileManager + ToolsetRegistry +
 * ExternalCatalogRegistry.import.
 */
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 fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const ProfileManager_1 = require("./cli/ProfileManager");
const ToolsetRegistry_1 = require("./orchestrator/toolset/ToolsetRegistry");
const ToolsetSchema_1 = require("./orchestrator/toolset/ToolsetSchema");
const ExternalCatalogRegistry_1 = require("./orchestrator/ExternalCatalogRegistry");
function assertTrue(cond, label) {
    if (!cond)
        throw new Error(`ASSERT_FAILED[${label}]`);
}
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'profile-toolset-mcp-'));
try {
    // ── ProfileManager ─────────────────────────────────────────────
    const pm = new ProfileManager_1.ProfileManager({ stateDir: tmp });
    assertTrue(pm.list().length === 0, 'profiles start empty');
    const safe = pm.create({
        id: 'sandboxed',
        name: 'Sandboxed',
        description: 'Locked-down profile',
        toolsetAllowed: ['file', 'memory'],
    });
    assertTrue(safe.id === 'sandboxed', '[5.2] create returns profile');
    assertTrue(safe.memoryNamespace === 'profile-sandboxed', '[5.2] auto-namespace');
    assertTrue(pm.list().length === 1, '[5.2] list includes new profile');
    console.log('[5.2] ProfileManager.create + list ✓');
    // use + active
    const useRes = pm.use('sandboxed');
    assertTrue(useRes.ok, '[5.2] use ok');
    const { profileId, profile } = pm.active();
    assertTrue(profileId === 'sandboxed' && profile?.toolsetAllowed.length === 2, '[5.2] active profile resolved');
    console.log('[5.2] ProfileManager.use + active ✓');
    // invalid id rejected
    let invalidIdRejected = false;
    try {
        pm.create({ id: 'BAD ID', name: 'x' });
    }
    catch (e) {
        invalidIdRejected = e.code === 'invalid_profile_id';
    }
    assertTrue(invalidIdRejected, '[5.2] invalid_profile_id surfaced');
    console.log('[5.2] invalid id rejected ✓');
    // import/export
    const exported = pm.export('sandboxed');
    assertTrue(exported !== null && exported.id === 'sandboxed', '[5.2] export round-trip');
    const importRes = pm.import({ ...exported, id: 'imported-from-export' });
    assertTrue(importRes.ok, '[5.2] import ok');
    assertTrue(pm.list().length === 2, '[5.2] imported visible in list');
    console.log('[5.2] ProfileManager.export + import ✓');
    // ── ToolsetRegistry ─────────────────────────────────────────────
    const ts = new ToolsetRegistry_1.ToolsetRegistry({ profileManager: pm });
    assertTrue(ts.list().length === ToolsetSchema_1.ALL_TOOLSETS.length, '[5.3] list returns all categories');
    const fileDecision = ts.isAllowed('file');
    assertTrue(fileDecision.allowed, '[5.3] file allowed on sandboxed profile');
    const browserDecision = ts.isAllowed('browser');
    assertTrue(!browserDecision.allowed, '[5.3] browser denied on sandboxed profile');
    console.log('[5.3] ToolsetRegistry decisions ✓');
    // Wildcard profile allows all
    pm.create({ id: 'powerful', name: 'Powerful', toolsetAllowed: ['*'] });
    pm.use('powerful');
    const ts2 = new ToolsetRegistry_1.ToolsetRegistry({ profileManager: pm });
    for (const c of ToolsetSchema_1.ALL_TOOLSETS) {
        assertTrue(ts2.isAllowed(c).allowed, `[5.3] wildcard allows ${c}`);
    }
    console.log('[5.3] wildcard profile allows all ✓');
    // Empty allowlist denies everything
    pm.create({ id: 'no-tools', name: 'No tools', toolsetAllowed: [] });
    const ts3 = new ToolsetRegistry_1.ToolsetRegistry({ profileManager: pm });
    assertTrue(!ts3.isAllowed('file', 'no-tools').allowed, '[5.3] empty allowlist denies file');
    console.log('[5.3] empty allowlist denies all ✓');
    // ── ExternalCatalogRegistry.import ──────────────────────────────
    const reg = new ExternalCatalogRegistry_1.ExternalCatalogRegistry();
    const ok = reg.import('github', 'some-pack');
    assertTrue(ok.ok, '[5.4] import-allowed source returns ok');
    const refOnly = reg.import('skills-sh', 'some-pack');
    assertTrue(!refOnly.ok && refOnly.error === 'source_is_reference_only', '[5.4] reference-only refused');
    const missing = reg.import('nonexistent', 'some-pack');
    assertTrue(!missing.ok && missing.error === 'source_not_found', '[5.4] missing source returns source_not_found');
    console.log('[5.4] ExternalCatalogRegistry.import ✓');
}
finally {
    fs.rmSync(tmp, { recursive: true, force: true });
}
console.log('TEST_PROFILE_TOOLSET_MCP_OK');
