"use strict";
/**
 * RED-GREEN tests for F-NEW-PRODUCT-PACK-VERSIONING-ROLLBACK (W1.4).
 *
 * @connexum/ai-governance  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_versioning_1 = require("./pack-versioning");
const company_pack_builder_1 = require("./company-pack-builder");
const capability_classes_1 = require("./capability-classes");
function makePack(version, rules = [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' }]) {
    return (0, company_pack_builder_1.buildCompanyPack)({
        id: 'co-pack',
        name: `co-pack ${version}`,
        orgId: 'org-x',
        version,
        rules,
    });
}
(0, node_test_1.describe)('F-NEW-PRODUCT-PACK-VERSIONING-ROLLBACK (W1.4)', () => {
    (0, node_test_1.it)('publishVersion stores a record in draft state and emits VERSION_PUBLISHED', () => {
        const events = [];
        const store = new pack_versioning_1.PackVersionStore((e) => events.push(e));
        const rec = store.publishVersion(makePack('1.0.0'), 'alice', 'first cut');
        assert.equal(rec.state, 'draft');
        assert.equal(rec.publishedBy, 'alice');
        assert.equal(events.length, 1);
        assert.equal(events[0].kind, 'VERSION_PUBLISHED');
        assert.equal(events[0].version, '1.0.0');
    });
    (0, node_test_1.it)('setActive transitions version to active and emits VERSION_ACTIVATED', () => {
        const events = [];
        const store = new pack_versioning_1.PackVersionStore((e) => events.push(e));
        store.publishVersion(makePack('1.0.0'), 'alice');
        const rec = store.setActive('co-pack', '1.0.0', 'alice');
        assert.equal(rec.state, 'active');
        const active = store.getActiveVersion('co-pack');
        assert.equal(active?.version, '1.0.0');
        assert.ok(events.find((e) => e.kind === 'VERSION_ACTIVATED'));
    });
    (0, node_test_1.it)('setActive on a new version archives the previous active', () => {
        const events = [];
        const store = new pack_versioning_1.PackVersionStore((e) => events.push(e));
        store.publishVersion(makePack('1.0.0'), 'alice');
        store.setActive('co-pack', '1.0.0', 'alice');
        store.publishVersion(makePack('1.1.0'), 'alice');
        store.setActive('co-pack', '1.1.0', 'alice');
        const v1 = store.getVersion('co-pack', '1.0.0');
        assert.equal(v1?.state, 'archived');
        const active = store.getActiveVersion('co-pack');
        assert.equal(active?.version, '1.1.0');
        assert.ok(events.find((e) => e.kind === 'VERSION_ARCHIVED' && e.version === '1.0.0'));
    });
    (0, node_test_1.it)('setShadow records the version in shadow state without affecting active', () => {
        const events = [];
        const store = new pack_versioning_1.PackVersionStore((e) => events.push(e));
        store.publishVersion(makePack('1.0.0'), 'alice');
        store.setActive('co-pack', '1.0.0', 'alice');
        store.publishVersion(makePack('1.1.0'), 'alice');
        store.setShadow('co-pack', '1.1.0', 'alice');
        const active = store.getActiveVersion('co-pack');
        const shadow = store.getShadowVersion('co-pack');
        assert.equal(active?.version, '1.0.0');
        assert.equal(shadow?.version, '1.1.0');
        assert.equal(shadow?.state, 'shadow');
        assert.ok(events.find((e) => e.kind === 'VERSION_SHADOW_ENABLED'));
    });
    (0, node_test_1.it)('cannot setShadow on the active version', () => {
        const store = new pack_versioning_1.PackVersionStore();
        store.publishVersion(makePack('1.0.0'), 'alice');
        store.setActive('co-pack', '1.0.0', 'alice');
        assert.throws(() => store.setShadow('co-pack', '1.0.0', 'alice'), /currently active/);
    });
    (0, node_test_1.it)('rollback: archives current active and promotes named version, emits PACK_ROLLED_BACK', () => {
        const events = [];
        const store = new pack_versioning_1.PackVersionStore((e) => events.push(e));
        store.publishVersion(makePack('1.0.0'), 'alice');
        store.setActive('co-pack', '1.0.0', 'alice');
        store.publishVersion(makePack('1.1.0'), 'alice', 'broke something');
        store.setActive('co-pack', '1.1.0', 'alice');
        const rollbackEventBefore = events.find((e) => e.kind === 'PACK_ROLLED_BACK');
        assert.equal(rollbackEventBefore, undefined);
        store.rollback('co-pack', '1.0.0', 'ops-bob');
        assert.equal(store.getActiveVersion('co-pack')?.version, '1.0.0');
        assert.equal(store.getVersion('co-pack', '1.1.0')?.state, 'archived');
        const rolledEvent = events.find((e) => e.kind === 'PACK_ROLLED_BACK');
        assert.ok(rolledEvent);
        assert.equal(rolledEvent?.version, '1.0.0');
        assert.equal(rolledEvent?.previousVersion, '1.1.0');
        assert.equal(rolledEvent?.actor, 'ops-bob');
    });
    (0, node_test_1.it)('rollback to the currently-active version throws', () => {
        const store = new pack_versioning_1.PackVersionStore();
        store.publishVersion(makePack('1.0.0'), 'alice');
        store.setActive('co-pack', '1.0.0', 'alice');
        assert.throws(() => store.rollback('co-pack', '1.0.0', 'alice'), /already on version/);
    });
    (0, node_test_1.it)('publishing same version twice throws (versions are immutable)', () => {
        const store = new pack_versioning_1.PackVersionStore();
        store.publishVersion(makePack('1.0.0'), 'alice');
        assert.throws(() => store.publishVersion(makePack('1.0.0'), 'alice'), /already has version/);
    });
    (0, node_test_1.it)('invalid version string throws', () => {
        const store = new pack_versioning_1.PackVersionStore();
        assert.throws(() => store.publishVersion(makePack('not-a-version'), 'alice'), /Invalid version/);
    });
    (0, node_test_1.it)('missing actor throws on publish / setActive / setShadow / rollback (fail-closed audit)', () => {
        const store = new pack_versioning_1.PackVersionStore();
        assert.throws(() => store.publishVersion(makePack('1.0.0'), ''), /publishedBy is required/);
        store.publishVersion(makePack('1.0.0'), 'alice');
        assert.throws(() => store.setActive('co-pack', '1.0.0', ''), /actor is required/);
        store.setActive('co-pack', '1.0.0', 'alice');
        store.publishVersion(makePack('1.1.0'), 'alice');
        assert.throws(() => store.setShadow('co-pack', '1.1.0', ''), /actor is required/);
        assert.throws(() => store.rollback('co-pack', '1.1.0', ''), /actor is required/);
    });
    (0, node_test_1.it)('listVersions returns all versions in publish order', () => {
        const store = new pack_versioning_1.PackVersionStore();
        store.publishVersion(makePack('1.0.0'), 'alice');
        store.publishVersion(makePack('1.1.0'), 'alice');
        store.publishVersion(makePack('2.0.0'), 'alice');
        const versions = store.listVersions('co-pack').map((r) => r.version);
        assert.deepEqual(versions, ['1.0.0', '1.1.0', '2.0.0']);
    });
    (0, node_test_1.it)('promoting a shadow to active clears its shadow status', () => {
        const store = new pack_versioning_1.PackVersionStore();
        store.publishVersion(makePack('1.0.0'), 'alice');
        store.setActive('co-pack', '1.0.0', 'alice');
        store.publishVersion(makePack('1.1.0'), 'alice');
        store.setShadow('co-pack', '1.1.0', 'alice');
        assert.equal(store.getShadowVersion('co-pack')?.version, '1.1.0');
        store.setActive('co-pack', '1.1.0', 'alice');
        assert.equal(store.getShadowVersion('co-pack'), undefined);
        assert.equal(store.getActiveVersion('co-pack')?.version, '1.1.0');
    });
});
//# sourceMappingURL=pack-versioning.test.js.map