import {type Connection, type Node} from '@xyflow/react'
import {expect, test} from 'vitest'
import {
  checkAddConnection,
  hasCycleError,
  nodeToSelfError,
  sourceNodeNotFoundError,
  targetNodeNotFoundError,
} from './FlowChecker.tsx'

const createNode = (id: string, type: string): Node => {
  return {
    data: {},
    position: {
      x: 0,
      y: 0
    },
    id,
    type,
  }
}

const createConnection = function (source: string, target: string, sourceHandle: string | null = null, targetHandle: string | null = null): Connection {
  return {
    source,
    target,
    sourceHandle,
    targetHandle,
  }
}

/* check add connection */
test(sourceNodeNotFoundError().message, () => {
  expect(() => checkAddConnection(createConnection('a', 'b'), [], []))
})

test(targetNodeNotFoundError().message, () => {
  expect(() => checkAddConnection(createConnection('a', 'b'), [createNode('a', 'normal-node')], []))
})

test(nodeToSelfError().message, () => {
  expect(() => {
    // language=JSON
    const {
      nodes,
      edges
    } = JSON.parse('{\n  "nodes": [\n    {\n      "id": "P14abHl4uY",\n      "type": "start-node",\n      "position": {\n        "x": 100,\n        "y": 100\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 82\n      }\n    },\n    {\n      "id": "3YDRebKqCX",\n      "type": "end-node",\n      "position": {\n        "x": 773.3027344262372,\n        "y": 101.42648884412338\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 74\n      },\n      "selected": false,\n      "dragging": false\n    },\n    {\n      "id": "YXJ91nHVaz",\n      "type": "llm-node",\n      "position": {\n        "x": 430.94541183662506,\n        "y": 101.42648884412338\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 74\n      },\n      "selected": true,\n      "dragging": false\n    }\n  ],\n  "edges": [\n    {\n      "source": "P14abHl4uY",\n      "target": "YXJ91nHVaz",\n      "id": "xy-edge__P14abHl4uY-YXJ91nHVaz"\n    },\n    {\n      "source": "YXJ91nHVaz",\n      "target": "3YDRebKqCX",\n      "id": "xy-edge__YXJ91nHVaz-3YDRebKqCX"\n    }\n  ],\n  "data": {}\n}')
    checkAddConnection(createConnection('YXJ91nHVaz', 'YXJ91nHVaz'), nodes, edges)
  }).toThrowError(nodeToSelfError())
})

test(hasCycleError().message, () => {
  expect(() => {
    // language=JSON
    const {
      nodes,
      edges,
    } = JSON.parse('{\n  "nodes": [\n    {\n      "id": "-DKfXm7r3f",\n      "type": "start-node",\n      "position": {\n        "x": -75.45812782717618,\n        "y": 14.410669352596976\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 82\n      },\n      "selected": false,\n      "dragging": false\n    },\n    {\n      "id": "2uL3Hw2CAW",\n      "type": "end-node",\n      "position": {\n        "x": 734.7875356349059,\n        "y": -1.2807079327602473\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 74\n      },\n      "selected": false,\n      "dragging": false\n    },\n    {\n      "id": "yp-yYfKUzC",\n      "type": "llm-node",\n      "position": {\n        "x": 338.2236369686051,\n        "y": -92.5759939566568\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 74\n      },\n      "selected": false,\n      "dragging": false\n    },\n    {\n      "id": "N4HQPN-NYZ",\n      "type": "llm-node",\n      "position": {\n        "x": 332.51768159211156,\n        "y": 114.26488844123382\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 74\n      },\n      "selected": true,\n      "dragging": false\n    }\n  ],\n  "edges": [\n    {\n      "source": "-DKfXm7r3f",\n      "target": "yp-yYfKUzC",\n      "id": "xy-edge__-DKfXm7r3f-yp-yYfKUzC"\n    },\n    {\n      "source": "yp-yYfKUzC",\n      "target": "2uL3Hw2CAW",\n      "id": "xy-edge__yp-yYfKUzC-2uL3Hw2CAW"\n    },\n    {\n      "source": "-DKfXm7r3f",\n      "target": "N4HQPN-NYZ",\n      "id": "xy-edge__-DKfXm7r3f-N4HQPN-NYZ"\n    },\n    {\n      "source": "N4HQPN-NYZ",\n      "target": "yp-yYfKUzC",\n      "id": "xy-edge__N4HQPN-NYZ-yp-yYfKUzC"\n    }\n  ],\n  "data": {}\n}')
    // language=JSON
    checkAddConnection(JSON.parse('{\n  "source": "yp-yYfKUzC",\n  "sourceHandle": null,\n  "target": "N4HQPN-NYZ",\n  "targetHandle": null\n}'), nodes, edges)
  }).toThrowError(hasCycleError())
})
