(graph: Graph)
| 12 | |
| 13 | const DUAL_OUTPUT_TYPES = new Set(['deviceGet', 'varGet']); |
| 14 | |
| 15 | const STATE_CONDITION_INPUT_TYPES = new Set(['condition', 'logicOr', 'logicAnd', 'logicNot']); |
| 16 | |
| 17 | /** Canvas annotations do not participate in execution. */ |
| 18 | const ANNOTATION_NODE_TYPES = new Set(['nop']); |
| 19 | |
| 20 | /** Accept both legacy plain text and rich-text inserts. */ |
| 21 | function annotationText(node: GraphNode): string { |
| 22 | const contents = (node.cfg as Record<string, unknown> | undefined)?.contents; |
| 23 | if (typeof contents === 'string') return contents; |
| 24 | if (!Array.isArray(contents)) return ''; |
| 25 | return contents |
| 26 | .map((seg) => { |
| 27 | const insert = (seg as Record<string, unknown> | null)?.insert; |
| 28 | return typeof insert === 'string' ? insert : ''; |
| 29 | }) |
| 30 | .join(''); |
| 31 | } |
| 32 | |
| 33 | function targetInfo(target: string): { id: string; port: string } | null { |
| 34 | const dotIdx = target.lastIndexOf('.'); |
| 35 | if (dotIdx === -1) return null; |
| 36 | return { id: target.substring(0, dotIdx), port: target.substring(dotIdx + 1) }; |
| 37 | } |
| 38 | |
| 39 | function isDeclaredConditionInput(node: GraphNode, port: string): boolean { |
| 40 | if (!Object.prototype.hasOwnProperty.call(node.inputs || {}, port)) return false; |
| 41 | if (node.type === 'condition') return port === 'condition'; |
| 42 | if (node.type === 'logicNot') return port === 'input'; |
| 43 | return (node.type === 'logicOr' || node.type === 'logicAnd') && /^input\d+$/.test(port); |
| 44 | } |
| 45 | |
| 46 | function reachesConditionInput(nodeId: string, nodeMap: Map<string, GraphNode>, visited: Set<string>): boolean { |
| 47 | if (visited.has(nodeId)) return false; |
| 48 | visited.add(nodeId); |
| 49 | |
| 50 | const node = nodeMap.get(nodeId); |
| 51 | if (!node) return false; |
| 52 | |
| 53 | for (const target of node.outputs?.output || []) { |
| 54 | const info = targetInfo(target); |
| 55 | if (!info) continue; |
| 56 | const targetNode = nodeMap.get(info.id); |
| 57 | if (!targetNode || !isDeclaredConditionInput(targetNode, info.port)) continue; |
| 58 | if (targetNode.type === 'condition') return true; |
| 59 | if (reachesConditionInput(targetNode.id, nodeMap, new Set(visited))) return true; |
| 60 | } |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | function canAcceptStateCondition(target: string, nodeMap: Map<string, GraphNode>): boolean { |
| 66 | const info = targetInfo(target); |
| 67 | if (!info) return false; |
| 68 | |
| 69 | const targetNode = nodeMap.get(info.id); |
| 70 | if (!targetNode || !STATE_CONDITION_INPUT_TYPES.has(targetNode.type)) return false; |
| 71 | if (!isDeclaredConditionInput(targetNode, info.port)) return false; |
no outgoing calls
no test coverage detected