{
  "name": "08 - Flaky Test Detector",
  "nodes": [
    {
      "parameters": {
        "content": "## 08 Flaky Test Detector\nWe have: the last 10 CI runs of 5 tests.\nWe get: a verdict for each test, and a fix for the flaky ones.\n\nflakyScore = flips / (runs - 1). 0.3 or more = flaky.\nAdd your OpenAI credential, then click Execute workflow.",
        "height": 200,
        "width": 460
      },
      "id": "08000000-0000-4000-8000-000000000000",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -60,
        -260
      ]
    },
    {
      "parameters": {},
      "id": "08000000-0000-4000-8000-000000000001",
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// Last 10 CI runs per test, oldest first (sample data)\nconst P = 'pass', F = 'fail';\nreturn [\n  { json: { test: 'checkout › order total includes tax', runs: [P, P, P, P, P, P, F, F, F, F], errors: ['Expected \"Total: $43.18\", received \"Total: $39.98\"'] } },\n  { json: { test: 'inventory › error_user can add backpack to cart', runs: [P, F, P, P, F, P, P, F, P, P], errors: ['Expected cart badge \"1\", received \"\"'] } },\n  { json: { test: 'cart › remove item updates badge', runs: [P, P, F, P, P, P, P, P, F, P], errors: ['Expected cart badge \"0\", received \"1\" right after the click'] } },\n  { json: { test: 'login › performance_glitch_user logs in', runs: [P, P, P, P, P, P, P, P, P, F], errors: ['TimeoutError: Timeout 3000ms exceeded'] } },\n  { json: { test: 'login › standard_user logs in', runs: [P, P, P, P, P, P, P, P, P, P], errors: [] } },\n];"
      },
      "id": "08000000-0000-4000-8000-000000000002",
      "name": "CI History",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// flips = how often the result changes between runs\nreturn $input.all().map(({ json: t }) => {\n  let flips = 0;\n  for (let i = 1; i < t.runs.length; i++) if (t.runs[i] !== t.runs[i - 1]) flips++;\n  const flakyScore = Math.round((flips / (t.runs.length - 1)) * 100) / 100;\n  const lastThreeFailed = t.runs.slice(-3).every(r => r === 'fail');\n  let verdict = 'stable';\n  if (t.runs.includes('fail')) verdict = flakyScore >= 0.3 ? 'flaky' : lastThreeFailed ? 'broken' : 'watch';\n  return { json: { test: t.test, flakyScore, verdict, errors: t.errors } };\n});"
      },
      "id": "08000000-0000-4000-8000-000000000003",
      "name": "Score Flakiness",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "ceedc85b-2352-5bf4-b284-1f0facb371f8",
              "leftValue": "={{ $json.verdict }}",
              "rightValue": "flaky",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "08000000-0000-4000-8000-000000000004",
      "name": "Keep Flaky",
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2,
      "position": [
        660,
        0
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=This Playwright test is flaky.\nTest: {{ $json.test }}\nErrors: {{ $json.errors.join(' | ') }}\n\nAnswer in 2 short lines. Line 1: the test name and the most likely cause. Line 2: one concrete Playwright fix."
      },
      "id": "08000000-0000-4000-8000-000000000005",
      "name": "Suggest Fix",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [
        880,
        0
      ]
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {}
      },
      "id": "08000000-0000-4000-8000-000000000006",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        880,
        220
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "CI History",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "CI History": {
      "main": [
        [
          {
            "node": "Score Flakiness",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Score Flakiness": {
      "main": [
        [
          {
            "node": "Keep Flaky",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Keep Flaky": {
      "main": [
        [
          {
            "node": "Suggest Fix",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Suggest Fix",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "active": false
}
