{
  "id": "c1e028d9b8414fad",
  "name": "n8n-check: Keep the eight relevant retrieval rows",
  "active": false,
  "nodes": [
    {
      "id": "2b1b1a2f-7bc2-438a-9c00-6073f48d2ad3",
      "name": "n8n-check start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -400,
        0
      ],
      "parameters": {}
    },
    {
      "id": "retrieved",
      "name": "Retrieved rows",
      "position": [
        -200,
        0
      ],
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "return JSON.parse(\"[{\\\"id\\\":\\\"policy-01\\\",\\\"score\\\":0.9,\\\"source\\\":\\\"current-index\\\"},{\\\"id\\\":\\\"policy-02\\\",\\\"score\\\":0.9,\\\"source\\\":\\\"current-index\\\"},{\\\"id\\\":\\\"policy-03\\\",\\\"score\\\":0.9,\\\"source\\\":\\\"current-index\\\"},{\\\"id\\\":\\\"policy-04\\\",\\\"score\\\":0.9,\\\"source\\\":\\\"current-index\\\"},{\\\"id\\\":\\\"policy-05\\\",\\\"score\\\":0.9,\\\"source\\\":\\\"current-index\\\"},{\\\"id\\\":\\\"policy-06\\\",\\\"score\\\":0.9,\\\"source\\\":\\\"current-index\\\"},{\\\"id\\\":\\\"policy-07\\\",\\\"score\\\":0.9,\\\"source\\\":\\\"current-index\\\"},{\\\"id\\\":\\\"policy-08\\\",\\\"score\\\":0.9,\\\"source\\\":\\\"current-index\\\"},{\\\"id\\\":\\\"distractor-09\\\",\\\"score\\\":0.2,\\\"source\\\":\\\"current-index\\\"}]\").map(json => ({json}));"
      }
    },
    {
      "id": "gate",
      "name": "Confidence Gate",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "return $input.all().filter(item => item.json.score >= 0.7 && item.json.source === \"legacy-index\");"
      }
    },
    {
      "id": "context",
      "name": "Build context",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        200,
        0
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const ids = $input.all().map(item => item.json.id);\nreturn [{ json: { count: ids.length, ids } }];"
      }
    },
    {
      "id": "78b1229b-0b87-426c-a0cb-33aa10e82f7a",
      "name": "Check expected output",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -400,
        240
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// Generated by n8n-check. Fixed fixture, one run per node; leave this branch last.\nfunction equalJson(a, b) {\n  if (Object.is(a, b)) return true;\n  if (!a || !b || typeof a !== 'object' || typeof b !== 'object') return false;\n  if (Array.isArray(a) !== Array.isArray(b)) return false;\n  const keys = Object.keys(a);\n  return keys.length === Object.keys(b).length &&\n    keys.every(key => Object.hasOwn(b, key) && equalJson(a[key], b[key]));\n}\nfunction checkItems(items, assertion) {\n  const label = `${assertion.node}: output ${assertion.output ?? 0}`;\n  if (assertion.count !== undefined && items.length !== assertion.count)\n    throw new Error(`n8n-check: ${label} expected ${assertion.count} items, got ${items.length}`);\n  if (assertion.equals !== undefined) {\n    const values = items.map((item, index) => {\n      if (!assertion.pluck) return item.json;\n      let value = item;\n      for (const key of assertion.pluck.split('.')) {\n        if (value === null || typeof value !== 'object' || !Object.hasOwn(value, key))\n          throw new Error(`n8n-check: ${label} item ${index} is missing ${assertion.pluck}`);\n        value = value[key];\n      }\n      return value;\n    });\n    if (!equalJson(values, assertion.equals))\n      throw new Error(`n8n-check: ${label}${assertion.pluck ? ` ${assertion.pluck}` : ''} differs. Expected ${JSON.stringify(assertion.equals)}; got ${JSON.stringify(values)}`);\n  }\n  return { node: assertion.node, output: assertion.output ?? 0, count: items.length, passed: true };\n}\nconst assertions = JSON.parse(\"[{\\\"node\\\":\\\"Build context\\\",\\\"equals\\\":[{\\\"count\\\":8,\\\"ids\\\":[\\\"policy-01\\\",\\\"policy-02\\\",\\\"policy-03\\\",\\\"policy-04\\\",\\\"policy-05\\\",\\\"policy-06\\\",\\\"policy-07\\\",\\\"policy-08\\\"]}]}]\");\nconst checks = assertions.map(assertion => {\n  const node = $(assertion.node);\n  const items = node.isExecuted ? node.all(assertion.output ?? 0, 0) : [];\n  return checkItems(items, assertion);\n});\nreturn [{ json: { status: 'passed', checks } }];"
      }
    }
  ],
  "connections": {
    "Retrieved rows": {
      "main": [
        [
          {
            "node": "Confidence Gate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Confidence Gate": {
      "main": [
        [
          {
            "node": "Build context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "n8n-check start": {
      "main": [
        [
          {
            "node": "Retrieved rows",
            "type": "main",
            "index": 0
          },
          {
            "node": "Check expected output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "executionTimeout": 60,
    "saveExecutionProgress": true
  }
}
