"use strict";
/**
 * RED-GREEN tests for pack-diff (W3.3 + W1.4 support).
 *
 * @connexum/ai-governance  F-NEW-PRODUCT-LIVE-PACK-EDITOR + F-NEW-PRODUCT-PACK-VERSIONING-ROLLBACK
 */
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 pack_diff_1 = require("./pack-diff");
const company_pack_builder_1 = require("./company-pack-builder");
const capability_classes_1 = require("./capability-classes");
(0, node_test_1.describe)('pack-diff', () => {
    (0, node_test_1.it)('identical packs produce noop diff', () => {
        const p = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.0.0',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' }],
        });
        const p2 = { ...p, version: '1.0.1' };
        const diff = (0, pack_diff_1.diffPacks)(p, p2);
        assert.equal(diff.isNoop, true);
        assert.equal(diff.changes.length, 0);
        assert.equal(diff.isolationChange, undefined);
    });
    (0, node_test_1.it)('added category appears as added', () => {
        const v1 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.0.0',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' }],
        });
        const v2 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.1.0',
            rules: [
                { capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' },
                { capability: capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC, action: 'REQUIRE_APPROVAL' },
            ],
        });
        const diff = (0, pack_diff_1.diffPacks)(v1, v2);
        assert.equal(diff.isNoop, false);
        assert.deepEqual(diff.added, ['capability:shell-exec']);
        assert.equal(diff.removed.length, 0);
        assert.equal(diff.changed.length, 0);
    });
    (0, node_test_1.it)('removed category appears as removed', () => {
        const v1 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.0.0',
            rules: [
                { capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' },
                { capability: capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC, action: 'REQUIRE_APPROVAL' },
            ],
        });
        const v2 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.1.0',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' }],
        });
        const diff = (0, pack_diff_1.diffPacks)(v1, v2);
        assert.deepEqual(diff.removed, ['capability:shell-exec']);
    });
    (0, node_test_1.it)('changed action appears as changed with previous + new values', () => {
        const v1 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.0.0',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC, action: 'REQUIRE_APPROVAL' }],
        });
        const v2 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.1.0',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC, action: 'BLOCK' }],
        });
        const diff = (0, pack_diff_1.diffPacks)(v1, v2);
        assert.equal(diff.changes.length, 1);
        const c = diff.changes[0];
        assert.equal(c.kind, 'changed');
        if (c.kind === 'changed') {
            assert.equal(c.previousAction, 'REQUIRE_APPROVAL');
            assert.equal(c.newAction, 'BLOCK');
        }
        assert.deepEqual(diff.changed, ['capability:shell-exec']);
    });
    (0, node_test_1.it)('isolation mode change is captured', () => {
        const v1 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.0.0',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' }],
            isolation: 'none',
        });
        const v2 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.1.0',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' }],
            isolation: 'sourced-only',
        });
        const diff = (0, pack_diff_1.diffPacks)(v1, v2);
        assert.deepEqual(diff.isolationChange, { from: 'none', to: 'sourced-only' });
        assert.equal(diff.isNoop, false);
    });
    (0, node_test_1.it)('mixed add + remove + change produces all three lists populated', () => {
        const v1 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.0.0',
            rules: [
                { capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' },
                { capability: capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC, action: 'REQUIRE_APPROVAL' },
            ],
        });
        const v2 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '2.0.0',
            rules: [
                { capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'WARN' }, // changed
                // SHELL_EXEC removed
                { capability: capability_classes_1.CAPABILITY_CLASS.FILESYSTEM_WRITE, action: 'BLOCK' }, // added
            ],
        });
        const diff = (0, pack_diff_1.diffPacks)(v1, v2);
        assert.deepEqual(diff.added, ['capability:filesystem-write']);
        assert.deepEqual(diff.removed, ['capability:shell-exec']);
        assert.deepEqual(diff.changed, ['capability:mcp']);
        assert.equal(diff.changes.length, 3);
    });
    (0, node_test_1.it)('changes list is sorted by category for stable diffs', () => {
        const v1 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.0.0',
            rules: [],
        });
        const v2 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.1.0',
            rules: [
                { capability: capability_classes_1.CAPABILITY_CLASS.WEB_FETCH, action: 'BLOCK' },
                { capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' },
                { capability: capability_classes_1.CAPABILITY_CLASS.FILESYSTEM_WRITE, action: 'BLOCK' },
            ],
        });
        const diff = (0, pack_diff_1.diffPacks)(v1, v2);
        // Sorted: capability:filesystem-write, capability:mcp, capability:web-fetch
        assert.deepEqual(diff.added, [
            'capability:filesystem-write',
            'capability:mcp',
            'capability:web-fetch',
        ]);
    });
    (0, node_test_1.it)('summarizeDiff produces compact human-readable text', () => {
        const v1 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.0.0',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' }],
        });
        const v2 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.1.0',
            rules: [
                { capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'WARN' },
                { capability: capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC, action: 'BLOCK' },
            ],
        });
        const s = (0, pack_diff_1.summarizeDiff)((0, pack_diff_1.diffPacks)(v1, v2));
        assert.match(s, /co: 1.0.0 -> 1.1.0/);
        assert.match(s, /1 added/);
        assert.match(s, /1 changed/);
    });
    (0, node_test_1.it)('summarizeDiff on a noop reports no functional changes', () => {
        const v1 = (0, company_pack_builder_1.buildCompanyPack)({
            id: 'co',
            name: 'co',
            orgId: 'org-x',
            version: '1.0.0',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' }],
        });
        const v2 = { ...v1, version: '1.0.1' };
        assert.match((0, pack_diff_1.summarizeDiff)((0, pack_diff_1.diffPacks)(v1, v2)), /no functional changes/);
    });
});
//# sourceMappingURL=pack-diff.test.js.map