{
  "id": "CON-005",
  "name": "Contact UPDATE via REST emits V2 audit head row with Entrypoint=REST_API",
  "description": "Regression test for issue #1917. PR #1850 shipped the V2 audit log pipeline (head row in _System_AuditLog with FieldName='__record__', Entrypoint stamped from AuditEntrypointMiddleware). A staging canary observed zero REST_API head rows, but the window was compromised by schema drift (#1916) and a mid-window env wipe (#1915). This case deterministically proves: a plain PATCH on an SObject with no BeforeSave / record-triggered flow (Contact.Description) produces exactly one V2 head row stamped Entrypoint='REST_API'. Requires AUDIT_V2_ENABLED=true in the backend env under test.",
  "object": "Contact",
  "setup": [
    {
      "name": "Seed Contact for audit v2 regression",
      "type": "sql",
      "sql": "INSERT INTO Contact (Id, LastName, Email, Phone, Description) VALUES ('0035g00000CON005AA0', 'AuditV2_TestContact', 'con005@test.com', '5550105', 'before') ON DUPLICATE KEY UPDATE LastName=VALUES(LastName), Description=VALUES(Description)",
      "id_var": "seed_Contact_0",
      "id_value": "0035g00000CON005AA0",
      "object": "Contact"
    },
    {
      "name": "Clear any pre-existing audit rows for this RecordId (idempotent reruns)",
      "type": "sql",
      "sql": "DELETE FROM _System_AuditLog WHERE RecordId = '0035g00000CON005AA0'"
    }
  ],
  "request_steps": [
    {
      "name": "PATCH Contact.Description (no BeforeSave flow on Contact at platform level)",
      "type": "http",
      "method": "PATCH",
      "endpoint": "/services/data/v63.0/sobjects/Contact/0035g00000CON005AA0",
      "payload": {
        "Description": "after-audit-v2-rest-wiring-1917"
      },
      "expects": { "status_code": 204 }
    },
    {
      "name": "Read audit log for this RecordId via /admin/audit/log (ORDER BY ChangedAt DESC)",
      "type": "http",
      "method": "GET",
      "endpoint": "/services/data/v63.0/admin/audit/log?object=Contact&recordId=0035g00000CON005AA0&limit=50",
      "expects": { "status_code": 200 },
      "response_assertions": [
        {
          "field": "records.0.FieldName",
          "operator": "eq",
          "value": "__record__"
        },
        {
          "field": "records.0.Entrypoint",
          "operator": "eq",
          "value": "REST_API"
        },
        {
          "field": "records.0.Action",
          "operator": "eq",
          "value": "UPDATE"
        },
        {
          "field": "records.0.ObjectApiName",
          "operator": "eq",
          "value": "Contact"
        },
        {
          "field": "records.0.RecordId",
          "operator": "eq",
          "value": "0035g00000CON005AA0"
        },
        {
          "field": "records.0.RequestId",
          "operator": "is_not_null",
          "value": true
        },
        {
          "field": "records.0.ChangesJSON",
          "operator": "contains",
          "value": "Description"
        },
        {
          "field": "records.0.Outcome",
          "operator": "eq",
          "value": "SUCCESS"
        }
      ]
    }
  ],
  "teardown": [
    {
      "type": "sql",
      "sql": "DELETE FROM _System_AuditLog WHERE RecordId = '0035g00000CON005AA0'"
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Contact WHERE Id = '0035g00000CON005AA0'"
    }
  ],
  "priority": "P1",
  "bug_ref": "#1917",
  "scene": "contact"
}
