"use strict";
/**
 * RED-GREEN tests for the pack-publish workflow orchestrator.
 *
 * @connexum/ai-governance  F-NEW-PRODUCT-AGENT-VISIBILITY-CLUSTER-2026-05-12
 */
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_publish_workflow_1 = require("./pack-publish-workflow");
const pack_versioning_1 = require("./pack-versioning");
const baseManifest = (over = {}) => ({
    agentId: 'a',
    tools: ['Bash', 'WebFetch'],
    hooks: { preToolUse: true, postToolUse: true, postResponse: true },
    llmProvider: 'connexum-proxy/anthropic',
    pluginIntegrityVerified: true,
    rateLimitsConfigured: true,
    declaredApprovalGates: [],
    declaredForbiddenCapabilities: [],
    ...over,
});
(0, node_test_1.describe)('publishGeneratedPack workflow', () => {
    (0, node_test_1.it)('scan -> auto-pack -> publish (draft default) returns full result', () => {
        const result = (0, pack_publish_workflow_1.publishGeneratedPack)({
            manifest: baseManifest(),
            publishedBy: 'alice',
            autoPack: { acceptAllSuggestedRules: true },
        });
        assert.ok(result.autoPack.pack.id.startsWith('auto-pack-'));
        assert.equal(result.versionRecord.state, 'draft');
        assert.equal(result.versionRecord.publishedBy, 'alice');
        assert.ok(result.events.some((e) => e.kind === 'VERSION_PUBLISHED'));
    });
    (0, node_test_1.it)('postPublish: active transitions the version to active', () => {
        const result = (0, pack_publish_workflow_1.publishGeneratedPack)({
            manifest: baseManifest(),
            publishedBy: 'alice',
            autoPack: { acceptAllSuggestedRules: true },
            postPublish: 'active',
        });
        assert.equal(result.versionRecord.state, 'active');
        assert.ok(result.events.some((e) => e.kind === 'VERSION_ACTIVATED'));
    });
    (0, node_test_1.it)('postPublish: shadow places the version in shadow state', () => {
        const result = (0, pack_publish_workflow_1.publishGeneratedPack)({
            manifest: baseManifest(),
            publishedBy: 'alice',
            autoPack: { acceptAllSuggestedRules: true },
            postPublish: 'shadow',
        });
        assert.equal(result.versionRecord.state, 'shadow');
        assert.ok(result.events.some((e) => e.kind === 'VERSION_SHADOW_ENABLED'));
    });
    (0, node_test_1.it)('default changelog includes finding counts + score + acceptance ratio', () => {
        const result = (0, pack_publish_workflow_1.publishGeneratedPack)({
            manifest: baseManifest(),
            publishedBy: 'alice',
            autoPack: { acceptAllSuggestedRules: true },
        });
        assert.match(result.versionRecord.changelog ?? '', /Findings:/);
        assert.match(result.versionRecord.changelog ?? '', /Build-structure score/);
        assert.match(result.versionRecord.changelog ?? '', /Rules accepted/);
    });
    (0, node_test_1.it)('custom changelog overrides the default', () => {
        const result = (0, pack_publish_workflow_1.publishGeneratedPack)({
            manifest: baseManifest(),
            publishedBy: 'alice',
            autoPack: { acceptAllSuggestedRules: true },
            changelog: 'manual override',
        });
        assert.equal(result.versionRecord.changelog, 'manual override');
    });
    (0, node_test_1.it)('missing publishedBy throws (fail-closed)', () => {
        assert.throws(() => (0, pack_publish_workflow_1.publishGeneratedPack)({
            manifest: baseManifest(),
            publishedBy: '',
            autoPack: { acceptAllSuggestedRules: true },
        }), /publishedBy is required/);
    });
    (0, node_test_1.it)('caller-supplied versionStore is used; subsequent publishes share state', () => {
        const events = [];
        const store = new pack_versioning_1.PackVersionStore((e) => events.push(e));
        // Publish v1 of agent-1, set active.
        const r1 = (0, pack_publish_workflow_1.publishGeneratedPack)({
            manifest: { ...baseManifest({ agentId: 'agent-1' }) },
            publishedBy: 'alice',
            autoPack: { acceptAllSuggestedRules: true, version: '1.0.0' },
            versionStore: store,
            postPublish: 'active',
        });
        assert.equal(r1.versionRecord.state, 'active');
        // Publish v2 of the SAME agent — strictest-wins applies inside the
        // generator; the version store sees two distinct versions.
        const r2 = (0, pack_publish_workflow_1.publishGeneratedPack)({
            manifest: { ...baseManifest({ agentId: 'agent-1' }) },
            publishedBy: 'alice',
            autoPack: { acceptAllSuggestedRules: true, version: '1.1.0' },
            versionStore: store,
            postPublish: 'active',
        });
        assert.equal(r2.versionRecord.state, 'active');
        const versions = store.listVersions(r1.autoPack.pack.id).map((v) => v.version);
        assert.deepEqual(versions, ['1.0.0', '1.1.0']);
        // After r2's setActive, v1 should be archived.
        assert.equal(store.getVersion(r1.autoPack.pack.id, '1.0.0')?.state, 'archived');
        assert.equal(store.getActiveVersion(r1.autoPack.pack.id)?.version, '1.1.0');
    });
    (0, node_test_1.it)('events array contains exactly the publish event when postPublish is draft', () => {
        const result = (0, pack_publish_workflow_1.publishGeneratedPack)({
            manifest: baseManifest(),
            publishedBy: 'alice',
            autoPack: { acceptAllSuggestedRules: true },
            // postPublish defaults to draft
        });
        const publishedEvents = result.events.filter((e) => e.kind === 'VERSION_PUBLISHED');
        const activatedEvents = result.events.filter((e) => e.kind === 'VERSION_ACTIVATED');
        assert.equal(publishedEvents.length, 1);
        assert.equal(activatedEvents.length, 0);
    });
    (0, node_test_1.it)('end-to-end: Lethal Trifecta manifest -> pack with proposed rules -> publish + active', () => {
        const result = (0, pack_publish_workflow_1.publishGeneratedPack)({
            manifest: baseManifest({ tools: ['WebFetch', 'Bash', 'postgres_query'] }),
            publishedBy: 'alice',
            autoPack: { acceptAllSuggestedRules: true },
            postPublish: 'active',
        });
        assert.ok(result.autoPack.acceptedRules.length >= 2);
        assert.ok(result.autoPack.reviewRequiredRisks.some((r) => r.startsWith('A01')));
        assert.equal(result.versionRecord.state, 'active');
    });
    // F-NEW-CLUSTER-UNWIRED-STUBS-2026-05-12 integration #1 — validator gate
    (0, node_test_1.it)('validation: successful publish returns the ValidationReport with ok=true', () => {
        const result = (0, pack_publish_workflow_1.publishGeneratedPack)({
            manifest: baseManifest(),
            publishedBy: 'alice',
            autoPack: { acceptAllSuggestedRules: true },
        });
        assert.equal(result.validation.ok, true);
        assert.deepEqual(result.validation.errors, []);
    });
    (0, node_test_1.it)('validation: invalid pack id throws PackValidationError carrying the report', () => {
        // packIdPrefix with whitespace -> built id "auto-pack-bad id-a" fails ID_RE
        let caught;
        try {
            (0, pack_publish_workflow_1.publishGeneratedPack)({
                manifest: baseManifest(),
                publishedBy: 'alice',
                autoPack: { acceptAllSuggestedRules: true, packIdPrefix: 'auto-pack-bad id' },
            });
        }
        catch (e) {
            caught = e;
        }
        assert.ok(caught instanceof pack_publish_workflow_1.PackValidationError);
        const err = caught;
        assert.equal(err.report.ok, false);
        assert.ok(err.report.errors.some((i) => i.field === 'id' && i.code === 'INVALID_FORMAT'));
    });
    (0, node_test_1.it)('validation: store is NOT mutated when validation fails (fail-closed)', () => {
        const events = [];
        const store = new pack_versioning_1.PackVersionStore((e) => events.push(e));
        try {
            (0, pack_publish_workflow_1.publishGeneratedPack)({
                manifest: baseManifest(),
                publishedBy: 'alice',
                autoPack: { acceptAllSuggestedRules: true, packIdPrefix: 'auto-pack-bad id' },
                versionStore: store,
            });
        }
        catch {
            // expected
        }
        // No VERSION_PUBLISHED event reached the store sink.
        assert.equal(events.filter((e) => e.kind === 'VERSION_PUBLISHED').length, 0);
    });
});
//# sourceMappingURL=pack-publish-workflow.test.js.map