All files connectionUtils.js

100% Statements 122/122
93.93% Branches 62/66
100% Functions 12/12
100% Lines 122/122

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 1231x 1x 1x 1x 1x 1x 1x 1x 20x 20x 1x 1x 12x 12x 1x 1x 5x 5x 1x 1x 11x 11x 1x 1x 12x 12x 12x 1x 1x 12x 1x 1x 33x 33x 9x 33x 7x 7x 7x 7x 7x 7x 33x 1x 1x 11x 11x 11x 1x 1x 10x 10x 10x 10x 10x 10x 10x 1x 1x 14x 14x 10x 10x 14x 14x 14x 1x 1x 1x 1x 17x 7x 7x 10x 17x 3x 3x 3x 8x 17x 17x 3x 3x 17x 17x 1x 1x 8x 3x 3x 8x 1x 1x 8x 1x 1x 8x 1x 1x 2x 8x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 3x 3x 8x 8x 8x 8x 8x 8x 8x 8x  
// ── Connection utility functions ───────────────────────────────────────
// Pure functions extracted from App.jsx so they can be independently tested.
 
import { socketSpecAcceptsType } from './constants.js';
 
// ── Handle ID helpers ─────────────────────────────────────────────────
 
export function getHandleType(handleId) {
  return handleId.split('::')[2];
}
 
export function getInputName(handleId) {
  return handleId.split('::')[1];
}
 
export function getOutputSlot(handleId) {
  return parseInt(handleId.split('::')[1], 10);
}
 
export function encodeProxyHandleRef(handleId) {
  return encodeURIComponent(String(handleId || ''));
}
 
export function decodeProxyHandleRef(encoded) {
  try {
    return decodeURIComponent(String(encoded || ''));
  } catch {
    return String(encoded || '');
  }
}
 
export function parseGroupProxyHandle(handleId) {
  const text = String(handleId || '');
  if (!text.startsWith('group-proxy::')) return null;
  const parts = text.split('::');
  if (parts.length < 5) return null;
  return {
    direction: parts[1],
    nodeId: parts[2],
    type: parts[3],
    realHandle: decodeProxyHandleRef(parts.slice(4).join('::')),
  };
}
 
export function getConnectionHandleType(handleId) {
  const proxy = parseGroupProxyHandle(handleId);
  return proxy?.type || getHandleType(handleId);
}
 
export function getResolvedHandleRef(nodeId, handleId) {
  const proxy = parseGroupProxyHandle(handleId);
  return {
    nodeId: proxy?.nodeId || nodeId,
    handleId: proxy?.realHandle || handleId,
    type: proxy?.type || getHandleType(handleId),
  };
}
 
export function getNodeInputSpecForHandle(node, handleId) {
  const definition = node?.data?.definition;
  if (!definition?.input) return null;
  const inputName = getInputName(handleId);
  return definition.input.required?.[inputName]
    || definition.input.optional?.[inputName]
    || null;
}
 
// ── Type compatibility ────────────────────────────────────────────────
 
export function outputTypeCanConnectToTarget(outputType, targetSpecOrType, outputAcceptedTypes = []) {
  if (socketSpecAcceptsType(outputType, targetSpecOrType)) {
    return true;
  }
  // Polymorphic output: the output socket declares it can also produce the target type
  if (outputAcceptedTypes.length > 0) {
    const targetType = Array.isArray(targetSpecOrType) ? targetSpecOrType[0] : targetSpecOrType;
    if (outputAcceptedTypes.includes(targetType)) return true;
  }
  return outputType === 'ANNOTATION_SOURCE'
    && !socketSpecAcceptsType('ANNOTATION_SOURCE', targetSpecOrType)
    && (
      socketSpecAcceptsType('DATA_FIELD', targetSpecOrType)
      || socketSpecAcceptsType('IMAGE', targetSpecOrType)
    );
}
 
export function resolveOutputTypeForTarget(outputType, targetSpecOrType) {
  if (outputType !== 'ANNOTATION_SOURCE') {
    return outputType;
  }
  if (socketSpecAcceptsType('ANNOTATION_SOURCE', targetSpecOrType)) {
    return 'ANNOTATION_SOURCE';
  }
  if (socketSpecAcceptsType('DATA_FIELD', targetSpecOrType)) {
    return 'DATA_FIELD';
  }
  if (socketSpecAcceptsType('IMAGE', targetSpecOrType)) {
    return 'IMAGE';
  }
  return 'ANNOTATION_SOURCE';
}
 
// ── Pure connection validation ────────────────────────────────────────
// Extracted from the isValidConnection useCallback so it can be unit-tested
// without a ReactFlow context. Pass a `getNodeFn` that mirrors reactFlow.getNode.
 
export function checkConnectionValid(connection, getNodeFn) {
  const srcType = getConnectionHandleType(connection.sourceHandle);
  const resolvedTarget = getResolvedHandleRef(connection.target, connection.targetHandle);
  const targetNode = getNodeFn(resolvedTarget.nodeId);
  const targetSpec = getNodeInputSpecForHandle(targetNode, resolvedTarget.handleId) || resolvedTarget.type;
  if (socketSpecAcceptsType(srcType, targetSpec)) return true;
  // Polymorphic output: check if the source output declares it can produce the target type
  const srcProxy = parseGroupProxyHandle(connection.sourceHandle);
  const srcNodeId = srcProxy ? srcProxy.nodeId : connection.source;
  const srcHandleId = srcProxy ? srcProxy.realHandle : connection.sourceHandle;
  const srcNode = getNodeFn(srcNodeId);
  const srcSlot = getOutputSlot(srcHandleId);
  const srcAcceptedTypes = srcNode?.data?.definition?.output_accepted_types?.[srcSlot] || [];
  const targetType = Array.isArray(targetSpec) ? targetSpec[0] : targetSpec;
  return Array.isArray(srcAcceptedTypes) && srcAcceptedTypes.includes(targetType);
}