"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_test_1 = __importDefault(require("node:test"));
const strict_1 = __importDefault(require("node:assert/strict"));
const audit_js_1 = require("../audit.js");
const baseInput = {
    schema: {
        labels: new Set(["Person", "Organization", "Task", "KnowledgeDocument"]),
        relationshipTypes: new Set(["KNOWS", "PARTICIPANT", "REFERENCES", "MENTIONS", "PART_OF"]),
    },
    agentName: "database-operator",
    sessionId: "session-abc",
    nodesCreated: 0,
    relsCreated: 0,
    orphanIds: [],
};
(0, node_test_1.default)("audit: clean write with provenance + known types yields zero warnings", () => {
    const cypher = `
    MATCH (p:Person {name: $name}), (o:Organization {name: $org})
    MERGE (p)-[r:KNOWS]->(o)
    SET r.createdAt = datetime(),
        r.createdByAgent = $agent,
        r.createdByTool = 'graph-cypher-write',
        r.createdBySession = $sessionId
  `;
    const warnings = (0, audit_js_1.auditCypherWrite)({ ...baseInput, cypher, relsCreated: 1 });
    strict_1.default.deepEqual(warnings, []);
});
(0, node_test_1.default)("audit: unknown edge type yields unknown-type-warning", () => {
    const cypher = "MATCH (a:Person), (b:Person) CREATE (a)-[:HAS_KNOWN]->(b)";
    const warnings = (0, audit_js_1.auditCypherWrite)({ ...baseInput, cypher, relsCreated: 1 });
    const w = warnings.find((x) => x.kind === "unknown-type-warning");
    strict_1.default.ok(w, "expected unknown-type-warning");
    strict_1.default.equal(w.kind, "unknown-type-warning");
    if (w.kind === "unknown-type-warning") {
        strict_1.default.equal(w.type, "HAS_KNOWN");
    }
});
(0, node_test_1.default)("audit: known APOC edge in YIELD does not trip unknown-type", () => {
    const cypher = "MATCH (a:Person), (b:Person) WHERE a.email = b.email AND id(a) < id(b) WITH a, b CALL apoc.refactor.mergeNodes([a, b]) YIELD node RETURN node";
    const warnings = (0, audit_js_1.auditCypherWrite)({ ...baseInput, cypher });
    const unknownTypeWarn = warnings.find((x) => x.kind === "unknown-type-warning");
    strict_1.default.equal(unknownTypeWarn, undefined);
});
(0, node_test_1.default)("audit: CREATE with no provenance stamps yields missing-provenance-warning", () => {
    const cypher = "CREATE (n:Person {name: 'Foo'})";
    const warnings = (0, audit_js_1.auditCypherWrite)({ ...baseInput, cypher, nodesCreated: 1 });
    const w = warnings.find((x) => x.kind === "missing-provenance-warning");
    strict_1.default.ok(w, "expected missing-provenance-warning");
    if (w.kind === "missing-provenance-warning") {
        strict_1.default.equal(w.created, 1);
        strict_1.default.equal(w.stamped, 0);
    }
});
(0, node_test_1.default)("audit: MERGE with createdBy* stamps does not trip missing-provenance", () => {
    const cypher = "MERGE (n:Person {email: $email}) ON CREATE SET n.createdByAgent = $agent, n.createdBySession = $sid";
    const warnings = (0, audit_js_1.auditCypherWrite)({ ...baseInput, cypher, nodesCreated: 1 });
    const w = warnings.find((x) => x.kind === "missing-provenance-warning");
    strict_1.default.equal(w, undefined);
});
(0, node_test_1.default)("audit: provenance check ignores stamps inside string literals", () => {
    const cypher = "CREATE (n:Person {bio: 'I have createdByAgent in my bio'})";
    const warnings = (0, audit_js_1.auditCypherWrite)({ ...baseInput, cypher, nodesCreated: 1 });
    const w = warnings.find((x) => x.kind === "missing-provenance-warning");
    strict_1.default.ok(w, "expected missing-provenance-warning despite stamp-like substring in literal");
});
(0, node_test_1.default)("audit: idempotent MERGE that matched existing node (nodesCreated=0) emits no missing-provenance-warning", () => {
    // Reviewer-flagged false-positive case. MERGE (n:Person {email}) without
    // ON CREATE SET stamps — static count = 1, stamps = 0. If the node
    // already existed, upstream reports nodesCreated=0 and no provenance was
    // needed; the audit must not warn.
    const cypher = "MERGE (n:Person {email: $email})";
    const warnings = (0, audit_js_1.auditCypherWrite)({ ...baseInput, cypher, nodesCreated: 0 });
    const w = warnings.find((x) => x.kind === "missing-provenance-warning");
    strict_1.default.equal(w, undefined, "must not warn when nodesCreated=0");
});
(0, node_test_1.default)("audit: orphanIds populated yields orphan-warning", () => {
    const warnings = (0, audit_js_1.auditCypherWrite)({
        ...baseInput,
        cypher: "CREATE (n:Person)",
        nodesCreated: 1,
        orphanIds: ["4:abc:1", "4:abc:2"],
    });
    const w = warnings.find((x) => x.kind === "orphan-warning");
    strict_1.default.ok(w, "expected orphan-warning");
    if (w.kind === "orphan-warning") {
        strict_1.default.deepEqual(w.orphanIds, ["4:abc:1", "4:abc:2"]);
    }
});
(0, node_test_1.default)("audit: empty orphanIds emits no orphan-warning", () => {
    const warnings = (0, audit_js_1.auditCypherWrite)({
        ...baseInput,
        cypher: "MATCH (a:Person), (b:Person) CREATE (a)-[:KNOWS]->(b)",
        relsCreated: 1,
        orphanIds: [],
    });
    const w = warnings.find((x) => x.kind === "orphan-warning");
    strict_1.default.equal(w, undefined);
});
(0, node_test_1.default)("formatAuditLine: accepted line includes counters + provenance", () => {
    const line = (0, audit_js_1.formatAuditLine)({
        kind: "accepted",
        cypherPrefix: "MATCH (a:Person) CREATE",
        nodesCreated: 1,
        relsCreated: 4,
        agentName: "database-operator",
        sessionId: "abc-123",
    });
    strict_1.default.match(line, /^\[graph-cypher-write\] accepted /);
    strict_1.default.match(line, /nodesCreated=1/);
    strict_1.default.match(line, /relsCreated=4/);
    strict_1.default.match(line, /agentName=database-operator/);
    strict_1.default.match(line, /sessionId=abc-123/);
});
(0, node_test_1.default)("formatAuditLine: orphan-warning line lists orphanIds", () => {
    const line = (0, audit_js_1.formatAuditLine)({
        kind: "orphan-warning",
        cypherPrefix: "CREATE (n:Person)",
        orphanIds: ["4:abc:1", "4:abc:2"],
    });
    strict_1.default.match(line, /^\[graph-cypher-write\] orphan-warning /);
    strict_1.default.match(line, /orphanIds=4:abc:1,4:abc:2/);
});
(0, node_test_1.default)("formatAuditLine: unknown-type-warning names the type", () => {
    const line = (0, audit_js_1.formatAuditLine)({
        kind: "unknown-type-warning",
        cypherPrefix: "CREATE (a)-[:HAS_KNOWN]->(b)",
        type: "HAS_KNOWN",
    });
    strict_1.default.match(line, /^\[graph-cypher-write\] unknown-type-warning /);
    strict_1.default.match(line, /type=HAS_KNOWN/);
});
(0, node_test_1.default)("formatAuditLine: missing-provenance-warning shows created vs stamped counts", () => {
    const line = (0, audit_js_1.formatAuditLine)({
        kind: "missing-provenance-warning",
        cypherPrefix: "CREATE (n:Person)",
        created: 3,
        stamped: 1,
    });
    strict_1.default.match(line, /^\[graph-cypher-write\] missing-provenance-warning /);
    strict_1.default.match(line, /created=3/);
    strict_1.default.match(line, /stamped=1/);
});
//# sourceMappingURL=audit.test.js.map