LCOV - code coverage report
Current view: top level - src/utils - nodeDefUtil.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 67.5 % 40 27
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { intersection, isNil, keys, union } from 'es-toolkit/compat'
       2              : 
       3              : import type {
       4              :   ComboInputSpec,
       5              :   ComboInputSpecV2,
       6              :   FloatInputSpec,
       7              :   InputSpec,
       8              :   IntInputSpec,
       9              :   NumericInputOptions
      10              : } from '@comfyorg/object-info-parser'
      11              : import {
      12              :   getComboSpecComboOptions,
      13              :   getInputSpecType,
      14              :   isComboInputSpec,
      15              :   isFloatInputSpec,
      16              :   isIntInputSpec
      17              : } from '@comfyorg/object-info-parser'
      18              : 
      19              : import { lcm } from './mathUtil'
      20              : 
      21         2355 : const IGNORE_KEYS = new Set<string>([
      22              :   'default',
      23              :   'forceInput',
      24              :   'defaultInput',
      25              :   'control_after_generate',
      26              :   'multiline',
      27              :   'tooltip',
      28              :   'dynamicPrompts'
      29              : ])
      30              : 
      31         2355 : const getRange = (options: Pick<NumericInputOptions, 'min' | 'max'>) => {
      32              :   const min = options.min ?? -Infinity
      33              :   const max = options.max ?? Infinity
      34           22 :   return { min, max }
      35              : }
      36              : 
      37         2355 : const mergeNumericInputSpec = <T extends IntInputSpec | FloatInputSpec>(
      38              :   spec1: T,
      39              :   spec2: T
      40              : ): InputSpec | null => {
      41           11 :   const type = spec1[0]
      42           11 :   const options1 = spec1[1] ?? {}
      43           11 :   const options2 = spec2[1] ?? {}
      44              : 
      45           11 :   const range1 = getRange(options1)
      46           11 :   const range2 = getRange(options2)
      47              : 
      48              :   // If the ranges do not overlap, return null
      49           11 :   if (range1.min > range2.max || range1.max < range2.min) {
      50            0 :     return null
      51              :   }
      52              : 
      53           11 :   const step1 = options1.step ?? 1
      54           11 :   const step2 = options2.step ?? 1
      55              : 
      56           11 :   const mergedOptions = {
      57              :     // Take intersection of ranges
      58              :     min: Math.max(range1.min, range2.min),
      59              :     max: Math.min(range1.max, range2.max),
      60              :     step: lcm(step1, step2)
      61              :   }
      62              : 
      63           11 :   return mergeCommonInputSpec(
      64              :     [type, { ...options1, ...mergedOptions }],
      65              :     [type, { ...options2, ...mergedOptions }]
      66              :   )
      67              : }
      68              : 
      69         2355 : const mergeComboInputSpec = <T extends ComboInputSpec | ComboInputSpecV2>(
      70              :   spec1: T,
      71              :   spec2: T
      72              : ): InputSpec | null => {
      73            0 :   const options1 = spec1[1] ?? {}
      74            0 :   const options2 = spec2[1] ?? {}
      75              : 
      76            0 :   const comboOptions1 = getComboSpecComboOptions(spec1)
      77            0 :   const comboOptions2 = getComboSpecComboOptions(spec2)
      78              : 
      79            0 :   const commonOptions = intersection(comboOptions1, comboOptions2)
      80              : 
      81              :   // If the intersection is empty, return null
      82            0 :   if (commonOptions.length === 0) {
      83            0 :     return null
      84              :   }
      85              : 
      86            0 :   return mergeCommonInputSpec(
      87              :     ['COMBO', { ...options1, options: commonOptions }],
      88              :     ['COMBO', { ...options2, options: commonOptions }]
      89              :   )
      90              : }
      91              : 
      92         2355 : const mergeCommonInputSpec = <T extends InputSpec>(
      93              :   spec1: T,
      94              :   spec2: T
      95              : ): T | null => {
      96           11 :   const type = getInputSpecType(spec1)
      97           11 :   const options1 = spec1[1] ?? {}
      98           11 :   const options2 = spec2[1] ?? {}
      99              : 
     100              :   const compareKeys = union(keys(options1), keys(options2)).filter(
     101              :     (key) => !IGNORE_KEYS.has(key)
     102              :   )
     103              : 
     104              :   const mergeIsValid = compareKeys.every((key) => {
     105           33 :     const value1 = options1[key]
     106           33 :     const value2 = options2[key]
     107           33 :     return value1 === value2 || (isNil(value1) && isNil(value2))
     108              :   })
     109              : 
     110           11 :   return mergeIsValid ? ([type, { ...options1, ...options2 }] as T) : null
     111              : }
     112              : 
     113              : /**
     114              :  * Merges two input specs.
     115              :  *
     116              :  * @param spec1 - The first input spec.
     117              :  * @param spec2 - The second input spec.
     118              :  * @returns The merged input spec, or null if the specs are not mergeable.
     119              :  */
     120         2355 : export const mergeInputSpec = (
     121              :   spec1: InputSpec,
     122              :   spec2: InputSpec
     123              : ): InputSpec | null => {
     124              :   const type1 = getInputSpecType(spec1)
     125              :   const type2 = getInputSpecType(spec2)
     126              : 
     127           11 :   if (type1 !== type2) {
     128            0 :     return null
     129              :   }
     130              : 
     131           11 :   if (isIntInputSpec(spec1) || isFloatInputSpec(spec1)) {
     132           11 :     return mergeNumericInputSpec(spec1, spec2 as typeof spec1)
     133              :   }
     134              : 
     135            0 :   if (isComboInputSpec(spec1)) {
     136            0 :     return mergeComboInputSpec(spec1, spec2 as typeof spec1)
     137              :   }
     138              : 
     139            0 :   return mergeCommonInputSpec(spec1, spec2)
     140              : }
        

Generated by: LCOV version 2.0-1