-
-
Notifications
You must be signed in to change notification settings - Fork 23.6k
fix(components): add missing regex handler to Condition node #5651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Aadil-Hasun
wants to merge
2
commits into
FlowiseAI:main
Choose a base branch
from
Aadil-Hasun:bugfix/condition-regex-operation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+239
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
219 changes: 219 additions & 0 deletions
219
packages/components/nodes/agentflow/Condition/Condition.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| const { nodeClass: Condition_Agentflow } = require('./Condition') | ||
| import { INodeData } from '../../../src/Interface' | ||
|
|
||
| // Helper function to create a valid INodeData object for Condition node | ||
| function createConditionNodeData(id: string, conditions: any[]): INodeData { | ||
| return { | ||
| id: id, | ||
| label: 'Condition', | ||
| name: 'conditionAgentflow', | ||
| type: 'Condition', | ||
| icon: 'condition.svg', | ||
| version: 1.0, | ||
| category: 'Agent Flows', | ||
| baseClasses: ['Condition'], | ||
| inputs: { | ||
| conditions: conditions | ||
| } | ||
| } | ||
| } | ||
|
|
||
| describe('Condition Agentflow - Regex Operation', () => { | ||
| let nodeClass: any | ||
|
|
||
| beforeEach(() => { | ||
| nodeClass = new Condition_Agentflow() | ||
| }) | ||
|
|
||
| describe('Valid regex patterns', () => { | ||
| it('should match when regex pattern matches value1', async () => { | ||
| const conditions = [ | ||
| { type: 'string', value1: 'hello world', operation: 'regex', value2: 'hello' } | ||
| ] | ||
| const nodeData = createConditionNodeData('test-regex-1', conditions) | ||
| const result = await nodeClass.run(nodeData, '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should not match when regex pattern does not match value1', async () => { | ||
| const conditions = [ | ||
| { type: 'string', value1: 'hello world', operation: 'regex', value2: '^world' } | ||
| ] | ||
| const nodeData = createConditionNodeData('test-regex-2', conditions) | ||
| const result = await nodeClass.run(nodeData, '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBeUndefined() | ||
| expect(result.output.conditions[1].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should match digits with [0-9]+ pattern', async () => { | ||
| const conditions = [ | ||
| { type: 'string', value1: 'test123abc', operation: 'regex', value2: '[0-9]+' } | ||
| ] | ||
| const result = await nodeClass.run(createConditionNodeData('test-3', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should match email pattern', async () => { | ||
| const conditions = [ | ||
| { type: 'string', value1: '[email protected]', operation: 'regex', value2: '^[a-zA-Z0-9.]+@[a-zA-Z0-9.]+$' } | ||
| ] | ||
| const result = await nodeClass.run(createConditionNodeData('test-4', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe('Invalid regex patterns', () => { | ||
| it('should return false (not crash) for invalid regex pattern', async () => { | ||
| const conditions = [ | ||
| { type: 'string', value1: 'test', operation: 'regex', value2: '[invalid(' } | ||
| ] | ||
| const result = await nodeClass.run(createConditionNodeData('test-invalid', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBeUndefined() | ||
| expect(result.output.conditions[1].isFulfilled).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe('Edge cases', () => { | ||
| it('should handle empty value1', async () => { | ||
| const conditions = [ | ||
| { type: 'string', value1: '', operation: 'regex', value2: '.*' } | ||
| ] | ||
| const result = await nodeClass.run(createConditionNodeData('test-empty', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle null value1', async () => { | ||
| const conditions = [ | ||
| { type: 'string', value1: null, operation: 'regex', value2: 'test' } | ||
| ] | ||
| const result = await nodeClass.run(createConditionNodeData('test-null', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBeUndefined() | ||
| expect(result.output.conditions[1].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should be case-sensitive by default', async () => { | ||
| const conditions = [ | ||
| { type: 'string', value1: 'Hello', operation: 'regex', value2: 'hello' } | ||
| ] | ||
| const result = await nodeClass.run(createConditionNodeData('test-case', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBeUndefined() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| expect(result.output.conditions[1].isFulfilled).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe('Flowise input escaping', () => { | ||
| it('should unescape brackets in pattern', async () => { | ||
| const conditions = [ | ||
| { type: 'string', value1: 'test123abc', operation: 'regex', value2: '\\[0-9\\]+' } | ||
| ] | ||
| const result = await nodeClass.run(createConditionNodeData('test-brackets', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should unescape double-backslash pattern', async () => { | ||
| const conditions = [ | ||
| { type: 'string', value1: 'test123abc', operation: 'regex', value2: '\\\\d+' } | ||
| ] | ||
| const result = await nodeClass.run(createConditionNodeData('test-backslash', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should unescape escaped asterisk', async () => { | ||
| // User typed go*al, Flowise stored as go\*al | ||
| const conditions = [ | ||
| { type: 'string', value1: 'goooal', operation: 'regex', value2: 'go\\*al' } | ||
| ] | ||
| const result = await nodeClass.run(createConditionNodeData('test-escaped-asterisk', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe('Complex regex patterns - all metacharacters', () => { | ||
| it('should handle ^ (start anchor)', async () => { | ||
| const conditions = [{ type: 'string', value1: 'hello world', operation: 'regex', value2: '^hello' }] | ||
| const result = await nodeClass.run(createConditionNodeData('test-caret', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle $ (end anchor)', async () => { | ||
| const conditions = [{ type: 'string', value1: 'hello world', operation: 'regex', value2: 'world$' }] | ||
| const result = await nodeClass.run(createConditionNodeData('test-dollar', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle . (any character)', async () => { | ||
| const conditions = [{ type: 'string', value1: 'cat', operation: 'regex', value2: 'c.t' }] | ||
| const result = await nodeClass.run(createConditionNodeData('test-dot', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle * (zero or more)', async () => { | ||
| const conditions = [{ type: 'string', value1: 'goooal', operation: 'regex', value2: 'go*al' }] | ||
| const result = await nodeClass.run(createConditionNodeData('test-star', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle + (one or more)', async () => { | ||
| const conditions = [{ type: 'string', value1: 'goooal', operation: 'regex', value2: 'go+al' }] | ||
| const result = await nodeClass.run(createConditionNodeData('test-plus', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle ? (zero or one)', async () => { | ||
| const conditions = [{ type: 'string', value1: 'color', operation: 'regex', value2: 'colou?r' }] | ||
| const result = await nodeClass.run(createConditionNodeData('test-question', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle | (alternation)', async () => { | ||
| const conditions = [{ type: 'string', value1: 'cat', operation: 'regex', value2: 'cat|dog' }] | ||
| const result = await nodeClass.run(createConditionNodeData('test-pipe', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle () (grouping)', async () => { | ||
| const conditions = [{ type: 'string', value1: 'abcabc', operation: 'regex', value2: '(abc)+' }] | ||
| const result = await nodeClass.run(createConditionNodeData('test-parens', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle {} (quantifier)', async () => { | ||
| const conditions = [{ type: 'string', value1: 'aaa', operation: 'regex', value2: 'a{3}' }] | ||
| const result = await nodeClass.run(createConditionNodeData('test-braces', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle complex email pattern', async () => { | ||
| const conditions = [{ | ||
| type: 'string', | ||
| value1: '[email protected]', | ||
| operation: 'regex', | ||
| value2: '^[a-z]+@[a-z]+\\.(com|org)$' | ||
| }] | ||
| const result = await nodeClass.run(createConditionNodeData('test-complex', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle URL pattern', async () => { | ||
| const conditions = [{ | ||
| type: 'string', | ||
| value1: 'https://example.com/path?query=1', | ||
| operation: 'regex', | ||
| value2: '^https?://[a-z.]+/.*$' | ||
| }] | ||
| const result = await nodeClass.run(createConditionNodeData('test-url', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle UUID pattern', async () => { | ||
| const conditions = [{ | ||
| type: 'string', | ||
| value1: '550e8400-e29b-41d4-a716-446655440000', | ||
| operation: 'regex', | ||
| value2: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' | ||
| }] | ||
| const result = await nodeClass.run(createConditionNodeData('test-uuid', conditions), '', { agentflowRuntime: { state: {} } }) | ||
| expect(result.output.conditions[0].isFulfilled).toBe(true) | ||
| }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make this test more robust and align with other tests for non-matching conditions (e.g., lines 44-45), you should also assert that the
elsecondition is fulfilled.