"use strict";
/**
 * RED-GREEN tests for signed-manifest push (W2.x infrastructure).
 *
 * @connexum/ai-governance  F-NEW-PRODUCT-AGENT-DISCOVERY-CLI push transport
 */
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 signed_manifest_1 = require("./signed-manifest");
const baseManifest = () => ({
    agentId: 'agent-1',
    name: 'Agent 1',
    tools: ['Bash', 'WebFetch'],
    hooks: { preToolUse: true, postToolUse: true, postResponse: true },
});
(0, node_test_1.describe)('signed-manifest push transport', () => {
    (0, node_test_1.it)('sign and verify with the same keypair succeeds', () => {
        const { publicKey, privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const signed = (0, signed_manifest_1.signManifest)(baseManifest(), 'org-x', privateKey);
        const payload = (0, signed_manifest_1.verifySignedManifest)(signed, publicKey, { expectedOrgId: 'org-x' });
        assert.equal(payload.orgId, 'org-x');
        assert.equal(payload.manifest.agentId, 'agent-1');
        assert.equal(payload.nonce.length, 32);
    });
    (0, node_test_1.it)('export/import public key round-trips', () => {
        const { publicKey, privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const signed = (0, signed_manifest_1.signManifest)(baseManifest(), 'org-x', privateKey);
        const exported = (0, signed_manifest_1.exportPublicKey)(publicKey);
        const reImported = (0, signed_manifest_1.importPublicKey)(exported);
        const payload = (0, signed_manifest_1.verifySignedManifest)(signed, reImported, { expectedOrgId: 'org-x' });
        assert.equal(payload.orgId, 'org-x');
    });
    (0, node_test_1.it)('verification rejects a tampered payload (INVALID_SIGNATURE)', () => {
        const { publicKey, privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const signed = (0, signed_manifest_1.signManifest)(baseManifest(), 'org-x', privateKey);
        // Tamper with the agentId after signing.
        const tampered = {
            ...signed,
            payload: {
                ...signed.payload,
                manifest: { ...signed.payload.manifest, agentId: 'attacker-agent' },
            },
        };
        assert.throws(() => (0, signed_manifest_1.verifySignedManifest)(tampered, publicKey, { expectedOrgId: 'org-x' }), (e) => e instanceof signed_manifest_1.ManifestSignatureError && e.code === 'INVALID_SIGNATURE');
    });
    (0, node_test_1.it)('verification rejects when signed by a different private key (INVALID_SIGNATURE)', () => {
        const { privateKey: pkA } = (0, signed_manifest_1.generateOrgKeyPair)();
        const { publicKey: pubB } = (0, signed_manifest_1.generateOrgKeyPair)();
        const signed = (0, signed_manifest_1.signManifest)(baseManifest(), 'org-x', pkA);
        assert.throws(() => (0, signed_manifest_1.verifySignedManifest)(signed, pubB, { expectedOrgId: 'org-x' }), (e) => e instanceof signed_manifest_1.ManifestSignatureError && e.code === 'INVALID_SIGNATURE');
    });
    (0, node_test_1.it)('verification rejects an org-id mismatch (ORG_MISMATCH)', () => {
        const { publicKey, privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const signed = (0, signed_manifest_1.signManifest)(baseManifest(), 'org-x', privateKey);
        assert.throws(() => (0, signed_manifest_1.verifySignedManifest)(signed, publicKey, { expectedOrgId: 'org-y' }), (e) => e instanceof signed_manifest_1.ManifestSignatureError && e.code === 'ORG_MISMATCH');
    });
    (0, node_test_1.it)('verification rejects an expired payload (EXPIRED)', () => {
        const { publicKey, privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const past = Date.now() - (signed_manifest_1.DEFAULT_MAX_AGE_SECONDS + 60) * 1000;
        const signed = (0, signed_manifest_1.signManifest)(baseManifest(), 'org-x', privateKey, { nowMs: past });
        assert.throws(() => (0, signed_manifest_1.verifySignedManifest)(signed, publicKey, { expectedOrgId: 'org-x' }), (e) => e instanceof signed_manifest_1.ManifestSignatureError && e.code === 'EXPIRED');
    });
    (0, node_test_1.it)('verification rejects a future-dated payload beyond skew tolerance', () => {
        const { publicKey, privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const future = Date.now() + (signed_manifest_1.DEFAULT_MAX_AGE_SECONDS + 60) * 1000;
        const signed = (0, signed_manifest_1.signManifest)(baseManifest(), 'org-x', privateKey, { nowMs: future });
        assert.throws(() => (0, signed_manifest_1.verifySignedManifest)(signed, publicKey, { expectedOrgId: 'org-x' }), (e) => e instanceof signed_manifest_1.ManifestSignatureError && e.code === 'MALFORMED');
    });
    (0, node_test_1.it)('verification accepts a recent payload within max-age', () => {
        const { publicKey, privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const recent = Date.now() - 60 * 1000; // 1 minute ago
        const signed = (0, signed_manifest_1.signManifest)(baseManifest(), 'org-x', privateKey, { nowMs: recent });
        const payload = (0, signed_manifest_1.verifySignedManifest)(signed, publicKey, { expectedOrgId: 'org-x' });
        assert.equal(payload.orgId, 'org-x');
    });
    (0, node_test_1.it)('different manifests produce different signatures (nonce + payload entropy)', () => {
        const { privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const s1 = (0, signed_manifest_1.signManifest)(baseManifest(), 'org-x', privateKey);
        const s2 = (0, signed_manifest_1.signManifest)(baseManifest(), 'org-x', privateKey);
        assert.notEqual(s1.signature, s2.signature, 'nonce should make signatures non-deterministic');
        assert.notEqual(s1.payload.nonce, s2.payload.nonce);
    });
    (0, node_test_1.it)('canonical JSON makes signatures key-order-independent (forward compat across SDKs)', () => {
        const { publicKey, privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const m1 = baseManifest();
        const signed = (0, signed_manifest_1.signManifest)(m1, 'org-x', privateKey);
        // Reconstruct the manifest with different key insertion order; the
        // canonical serialiser sorts keys, so verification must still succeed.
        const reorderedManifest = {
            tools: m1.tools,
            hooks: m1.hooks,
            name: m1.name,
            agentId: m1.agentId,
        };
        const signedReorder = {
            ...signed,
            payload: { ...signed.payload, manifest: reorderedManifest },
        };
        // Same data; expect signature to still verify.
        const payload = (0, signed_manifest_1.verifySignedManifest)(signedReorder, publicKey, { expectedOrgId: 'org-x' });
        assert.equal(payload.orgId, 'org-x');
    });
    (0, node_test_1.it)('empty orgId on sign throws MALFORMED', () => {
        const { privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        assert.throws(() => (0, signed_manifest_1.signManifest)(baseManifest(), '', privateKey), (e) => e instanceof signed_manifest_1.ManifestSignatureError && e.code === 'MALFORMED');
    });
    (0, node_test_1.it)('signature is a non-empty base64url string', () => {
        const { privateKey } = (0, signed_manifest_1.generateOrgKeyPair)();
        const signed = (0, signed_manifest_1.signManifest)(baseManifest(), 'org-x', privateKey);
        assert.ok(signed.signature.length > 0);
        assert.match(signed.signature, /^[A-Za-z0-9_-]+$/);
        assert.equal(signed.alg, 'ed25519');
    });
});
//# sourceMappingURL=signed-manifest.test.js.map