Files
ComfyUI_frontend/browser_tests/fixtures/utils/subgraphUtils.ts

59 lines
1.3 KiB
TypeScript

/**
* Represents a subgraph's graph object with inputs and outputs slots.
*/
export interface SubgraphGraph {
inputs: SubgraphSlot[]
outputs: SubgraphSlot[]
}
export interface SubgraphSlot {
label?: string
name?: string
displayName?: string
}
/**
* Type guard to check if a graph object is a subgraph.
*/
export function isSubgraph(graph: unknown): graph is SubgraphGraph {
return (
graph !== null &&
typeof graph === 'object' &&
'inputs' in graph &&
'outputs' in graph &&
Array.isArray((graph as SubgraphGraph).inputs) &&
Array.isArray((graph as SubgraphGraph).outputs)
)
}
/**
* Assertion function that throws if the graph is not a subgraph.
*/
export function assertSubgraph(
graph: unknown,
message = 'Not in subgraph'
): asserts graph is SubgraphGraph {
if (!isSubgraph(graph)) {
throw new Error(message)
}
}
/**
* Inline assertion for use inside page.evaluate() browser context.
* Returns a string that can be used with Function constructor or eval.
*/
export const SUBGRAPH_ASSERT_INLINE = `
const assertSubgraph = (graph) => {
if (
graph === null ||
typeof graph !== 'object' ||
!('inputs' in graph) ||
!('outputs' in graph) ||
!Array.isArray(graph.inputs) ||
!Array.isArray(graph.outputs)
) {
throw new Error('Not in subgraph');
}
};
`