1 Commits

Author SHA1 Message Date
czlonkowski
ce4bacefcb fix: improve wget healthcheck with proper error handling
- Add explicit shell wrapper for proper error handling
- Use --quiet instead of --no-verbose for consistency
- Add --timeout=10 to match Docker healthcheck timeout
- Add || exit 1 to ensure proper failure on HTTP errors
- This ensures the healthcheck fails correctly on 4xx/5xx responses

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-04 23:51:57 +02:00
35 changed files with 1584 additions and 8421 deletions

View File

@@ -93,84 +93,71 @@ jobs:
- name: Post benchmark comparison to PR
if: always()
uses: actions/github-script@v7
continue-on-error: true
with:
script: |
const fs = require('fs');
let comment = '## ⚡ Benchmark Comparison\n\n';
try {
const fs = require('fs');
let comment = '## ⚡ Benchmark Comparison\n\n';
try {
if (fs.existsSync('benchmark-comparison.md')) {
const comparison = fs.readFileSync('benchmark-comparison.md', 'utf8');
comment += comparison;
} else {
comment += 'Benchmark comparison could not be generated.';
}
} catch (error) {
comment += `Error reading benchmark comparison: ${error.message}`;
if (fs.existsSync('benchmark-comparison.md')) {
const comparison = fs.readFileSync('benchmark-comparison.md', 'utf8');
comment += comparison;
} else {
comment += 'Benchmark comparison could not be generated.';
}
comment += '\n\n---\n';
comment += `*[View full benchmark results](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})*`;
// Find existing comment
const { data: comments } = await github.rest.issues.listComments({
} catch (error) {
comment += `Error reading benchmark comparison: ${error.message}`;
}
comment += '\n\n---\n';
comment += `*[View full benchmark results](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})*`;
// Find existing comment
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('## ⚡ Benchmark Comparison')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: comment
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('## ⚡ Benchmark Comparison')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: comment
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment
});
}
} catch (error) {
console.error('Failed to create/update PR comment:', error.message);
console.log('This is likely due to insufficient permissions for external PRs.');
console.log('Benchmark comparison has been saved to artifacts instead.');
}
# Add status check
- name: Set benchmark status
if: always()
uses: actions/github-script@v7
continue-on-error: true
with:
script: |
try {
const hasRegression = '${{ steps.compare.outputs.REGRESSION }}' === 'true';
const state = hasRegression ? 'failure' : 'success';
const description = hasRegression
? 'Performance regressions detected'
: 'No performance regressions';
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: context.sha,
state: state,
target_url: `https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}`,
description: description,
context: 'benchmarks/regression-check'
});
} catch (error) {
console.error('Failed to create commit status:', error.message);
console.log('This is likely due to insufficient permissions for external PRs.');
}
const hasRegression = '${{ steps.compare.outputs.REGRESSION }}' === 'true';
const state = hasRegression ? 'failure' : 'success';
const description = hasRegression
? 'Performance regressions detected'
: 'No performance regressions';
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: context.sha,
state: state,
target_url: `https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}`,
description: description,
context: 'benchmarks/regression-check'
});

View File

@@ -103,14 +103,12 @@ jobs:
# Store benchmark results and compare
- name: Store benchmark result
uses: benchmark-action/github-action-benchmark@v1
continue-on-error: true
id: benchmark
with:
name: n8n-mcp Benchmarks
tool: 'customSmallerIsBetter'
output-file-path: benchmark-results-formatted.json
github-token: ${{ secrets.GITHUB_TOKEN }}
auto-push: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
auto-push: true
# Where to store benchmark data
benchmark-data-dir-path: 'benchmarks'
# Alert when performance regresses by 10%
@@ -122,60 +120,52 @@ jobs:
summary-always: true
# Max number of data points to retain
max-items-in-chart: 50
fail-on-alert: false
# Comment on PR with benchmark results
- name: Comment PR with results
uses: actions/github-script@v7
if: github.event_name == 'pull_request'
continue-on-error: true
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
try {
const fs = require('fs');
const summary = JSON.parse(fs.readFileSync('benchmark-summary.json', 'utf8'));
// Format results for PR comment
let comment = '## 📊 Performance Benchmark Results\n\n';
comment += `🕐 Run at: ${new Date(summary.timestamp).toLocaleString()}\n\n`;
comment += '| Benchmark | Time | Ops/sec | Range |\n';
comment += '|-----------|------|---------|-------|\n';
// Group benchmarks by category
const categories = {};
for (const benchmark of summary.benchmarks) {
const [category, ...nameParts] = benchmark.name.split(' - ');
if (!categories[category]) categories[category] = [];
categories[category].push({
...benchmark,
shortName: nameParts.join(' - ')
});
}
// Display by category
for (const [category, benchmarks] of Object.entries(categories)) {
comment += `\n### ${category}\n`;
for (const benchmark of benchmarks) {
comment += `| ${benchmark.shortName} | ${benchmark.time} | ${benchmark.opsPerSec} | ${benchmark.range} |\n`;
}
}
// Add comparison link
comment += '\n\n📈 [View historical benchmark trends](https://czlonkowski.github.io/n8n-mcp/benchmarks/)\n';
comment += '\n⚡ Performance regressions >10% will be flagged automatically.\n';
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
const fs = require('fs');
const summary = JSON.parse(fs.readFileSync('benchmark-summary.json', 'utf8'));
// Format results for PR comment
let comment = '## 📊 Performance Benchmark Results\n\n';
comment += `🕐 Run at: ${new Date(summary.timestamp).toLocaleString()}\n\n`;
comment += '| Benchmark | Time | Ops/sec | Range |\n';
comment += '|-----------|------|---------|-------|\n';
// Group benchmarks by category
const categories = {};
for (const benchmark of summary.benchmarks) {
const [category, ...nameParts] = benchmark.name.split(' - ');
if (!categories[category]) categories[category] = [];
categories[category].push({
...benchmark,
shortName: nameParts.join(' - ')
});
} catch (error) {
console.error('Failed to create PR comment:', error.message);
console.log('This is likely due to insufficient permissions for external PRs.');
console.log('Benchmark results have been saved to artifacts instead.');
}
// Display by category
for (const [category, benchmarks] of Object.entries(categories)) {
comment += `\n### ${category}\n`;
for (const benchmark of benchmarks) {
comment += `| ${benchmark.shortName} | ${benchmark.time} | ${benchmark.opsPerSec} | ${benchmark.range} |\n`;
}
}
// Add comparison link
comment += '\n\n📈 [View historical benchmark trends](https://czlonkowski.github.io/n8n-mcp/benchmarks/)\n';
comment += '\n⚡ Performance regressions >10% will be flagged automatically.\n';
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
# Deploy benchmark results to GitHub Pages
deploy:

View File

@@ -6,6 +6,19 @@ on:
paths:
- 'package.json'
- 'package.runtime.json'
paths-ignore:
- '**.md'
- '**.txt'
- 'docs/**'
- 'examples/**'
- '.github/FUNDING.yml'
- '.github/ISSUE_TEMPLATE/**'
- '.github/pull_request_template.md'
- '.gitignore'
- 'LICENSE*'
- 'ATTRIBUTION.md'
- 'SECURITY.md'
- 'CODE_OF_CONDUCT.md'
permissions:
contents: write

View File

@@ -148,7 +148,6 @@ jobs:
- name: Create test report comment
if: github.event_name == 'pull_request' && always()
uses: actions/github-script@v7
continue-on-error: true
with:
script: |
const fs = require('fs');
@@ -162,40 +161,34 @@ jobs:
console.error('Error reading test summary:', error);
}
try {
// Find existing comment
const { data: comments } = await github.rest.issues.listComments({
// Find existing comment
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('## Test Results')
);
if (botComment) {
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: summary
});
} else {
// Create new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: summary
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('## Test Results')
);
if (botComment) {
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: summary
});
} else {
// Create new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: summary
});
}
} catch (error) {
console.error('Failed to create/update PR comment:', error.message);
console.log('This is likely due to insufficient permissions for external PRs.');
console.log('Test results have been saved to the job summary instead.');
}
# Generate job summary
@@ -267,13 +260,11 @@ jobs:
- name: Publish test results
uses: dorny/test-reporter@v1
if: always()
continue-on-error: true
with:
name: Test Results
path: 'artifacts/test-results-*/test-results/junit.xml'
reporter: java-junit
fail-on-error: false
fail-on-empty: false
# Create a combined artifact with all results
- name: Create combined results artifact

View File

@@ -180,9 +180,6 @@ The MCP server exposes tools in several categories:
- Sub-agents are not allowed to spawn further sub-agents
- When you use sub-agents, do not allow them to commit and push. That should be done by you
### Development Best Practices
- Run typecheck and lint after every code change
# important-instruction-reminders
Do what has been asked; nothing more, nothing less.
NEVER create files unless they're absolutely necessary for achieving your goal.

View File

@@ -2,7 +2,7 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![GitHub stars](https://img.shields.io/github/stars/czlonkowski/n8n-mcp?style=social)](https://github.com/czlonkowski/n8n-mcp)
[![Version](https://img.shields.io/badge/version-2.10.2-blue.svg)](https://github.com/czlonkowski/n8n-mcp)
[![Version](https://img.shields.io/badge/version-2.10.1-blue.svg)](https://github.com/czlonkowski/n8n-mcp)
[![npm version](https://img.shields.io/npm/v/n8n-mcp.svg)](https://www.npmjs.com/package/n8n-mcp)
[![codecov](https://codecov.io/gh/czlonkowski/n8n-mcp/graph/badge.svg?token=YOUR_TOKEN)](https://codecov.io/gh/czlonkowski/n8n-mcp)
[![Tests](https://img.shields.io/badge/tests-1356%20passing-brightgreen.svg)](https://github.com/czlonkowski/n8n-mcp/actions)

View File

@@ -1,41 +0,0 @@
# Jekyll configuration for GitHub Pages
# This is only used for serving benchmark results
# Only process benchmark-related files
include:
- index.html
- benchmarks/
# Exclude everything else to prevent Liquid syntax errors
exclude:
- "*.md"
- "*.json"
- "*.ts"
- "*.js"
- "*.yml"
- src/
- tests/
- docs/
- scripts/
- dist/
- node_modules/
- package.json
- package-lock.json
- tsconfig.json
- README.md
- CHANGELOG.md
- LICENSE
- Dockerfile*
- docker-compose*
- .github/
- .vscode/
- .claude/
- deploy/
- examples/
- data/
# Disable Jekyll processing for files we don't want processed
plugins: []
# Use simple theme
theme: null

Binary file not shown.

View File

@@ -7,65 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [2.10.3] - 2025-08-07
### Fixed
- **Validation System Robustness**: Fixed multiple critical validation issues affecting AI agents and workflow validation (fixes #58, #68, #70, #73)
- **Issue #73**: Fixed `validate_node_minimal` crash when config is undefined
- Added safe property access with optional chaining (`config?.resource`)
- Tool now handles undefined, null, and malformed configs gracefully
- **Issue #58**: Fixed `validate_node_operation` crash on invalid nodeType
- Added type checking before calling string methods
- Prevents "Cannot read properties of undefined (reading 'replace')" error
- **Issue #70**: Fixed validation profile settings being ignored
- Extended profile parameter to all validation phases (nodes, connections, expressions)
- Added Sticky Notes filtering to reduce false positives
- Enhanced cycle detection to allow legitimate loops (SplitInBatches)
- **Issue #68**: Added error recovery suggestions for AI agents
- New `addErrorRecoverySuggestions()` method provides actionable recovery steps
- Categorizes errors and suggests specific fixes for each type
- Helps AI agents self-correct when validation fails
### Added
- **Input Validation System**: Comprehensive validation for all MCP tool inputs
- Created `validation-schemas.ts` with custom validation utilities
- No external dependencies - pure TypeScript implementation
- Tool-specific validation schemas for all MCP tools
- Clear error messages with field-level details
- **Enhanced Cycle Detection**: Improved detection of legitimate loops vs actual cycles
- Recognizes SplitInBatches loop patterns as valid
- Reduces false positive cycle warnings
- **Comprehensive Test Suite**: Added 16 tests covering all validation fixes
- Tests for crash prevention with malformed inputs
- Tests for profile behavior across validation phases
- Tests for error recovery suggestions
- Tests for legitimate loop patterns
### Enhanced
- **Validation Profiles**: Now consistently applied across all validation phases
- `minimal`: Reduces warnings for basic validation
- `runtime`: Standard validation for production workflows
- `ai-friendly`: Optimized for AI agent workflow creation
- `strict`: Maximum validation for critical workflows
- **Error Messages**: More helpful and actionable for both humans and AI agents
- Specific recovery suggestions for common errors
- Clear guidance on fixing validation issues
- Examples of correct configurations
## [2.10.2] - 2025-08-05
### Updated
- **n8n Dependencies**: Updated to latest versions for compatibility and new features
- n8n: 1.104.1 → 1.105.2
- n8n-core: 1.103.1 → 1.104.1
- n8n-workflow: 1.101.0 → 1.102.1
- @n8n/n8n-nodes-langchain: 1.103.1 → 1.104.1
- **Node Database**: Rebuilt with 534 nodes from updated n8n packages
- **Template Library**: Fetched 499 workflow templates from the last 12 months
- Templates are filtered to include only those created or updated within the past year
- This ensures the template library contains fresh and actively maintained workflows
- All 1,620 tests passing with updated dependencies
## [2.10.1] - 2025-08-02
### Fixed
@@ -1143,7 +1084,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Basic n8n and MCP integration
- Core workflow automation features
[2.10.2]: https://github.com/czlonkowski/n8n-mcp/compare/v2.10.1...v2.10.2
[2.10.1]: https://github.com/czlonkowski/n8n-mcp/compare/v2.10.0...v2.10.1
[2.10.0]: https://github.com/czlonkowski/n8n-mcp/compare/v2.9.1...v2.10.0
[2.9.1]: https://github.com/czlonkowski/n8n-mcp/compare/v2.9.0...v2.9.1

View File

@@ -1,6 +1,6 @@
# n8n-MCP Deployment Guide
This guide covers how to deploy n8n-MCP and connect it to your n8n instance. Whether you're testing locally or deploying to production, we'll show you how to set up n8n-MCP for use with n8n's MCP Client Tool node.
This guide covers how to deploy n8n-MCP and connect it to AI Agent nodes with the standard MCP Client Tool. Whether you're testing locally or deploying to production, we'll show you how to set it up.
## Table of Contents
- [Overview](#overview)
@@ -35,15 +35,15 @@ cd n8n-mcp
npm install
npm run build
# Run the integration test script
./scripts/test-n8n-integration.sh
# Run the test script
./scripts/test-n8n-mode.sh
```
This script will:
1. Start a real n8n instance in Docker
2. Start n8n-MCP server configured for n8n
3. Guide you through API key setup for workflow management
4. Test the complete integration between n8n and n8n-MCP
1. Start n8n-MCP in n8n mode on port 3001
2. Enable debug logging for troubleshooting
3. Run comprehensive protocol tests
4. Display results and any issues found
### Manual Local Setup
@@ -86,8 +86,8 @@ curl http://localhost:3001/mcp
| `MCP_MODE` | Yes | Enables HTTP mode for n8n MCP Client | `http` |
| `N8N_API_URL` | Yes* | URL of your n8n instance | `http://localhost:5678` |
| `N8N_API_KEY` | Yes* | n8n API key for workflow management | `n8n_api_xxx...` |
| `MCP_AUTH_TOKEN` | Yes | Authentication token for MCP requests (min 32 chars) | `secure-random-32-char-token` |
| `AUTH_TOKEN` | Yes | **MUST match MCP_AUTH_TOKEN exactly** | `secure-random-32-char-token` |
| `MCP_AUTH_TOKEN` | Yes | Authentication token for MCP requests | `secure-random-32-char-token` |
| `AUTH_TOKEN` | Yes | Must match MCP_AUTH_TOKEN | `secure-random-32-char-token` |
| `PORT` | No | Port for the HTTP server | `3000` (default) |
| `LOG_LEVEL` | No | Logging verbosity | `info`, `debug`, `error` |
@@ -103,25 +103,21 @@ Starting with version 2.9.2, we use a single optimized Dockerfile for all deploy
## Production Deployment
> **⚠️ Critical**: Docker caches images locally. Always run `docker pull ghcr.io/czlonkowski/n8n-mcp:latest` before deploying to ensure you have the latest version. This simple step prevents most deployment issues.
### Same Server as n8n
If you're running n8n-MCP on the same server as your n8n instance:
### Using Pre-built Image (Recommended)
### Building from Source (Recommended)
The pre-built images are automatically updated with each release and are the easiest way to get started.
**IMPORTANT**: Always pull the latest image to avoid using cached versions:
For the latest features and bug fixes, build from source:
```bash
# ALWAYS pull the latest image first
docker pull ghcr.io/czlonkowski/n8n-mcp:latest
# Clone and build
git clone https://github.com/czlonkowski/n8n-mcp.git
cd n8n-mcp
# Generate a secure token (save this!)
AUTH_TOKEN=$(openssl rand -hex 32)
echo "Your AUTH_TOKEN: $AUTH_TOKEN"
# Build Docker image
docker build -t n8n-mcp:latest .
# Create a Docker network if n8n uses one
docker network create n8n-net
@@ -135,35 +131,35 @@ docker run -d \
-e MCP_MODE=http \
-e N8N_API_URL=http://n8n:5678 \
-e N8N_API_KEY=your-n8n-api-key \
-e MCP_AUTH_TOKEN=$AUTH_TOKEN \
-e AUTH_TOKEN=$AUTH_TOKEN \
-e MCP_AUTH_TOKEN=$(openssl rand -hex 32) \
-e AUTH_TOKEN=$(openssl rand -hex 32) \
-e LOG_LEVEL=info \
--restart unless-stopped \
ghcr.io/czlonkowski/n8n-mcp:latest
n8n-mcp:latest
```
### Building from Source (Advanced Users)
### Using Pre-built Image (May Be Outdated)
Only build from source if you need custom modifications or are contributing to development:
⚠️ **Warning**: Pre-built images may be outdated due to CI/CD synchronization issues. Always check the [GitHub releases](https://github.com/czlonkowski/n8n-mcp/releases) for the latest version.
```bash
# Clone and build
git clone https://github.com/czlonkowski/n8n-mcp.git
cd n8n-mcp
# Create a Docker network if n8n uses one
docker network create n8n-net
# Build Docker image
docker build -t n8n-mcp:latest .
# Run using your local image
# Run n8n-MCP container
docker run -d \
--name n8n-mcp \
--network n8n-net \
-p 3000:3000 \
-e N8N_MODE=true \
-e MCP_MODE=http \
-e N8N_API_URL=http://n8n:5678 \
-e N8N_API_KEY=your-n8n-api-key \
-e MCP_AUTH_TOKEN=$(openssl rand -hex 32) \
-e AUTH_TOKEN=$(openssl rand -hex 32) \
# ... other settings
n8n-mcp:latest
-e LOG_LEVEL=info \
--restart unless-stopped \
ghcr.io/czlonkowski/n8n-mcp:latest
```
### Using systemd (for native installation)
@@ -202,14 +198,14 @@ sudo systemctl start n8n-mcp
Deploy n8n-MCP on a separate server from your n8n instance:
#### Quick Docker Deployment (Recommended)
**Always pull the latest image to ensure you have the current version:**
#### Quick Docker Deployment (Build from Source)
```bash
# On your cloud server (Hetzner, AWS, DigitalOcean, etc.)
# ALWAYS pull the latest image first
docker pull ghcr.io/czlonkowski/n8n-mcp:latest
# First, clone and build
git clone https://github.com/czlonkowski/n8n-mcp.git
cd n8n-mcp
docker build -t n8n-mcp:latest .
# Generate auth tokens
AUTH_TOKEN=$(openssl rand -hex 32)
@@ -227,25 +223,31 @@ docker run -d \
-e AUTH_TOKEN=$AUTH_TOKEN \
-e LOG_LEVEL=info \
--restart unless-stopped \
ghcr.io/czlonkowski/n8n-mcp:latest
n8n-mcp:latest
```
#### Building from Source (Advanced)
#### Quick Docker Deployment (Pre-built Image)
Only needed if you're modifying the code:
⚠️ **Warning**: May be outdated. Check [releases](https://github.com/czlonkowski/n8n-mcp/releases) first.
```bash
# Clone and build
git clone https://github.com/czlonkowski/n8n-mcp.git
cd n8n-mcp
docker build -t n8n-mcp:latest .
# Generate auth tokens
AUTH_TOKEN=$(openssl rand -hex 32)
echo "Save this AUTH_TOKEN: $AUTH_TOKEN"
# Run using local image
# Run the container
docker run -d \
--name n8n-mcp \
-p 3000:3000 \
# ... same environment variables as above
n8n-mcp:latest
-e N8N_MODE=true \
-e MCP_MODE=http \
-e N8N_API_URL=https://your-n8n-instance.com \
-e N8N_API_KEY=your-n8n-api-key \
-e MCP_AUTH_TOKEN=$AUTH_TOKEN \
-e AUTH_TOKEN=$AUTH_TOKEN \
-e LOG_LEVEL=info \
--restart unless-stopped \
ghcr.io/czlonkowski/n8n-mcp:latest
```
#### Full Production Setup (Hetzner/AWS/DigitalOcean)
@@ -267,7 +269,61 @@ curl -fsSL https://get.docker.com | sh
3. **Deploy n8n-MCP with SSL** (using Caddy for automatic HTTPS):
**Using Docker Compose (Recommended)**
**Option A: Build from Source (Recommended)**
```bash
# Clone and prepare
git clone https://github.com/czlonkowski/n8n-mcp.git
cd n8n-mcp
# Build local image
docker build -t n8n-mcp:latest .
# Create docker-compose.yml
cat > docker-compose.yml << 'EOF'
version: '3.8'
services:
n8n-mcp:
image: n8n-mcp:latest # Using locally built image
container_name: n8n-mcp
restart: unless-stopped
environment:
- N8N_MODE=true
- MCP_MODE=http
- N8N_API_URL=${N8N_API_URL}
- N8N_API_KEY=${N8N_API_KEY}
- MCP_AUTH_TOKEN=${MCP_AUTH_TOKEN}
- AUTH_TOKEN=${AUTH_TOKEN}
- PORT=3000
- LOG_LEVEL=info
networks:
- web
caddy:
image: caddy:2-alpine
container_name: caddy
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- caddy_data:/data
- caddy_config:/config
networks:
- web
networks:
web:
driver: bridge
volumes:
caddy_data:
caddy_config:
EOF
```
**Option B: Pre-built Image (May Be Outdated)**
```bash
# Create docker-compose.yml
cat > docker-compose.yml << 'EOF'
@@ -276,7 +332,6 @@ version: '3.8'
services:
n8n-mcp:
image: ghcr.io/czlonkowski/n8n-mcp:latest
pull_policy: always # Always pull latest image
container_name: n8n-mcp
restart: unless-stopped
environment:
@@ -315,56 +370,7 @@ volumes:
EOF
```
**Note**: The `pull_policy: always` ensures you always get the latest version.
**Building from Source (if needed)**
```bash
# Only if you need custom modifications
git clone https://github.com/czlonkowski/n8n-mcp.git
cd n8n-mcp
docker build -t n8n-mcp:local .
# Then update docker-compose.yml to use:
# image: n8n-mcp:local
container_name: n8n-mcp
restart: unless-stopped
environment:
- N8N_MODE=true
- MCP_MODE=http
- N8N_API_URL=${N8N_API_URL}
- N8N_API_KEY=${N8N_API_KEY}
- MCP_AUTH_TOKEN=${MCP_AUTH_TOKEN}
- AUTH_TOKEN=${AUTH_TOKEN}
- PORT=3000
- LOG_LEVEL=info
networks:
- web
caddy:
image: caddy:2-alpine
container_name: caddy
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- caddy_data:/data
- caddy_config:/config
networks:
- web
networks:
web:
driver: bridge
volumes:
caddy_data:
caddy_config:
EOF
```
**Complete the Setup**
**Complete Setup (Both Options)**
```bash
# Create Caddyfile
cat > Caddyfile << 'EOF'
@@ -475,21 +481,12 @@ You are an n8n workflow expert. Use the MCP tools to:
- **IP Whitelisting**: Consider restricting access to known n8n instances
### Docker Security
- **Always pull latest images**: Docker caches images locally, so run `docker pull` before deployment
- Run containers with `--read-only` flag if possible
- Use specific image versions instead of `:latest` in production
- Regular updates: `docker pull ghcr.io/czlonkowski/n8n-mcp:latest`
## Troubleshooting
### Docker Image Issues
**Using Outdated Cached Images**
- **Symptom**: Missing features, old bugs reappearing, features not working as documented
- **Cause**: Docker uses locally cached images instead of pulling the latest version
- **Solution**: Always run `docker pull ghcr.io/czlonkowski/n8n-mcp:latest` before deployment
- **Verification**: Check image age with `docker images | grep n8n-mcp`
### Common Configuration Issues
**Missing `MCP_MODE=http` Environment Variable**
@@ -575,10 +572,10 @@ You are an n8n workflow expert. Use the MCP tools to:
### Version Compatibility Issues
**"Features Not Working as Expected"**
**"Outdated Docker Image"**
- **Symptom**: Missing features, old bugs, or compatibility issues
- **Solution**: Pull the latest image: `docker pull ghcr.io/czlonkowski/n8n-mcp:latest`
- **Check**: Verify image date with `docker inspect ghcr.io/czlonkowski/n8n-mcp:latest | grep Created`
- **Solution**: Build from source instead of using pre-built images
- **Check**: Compare your image version with [GitHub releases](https://github.com/czlonkowski/n8n-mcp/releases)
**"Protocol version mismatch"**
- n8n-MCP automatically uses version 2024-11-05 for n8n compatibility
@@ -746,6 +743,48 @@ curl http://localhost:3001/mcp
- **Response time**: Average 12ms for queries
- **Caching**: Built-in 15-minute cache for repeated queries
## Railway Deployment for n8n Integration
[![Deploy on Railway](https://railway.com/button.svg)](https://railway.com/deploy/n8n-mcp?referralCode=n8n-mcp)
If you're using the **Deploy to Railway** button, you'll need to modify some environment variables since Railway uses a different Docker image (`Dockerfile.railway`).
### Required Environment Variable Changes
When deploying with Railway for n8n integration, add these variables in your Railway dashboard:
1. **Go to Railway dashboard** → Your service → **Variables tab**
2. **Add the following variables**:
```bash
# Required for n8n integration mode
N8N_MODE=true
# Already set by Railway template, but verify:
MCP_MODE=http # Required for HTTP mode
MCP_AUTH_TOKEN=<your-token> # Must match AUTH_TOKEN
AUTH_TOKEN=<your-token> # Same value as MCP_AUTH_TOKEN
# Optional: For workflow management features
N8N_API_URL=https://your-n8n-instance.com
N8N_API_KEY=your-n8n-api-key
```
3. **Save changes** - Railway will automatically redeploy
### Connecting n8n to Railway-deployed n8n-MCP
In your n8n workflow, configure the MCP Client Tool with:
```
Server URL: https://your-app.up.railway.app/mcp
Auth Token: [Your AUTH_TOKEN value]
Transport: HTTP Streamable (SSE)
```
> **Note**: The Railway deployment automatically includes all required dependencies and uses the optimized `Dockerfile.railway` which is compatible with both Claude Desktop and n8n integrations.
For more details on Railway deployment, see our [Railway Deployment Guide](./RAILWAY_DEPLOYMENT.md).
## Next Steps
- Test your setup with the [MCP Client Tool in n8n](https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-langchain.mcpclienttool/)
@@ -755,4 +794,4 @@ curl http://localhost:3001/mcp
---
Need help? Open an issue on [GitHub](https://github.com/czlonkowski/n8n-mcp/issues) or check the [n8n forums](https://community.n8n.io)
Need help? Open an issue on [GitHub](https://github.com/czlonkowski/n8n-mcp/issues) or check the [n8n forums](https://community.n8n.io).

3604
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{
"name": "n8n-mcp",
"version": "2.10.3",
"version": "2.10.1",
"description": "Integration between n8n workflow automation and Model Context Protocol (MCP)",
"main": "dist/index.js",
"bin": {
@@ -128,12 +128,12 @@
},
"dependencies": {
"@modelcontextprotocol/sdk": "^1.13.2",
"@n8n/n8n-nodes-langchain": "^1.104.1",
"@n8n/n8n-nodes-langchain": "^1.103.1",
"dotenv": "^16.5.0",
"express": "^5.1.0",
"n8n": "^1.105.2",
"n8n-core": "^1.104.1",
"n8n-workflow": "^1.102.1",
"n8n": "^1.104.1",
"n8n-core": "^1.103.1",
"n8n-workflow": "^1.101.0",
"sql.js": "^1.13.0",
"uuid": "^10.0.0"
},

327
scripts/debug-n8n-mode.js Normal file
View File

@@ -0,0 +1,327 @@
#!/usr/bin/env node
/**
* Debug script for n8n integration issues
* Tests MCP protocol compliance and identifies schema validation problems
*/
const http = require('http');
const crypto = require('crypto');
const MCP_PORT = process.env.MCP_PORT || 3001;
const AUTH_TOKEN = process.env.AUTH_TOKEN || 'test-token-for-n8n-testing-minimum-32-chars';
console.log('🔍 Debugging n8n MCP Integration Issues');
console.log('=====================================\n');
// Test data for different MCP protocol calls
const testCases = [
{
name: 'MCP Initialize',
path: '/mcp',
method: 'POST',
data: {
jsonrpc: '2.0',
method: 'initialize',
params: {
protocolVersion: '2025-03-26',
capabilities: {
tools: {}
},
clientInfo: {
name: 'n8n-debug-test',
version: '1.0.0'
}
},
id: 1
}
},
{
name: 'Tools List',
path: '/mcp',
method: 'POST',
sessionId: null, // Will be set after initialize
data: {
jsonrpc: '2.0',
method: 'tools/list',
params: {},
id: 2
}
},
{
name: 'Tools Call - tools_documentation',
path: '/mcp',
method: 'POST',
sessionId: null, // Will be set after initialize
data: {
jsonrpc: '2.0',
method: 'tools/call',
params: {
name: 'tools_documentation',
arguments: {}
},
id: 3
}
},
{
name: 'Tools Call - get_node_essentials',
path: '/mcp',
method: 'POST',
sessionId: null, // Will be set after initialize
data: {
jsonrpc: '2.0',
method: 'tools/call',
params: {
name: 'get_node_essentials',
arguments: {
nodeType: 'nodes-base.httpRequest'
}
},
id: 4
}
}
];
async function makeRequest(testCase) {
return new Promise((resolve, reject) => {
const data = JSON.stringify(testCase.data);
const options = {
hostname: 'localhost',
port: MCP_PORT,
path: testCase.path,
method: testCase.method,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data),
'Authorization': `Bearer ${AUTH_TOKEN}`,
'Accept': 'application/json, text/event-stream' // Fix for StreamableHTTPServerTransport
}
};
// Add session ID header if available
if (testCase.sessionId) {
options.headers['Mcp-Session-Id'] = testCase.sessionId;
}
console.log(`📤 Making request: ${testCase.name}`);
console.log(` Method: ${testCase.method} ${testCase.path}`);
if (testCase.sessionId) {
console.log(` Session-ID: ${testCase.sessionId}`);
}
console.log(` Data: ${data}`);
const req = http.request(options, (res) => {
let responseData = '';
console.log(`📥 Response Status: ${res.statusCode}`);
console.log(` Headers:`, res.headers);
res.on('data', (chunk) => {
responseData += chunk;
});
res.on('end', () => {
try {
let parsed;
// Handle SSE format response
if (responseData.startsWith('event: message\ndata: ')) {
const dataLine = responseData.split('\n').find(line => line.startsWith('data: '));
if (dataLine) {
const jsonData = dataLine.substring(6); // Remove 'data: '
parsed = JSON.parse(jsonData);
} else {
throw new Error('Could not extract JSON from SSE response');
}
} else {
parsed = JSON.parse(responseData);
}
resolve({
statusCode: res.statusCode,
headers: res.headers,
data: parsed,
raw: responseData
});
} catch (e) {
resolve({
statusCode: res.statusCode,
headers: res.headers,
data: null,
raw: responseData,
parseError: e.message
});
}
});
});
req.on('error', (err) => {
reject(err);
});
req.write(data);
req.end();
});
}
async function validateMCPResponse(testCase, response) {
console.log(`✅ Validating response for: ${testCase.name}`);
const issues = [];
// Check HTTP status
if (response.statusCode !== 200) {
issues.push(`❌ Expected HTTP 200, got ${response.statusCode}`);
}
// Check JSON-RPC structure
if (!response.data) {
issues.push(`❌ Response is not valid JSON: ${response.parseError}`);
return issues;
}
if (response.data.jsonrpc !== '2.0') {
issues.push(`❌ Missing or invalid jsonrpc field: ${response.data.jsonrpc}`);
}
if (response.data.id !== testCase.data.id) {
issues.push(`❌ ID mismatch: expected ${testCase.data.id}, got ${response.data.id}`);
}
// Method-specific validation
if (testCase.data.method === 'initialize') {
if (!response.data.result) {
issues.push(`❌ Initialize response missing result field`);
} else {
if (!response.data.result.protocolVersion) {
issues.push(`❌ Initialize response missing protocolVersion`);
} else if (response.data.result.protocolVersion !== '2025-03-26') {
issues.push(`❌ Protocol version mismatch: expected 2025-03-26, got ${response.data.result.protocolVersion}`);
}
if (!response.data.result.capabilities) {
issues.push(`❌ Initialize response missing capabilities`);
}
if (!response.data.result.serverInfo) {
issues.push(`❌ Initialize response missing serverInfo`);
}
}
// Extract session ID for subsequent requests
if (response.headers['mcp-session-id']) {
console.log(`📋 Session ID: ${response.headers['mcp-session-id']}`);
return { issues, sessionId: response.headers['mcp-session-id'] };
} else {
issues.push(`❌ Initialize response missing Mcp-Session-Id header`);
}
}
if (testCase.data.method === 'tools/list') {
if (!response.data.result || !response.data.result.tools) {
issues.push(`❌ Tools list response missing tools array`);
} else {
console.log(`📋 Found ${response.data.result.tools.length} tools`);
}
}
if (testCase.data.method === 'tools/call') {
if (!response.data.result) {
issues.push(`❌ Tool call response missing result field`);
} else if (!response.data.result.content) {
issues.push(`❌ Tool call response missing content array`);
} else if (!Array.isArray(response.data.result.content)) {
issues.push(`❌ Tool call response content is not an array`);
} else {
// Validate content structure
for (let i = 0; i < response.data.result.content.length; i++) {
const content = response.data.result.content[i];
if (!content.type) {
issues.push(`❌ Content item ${i} missing type field`);
}
if (content.type === 'text' && !content.text) {
issues.push(`❌ Text content item ${i} missing text field`);
}
}
}
}
if (issues.length === 0) {
console.log(`${testCase.name} validation passed`);
} else {
console.log(`${testCase.name} validation failed:`);
issues.forEach(issue => console.log(` ${issue}`));
}
return { issues };
}
async function runTests() {
console.log('Starting MCP protocol compliance tests...\n');
let sessionId = null;
let allIssues = [];
for (const testCase of testCases) {
try {
// Set session ID from previous test
if (sessionId && testCase.name !== 'MCP Initialize') {
testCase.sessionId = sessionId;
}
const response = await makeRequest(testCase);
console.log(`📄 Raw Response: ${response.raw}\n`);
const validation = await validateMCPResponse(testCase, response);
if (validation.sessionId) {
sessionId = validation.sessionId;
}
allIssues.push(...validation.issues);
console.log('─'.repeat(50));
} catch (error) {
console.error(`❌ Request failed for ${testCase.name}:`, error.message);
allIssues.push(`Request failed for ${testCase.name}: ${error.message}`);
}
}
// Summary
console.log('\n📊 SUMMARY');
console.log('==========');
if (allIssues.length === 0) {
console.log('🎉 All tests passed! MCP protocol compliance looks good.');
} else {
console.log(`❌ Found ${allIssues.length} issues:`);
allIssues.forEach((issue, i) => {
console.log(` ${i + 1}. ${issue}`);
});
}
console.log('\n🔍 Recommendations:');
console.log('1. Check MCP server logs at /tmp/mcp-server.log');
console.log('2. Verify protocol version consistency (should be 2025-03-26)');
console.log('3. Ensure tool schemas match MCP specification exactly');
console.log('4. Test with actual n8n MCP Client Tool node');
}
// Check if MCP server is running
console.log(`Checking if MCP server is running at localhost:${MCP_PORT}...`);
const healthCheck = http.get(`http://localhost:${MCP_PORT}/health`, (res) => {
if (res.statusCode === 200) {
console.log('✅ MCP server is running\n');
runTests().catch(console.error);
} else {
console.error('❌ MCP server health check failed:', res.statusCode);
process.exit(1);
}
}).on('error', (err) => {
console.error('❌ MCP server is not running. Please start it first:', err.message);
console.error('Use: npm run start:n8n');
process.exit(1);
});

95
scripts/test-n8n-mode.sh Executable file
View File

@@ -0,0 +1,95 @@
#!/bin/bash
# Test script for n8n MCP integration fixes
set -e
echo "🔧 Testing n8n MCP Integration Fixes"
echo "===================================="
# Configuration
MCP_PORT=${MCP_PORT:-3001}
AUTH_TOKEN=${AUTH_TOKEN:-"test-token-for-n8n-testing-minimum-32-chars"}
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Cleanup function
cleanup() {
echo -e "\n${YELLOW}🧹 Cleaning up...${NC}"
if [ -n "$MCP_PID" ] && kill -0 $MCP_PID 2>/dev/null; then
echo "Stopping MCP server..."
kill $MCP_PID 2>/dev/null || true
wait $MCP_PID 2>/dev/null || true
fi
echo -e "${GREEN}✅ Cleanup complete${NC}"
}
trap cleanup EXIT INT TERM
# Check if we're in the right directory
if [ ! -f "package.json" ] || [ ! -d "dist" ]; then
echo -e "${RED}❌ Error: Must run from n8n-mcp directory${NC}"
exit 1
fi
# Build the project (our fixes)
echo -e "${YELLOW}📦 Building project with fixes...${NC}"
npm run build
# Start MCP server in n8n mode
echo -e "\n${GREEN}🚀 Starting MCP server in n8n mode...${NC}"
N8N_MODE=true \
MCP_MODE=http \
AUTH_TOKEN="${AUTH_TOKEN}" \
PORT=${MCP_PORT} \
DEBUG_MCP=true \
node dist/mcp/index.js > /tmp/mcp-n8n-test.log 2>&1 &
MCP_PID=$!
echo -e "${YELLOW}📄 MCP server logs: /tmp/mcp-n8n-test.log${NC}"
# Wait for server to start
echo -e "${YELLOW}⏳ Waiting for MCP server to start...${NC}"
for i in {1..15}; do
if curl -s http://localhost:${MCP_PORT}/health >/dev/null 2>&1; then
echo -e "${GREEN}✅ MCP server is ready!${NC}"
break
fi
if [ $i -eq 15 ]; then
echo -e "${RED}❌ MCP server failed to start${NC}"
echo "Server logs:"
cat /tmp/mcp-n8n-test.log
exit 1
fi
sleep 1
done
# Test the protocol fixes
echo -e "\n${BLUE}🧪 Testing protocol fixes...${NC}"
# Run our debug script
echo -e "${YELLOW}Running comprehensive MCP protocol tests...${NC}"
node scripts/debug-n8n-mode.js
echo -e "\n${GREEN}🎉 Test complete!${NC}"
echo -e "\n📋 Summary of fixes applied:"
echo -e " ✅ Fixed protocol version mismatch (now using 2025-03-26)"
echo -e " ✅ Enhanced tool response formatting and size validation"
echo -e " ✅ Added comprehensive parameter validation"
echo -e " ✅ Improved error handling and logging"
echo -e " ✅ Added initialization request debugging"
echo -e "\n📝 Next steps:"
echo -e " 1. If tests pass, the n8n schema validation errors should be resolved"
echo -e " 2. Test with actual n8n MCP Client Tool node"
echo -e " 3. Monitor logs at /tmp/mcp-n8n-test.log for any remaining issues"
echo -e "\n${YELLOW}Press any key to view recent server logs, or Ctrl+C to exit...${NC}"
read -n 1
echo -e "\n${BLUE}📄 Recent server logs:${NC}"
tail -50 /tmp/mcp-n8n-test.log

428
scripts/test-n8n-mode.ts Normal file
View File

@@ -0,0 +1,428 @@
#!/usr/bin/env ts-node
/**
* TypeScript test script for n8n MCP integration fixes
* Tests the protocol changes and identifies any remaining issues
*/
import http from 'http';
import { spawn, ChildProcess } from 'child_process';
import path from 'path';
interface TestResult {
name: string;
passed: boolean;
error?: string;
response?: any;
}
class N8nMcpTester {
private mcpProcess: ChildProcess | null = null;
private readonly mcpPort = 3001;
private readonly authToken = 'test-token-for-n8n-testing-minimum-32-chars';
private sessionId: string | null = null;
async start(): Promise<void> {
console.log('🔧 Testing n8n MCP Integration Fixes');
console.log('====================================\n');
try {
await this.startMcpServer();
await this.runTests();
} finally {
await this.cleanup();
}
}
private async startMcpServer(): Promise<void> {
console.log('📦 Starting MCP server in n8n mode...');
const projectRoot = path.resolve(__dirname, '..');
this.mcpProcess = spawn('node', ['dist/mcp/index.js'], {
cwd: projectRoot,
env: {
...process.env,
N8N_MODE: 'true',
MCP_MODE: 'http',
AUTH_TOKEN: this.authToken,
PORT: this.mcpPort.toString(),
DEBUG_MCP: 'true'
},
stdio: ['ignore', 'pipe', 'pipe']
});
// Log server output
this.mcpProcess.stdout?.on('data', (data) => {
console.log(`[MCP] ${data.toString().trim()}`);
});
this.mcpProcess.stderr?.on('data', (data) => {
console.error(`[MCP ERROR] ${data.toString().trim()}`);
});
// Wait for server to be ready
await this.waitForServer();
}
private async waitForServer(): Promise<void> {
console.log('⏳ Waiting for MCP server to be ready...');
for (let i = 0; i < 30; i++) {
try {
await this.makeHealthCheck();
console.log('✅ MCP server is ready!\n');
return;
} catch (error) {
if (i === 29) {
throw new Error('MCP server failed to start within 30 seconds');
}
await this.sleep(1000);
}
}
}
private makeHealthCheck(): Promise<void> {
return new Promise((resolve, reject) => {
const req = http.get(`http://localhost:${this.mcpPort}/health`, (res) => {
if (res.statusCode === 200) {
resolve();
} else {
reject(new Error(`Health check failed: ${res.statusCode}`));
}
});
req.on('error', reject);
req.setTimeout(5000, () => {
req.destroy();
reject(new Error('Health check timeout'));
});
});
}
private async runTests(): Promise<void> {
const tests: TestResult[] = [];
// Test 1: Initialize with correct protocol version
tests.push(await this.testInitialize());
// Test 2: List tools
tests.push(await this.testListTools());
// Test 3: Call tools_documentation
tests.push(await this.testToolCall('tools_documentation', {}));
// Test 4: Call get_node_essentials with parameters
tests.push(await this.testToolCall('get_node_essentials', {
nodeType: 'nodes-base.httpRequest'
}));
// Test 5: Call with invalid parameters (should handle gracefully)
tests.push(await this.testToolCallInvalid());
this.printResults(tests);
}
private async testInitialize(): Promise<TestResult> {
console.log('🧪 Testing MCP Initialize...');
try {
const response = await this.makeRequest('POST', '/mcp', {
jsonrpc: '2.0',
method: 'initialize',
params: {
protocolVersion: '2025-03-26',
capabilities: { tools: {} },
clientInfo: { name: 'n8n-test', version: '1.0.0' }
},
id: 1
});
if (response.statusCode !== 200) {
return {
name: 'Initialize',
passed: false,
error: `HTTP ${response.statusCode}`
};
}
const data = JSON.parse(response.body);
// Extract session ID
this.sessionId = response.headers['mcp-session-id'] as string;
if (data.result?.protocolVersion === '2025-03-26') {
return {
name: 'Initialize',
passed: true,
response: data
};
} else {
return {
name: 'Initialize',
passed: false,
error: `Wrong protocol version: ${data.result?.protocolVersion}`,
response: data
};
}
} catch (error) {
return {
name: 'Initialize',
passed: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
}
private async testListTools(): Promise<TestResult> {
console.log('🧪 Testing Tools List...');
try {
const response = await this.makeRequest('POST', '/mcp', {
jsonrpc: '2.0',
method: 'tools/list',
params: {},
id: 2
}, this.sessionId);
if (response.statusCode !== 200) {
return {
name: 'List Tools',
passed: false,
error: `HTTP ${response.statusCode}`
};
}
const data = JSON.parse(response.body);
if (data.result?.tools && Array.isArray(data.result.tools)) {
return {
name: 'List Tools',
passed: true,
response: { toolCount: data.result.tools.length }
};
} else {
return {
name: 'List Tools',
passed: false,
error: 'Missing or invalid tools array',
response: data
};
}
} catch (error) {
return {
name: 'List Tools',
passed: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
}
private async testToolCall(toolName: string, args: any): Promise<TestResult> {
console.log(`🧪 Testing Tool Call: ${toolName}...`);
try {
const response = await this.makeRequest('POST', '/mcp', {
jsonrpc: '2.0',
method: 'tools/call',
params: {
name: toolName,
arguments: args
},
id: 3
}, this.sessionId);
if (response.statusCode !== 200) {
return {
name: `Tool Call: ${toolName}`,
passed: false,
error: `HTTP ${response.statusCode}`
};
}
const data = JSON.parse(response.body);
if (data.result?.content && Array.isArray(data.result.content)) {
return {
name: `Tool Call: ${toolName}`,
passed: true,
response: { contentItems: data.result.content.length }
};
} else {
return {
name: `Tool Call: ${toolName}`,
passed: false,
error: 'Missing or invalid content array',
response: data
};
}
} catch (error) {
return {
name: `Tool Call: ${toolName}`,
passed: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
}
private async testToolCallInvalid(): Promise<TestResult> {
console.log('🧪 Testing Tool Call with invalid parameters...');
try {
const response = await this.makeRequest('POST', '/mcp', {
jsonrpc: '2.0',
method: 'tools/call',
params: {
name: 'get_node_essentials',
arguments: {} // Missing required nodeType parameter
},
id: 4
}, this.sessionId);
if (response.statusCode !== 200) {
return {
name: 'Tool Call: Invalid Params',
passed: false,
error: `HTTP ${response.statusCode}`
};
}
const data = JSON.parse(response.body);
// Should either return an error response or handle gracefully
if (data.error || (data.result?.isError && data.result?.content)) {
return {
name: 'Tool Call: Invalid Params',
passed: true,
response: { handledGracefully: true }
};
} else {
return {
name: 'Tool Call: Invalid Params',
passed: false,
error: 'Did not handle invalid parameters properly',
response: data
};
}
} catch (error) {
return {
name: 'Tool Call: Invalid Params',
passed: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
}
private makeRequest(method: string, path: string, data?: any, sessionId?: string | null): Promise<{
statusCode: number;
headers: http.IncomingHttpHeaders;
body: string;
}> {
return new Promise((resolve, reject) => {
const postData = data ? JSON.stringify(data) : '';
const options: http.RequestOptions = {
hostname: 'localhost',
port: this.mcpPort,
path,
method,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.authToken}`,
...(postData && { 'Content-Length': Buffer.byteLength(postData) }),
...(sessionId && { 'Mcp-Session-Id': sessionId })
}
};
const req = http.request(options, (res) => {
let body = '';
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
resolve({
statusCode: res.statusCode || 0,
headers: res.headers,
body
});
});
});
req.on('error', reject);
req.setTimeout(10000, () => {
req.destroy();
reject(new Error('Request timeout'));
});
if (postData) {
req.write(postData);
}
req.end();
});
}
private printResults(tests: TestResult[]): void {
console.log('\n📊 TEST RESULTS');
console.log('================');
const passed = tests.filter(t => t.passed).length;
const total = tests.length;
tests.forEach(test => {
const status = test.passed ? '✅' : '❌';
console.log(`${status} ${test.name}`);
if (!test.passed && test.error) {
console.log(` Error: ${test.error}`);
}
if (test.response) {
console.log(` Response: ${JSON.stringify(test.response, null, 2)}`);
}
});
console.log(`\n📈 Summary: ${passed}/${total} tests passed`);
if (passed === total) {
console.log('🎉 All tests passed! The n8n integration fixes should resolve the schema validation errors.');
} else {
console.log('❌ Some tests failed. Please review the errors above.');
}
}
private async cleanup(): Promise<void> {
console.log('\n🧹 Cleaning up...');
if (this.mcpProcess) {
this.mcpProcess.kill('SIGTERM');
// Wait for graceful shutdown
await new Promise<void>((resolve) => {
if (!this.mcpProcess) {
resolve();
return;
}
const timeout = setTimeout(() => {
this.mcpProcess?.kill('SIGKILL');
resolve();
}, 5000);
this.mcpProcess.on('exit', () => {
clearTimeout(timeout);
resolve();
});
});
}
console.log('✅ Cleanup complete');
}
private sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
// Run the tests
if (require.main === module) {
const tester = new N8nMcpTester();
tester.start().catch(console.error);
}
export { N8nMcpTester };

View File

@@ -22,9 +22,8 @@ export class NodeRepository {
node_type, package_name, display_name, description,
category, development_style, is_ai_tool, is_trigger,
is_webhook, is_versioned, version, documentation,
properties_schema, operations, credentials_required,
outputs, output_names
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
properties_schema, operations, credentials_required
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
stmt.run(
@@ -42,9 +41,7 @@ export class NodeRepository {
node.documentation || null,
JSON.stringify(node.properties, null, 2),
JSON.stringify(node.operations, null, 2),
JSON.stringify(node.credentials, null, 2),
node.outputs ? JSON.stringify(node.outputs, null, 2) : null,
node.outputNames ? JSON.stringify(node.outputNames, null, 2) : null
JSON.stringify(node.credentials, null, 2)
);
}
@@ -73,9 +70,7 @@ export class NodeRepository {
properties: this.safeJsonParse(row.properties_schema, []),
operations: this.safeJsonParse(row.operations, []),
credentials: this.safeJsonParse(row.credentials_required, []),
hasDocumentation: !!row.documentation,
outputs: row.outputs ? this.safeJsonParse(row.outputs, null) : null,
outputNames: row.output_names ? this.safeJsonParse(row.output_names, null) : null
hasDocumentation: !!row.documentation
};
}
@@ -243,9 +238,7 @@ export class NodeRepository {
properties: this.safeJsonParse(row.properties_schema, []),
operations: this.safeJsonParse(row.operations, []),
credentials: this.safeJsonParse(row.credentials_required, []),
hasDocumentation: !!row.documentation,
outputs: row.outputs ? this.safeJsonParse(row.outputs, null) : null,
outputNames: row.output_names ? this.safeJsonParse(row.output_names, null) : null
hasDocumentation: !!row.documentation
};
}
}

View File

@@ -15,8 +15,6 @@ CREATE TABLE IF NOT EXISTS nodes (
properties_schema TEXT,
operations TEXT,
credentials_required TEXT,
outputs TEXT, -- JSON array of output definitions
output_names TEXT, -- JSON array of output names
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

View File

@@ -50,12 +50,8 @@ export class DocsMapper {
for (const relativePath of possiblePaths) {
try {
const fullPath = path.join(this.docsPath, relativePath);
let content = await fs.readFile(fullPath, 'utf-8');
const content = await fs.readFile(fullPath, 'utf-8');
console.log(` ✓ Found docs at: ${relativePath}`);
// Inject special guidance for loop nodes
content = this.enhanceLoopNodeDocumentation(nodeType, content);
return content;
} catch (error) {
// File doesn't exist, try next
@@ -66,56 +62,4 @@ export class DocsMapper {
console.log(` ✗ No docs found for ${nodeName}`);
return null;
}
private enhanceLoopNodeDocumentation(nodeType: string, content: string): string {
// Add critical output index information for SplitInBatches
if (nodeType.includes('splitInBatches')) {
const outputGuidance = `
## CRITICAL OUTPUT CONNECTION INFORMATION
**⚠️ OUTPUT INDICES ARE COUNTERINTUITIVE ⚠️**
The SplitInBatches node has TWO outputs with specific indices:
- **Output 0 (index 0) = "done"**: Receives final processed data when loop completes
- **Output 1 (index 1) = "loop"**: Receives current batch data during iteration
### Correct Connection Pattern:
1. Connect nodes that PROCESS items inside the loop to **Output 1 ("loop")**
2. Connect nodes that run AFTER the loop completes to **Output 0 ("done")**
3. The last processing node in the loop must connect back to the SplitInBatches node
### Common Mistake:
AI assistants often connect these backwards because the logical flow (loop first, then done) doesn't match the technical indices (done=0, loop=1).
`;
// Insert after the main description
const insertPoint = content.indexOf('## When to use');
if (insertPoint > -1) {
content = content.slice(0, insertPoint) + outputGuidance + content.slice(insertPoint);
} else {
// Append if no good insertion point found
content = outputGuidance + '\n' + content;
}
}
// Add guidance for IF node
if (nodeType.includes('.if')) {
const outputGuidance = `
## Output Connection Information
The IF node has TWO outputs:
- **Output 0 (index 0) = "true"**: Items that match the condition
- **Output 1 (index 1) = "false"**: Items that do not match the condition
`;
const insertPoint = content.indexOf('## Node parameters');
if (insertPoint > -1) {
content = content.slice(0, insertPoint) + outputGuidance + content.slice(insertPoint);
}
}
return content;
}
}

View File

@@ -28,7 +28,6 @@ import { handleUpdatePartialWorkflow } from './handlers-workflow-diff';
import { getToolDocumentation, getToolsOverview } from './tools-documentation';
import { PROJECT_VERSION } from '../utils/version';
import { normalizeNodeType, getNodeTypeAlternatives, getWorkflowNodeType } from '../utils/node-utils';
import { ToolValidation, Validator, ValidationError } from '../utils/validation-schemas';
import {
negotiateProtocolVersion,
logProtocolNegotiation,
@@ -461,77 +460,9 @@ export class N8NDocumentationMCPServer {
}
/**
* Enhanced parameter validation using schemas
* Validate required parameters for tool execution
*/
private validateToolParams(toolName: string, args: any, legacyRequiredParams?: string[]): void {
try {
// If legacy required params are provided, use the new validation but fall back to basic if needed
let validationResult;
switch (toolName) {
case 'validate_node_operation':
validationResult = ToolValidation.validateNodeOperation(args);
break;
case 'validate_node_minimal':
validationResult = ToolValidation.validateNodeMinimal(args);
break;
case 'validate_workflow':
case 'validate_workflow_connections':
case 'validate_workflow_expressions':
validationResult = ToolValidation.validateWorkflow(args);
break;
case 'search_nodes':
validationResult = ToolValidation.validateSearchNodes(args);
break;
case 'list_node_templates':
validationResult = ToolValidation.validateListNodeTemplates(args);
break;
case 'n8n_create_workflow':
validationResult = ToolValidation.validateCreateWorkflow(args);
break;
case 'n8n_get_workflow':
case 'n8n_get_workflow_details':
case 'n8n_get_workflow_structure':
case 'n8n_get_workflow_minimal':
case 'n8n_update_full_workflow':
case 'n8n_delete_workflow':
case 'n8n_validate_workflow':
case 'n8n_get_execution':
case 'n8n_delete_execution':
validationResult = ToolValidation.validateWorkflowId(args);
break;
default:
// For tools not yet migrated to schema validation, use basic validation
return this.validateToolParamsBasic(toolName, args, legacyRequiredParams || []);
}
if (!validationResult.valid) {
const errorMessage = Validator.formatErrors(validationResult, toolName);
logger.error(`Parameter validation failed for ${toolName}:`, errorMessage);
throw new ValidationError(errorMessage);
}
} catch (error) {
// Handle validation errors properly
if (error instanceof ValidationError) {
throw error; // Re-throw validation errors as-is
}
// Handle unexpected errors from validation system
logger.error(`Validation system error for ${toolName}:`, error);
// Provide a user-friendly error message
const errorMessage = error instanceof Error
? `Internal validation error: ${error.message}`
: `Internal validation error while processing ${toolName}`;
throw new Error(errorMessage);
}
}
/**
* Legacy parameter validation (fallback)
*/
private validateToolParamsBasic(toolName: string, args: any, requiredParams: string[]): void {
private validateToolParams(toolName: string, args: any, requiredParams: string[]): void {
const missing: string[] = [];
for (const param of requiredParams) {
@@ -688,17 +619,12 @@ export class N8NDocumentationMCPServer {
fix: 'Provide config as an object with node properties'
}],
warnings: [],
suggestions: [
'🔧 RECOVERY: Invalid config detected. Fix with:',
' • Ensure config is an object: { "resource": "...", "operation": "..." }',
' • Use get_node_essentials to see required fields for this node type',
' • Check if the node type is correct before configuring it'
],
suggestions: [],
summary: {
hasErrors: true,
errorCount: 1,
warningCount: 0,
suggestionCount: 3
suggestionCount: 0
}
};
}
@@ -712,10 +638,7 @@ export class N8NDocumentationMCPServer {
nodeType: args.nodeType || 'unknown',
displayName: 'Unknown Node',
valid: false,
missingRequiredFields: [
'Invalid config format - expected object',
'🔧 RECOVERY: Use format { "resource": "...", "operation": "..." } or {} for empty config'
]
missingRequiredFields: ['Invalid config format - expected object']
};
}
return this.validateNodeMinimal(args.nodeType, args.config);
@@ -911,26 +834,10 @@ export class N8NDocumentationMCPServer {
null
};
// Process outputs to provide clear mapping
let outputs = undefined;
if (node.outputNames && node.outputNames.length > 0) {
outputs = node.outputNames.map((name: string, index: number) => {
// Special handling for loop nodes like SplitInBatches
const descriptions = this.getOutputDescriptions(node.nodeType, name, index);
return {
index,
name,
description: descriptions.description,
connectionGuidance: descriptions.connectionGuidance
};
});
}
return {
...node,
workflowNodeType: getWorkflowNodeType(node.package, node.nodeType),
aiToolCapabilities,
outputs
aiToolCapabilities
};
}
@@ -2030,52 +1937,6 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
};
}
private getOutputDescriptions(nodeType: string, outputName: string, index: number): { description: string, connectionGuidance: string } {
// Special handling for loop nodes
if (nodeType === 'nodes-base.splitInBatches') {
if (outputName === 'done' && index === 0) {
return {
description: 'Final processed data after all iterations complete',
connectionGuidance: 'Connect to nodes that should run AFTER the loop completes'
};
} else if (outputName === 'loop' && index === 1) {
return {
description: 'Current batch data for this iteration',
connectionGuidance: 'Connect to nodes that process items INSIDE the loop (and connect their output back to this node)'
};
}
}
// Special handling for IF node
if (nodeType === 'nodes-base.if') {
if (outputName === 'true' && index === 0) {
return {
description: 'Items that match the condition',
connectionGuidance: 'Connect to nodes that handle the TRUE case'
};
} else if (outputName === 'false' && index === 1) {
return {
description: 'Items that do not match the condition',
connectionGuidance: 'Connect to nodes that handle the FALSE case'
};
}
}
// Special handling for Switch node
if (nodeType === 'nodes-base.switch') {
return {
description: `Output ${index}: ${outputName || 'Route ' + index}`,
connectionGuidance: `Connect to nodes for the "${outputName || 'route ' + index}" case`
};
}
// Default handling
return {
description: outputName || `Output ${index}`,
connectionGuidance: `Connect to downstream nodes`
};
}
private getCommonAIToolUseCases(nodeType: string): string[] {
const useCaseMap: Record<string, string[]> = {
'nodes-base.slack': [
@@ -2218,12 +2079,12 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
// Get properties
const properties = node.properties || [];
// Extract operation context (safely handle undefined config properties)
// Extract operation context
const operationContext = {
resource: config?.resource,
operation: config?.operation,
action: config?.action,
mode: config?.mode
resource: config.resource,
operation: config.operation,
action: config.action,
mode: config.mode
};
// Find missing required fields
@@ -2240,7 +2101,7 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
// Check show conditions
if (prop.displayOptions.show) {
for (const [key, values] of Object.entries(prop.displayOptions.show)) {
const configValue = config?.[key];
const configValue = config[key];
const expectedValues = Array.isArray(values) ? values : [values];
if (!expectedValues.includes(configValue)) {
@@ -2253,7 +2114,7 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
// Check hide conditions
if (isVisible && prop.displayOptions.hide) {
for (const [key, values] of Object.entries(prop.displayOptions.hide)) {
const configValue = config?.[key];
const configValue = config[key];
const expectedValues = Array.isArray(values) ? values : [values];
if (expectedValues.includes(configValue)) {
@@ -2266,8 +2127,8 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
if (!isVisible) continue;
}
// Check if field is missing (safely handle null/undefined config)
if (!config || !(prop.name in config)) {
// Check if field is missing
if (!(prop.name in config)) {
missingFields.push(prop.displayName || prop.name);
}
}

View File

@@ -16,19 +16,14 @@ export interface ParsedNode {
isVersioned: boolean;
packageName: string;
documentation?: string;
outputs?: any[];
outputNames?: string[];
}
export class NodeParser {
private propertyExtractor = new PropertyExtractor();
private currentNodeClass: any = null;
parse(nodeClass: any, packageName: string): ParsedNode {
this.currentNodeClass = nodeClass;
// Get base description (handles versioned nodes)
const description = this.getNodeDescription(nodeClass);
const outputInfo = this.extractOutputs(description);
return {
style: this.detectStyle(nodeClass),
@@ -44,9 +39,7 @@ export class NodeParser {
operations: this.propertyExtractor.extractOperations(nodeClass),
version: this.extractVersion(nodeClass),
isVersioned: this.detectVersioned(nodeClass),
packageName: packageName,
outputs: outputInfo.outputs,
outputNames: outputInfo.outputNames
packageName: packageName
};
}
@@ -229,51 +222,4 @@ export class NodeParser {
return false;
}
private extractOutputs(description: any): { outputs?: any[], outputNames?: string[] } {
const result: { outputs?: any[], outputNames?: string[] } = {};
// First check the base description
if (description.outputs) {
result.outputs = Array.isArray(description.outputs) ? description.outputs : [description.outputs];
}
if (description.outputNames) {
result.outputNames = Array.isArray(description.outputNames) ? description.outputNames : [description.outputNames];
}
// If no outputs found and this is a versioned node, check the latest version
if (!result.outputs && !result.outputNames) {
const nodeClass = this.currentNodeClass; // We'll need to track this
if (nodeClass) {
try {
const instance = new nodeClass();
if (instance.nodeVersions) {
// Get the latest version
const versions = Object.keys(instance.nodeVersions).map(Number);
const latestVersion = Math.max(...versions);
const versionedDescription = instance.nodeVersions[latestVersion]?.description;
if (versionedDescription) {
if (versionedDescription.outputs) {
result.outputs = Array.isArray(versionedDescription.outputs)
? versionedDescription.outputs
: [versionedDescription.outputs];
}
if (versionedDescription.outputNames) {
result.outputNames = Array.isArray(versionedDescription.outputNames)
? versionedDescription.outputNames
: [versionedDescription.outputNames];
}
}
}
} catch (e) {
// Ignore errors from instantiating node
}
}
}
return result;
}
}

View File

@@ -45,19 +45,6 @@ export class EnhancedConfigValidator extends ConfigValidator {
mode: ValidationMode = 'operation',
profile: ValidationProfile = 'ai-friendly'
): EnhancedValidationResult {
// Input validation - ensure parameters are valid
if (typeof nodeType !== 'string') {
throw new Error(`Invalid nodeType: expected string, got ${typeof nodeType}`);
}
if (!config || typeof config !== 'object') {
throw new Error(`Invalid config: expected object, got ${typeof config}`);
}
if (!Array.isArray(properties)) {
throw new Error(`Invalid properties: expected array, got ${typeof properties}`);
}
// Extract operation context from config
const operationContext = this.extractOperationContext(config);
@@ -203,17 +190,6 @@ export class EnhancedConfigValidator extends ConfigValidator {
config: Record<string, any>,
result: EnhancedValidationResult
): void {
// Type safety check - this should never happen with proper validation
if (typeof nodeType !== 'string') {
result.errors.push({
type: 'invalid_type',
property: 'nodeType',
message: `Invalid nodeType: expected string, got ${typeof nodeType}`,
fix: 'Provide a valid node type string (e.g., "nodes-base.webhook")'
});
return;
}
// First, validate fixedCollection properties for known problematic nodes
this.validateFixedCollectionStructures(nodeType, config, result);

View File

@@ -72,25 +72,11 @@ export interface WorkflowValidationResult {
}
export class WorkflowValidator {
private currentWorkflow: WorkflowJson | null = null;
constructor(
private nodeRepository: NodeRepository,
private nodeValidator: typeof EnhancedConfigValidator
) {}
/**
* Check if a node is a Sticky Note or other non-executable node
*/
private isStickyNote(node: WorkflowNode): boolean {
const stickyNoteTypes = [
'n8n-nodes-base.stickyNote',
'nodes-base.stickyNote',
'@n8n/n8n-nodes-base.stickyNote'
];
return stickyNoteTypes.includes(node.type);
}
/**
* Validate a complete workflow
*/
@@ -103,9 +89,6 @@ export class WorkflowValidator {
profile?: 'minimal' | 'runtime' | 'ai-friendly' | 'strict';
} = {}
): Promise<WorkflowValidationResult> {
// Store current workflow for access in helper methods
this.currentWorkflow = workflow;
const {
validateNodes = true,
validateConnections = true,
@@ -139,10 +122,9 @@ export class WorkflowValidator {
return result;
}
// Update statistics after null check (exclude sticky notes from counts)
const executableNodes = Array.isArray(workflow.nodes) ? workflow.nodes.filter(n => !this.isStickyNote(n)) : [];
result.statistics.totalNodes = executableNodes.length;
result.statistics.enabledNodes = executableNodes.filter(n => !n.disabled).length;
// Update statistics after null check
result.statistics.totalNodes = Array.isArray(workflow.nodes) ? workflow.nodes.length : 0;
result.statistics.enabledNodes = Array.isArray(workflow.nodes) ? workflow.nodes.filter(n => !n.disabled).length : 0;
// Basic workflow structure validation
this.validateWorkflowStructure(workflow, result);
@@ -156,26 +138,21 @@ export class WorkflowValidator {
// Validate connections if requested
if (validateConnections) {
this.validateConnections(workflow, result, profile);
this.validateConnections(workflow, result);
}
// Validate expressions if requested
if (validateExpressions && workflow.nodes.length > 0) {
this.validateExpressions(workflow, result, profile);
this.validateExpressions(workflow, result);
}
// Check workflow patterns and best practices
if (workflow.nodes.length > 0) {
this.checkWorkflowPatterns(workflow, result, profile);
this.checkWorkflowPatterns(workflow, result);
}
// Add suggestions based on findings
this.generateSuggestions(workflow, result);
// Add AI-specific recovery suggestions if there are errors
if (result.errors.length > 0) {
this.addErrorRecoverySuggestions(result);
}
}
} catch (error) {
@@ -326,7 +303,7 @@ export class WorkflowValidator {
profile: string
): Promise<void> {
for (const node of workflow.nodes) {
if (node.disabled || this.isStickyNote(node)) continue;
if (node.disabled) continue;
try {
// Validate node name length
@@ -518,8 +495,7 @@ export class WorkflowValidator {
*/
private validateConnections(
workflow: WorkflowJson,
result: WorkflowValidationResult,
profile: string = 'runtime'
result: WorkflowValidationResult
): void {
const nodeMap = new Map(workflow.nodes.map(n => [n.name, n]));
const nodeIdMap = new Map(workflow.nodes.map(n => [n.id, n]));
@@ -610,9 +586,9 @@ export class WorkflowValidator {
}
});
// Check for orphaned nodes (exclude sticky notes)
// Check for orphaned nodes
for (const node of workflow.nodes) {
if (node.disabled || this.isStickyNote(node)) continue;
if (node.disabled) continue;
const normalizedType = node.type.replace('n8n-nodes-base.', 'nodes-base.');
const isTrigger = normalizedType.toLowerCase().includes('trigger') ||
@@ -631,8 +607,8 @@ export class WorkflowValidator {
}
}
// Check for cycles (skip in minimal profile to reduce false positives)
if (profile !== 'minimal' && this.hasCycle(workflow)) {
// Check for cycles
if (this.hasCycle(workflow)) {
result.errors.push({
type: 'error',
message: 'Workflow contains a cycle (infinite loop)'
@@ -651,9 +627,6 @@ export class WorkflowValidator {
result: WorkflowValidationResult,
outputType: 'main' | 'error' | 'ai_tool'
): void {
// Get source node for special validation
const sourceNode = nodeMap.get(sourceName);
outputs.forEach((outputConnections, outputIndex) => {
if (!outputConnections) return;
@@ -668,26 +641,12 @@ export class WorkflowValidator {
return;
}
// Special validation for SplitInBatches node
if (sourceNode && sourceNode.type === 'n8n-nodes-base.splitInBatches') {
this.validateSplitInBatchesConnection(
sourceNode,
outputIndex,
connection,
nodeMap,
result
);
}
// Check for self-referencing connections
if (connection.node === sourceName) {
// This is only a warning for non-loop nodes
if (sourceNode && sourceNode.type !== 'n8n-nodes-base.splitInBatches') {
result.warnings.push({
type: 'warning',
message: `Node "${sourceName}" has a self-referencing connection. This can cause infinite loops.`
});
}
result.warnings.push({
type: 'warning',
message: `Node "${sourceName}" has a self-referencing connection. This can cause infinite loops.`
});
}
const targetNode = nodeMap.get(connection.node);
@@ -769,31 +728,12 @@ export class WorkflowValidator {
/**
* Check if workflow has cycles
* Allow legitimate loops for SplitInBatches and similar loop nodes
*/
private hasCycle(workflow: WorkflowJson): boolean {
const visited = new Set<string>();
const recursionStack = new Set<string>();
const nodeTypeMap = new Map<string, string>();
// Build node type map (exclude sticky notes)
workflow.nodes.forEach(node => {
if (!this.isStickyNote(node)) {
nodeTypeMap.set(node.name, node.type);
}
});
// Known legitimate loop node types
const loopNodeTypes = [
'n8n-nodes-base.splitInBatches',
'nodes-base.splitInBatches',
'n8n-nodes-base.itemLists',
'nodes-base.itemLists',
'n8n-nodes-base.loop',
'nodes-base.loop'
];
const hasCycleDFS = (nodeName: string, pathFromLoopNode: boolean = false): boolean => {
const hasCycleDFS = (nodeName: string): boolean => {
visited.add(nodeName);
recursionStack.add(nodeName);
@@ -819,23 +759,11 @@ export class WorkflowValidator {
});
}
const currentNodeType = nodeTypeMap.get(nodeName);
const isLoopNode = loopNodeTypes.includes(currentNodeType || '');
for (const target of allTargets) {
if (!visited.has(target)) {
if (hasCycleDFS(target, pathFromLoopNode || isLoopNode)) return true;
if (hasCycleDFS(target)) return true;
} else if (recursionStack.has(target)) {
// Allow cycles that involve legitimate loop nodes
const targetNodeType = nodeTypeMap.get(target);
const isTargetLoopNode = loopNodeTypes.includes(targetNodeType || '');
// If this cycle involves a loop node, it's legitimate
if (isTargetLoopNode || pathFromLoopNode || isLoopNode) {
continue; // Allow this cycle
}
return true; // Reject other cycles
return true;
}
}
}
@@ -844,9 +772,9 @@ export class WorkflowValidator {
return false;
};
// Check from all executable nodes (exclude sticky notes)
// Check from all nodes
for (const node of workflow.nodes) {
if (!this.isStickyNote(node) && !visited.has(node.name)) {
if (!visited.has(node.name)) {
if (hasCycleDFS(node.name)) return true;
}
}
@@ -859,13 +787,12 @@ export class WorkflowValidator {
*/
private validateExpressions(
workflow: WorkflowJson,
result: WorkflowValidationResult,
profile: string = 'runtime'
result: WorkflowValidationResult
): void {
const nodeNames = workflow.nodes.map(n => n.name);
for (const node of workflow.nodes) {
if (node.disabled || this.isStickyNote(node)) continue;
if (node.disabled) continue;
// Create expression context
const context = {
@@ -954,27 +881,23 @@ export class WorkflowValidator {
*/
private checkWorkflowPatterns(
workflow: WorkflowJson,
result: WorkflowValidationResult,
profile: string = 'runtime'
result: WorkflowValidationResult
): void {
// Check for error handling
const hasErrorHandling = Object.values(workflow.connections).some(
outputs => outputs.error && outputs.error.length > 0
);
// Only suggest error handling in stricter profiles
if (!hasErrorHandling && workflow.nodes.length > 3 && profile !== 'minimal') {
if (!hasErrorHandling && workflow.nodes.length > 3) {
result.warnings.push({
type: 'warning',
message: 'Consider adding error handling to your workflow'
});
}
// Check node-level error handling properties for ALL executable nodes
// Check node-level error handling properties for ALL nodes
for (const node of workflow.nodes) {
if (!this.isStickyNote(node)) {
this.checkNodeErrorHandling(node, workflow, result);
}
this.checkNodeErrorHandling(node, workflow, result);
}
// Check for very long linear workflows
@@ -1547,205 +1470,4 @@ export class WorkflowValidator {
);
}
}
/**
* Validate SplitInBatches node connections for common mistakes
*/
private validateSplitInBatchesConnection(
sourceNode: WorkflowNode,
outputIndex: number,
connection: { node: string; type: string; index: number },
nodeMap: Map<string, WorkflowNode>,
result: WorkflowValidationResult
): void {
const targetNode = nodeMap.get(connection.node);
if (!targetNode) return;
// Check if connections appear to be reversed
// Output 0 = "done", Output 1 = "loop"
if (outputIndex === 0) {
// This is the "done" output (index 0)
// Check if target looks like it should be in the loop
const targetType = targetNode.type.toLowerCase();
const targetName = targetNode.name.toLowerCase();
// Common patterns that suggest this node should be inside the loop
if (targetType.includes('function') ||
targetType.includes('code') ||
targetType.includes('item') ||
targetName.includes('process') ||
targetName.includes('transform') ||
targetName.includes('handle')) {
// Check if this node connects back to the SplitInBatches
const hasLoopBack = this.checkForLoopBack(targetNode.name, sourceNode.name, nodeMap);
if (hasLoopBack) {
result.errors.push({
type: 'error',
nodeId: sourceNode.id,
nodeName: sourceNode.name,
message: `SplitInBatches outputs appear reversed! Node "${targetNode.name}" is connected to output 0 ("done") but connects back to the loop. It should be connected to output 1 ("loop") instead. Remember: Output 0 = "done" (post-loop), Output 1 = "loop" (inside loop).`
});
} else {
result.warnings.push({
type: 'warning',
nodeId: sourceNode.id,
nodeName: sourceNode.name,
message: `Node "${targetNode.name}" is connected to the "done" output (index 0) but appears to be a processing node. Consider connecting it to the "loop" output (index 1) if it should process items inside the loop.`
});
}
}
} else if (outputIndex === 1) {
// This is the "loop" output (index 1)
// Check if target looks like it should be after the loop
const targetType = targetNode.type.toLowerCase();
const targetName = targetNode.name.toLowerCase();
// Common patterns that suggest this node should be after the loop
if (targetType.includes('aggregate') ||
targetType.includes('merge') ||
targetType.includes('email') ||
targetType.includes('slack') ||
targetName.includes('final') ||
targetName.includes('complete') ||
targetName.includes('summary') ||
targetName.includes('report')) {
result.warnings.push({
type: 'warning',
nodeId: sourceNode.id,
nodeName: sourceNode.name,
message: `Node "${targetNode.name}" is connected to the "loop" output (index 1) but appears to be a post-processing node. Consider connecting it to the "done" output (index 0) if it should run after all iterations complete.`
});
}
// Check if loop output doesn't eventually connect back
const hasLoopBack = this.checkForLoopBack(targetNode.name, sourceNode.name, nodeMap);
if (!hasLoopBack) {
result.warnings.push({
type: 'warning',
nodeId: sourceNode.id,
nodeName: sourceNode.name,
message: `The "loop" output connects to "${targetNode.name}" but doesn't connect back to the SplitInBatches node. The last node in the loop should connect back to complete the iteration.`
});
}
}
}
/**
* Check if a node eventually connects back to a target node
*/
private checkForLoopBack(
startNode: string,
targetNode: string,
nodeMap: Map<string, WorkflowNode>,
visited: Set<string> = new Set(),
maxDepth: number = 50
): boolean {
if (maxDepth <= 0) return false; // Prevent stack overflow
if (visited.has(startNode)) return false;
visited.add(startNode);
const node = nodeMap.get(startNode);
if (!node) return false;
// Access connections from the workflow structure, not the node
// We need to access this.currentWorkflow.connections[startNode]
const connections = (this as any).currentWorkflow?.connections[startNode];
if (!connections) return false;
for (const [outputType, outputs] of Object.entries(connections)) {
if (!Array.isArray(outputs)) continue;
for (const outputConnections of outputs) {
if (!Array.isArray(outputConnections)) continue;
for (const conn of outputConnections) {
if (conn.node === targetNode) {
return true;
}
// Recursively check connected nodes
if (this.checkForLoopBack(conn.node, targetNode, nodeMap, visited, maxDepth - 1)) {
return true;
}
}
}
}
return false;
}
/**
* Add AI-specific error recovery suggestions
*/
private addErrorRecoverySuggestions(result: WorkflowValidationResult): void {
// Categorize errors and provide specific recovery actions
const errorTypes = {
nodeType: result.errors.filter(e => e.message.includes('node type') || e.message.includes('Node type')),
connection: result.errors.filter(e => e.message.includes('connection') || e.message.includes('Connection')),
structure: result.errors.filter(e => e.message.includes('structure') || e.message.includes('nodes must be')),
configuration: result.errors.filter(e => e.message.includes('property') || e.message.includes('field')),
typeVersion: result.errors.filter(e => e.message.includes('typeVersion'))
};
// Add recovery suggestions based on error types
if (errorTypes.nodeType.length > 0) {
result.suggestions.unshift(
'🔧 RECOVERY: Invalid node types detected. Use these patterns:',
' • For core nodes: "n8n-nodes-base.nodeName" (e.g., "n8n-nodes-base.webhook")',
' • For AI nodes: "@n8n/n8n-nodes-langchain.nodeName"',
' • Never use just the node name without package prefix'
);
}
if (errorTypes.connection.length > 0) {
result.suggestions.unshift(
'🔧 RECOVERY: Connection errors detected. Fix with:',
' • Use node NAMES in connections, not IDs or types',
' • Structure: { "Source Node Name": { "main": [[{ "node": "Target Node Name", "type": "main", "index": 0 }]] } }',
' • Ensure all referenced nodes exist in the workflow'
);
}
if (errorTypes.structure.length > 0) {
result.suggestions.unshift(
'🔧 RECOVERY: Workflow structure errors. Fix with:',
' • Ensure "nodes" is an array: "nodes": [...]',
' • Ensure "connections" is an object: "connections": {...}',
' • Add at least one node to create a valid workflow'
);
}
if (errorTypes.configuration.length > 0) {
result.suggestions.unshift(
'🔧 RECOVERY: Node configuration errors. Fix with:',
' • Check required fields using validate_node_minimal first',
' • Use get_node_essentials to see what fields are needed',
' • Ensure operation-specific fields match the node\'s requirements'
);
}
if (errorTypes.typeVersion.length > 0) {
result.suggestions.unshift(
'🔧 RECOVERY: TypeVersion errors. Fix with:',
' • Add "typeVersion": 1 (or latest version) to each node',
' • Use get_node_info to check the correct version for each node type'
);
}
// Add general recovery workflow
if (result.errors.length > 3) {
result.suggestions.push(
'📋 SUGGESTED WORKFLOW: Too many errors detected. Try this approach:',
' 1. Fix structural issues first (nodes array, connections object)',
' 2. Validate node types and fix invalid ones',
' 3. Add required typeVersion to all nodes',
' 4. Test connections step by step',
' 5. Use validate_node_minimal on individual nodes to verify configuration'
);
}
}
}

View File

@@ -1,312 +0,0 @@
/**
* Zod validation schemas for MCP tool parameters
* Provides robust input validation with detailed error messages
*/
// Simple validation without zod for now, since it's not installed
// We can use TypeScript's built-in validation with better error messages
export class ValidationError extends Error {
constructor(message: string, public field?: string, public value?: any) {
super(message);
this.name = 'ValidationError';
}
}
export interface ValidationResult {
valid: boolean;
errors: Array<{
field: string;
message: string;
value?: any;
}>;
}
/**
* Basic validation utilities
*/
export class Validator {
/**
* Validate that a value is a non-empty string
*/
static validateString(value: any, fieldName: string, required: boolean = true): ValidationResult {
const errors: Array<{field: string, message: string, value?: any}> = [];
if (required && (value === undefined || value === null)) {
errors.push({
field: fieldName,
message: `${fieldName} is required`,
value
});
} else if (value !== undefined && value !== null && typeof value !== 'string') {
errors.push({
field: fieldName,
message: `${fieldName} must be a string, got ${typeof value}`,
value
});
} else if (required && typeof value === 'string' && value.trim().length === 0) {
errors.push({
field: fieldName,
message: `${fieldName} cannot be empty`,
value
});
}
return {
valid: errors.length === 0,
errors
};
}
/**
* Validate that a value is a valid object (not null, not array)
*/
static validateObject(value: any, fieldName: string, required: boolean = true): ValidationResult {
const errors: Array<{field: string, message: string, value?: any}> = [];
if (required && (value === undefined || value === null)) {
errors.push({
field: fieldName,
message: `${fieldName} is required`,
value
});
} else if (value !== undefined && value !== null) {
if (typeof value !== 'object') {
errors.push({
field: fieldName,
message: `${fieldName} must be an object, got ${typeof value}`,
value
});
} else if (Array.isArray(value)) {
errors.push({
field: fieldName,
message: `${fieldName} must be an object, not an array`,
value
});
}
}
return {
valid: errors.length === 0,
errors
};
}
/**
* Validate that a value is an array
*/
static validateArray(value: any, fieldName: string, required: boolean = true): ValidationResult {
const errors: Array<{field: string, message: string, value?: any}> = [];
if (required && (value === undefined || value === null)) {
errors.push({
field: fieldName,
message: `${fieldName} is required`,
value
});
} else if (value !== undefined && value !== null && !Array.isArray(value)) {
errors.push({
field: fieldName,
message: `${fieldName} must be an array, got ${typeof value}`,
value
});
}
return {
valid: errors.length === 0,
errors
};
}
/**
* Validate that a value is a number
*/
static validateNumber(value: any, fieldName: string, required: boolean = true, min?: number, max?: number): ValidationResult {
const errors: Array<{field: string, message: string, value?: any}> = [];
if (required && (value === undefined || value === null)) {
errors.push({
field: fieldName,
message: `${fieldName} is required`,
value
});
} else if (value !== undefined && value !== null) {
if (typeof value !== 'number' || isNaN(value)) {
errors.push({
field: fieldName,
message: `${fieldName} must be a number, got ${typeof value}`,
value
});
} else {
if (min !== undefined && value < min) {
errors.push({
field: fieldName,
message: `${fieldName} must be at least ${min}, got ${value}`,
value
});
}
if (max !== undefined && value > max) {
errors.push({
field: fieldName,
message: `${fieldName} must be at most ${max}, got ${value}`,
value
});
}
}
}
return {
valid: errors.length === 0,
errors
};
}
/**
* Validate that a value is one of allowed values
*/
static validateEnum<T>(value: any, fieldName: string, allowedValues: T[], required: boolean = true): ValidationResult {
const errors: Array<{field: string, message: string, value?: any}> = [];
if (required && (value === undefined || value === null)) {
errors.push({
field: fieldName,
message: `${fieldName} is required`,
value
});
} else if (value !== undefined && value !== null && !allowedValues.includes(value)) {
errors.push({
field: fieldName,
message: `${fieldName} must be one of: ${allowedValues.join(', ')}, got "${value}"`,
value
});
}
return {
valid: errors.length === 0,
errors
};
}
/**
* Combine multiple validation results
*/
static combineResults(...results: ValidationResult[]): ValidationResult {
const allErrors = results.flatMap(r => r.errors);
return {
valid: allErrors.length === 0,
errors: allErrors
};
}
/**
* Create a detailed error message from validation result
*/
static formatErrors(result: ValidationResult, toolName?: string): string {
if (result.valid) return '';
const prefix = toolName ? `${toolName}: ` : '';
const errors = result.errors.map(e => `${e.field}: ${e.message}`).join('\n');
return `${prefix}Validation failed:\n${errors}`;
}
}
/**
* Tool-specific validation schemas
*/
export class ToolValidation {
/**
* Validate parameters for validate_node_operation tool
*/
static validateNodeOperation(args: any): ValidationResult {
const nodeTypeResult = Validator.validateString(args.nodeType, 'nodeType');
const configResult = Validator.validateObject(args.config, 'config');
const profileResult = Validator.validateEnum(
args.profile,
'profile',
['minimal', 'runtime', 'ai-friendly', 'strict'],
false // optional
);
return Validator.combineResults(nodeTypeResult, configResult, profileResult);
}
/**
* Validate parameters for validate_node_minimal tool
*/
static validateNodeMinimal(args: any): ValidationResult {
const nodeTypeResult = Validator.validateString(args.nodeType, 'nodeType');
const configResult = Validator.validateObject(args.config, 'config');
return Validator.combineResults(nodeTypeResult, configResult);
}
/**
* Validate parameters for validate_workflow tool
*/
static validateWorkflow(args: any): ValidationResult {
const workflowResult = Validator.validateObject(args.workflow, 'workflow');
// Validate workflow structure if it's an object
let nodesResult: ValidationResult = { valid: true, errors: [] };
let connectionsResult: ValidationResult = { valid: true, errors: [] };
if (workflowResult.valid && args.workflow) {
nodesResult = Validator.validateArray(args.workflow.nodes, 'workflow.nodes');
connectionsResult = Validator.validateObject(args.workflow.connections, 'workflow.connections');
}
const optionsResult = args.options ?
Validator.validateObject(args.options, 'options', false) :
{ valid: true, errors: [] };
return Validator.combineResults(workflowResult, nodesResult, connectionsResult, optionsResult);
}
/**
* Validate parameters for search_nodes tool
*/
static validateSearchNodes(args: any): ValidationResult {
const queryResult = Validator.validateString(args.query, 'query');
const limitResult = Validator.validateNumber(args.limit, 'limit', false, 1, 200);
const modeResult = Validator.validateEnum(
args.mode,
'mode',
['OR', 'AND', 'FUZZY'],
false
);
return Validator.combineResults(queryResult, limitResult, modeResult);
}
/**
* Validate parameters for list_node_templates tool
*/
static validateListNodeTemplates(args: any): ValidationResult {
const nodeTypesResult = Validator.validateArray(args.nodeTypes, 'nodeTypes');
const limitResult = Validator.validateNumber(args.limit, 'limit', false, 1, 50);
return Validator.combineResults(nodeTypesResult, limitResult);
}
/**
* Validate parameters for n8n workflow operations
*/
static validateWorkflowId(args: any): ValidationResult {
return Validator.validateString(args.id, 'id');
}
/**
* Validate parameters for n8n_create_workflow tool
*/
static validateCreateWorkflow(args: any): ValidationResult {
const nameResult = Validator.validateString(args.name, 'name');
const nodesResult = Validator.validateArray(args.nodes, 'nodes');
const connectionsResult = Validator.validateObject(args.connections, 'connections');
const settingsResult = args.settings ?
Validator.validateObject(args.settings, 'settings', false) :
{ valid: true, errors: [] };
return Validator.combineResults(nameResult, nodesResult, connectionsResult, settingsResult);
}
}

View File

@@ -124,9 +124,9 @@ describe('MCP Tool Invocation', () => {
const andNodes = andResult.results;
expect(andNodes.length).toBeLessThanOrEqual(orNodes.length);
// FUZZY mode - use less typo-heavy search
// FUZZY mode
const fuzzyResponse = await client.callTool({ name: 'search_nodes', arguments: {
query: 'http req', // Partial match should work
query: 'htpp requst', // Intentional typos
mode: 'FUZZY'
}});
const fuzzyResult = JSON.parse(((fuzzyResponse as any).content[0]).text);

View File

@@ -83,9 +83,7 @@ describe('NodeRepository - Core Functionality', () => {
isWebhook: false,
isVersioned: true,
version: '1.0',
documentation: 'HTTP Request documentation',
outputs: undefined,
outputNames: undefined
documentation: 'HTTP Request documentation'
};
repository.saveNode(parsedNode);
@@ -110,9 +108,7 @@ describe('NodeRepository - Core Functionality', () => {
'HTTP Request documentation',
JSON.stringify([{ name: 'url', type: 'string' }], null, 2),
JSON.stringify([{ name: 'execute', displayName: 'Execute' }], null, 2),
JSON.stringify([{ name: 'httpBasicAuth' }], null, 2),
null, // outputs
null // outputNames
JSON.stringify([{ name: 'httpBasicAuth' }], null, 2)
);
});
@@ -129,9 +125,7 @@ describe('NodeRepository - Core Functionality', () => {
isAITool: true,
isTrigger: true,
isWebhook: true,
isVersioned: false,
outputs: undefined,
outputNames: undefined
isVersioned: false
};
repository.saveNode(minimalNode);
@@ -163,9 +157,7 @@ describe('NodeRepository - Core Functionality', () => {
properties_schema: JSON.stringify([{ name: 'url', type: 'string' }]),
operations: JSON.stringify([{ name: 'execute' }]),
credentials_required: JSON.stringify([{ name: 'httpBasicAuth' }]),
documentation: 'HTTP docs',
outputs: null,
output_names: null
documentation: 'HTTP docs'
};
mockAdapter._setMockData('node:nodes-base.httpRequest', mockRow);
@@ -187,9 +179,7 @@ describe('NodeRepository - Core Functionality', () => {
properties: [{ name: 'url', type: 'string' }],
operations: [{ name: 'execute' }],
credentials: [{ name: 'httpBasicAuth' }],
hasDocumentation: true,
outputs: null,
outputNames: null
hasDocumentation: true
});
});
@@ -214,9 +204,7 @@ describe('NodeRepository - Core Functionality', () => {
properties_schema: '{invalid json',
operations: 'not json at all',
credentials_required: '{"valid": "json"}',
documentation: null,
outputs: null,
output_names: null
documentation: null
};
mockAdapter._setMockData('node:nodes-base.broken', mockRow);
@@ -332,9 +320,7 @@ describe('NodeRepository - Core Functionality', () => {
isAITool: false,
isTrigger: false,
isWebhook: false,
isVersioned: false,
outputs: undefined,
outputNames: undefined
isVersioned: false
};
repository.saveNode(node);
@@ -362,9 +348,7 @@ describe('NodeRepository - Core Functionality', () => {
properties_schema: '[]',
operations: '[]',
credentials_required: '[]',
documentation: null,
outputs: null,
output_names: null
documentation: null
};
mockAdapter._setMockData('node:nodes-base.bool-test', mockRow);

View File

@@ -1,568 +0,0 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { NodeRepository } from '@/database/node-repository';
import { DatabaseAdapter } from '@/database/database-adapter';
import { ParsedNode } from '@/parsers/node-parser';
describe('NodeRepository - Outputs Handling', () => {
let repository: NodeRepository;
let mockDb: DatabaseAdapter;
let mockStatement: any;
beforeEach(() => {
mockStatement = {
run: vi.fn(),
get: vi.fn(),
all: vi.fn()
};
mockDb = {
prepare: vi.fn().mockReturnValue(mockStatement),
transaction: vi.fn(),
exec: vi.fn(),
close: vi.fn(),
pragma: vi.fn()
} as any;
repository = new NodeRepository(mockDb);
});
describe('saveNode with outputs', () => {
it('should save node with outputs and outputNames correctly', () => {
const outputs = [
{ displayName: 'Done', description: 'Final results when loop completes' },
{ displayName: 'Loop', description: 'Current batch data during iteration' }
];
const outputNames = ['done', 'loop'];
const node: ParsedNode = {
style: 'programmatic',
nodeType: 'nodes-base.splitInBatches',
displayName: 'Split In Batches',
description: 'Split data into batches',
category: 'transform',
properties: [],
credentials: [],
isAITool: false,
isTrigger: false,
isWebhook: false,
operations: [],
version: '3',
isVersioned: false,
packageName: 'n8n-nodes-base',
outputs,
outputNames
};
repository.saveNode(node);
expect(mockDb.prepare).toHaveBeenCalledWith(`
INSERT OR REPLACE INTO nodes (
node_type, package_name, display_name, description,
category, development_style, is_ai_tool, is_trigger,
is_webhook, is_versioned, version, documentation,
properties_schema, operations, credentials_required,
outputs, output_names
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
expect(mockStatement.run).toHaveBeenCalledWith(
'nodes-base.splitInBatches',
'n8n-nodes-base',
'Split In Batches',
'Split data into batches',
'transform',
'programmatic',
0, // false
0, // false
0, // false
0, // false
'3',
null, // documentation
JSON.stringify([], null, 2), // properties
JSON.stringify([], null, 2), // operations
JSON.stringify([], null, 2), // credentials
JSON.stringify(outputs, null, 2), // outputs
JSON.stringify(outputNames, null, 2) // output_names
);
});
it('should save node with only outputs (no outputNames)', () => {
const outputs = [
{ displayName: 'True', description: 'Items that match condition' },
{ displayName: 'False', description: 'Items that do not match condition' }
];
const node: ParsedNode = {
style: 'programmatic',
nodeType: 'nodes-base.if',
displayName: 'IF',
description: 'Route items based on conditions',
category: 'transform',
properties: [],
credentials: [],
isAITool: false,
isTrigger: false,
isWebhook: false,
operations: [],
version: '2',
isVersioned: false,
packageName: 'n8n-nodes-base',
outputs
// no outputNames
};
repository.saveNode(node);
const callArgs = mockStatement.run.mock.calls[0];
expect(callArgs[15]).toBe(JSON.stringify(outputs, null, 2)); // outputs
expect(callArgs[16]).toBe(null); // output_names should be null
});
it('should save node with only outputNames (no outputs)', () => {
const outputNames = ['main', 'error'];
const node: ParsedNode = {
style: 'programmatic',
nodeType: 'nodes-base.customNode',
displayName: 'Custom Node',
description: 'Custom node with output names only',
category: 'transform',
properties: [],
credentials: [],
isAITool: false,
isTrigger: false,
isWebhook: false,
operations: [],
version: '1',
isVersioned: false,
packageName: 'n8n-nodes-base',
outputNames
// no outputs
};
repository.saveNode(node);
const callArgs = mockStatement.run.mock.calls[0];
expect(callArgs[15]).toBe(null); // outputs should be null
expect(callArgs[16]).toBe(JSON.stringify(outputNames, null, 2)); // output_names
});
it('should save node without outputs or outputNames', () => {
const node: ParsedNode = {
style: 'programmatic',
nodeType: 'nodes-base.httpRequest',
displayName: 'HTTP Request',
description: 'Make HTTP requests',
category: 'input',
properties: [],
credentials: [],
isAITool: false,
isTrigger: false,
isWebhook: false,
operations: [],
version: '4',
isVersioned: false,
packageName: 'n8n-nodes-base'
// no outputs or outputNames
};
repository.saveNode(node);
const callArgs = mockStatement.run.mock.calls[0];
expect(callArgs[15]).toBe(null); // outputs should be null
expect(callArgs[16]).toBe(null); // output_names should be null
});
it('should handle empty outputs and outputNames arrays', () => {
const node: ParsedNode = {
style: 'programmatic',
nodeType: 'nodes-base.emptyNode',
displayName: 'Empty Node',
description: 'Node with empty outputs',
category: 'misc',
properties: [],
credentials: [],
isAITool: false,
isTrigger: false,
isWebhook: false,
operations: [],
version: '1',
isVersioned: false,
packageName: 'n8n-nodes-base',
outputs: [],
outputNames: []
};
repository.saveNode(node);
const callArgs = mockStatement.run.mock.calls[0];
expect(callArgs[15]).toBe(JSON.stringify([], null, 2)); // outputs
expect(callArgs[16]).toBe(JSON.stringify([], null, 2)); // output_names
});
});
describe('getNode with outputs', () => {
it('should retrieve node with outputs and outputNames correctly', () => {
const outputs = [
{ displayName: 'Done', description: 'Final results when loop completes' },
{ displayName: 'Loop', description: 'Current batch data during iteration' }
];
const outputNames = ['done', 'loop'];
const mockRow = {
node_type: 'nodes-base.splitInBatches',
display_name: 'Split In Batches',
description: 'Split data into batches',
category: 'transform',
development_style: 'programmatic',
package_name: 'n8n-nodes-base',
is_ai_tool: 0,
is_trigger: 0,
is_webhook: 0,
is_versioned: 0,
version: '3',
properties_schema: JSON.stringify([]),
operations: JSON.stringify([]),
credentials_required: JSON.stringify([]),
documentation: null,
outputs: JSON.stringify(outputs),
output_names: JSON.stringify(outputNames)
};
mockStatement.get.mockReturnValue(mockRow);
const result = repository.getNode('nodes-base.splitInBatches');
expect(result).toEqual({
nodeType: 'nodes-base.splitInBatches',
displayName: 'Split In Batches',
description: 'Split data into batches',
category: 'transform',
developmentStyle: 'programmatic',
package: 'n8n-nodes-base',
isAITool: false,
isTrigger: false,
isWebhook: false,
isVersioned: false,
version: '3',
properties: [],
operations: [],
credentials: [],
hasDocumentation: false,
outputs,
outputNames
});
});
it('should retrieve node with only outputs (null outputNames)', () => {
const outputs = [
{ displayName: 'True', description: 'Items that match condition' }
];
const mockRow = {
node_type: 'nodes-base.if',
display_name: 'IF',
description: 'Route items',
category: 'transform',
development_style: 'programmatic',
package_name: 'n8n-nodes-base',
is_ai_tool: 0,
is_trigger: 0,
is_webhook: 0,
is_versioned: 0,
version: '2',
properties_schema: JSON.stringify([]),
operations: JSON.stringify([]),
credentials_required: JSON.stringify([]),
documentation: null,
outputs: JSON.stringify(outputs),
output_names: null
};
mockStatement.get.mockReturnValue(mockRow);
const result = repository.getNode('nodes-base.if');
expect(result.outputs).toEqual(outputs);
expect(result.outputNames).toBe(null);
});
it('should retrieve node with only outputNames (null outputs)', () => {
const outputNames = ['main'];
const mockRow = {
node_type: 'nodes-base.customNode',
display_name: 'Custom Node',
description: 'Custom node',
category: 'misc',
development_style: 'programmatic',
package_name: 'n8n-nodes-base',
is_ai_tool: 0,
is_trigger: 0,
is_webhook: 0,
is_versioned: 0,
version: '1',
properties_schema: JSON.stringify([]),
operations: JSON.stringify([]),
credentials_required: JSON.stringify([]),
documentation: null,
outputs: null,
output_names: JSON.stringify(outputNames)
};
mockStatement.get.mockReturnValue(mockRow);
const result = repository.getNode('nodes-base.customNode');
expect(result.outputs).toBe(null);
expect(result.outputNames).toEqual(outputNames);
});
it('should retrieve node without outputs or outputNames', () => {
const mockRow = {
node_type: 'nodes-base.httpRequest',
display_name: 'HTTP Request',
description: 'Make HTTP requests',
category: 'input',
development_style: 'programmatic',
package_name: 'n8n-nodes-base',
is_ai_tool: 0,
is_trigger: 0,
is_webhook: 0,
is_versioned: 0,
version: '4',
properties_schema: JSON.stringify([]),
operations: JSON.stringify([]),
credentials_required: JSON.stringify([]),
documentation: null,
outputs: null,
output_names: null
};
mockStatement.get.mockReturnValue(mockRow);
const result = repository.getNode('nodes-base.httpRequest');
expect(result.outputs).toBe(null);
expect(result.outputNames).toBe(null);
});
it('should handle malformed JSON gracefully', () => {
const mockRow = {
node_type: 'nodes-base.malformed',
display_name: 'Malformed Node',
description: 'Node with malformed JSON',
category: 'misc',
development_style: 'programmatic',
package_name: 'n8n-nodes-base',
is_ai_tool: 0,
is_trigger: 0,
is_webhook: 0,
is_versioned: 0,
version: '1',
properties_schema: JSON.stringify([]),
operations: JSON.stringify([]),
credentials_required: JSON.stringify([]),
documentation: null,
outputs: '{invalid json}',
output_names: '[invalid, json'
};
mockStatement.get.mockReturnValue(mockRow);
const result = repository.getNode('nodes-base.malformed');
// Should use default values when JSON parsing fails
expect(result.outputs).toBe(null);
expect(result.outputNames).toBe(null);
});
it('should return null for non-existent node', () => {
mockStatement.get.mockReturnValue(null);
const result = repository.getNode('nodes-base.nonExistent');
expect(result).toBe(null);
});
it('should handle SplitInBatches counterintuitive output order correctly', () => {
// Test that the output order is preserved: done=0, loop=1
const outputs = [
{ displayName: 'Done', description: 'Final results when loop completes', index: 0 },
{ displayName: 'Loop', description: 'Current batch data during iteration', index: 1 }
];
const outputNames = ['done', 'loop'];
const mockRow = {
node_type: 'nodes-base.splitInBatches',
display_name: 'Split In Batches',
description: 'Split data into batches',
category: 'transform',
development_style: 'programmatic',
package_name: 'n8n-nodes-base',
is_ai_tool: 0,
is_trigger: 0,
is_webhook: 0,
is_versioned: 0,
version: '3',
properties_schema: JSON.stringify([]),
operations: JSON.stringify([]),
credentials_required: JSON.stringify([]),
documentation: null,
outputs: JSON.stringify(outputs),
output_names: JSON.stringify(outputNames)
};
mockStatement.get.mockReturnValue(mockRow);
const result = repository.getNode('nodes-base.splitInBatches');
// Verify order is preserved
expect(result.outputs[0].displayName).toBe('Done');
expect(result.outputs[1].displayName).toBe('Loop');
expect(result.outputNames[0]).toBe('done');
expect(result.outputNames[1]).toBe('loop');
});
});
describe('parseNodeRow with outputs', () => {
it('should parse node row with outputs correctly using parseNodeRow', () => {
const outputs = [{ displayName: 'Output' }];
const outputNames = ['main'];
const mockRow = {
node_type: 'nodes-base.test',
display_name: 'Test',
description: 'Test node',
category: 'misc',
development_style: 'programmatic',
package_name: 'n8n-nodes-base',
is_ai_tool: 0,
is_trigger: 0,
is_webhook: 0,
is_versioned: 0,
version: '1',
properties_schema: JSON.stringify([]),
operations: JSON.stringify([]),
credentials_required: JSON.stringify([]),
documentation: null,
outputs: JSON.stringify(outputs),
output_names: JSON.stringify(outputNames)
};
mockStatement.all.mockReturnValue([mockRow]);
const results = repository.getAllNodes(1);
expect(results[0].outputs).toEqual(outputs);
expect(results[0].outputNames).toEqual(outputNames);
});
it('should handle empty string as null for outputs', () => {
const mockRow = {
node_type: 'nodes-base.empty',
display_name: 'Empty',
description: 'Empty node',
category: 'misc',
development_style: 'programmatic',
package_name: 'n8n-nodes-base',
is_ai_tool: 0,
is_trigger: 0,
is_webhook: 0,
is_versioned: 0,
version: '1',
properties_schema: JSON.stringify([]),
operations: JSON.stringify([]),
credentials_required: JSON.stringify([]),
documentation: null,
outputs: '', // empty string
output_names: '' // empty string
};
mockStatement.all.mockReturnValue([mockRow]);
const results = repository.getAllNodes(1);
// Empty strings should be treated as null since they fail JSON parsing
expect(results[0].outputs).toBe(null);
expect(results[0].outputNames).toBe(null);
});
});
describe('complex output structures', () => {
it('should handle complex output objects with metadata', () => {
const complexOutputs = [
{
displayName: 'Done',
name: 'done',
type: 'main',
hint: 'Receives the final data after all batches have been processed',
description: 'Final results when loop completes',
index: 0
},
{
displayName: 'Loop',
name: 'loop',
type: 'main',
hint: 'Receives the current batch data during each iteration',
description: 'Current batch data during iteration',
index: 1
}
];
const node: ParsedNode = {
style: 'programmatic',
nodeType: 'nodes-base.splitInBatches',
displayName: 'Split In Batches',
description: 'Split data into batches',
category: 'transform',
properties: [],
credentials: [],
isAITool: false,
isTrigger: false,
isWebhook: false,
operations: [],
version: '3',
isVersioned: false,
packageName: 'n8n-nodes-base',
outputs: complexOutputs,
outputNames: ['done', 'loop']
};
repository.saveNode(node);
// Simulate retrieval
const mockRow = {
node_type: 'nodes-base.splitInBatches',
display_name: 'Split In Batches',
description: 'Split data into batches',
category: 'transform',
development_style: 'programmatic',
package_name: 'n8n-nodes-base',
is_ai_tool: 0,
is_trigger: 0,
is_webhook: 0,
is_versioned: 0,
version: '3',
properties_schema: JSON.stringify([]),
operations: JSON.stringify([]),
credentials_required: JSON.stringify([]),
documentation: null,
outputs: JSON.stringify(complexOutputs),
output_names: JSON.stringify(['done', 'loop'])
};
mockStatement.get.mockReturnValue(mockRow);
const result = repository.getNode('nodes-base.splitInBatches');
expect(result.outputs).toEqual(complexOutputs);
expect(result.outputs[0]).toMatchObject({
displayName: 'Done',
name: 'done',
type: 'main',
hint: 'Receives the final data after all batches have been processed'
});
});
});
});

View File

@@ -299,268 +299,6 @@ describe('DocsMapper', () => {
});
});
describe('enhanceLoopNodeDocumentation - SplitInBatches', () => {
it('should enhance SplitInBatches documentation with output guidance', async () => {
const originalContent = `# Split In Batches Node
This node splits data into batches.
## When to use
Use this node when you need to process large datasets in smaller chunks.
## Parameters
- batchSize: Number of items per batch
`;
vi.mocked(fs.readFile).mockResolvedValueOnce(originalContent);
const result = await docsMapper.fetchDocumentation('splitInBatches');
expect(result).not.toBeNull();
expect(result!).toContain('CRITICAL OUTPUT CONNECTION INFORMATION');
expect(result!).toContain('⚠️ OUTPUT INDICES ARE COUNTERINTUITIVE ⚠️');
expect(result!).toContain('Output 0 (index 0) = "done"');
expect(result!).toContain('Output 1 (index 1) = "loop"');
expect(result!).toContain('Correct Connection Pattern:');
expect(result!).toContain('Common Mistake:');
expect(result!).toContain('AI assistants often connect these backwards');
// Should insert before "When to use" section
const insertionIndex = result!.indexOf('## When to use');
const guidanceIndex = result!.indexOf('CRITICAL OUTPUT CONNECTION INFORMATION');
expect(guidanceIndex).toBeLessThan(insertionIndex);
expect(guidanceIndex).toBeGreaterThan(0);
});
it('should enhance SplitInBatches documentation when no "When to use" section exists', async () => {
const originalContent = `# Split In Batches Node
This node splits data into batches.
## Parameters
- batchSize: Number of items per batch
`;
vi.mocked(fs.readFile).mockResolvedValueOnce(originalContent);
const result = await docsMapper.fetchDocumentation('splitInBatches');
expect(result).not.toBeNull();
expect(result!).toContain('CRITICAL OUTPUT CONNECTION INFORMATION');
// Should be inserted at the beginning since no "When to use" section
expect(result!.indexOf('CRITICAL OUTPUT CONNECTION INFORMATION')).toBeLessThan(
result!.indexOf('# Split In Batches Node')
);
});
it('should handle splitInBatches in various node type formats', async () => {
const testCases = [
'splitInBatches',
'n8n-nodes-base.splitInBatches',
'nodes-base.splitInBatches'
];
for (const nodeType of testCases) {
const originalContent = '# Split In Batches\nOriginal content';
vi.mocked(fs.readFile).mockResolvedValueOnce(originalContent);
const result = await docsMapper.fetchDocumentation(nodeType);
expect(result).toContain('CRITICAL OUTPUT CONNECTION INFORMATION');
expect(result).toContain('Output 0 (index 0) = "done"');
}
});
it('should provide specific guidance for correct connection patterns', async () => {
const originalContent = '# Split In Batches\n## When to use\nContent';
vi.mocked(fs.readFile).mockResolvedValueOnce(originalContent);
const result = await docsMapper.fetchDocumentation('splitInBatches');
expect(result).toContain('Connect nodes that PROCESS items inside the loop to **Output 1 ("loop")**');
expect(result).toContain('Connect nodes that run AFTER the loop completes to **Output 0 ("done")**');
expect(result).toContain('The last processing node in the loop must connect back to the SplitInBatches node');
});
it('should explain the common AI assistant mistake', async () => {
const originalContent = '# Split In Batches\n## When to use\nContent';
vi.mocked(fs.readFile).mockResolvedValueOnce(originalContent);
const result = await docsMapper.fetchDocumentation('splitInBatches');
expect(result).toContain('AI assistants often connect these backwards');
expect(result).toContain('logical flow (loop first, then done) doesn\'t match the technical indices (done=0, loop=1)');
});
it('should not enhance non-splitInBatches nodes with loop guidance', async () => {
const originalContent = '# HTTP Request Node\nContent';
vi.mocked(fs.readFile).mockResolvedValueOnce(originalContent);
const result = await docsMapper.fetchDocumentation('httpRequest');
expect(result).not.toContain('CRITICAL OUTPUT CONNECTION INFORMATION');
expect(result).not.toContain('counterintuitive');
expect(result).toBe(originalContent); // Should be unchanged
});
});
describe('enhanceLoopNodeDocumentation - IF node', () => {
it('should enhance IF node documentation with output guidance', async () => {
const originalContent = `# IF Node
Route items based on conditions.
## Node parameters
Configure your conditions here.
`;
vi.mocked(fs.readFile).mockResolvedValueOnce(originalContent);
const result = await docsMapper.fetchDocumentation('n8n-nodes-base.if');
expect(result).not.toBeNull();
expect(result!).toContain('Output Connection Information');
expect(result!).toContain('Output 0 (index 0) = "true"');
expect(result!).toContain('Output 1 (index 1) = "false"');
expect(result!).toContain('Items that match the condition');
expect(result!).toContain('Items that do not match the condition');
// Should insert before "Node parameters" section
const parametersIndex = result!.indexOf('## Node parameters');
const outputInfoIndex = result!.indexOf('Output Connection Information');
expect(outputInfoIndex).toBeLessThan(parametersIndex);
expect(outputInfoIndex).toBeGreaterThan(0);
});
it('should handle IF node when no "Node parameters" section exists', async () => {
const originalContent = `# IF Node
Route items based on conditions.
## Usage
Use this node to route data.
`;
vi.mocked(fs.readFile).mockResolvedValueOnce(originalContent);
const result = await docsMapper.fetchDocumentation('n8n-nodes-base.if');
// When no "Node parameters" section exists, no enhancement is applied
expect(result).toBe(originalContent);
});
it('should handle various IF node type formats', async () => {
const testCases = [
'if',
'n8n-nodes-base.if',
'nodes-base.if'
];
for (const nodeType of testCases) {
const originalContent = '# IF Node\n## Node parameters\nContent';
vi.mocked(fs.readFile).mockResolvedValueOnce(originalContent);
const result = await docsMapper.fetchDocumentation(nodeType);
if (nodeType.includes('.if')) {
expect(result).toContain('Output Connection Information');
expect(result).toContain('Output 0 (index 0) = "true"');
expect(result).toContain('Output 1 (index 1) = "false"');
} else {
// For 'if' without dot, no enhancement is applied
expect(result).toBe(originalContent);
}
}
});
});
describe('enhanceLoopNodeDocumentation - edge cases', () => {
it('should handle content without clear insertion points', async () => {
const originalContent = 'Simple content without markdown sections';
vi.mocked(fs.readFile).mockResolvedValueOnce(originalContent);
const result = await docsMapper.fetchDocumentation('splitInBatches');
expect(result).not.toBeNull();
expect(result!).toContain('CRITICAL OUTPUT CONNECTION INFORMATION');
// Should be prepended when no insertion point found (but there's a newline before original content)
const guidanceIndex = result!.indexOf('CRITICAL OUTPUT CONNECTION INFORMATION');
expect(guidanceIndex).toBeLessThan(result!.indexOf('Simple content'));
expect(guidanceIndex).toBeLessThanOrEqual(5); // Allow for some whitespace
});
it('should handle empty content', async () => {
const originalContent = '';
vi.mocked(fs.readFile).mockResolvedValueOnce(originalContent);
const result = await docsMapper.fetchDocumentation('splitInBatches');
expect(result).not.toBeNull();
expect(result!).toContain('CRITICAL OUTPUT CONNECTION INFORMATION');
expect(result!.length).toBeGreaterThan(0);
});
it('should handle content with multiple "When to use" sections', async () => {
const originalContent = `# Split In Batches
## When to use (overview)
General usage.
## When to use (detailed)
Detailed usage.
`;
vi.mocked(fs.readFile).mockResolvedValueOnce(originalContent);
const result = await docsMapper.fetchDocumentation('splitInBatches');
expect(result).not.toBeNull();
expect(result!).toContain('CRITICAL OUTPUT CONNECTION INFORMATION');
// Should insert before first occurrence
const firstWhenToUse = result!.indexOf('## When to use (overview)');
const guidanceIndex = result!.indexOf('CRITICAL OUTPUT CONNECTION INFORMATION');
expect(guidanceIndex).toBeLessThan(firstWhenToUse);
});
it('should not double-enhance already enhanced content', async () => {
const alreadyEnhancedContent = `# Split In Batches
## CRITICAL OUTPUT CONNECTION INFORMATION
Already enhanced.
## When to use
Content here.
`;
vi.mocked(fs.readFile).mockResolvedValueOnce(alreadyEnhancedContent);
const result = await docsMapper.fetchDocumentation('splitInBatches');
// Should still add enhancement (method doesn't check for existing enhancements)
expect(result).not.toBeNull();
const criticalSections = (result!.match(/CRITICAL OUTPUT CONNECTION INFORMATION/g) || []).length;
expect(criticalSections).toBe(2); // Original + new enhancement
});
it('should handle very large content efficiently', async () => {
const largeContent = 'a'.repeat(100000) + '\n## When to use\n' + 'b'.repeat(100000);
vi.mocked(fs.readFile).mockResolvedValueOnce(largeContent);
const result = await docsMapper.fetchDocumentation('splitInBatches');
expect(result).not.toBeNull();
expect(result!).toContain('CRITICAL OUTPUT CONNECTION INFORMATION');
expect(result!.length).toBeGreaterThan(largeContent.length);
});
});
describe('DocsMapper instance', () => {
it('should use consistent docsPath across instances', () => {
const mapper1 = new DocsMapper();

View File

@@ -1,473 +0,0 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { NodeParser } from '@/parsers/node-parser';
import { PropertyExtractor } from '@/parsers/property-extractor';
// Mock PropertyExtractor
vi.mock('@/parsers/property-extractor');
describe('NodeParser - Output Extraction', () => {
let parser: NodeParser;
let mockPropertyExtractor: any;
beforeEach(() => {
vi.clearAllMocks();
mockPropertyExtractor = {
extractProperties: vi.fn().mockReturnValue([]),
extractCredentials: vi.fn().mockReturnValue([]),
detectAIToolCapability: vi.fn().mockReturnValue(false),
extractOperations: vi.fn().mockReturnValue([])
};
(PropertyExtractor as any).mockImplementation(() => mockPropertyExtractor);
parser = new NodeParser();
});
describe('extractOutputs method', () => {
it('should extract outputs array from base description', () => {
const outputs = [
{ displayName: 'Done', description: 'Final results when loop completes' },
{ displayName: 'Loop', description: 'Current batch data during iteration' }
];
const nodeDescription = {
name: 'splitInBatches',
displayName: 'Split In Batches',
outputs
};
const NodeClass = class {
description = nodeDescription;
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputs).toEqual(outputs);
expect(result.outputNames).toBeUndefined();
});
it('should extract outputNames array from base description', () => {
const outputNames = ['done', 'loop'];
const nodeDescription = {
name: 'splitInBatches',
displayName: 'Split In Batches',
outputNames
};
const NodeClass = class {
description = nodeDescription;
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputNames).toEqual(outputNames);
expect(result.outputs).toBeUndefined();
});
it('should extract both outputs and outputNames when both are present', () => {
const outputs = [
{ displayName: 'Done', description: 'Final results when loop completes' },
{ displayName: 'Loop', description: 'Current batch data during iteration' }
];
const outputNames = ['done', 'loop'];
const nodeDescription = {
name: 'splitInBatches',
displayName: 'Split In Batches',
outputs,
outputNames
};
const NodeClass = class {
description = nodeDescription;
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputs).toEqual(outputs);
expect(result.outputNames).toEqual(outputNames);
});
it('should convert single output to array format', () => {
const singleOutput = { displayName: 'Output', description: 'Single output' };
const nodeDescription = {
name: 'singleOutputNode',
displayName: 'Single Output Node',
outputs: singleOutput
};
const NodeClass = class {
description = nodeDescription;
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputs).toEqual([singleOutput]);
});
it('should convert single outputName to array format', () => {
const nodeDescription = {
name: 'singleOutputNode',
displayName: 'Single Output Node',
outputNames: 'main'
};
const NodeClass = class {
description = nodeDescription;
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputNames).toEqual(['main']);
});
it('should extract outputs from versioned node when not in base description', () => {
const versionedOutputs = [
{ displayName: 'True', description: 'Items that match condition' },
{ displayName: 'False', description: 'Items that do not match condition' }
];
const NodeClass = class {
description = {
name: 'if',
displayName: 'IF'
// No outputs in base description
};
nodeVersions = {
1: {
description: {
outputs: versionedOutputs
}
},
2: {
description: {
outputs: versionedOutputs,
outputNames: ['true', 'false']
}
}
};
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
// Should get outputs from latest version (2)
expect(result.outputs).toEqual(versionedOutputs);
expect(result.outputNames).toEqual(['true', 'false']);
});
it('should handle node instantiation failure gracefully', () => {
const NodeClass = class {
// Static description that can be accessed when instantiation fails
static description = {
name: 'problematic',
displayName: 'Problematic Node'
};
constructor() {
throw new Error('Cannot instantiate');
}
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputs).toBeUndefined();
expect(result.outputNames).toBeUndefined();
});
it('should return empty result when no outputs found anywhere', () => {
const nodeDescription = {
name: 'noOutputs',
displayName: 'No Outputs Node'
// No outputs or outputNames
};
const NodeClass = class {
description = nodeDescription;
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputs).toBeUndefined();
expect(result.outputNames).toBeUndefined();
});
it('should handle complex versioned node structure', () => {
const NodeClass = class VersionedNodeType {
baseDescription = {
name: 'complexVersioned',
displayName: 'Complex Versioned Node',
defaultVersion: 3
};
nodeVersions = {
1: {
description: {
outputs: [{ displayName: 'V1 Output' }]
}
},
2: {
description: {
outputs: [
{ displayName: 'V2 Output 1' },
{ displayName: 'V2 Output 2' }
]
}
},
3: {
description: {
outputs: [
{ displayName: 'V3 True', description: 'True branch' },
{ displayName: 'V3 False', description: 'False branch' }
],
outputNames: ['true', 'false']
}
}
};
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
// Should use latest version (3)
expect(result.outputs).toEqual([
{ displayName: 'V3 True', description: 'True branch' },
{ displayName: 'V3 False', description: 'False branch' }
]);
expect(result.outputNames).toEqual(['true', 'false']);
});
it('should prefer base description outputs over versioned when both exist', () => {
const baseOutputs = [{ displayName: 'Base Output' }];
const versionedOutputs = [{ displayName: 'Versioned Output' }];
const NodeClass = class {
description = {
name: 'preferBase',
displayName: 'Prefer Base',
outputs: baseOutputs
};
nodeVersions = {
1: {
description: {
outputs: versionedOutputs
}
}
};
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputs).toEqual(baseOutputs);
});
it('should handle IF node with typical output structure', () => {
const ifOutputs = [
{ displayName: 'True', description: 'Items that match the condition' },
{ displayName: 'False', description: 'Items that do not match the condition' }
];
const NodeClass = class {
description = {
name: 'if',
displayName: 'IF',
outputs: ifOutputs,
outputNames: ['true', 'false']
};
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputs).toEqual(ifOutputs);
expect(result.outputNames).toEqual(['true', 'false']);
});
it('should handle SplitInBatches node with counterintuitive output structure', () => {
const splitInBatchesOutputs = [
{ displayName: 'Done', description: 'Final results when loop completes' },
{ displayName: 'Loop', description: 'Current batch data during iteration' }
];
const NodeClass = class {
description = {
name: 'splitInBatches',
displayName: 'Split In Batches',
outputs: splitInBatchesOutputs,
outputNames: ['done', 'loop']
};
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputs).toEqual(splitInBatchesOutputs);
expect(result.outputNames).toEqual(['done', 'loop']);
// Verify the counterintuitive order: done=0, loop=1
expect(result.outputs).toBeDefined();
expect(result.outputNames).toBeDefined();
expect(result.outputs![0].displayName).toBe('Done');
expect(result.outputs![1].displayName).toBe('Loop');
expect(result.outputNames![0]).toBe('done');
expect(result.outputNames![1]).toBe('loop');
});
it('should handle Switch node with multiple outputs', () => {
const switchOutputs = [
{ displayName: 'Output 1', description: 'First branch' },
{ displayName: 'Output 2', description: 'Second branch' },
{ displayName: 'Output 3', description: 'Third branch' },
{ displayName: 'Fallback', description: 'Default branch when no conditions match' }
];
const NodeClass = class {
description = {
name: 'switch',
displayName: 'Switch',
outputs: switchOutputs,
outputNames: ['0', '1', '2', 'fallback']
};
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputs).toEqual(switchOutputs);
expect(result.outputNames).toEqual(['0', '1', '2', 'fallback']);
});
it('should handle empty outputs array', () => {
const NodeClass = class {
description = {
name: 'emptyOutputs',
displayName: 'Empty Outputs',
outputs: [],
outputNames: []
};
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputs).toEqual([]);
expect(result.outputNames).toEqual([]);
});
it('should handle mismatched outputs and outputNames arrays', () => {
const outputs = [
{ displayName: 'Output 1' },
{ displayName: 'Output 2' }
];
const outputNames = ['first', 'second', 'third']; // One extra
const NodeClass = class {
description = {
name: 'mismatched',
displayName: 'Mismatched Arrays',
outputs,
outputNames
};
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputs).toEqual(outputs);
expect(result.outputNames).toEqual(outputNames);
});
});
describe('real-world node structures', () => {
it('should handle actual n8n SplitInBatches node structure', () => {
// This mimics the actual structure from n8n-nodes-base
const NodeClass = class {
description = {
name: 'splitInBatches',
displayName: 'Split In Batches',
description: 'Split data into batches and iterate over each batch',
icon: 'fa:th-large',
group: ['transform'],
version: 3,
outputs: [
{
displayName: 'Done',
name: 'done',
type: 'main',
hint: 'Receives the final data after all batches have been processed'
},
{
displayName: 'Loop',
name: 'loop',
type: 'main',
hint: 'Receives the current batch data during each iteration'
}
],
outputNames: ['done', 'loop']
};
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputs).toHaveLength(2);
expect(result.outputs).toBeDefined();
expect(result.outputs![0].displayName).toBe('Done');
expect(result.outputs![1].displayName).toBe('Loop');
expect(result.outputNames).toEqual(['done', 'loop']);
});
it('should handle actual n8n IF node structure', () => {
// This mimics the actual structure from n8n-nodes-base
const NodeClass = class {
description = {
name: 'if',
displayName: 'IF',
description: 'Route items to different outputs based on conditions',
icon: 'fa:map-signs',
group: ['transform'],
version: 2,
outputs: [
{
displayName: 'True',
name: 'true',
type: 'main',
hint: 'Items that match the condition'
},
{
displayName: 'False',
name: 'false',
type: 'main',
hint: 'Items that do not match the condition'
}
],
outputNames: ['true', 'false']
};
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputs).toHaveLength(2);
expect(result.outputs).toBeDefined();
expect(result.outputs![0].displayName).toBe('True');
expect(result.outputs![1].displayName).toBe('False');
expect(result.outputNames).toEqual(['true', 'false']);
});
it('should handle single-output nodes like HTTP Request', () => {
const NodeClass = class {
description = {
name: 'httpRequest',
displayName: 'HTTP Request',
description: 'Make HTTP requests',
icon: 'fa:at',
group: ['input'],
version: 4
// No outputs specified - single main output implied
};
};
const result = parser.parse(NodeClass, 'n8n-nodes-base');
expect(result.outputs).toBeUndefined();
expect(result.outputNames).toBeUndefined();
});
});
});

View File

@@ -1,865 +0,0 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { WorkflowValidator } from '@/services/workflow-validator';
import { NodeRepository } from '@/database/node-repository';
import { EnhancedConfigValidator } from '@/services/enhanced-config-validator';
// Mock dependencies
vi.mock('@/database/node-repository');
vi.mock('@/services/enhanced-config-validator');
describe('Loop Output Fix - Edge Cases', () => {
let validator: WorkflowValidator;
let mockNodeRepository: any;
let mockNodeValidator: any;
beforeEach(() => {
vi.clearAllMocks();
mockNodeRepository = {
getNode: vi.fn((nodeType: string) => {
// Default return
if (nodeType === 'nodes-base.splitInBatches') {
return {
nodeType: 'nodes-base.splitInBatches',
outputs: [
{ displayName: 'Done', name: 'done' },
{ displayName: 'Loop', name: 'loop' }
],
outputNames: ['done', 'loop'],
properties: []
};
}
return {
nodeType,
properties: []
};
})
};
mockNodeValidator = {
validateWithMode: vi.fn().mockReturnValue({
errors: [],
warnings: []
})
};
validator = new WorkflowValidator(mockNodeRepository, mockNodeValidator);
});
describe('Nodes without outputs', () => {
it('should handle nodes with null outputs gracefully', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.httpRequest',
outputs: null,
outputNames: null,
properties: []
});
const workflow = {
name: 'No Outputs Workflow',
nodes: [
{
id: '1',
name: 'HTTP Request',
type: 'n8n-nodes-base.httpRequest',
position: [100, 100],
parameters: { url: 'https://example.com' }
},
{
id: '2',
name: 'Set',
type: 'n8n-nodes-base.set',
position: [300, 100],
parameters: {}
}
],
connections: {
'HTTP Request': {
main: [
[{ node: 'Set', type: 'main', index: 0 }]
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should not crash or produce output-related errors
expect(result).toBeDefined();
const outputErrors = result.errors.filter(e =>
e.message?.includes('output') && !e.message?.includes('Connection')
);
expect(outputErrors).toHaveLength(0);
});
it('should handle nodes with undefined outputs gracefully', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.webhook',
// outputs and outputNames are undefined
properties: []
});
const workflow = {
name: 'Undefined Outputs Workflow',
nodes: [
{
id: '1',
name: 'Webhook',
type: 'n8n-nodes-base.webhook',
position: [100, 100],
parameters: {}
}
],
connections: {}
};
const result = await validator.validateWorkflow(workflow as any);
expect(result).toBeDefined();
expect(result.valid).toBeTruthy(); // Empty workflow with webhook should be valid
});
it('should handle nodes with empty outputs array', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.customNode',
outputs: [],
outputNames: [],
properties: []
});
const workflow = {
name: 'Empty Outputs Workflow',
nodes: [
{
id: '1',
name: 'Custom Node',
type: 'n8n-nodes-base.customNode',
position: [100, 100],
parameters: {}
}
],
connections: {
'Custom Node': {
main: [
[{ node: 'Custom Node', type: 'main', index: 0 }] // Self-reference
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should warn about self-reference but not crash
const selfRefWarnings = result.warnings.filter(w =>
w.message?.includes('self-referencing')
);
expect(selfRefWarnings).toHaveLength(1);
});
});
describe('Invalid connection indices', () => {
it('should handle negative connection indices', async () => {
// Use default mock that includes outputs for SplitInBatches
const workflow = {
name: 'Negative Index Workflow',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: 'Set',
type: 'n8n-nodes-base.set',
position: [300, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[{ node: 'Set', type: 'main', index: -1 }] // Invalid negative index
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
const negativeIndexErrors = result.errors.filter(e =>
e.message?.includes('Invalid connection index -1')
);
expect(negativeIndexErrors).toHaveLength(1);
expect(negativeIndexErrors[0].message).toContain('must be non-negative');
});
it('should handle very large connection indices', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.switch',
outputs: [
{ displayName: 'Output 1' },
{ displayName: 'Output 2' }
],
properties: []
});
const workflow = {
name: 'Large Index Workflow',
nodes: [
{
id: '1',
name: 'Switch',
type: 'n8n-nodes-base.switch',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: 'Set',
type: 'n8n-nodes-base.set',
position: [300, 100],
parameters: {}
}
],
connections: {
'Switch': {
main: [
[{ node: 'Set', type: 'main', index: 999 }] // Very large index
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should validate without crashing (n8n allows large indices)
expect(result).toBeDefined();
});
});
describe('Malformed connection structures', () => {
it('should handle null connection objects', async () => {
// Use default mock that includes outputs for SplitInBatches
const workflow = {
name: 'Null Connections Workflow',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
null, // Null output
[{ node: 'NonExistent', type: 'main', index: 0 }]
] as any
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should handle gracefully without crashing
expect(result).toBeDefined();
});
it('should handle missing connection properties', async () => {
// Use default mock that includes outputs for SplitInBatches
const workflow = {
name: 'Malformed Connections Workflow',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: 'Set',
type: 'n8n-nodes-base.set',
position: [300, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[
{ node: 'Set' } as any, // Missing type and index
{ type: 'main', index: 0 } as any, // Missing node
{} as any // Empty object
]
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should handle malformed connections but report errors
expect(result).toBeDefined();
expect(result.errors.length).toBeGreaterThan(0);
});
});
describe('Deep loop back detection limits', () => {
it('should respect maxDepth limit in checkForLoopBack', async () => {
// Use default mock that includes outputs for SplitInBatches
// Create a very deep chain that exceeds maxDepth (50)
const nodes = [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
}
];
const connections: any = {
'Split In Batches': {
main: [
[], // Done output
[{ node: 'Node1', type: 'main', index: 0 }] // Loop output
]
}
};
// Create chain of 60 nodes (exceeds maxDepth of 50)
for (let i = 1; i <= 60; i++) {
nodes.push({
id: (i + 1).toString(),
name: `Node${i}`,
type: 'n8n-nodes-base.set',
position: [100 + i * 50, 100],
parameters: {}
});
if (i < 60) {
connections[`Node${i}`] = {
main: [[{ node: `Node${i + 1}`, type: 'main', index: 0 }]]
};
} else {
// Last node connects back to Split In Batches
connections[`Node${i}`] = {
main: [[{ node: 'Split In Batches', type: 'main', index: 0 }]]
};
}
}
const workflow = {
name: 'Deep Chain Workflow',
nodes,
connections
};
const result = await validator.validateWorkflow(workflow as any);
// Should warn about missing loop back because depth limit prevents detection
const loopBackWarnings = result.warnings.filter(w =>
w.message?.includes('doesn\'t connect back')
);
expect(loopBackWarnings).toHaveLength(1);
});
it('should handle circular references without infinite loops', async () => {
// Use default mock that includes outputs for SplitInBatches
const workflow = {
name: 'Circular Reference Workflow',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: 'NodeA',
type: 'n8n-nodes-base.set',
position: [300, 100],
parameters: {}
},
{
id: '3',
name: 'NodeB',
type: 'n8n-nodes-base.function',
position: [500, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[],
[{ node: 'NodeA', type: 'main', index: 0 }]
]
},
'NodeA': {
main: [
[{ node: 'NodeB', type: 'main', index: 0 }]
]
},
'NodeB': {
main: [
[{ node: 'NodeA', type: 'main', index: 0 }] // Circular: B -> A -> B -> A ...
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should complete without hanging and warn about missing loop back
expect(result).toBeDefined();
const loopBackWarnings = result.warnings.filter(w =>
w.message?.includes('doesn\'t connect back')
);
expect(loopBackWarnings).toHaveLength(1);
});
it('should handle self-referencing nodes in loop back detection', async () => {
// Use default mock that includes outputs for SplitInBatches
const workflow = {
name: 'Self Reference Workflow',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: 'SelfRef',
type: 'n8n-nodes-base.set',
position: [300, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[],
[{ node: 'SelfRef', type: 'main', index: 0 }]
]
},
'SelfRef': {
main: [
[{ node: 'SelfRef', type: 'main', index: 0 }] // Self-reference instead of loop back
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should warn about missing loop back and self-reference
const loopBackWarnings = result.warnings.filter(w =>
w.message?.includes('doesn\'t connect back')
);
const selfRefWarnings = result.warnings.filter(w =>
w.message?.includes('self-referencing')
);
expect(loopBackWarnings).toHaveLength(1);
expect(selfRefWarnings).toHaveLength(1);
});
});
describe('Complex output structures', () => {
it('should handle nodes with many outputs', async () => {
const manyOutputs = Array.from({ length: 20 }, (_, i) => ({
displayName: `Output ${i + 1}`,
name: `output${i + 1}`,
description: `Output number ${i + 1}`
}));
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.complexSwitch',
outputs: manyOutputs,
outputNames: manyOutputs.map(o => o.name),
properties: []
});
const workflow = {
name: 'Many Outputs Workflow',
nodes: [
{
id: '1',
name: 'Complex Switch',
type: 'n8n-nodes-base.complexSwitch',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: 'Set',
type: 'n8n-nodes-base.set',
position: [300, 100],
parameters: {}
}
],
connections: {
'Complex Switch': {
main: Array.from({ length: 20 }, () => [
{ node: 'Set', type: 'main', index: 0 }
])
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should handle without performance issues
expect(result).toBeDefined();
});
it('should handle mixed output types (main, error, ai_tool)', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.complexNode',
outputs: [
{ displayName: 'Main', type: 'main' },
{ displayName: 'Error', type: 'error' }
],
properties: []
});
const workflow = {
name: 'Mixed Output Types Workflow',
nodes: [
{
id: '1',
name: 'Complex Node',
type: 'n8n-nodes-base.complexNode',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: 'Main Handler',
type: 'n8n-nodes-base.set',
position: [300, 50],
parameters: {}
},
{
id: '3',
name: 'Error Handler',
type: 'n8n-nodes-base.set',
position: [300, 150],
parameters: {}
},
{
id: '4',
name: 'Tool',
type: 'n8n-nodes-base.httpRequest',
position: [500, 100],
parameters: {}
}
],
connections: {
'Complex Node': {
main: [
[{ node: 'Main Handler', type: 'main', index: 0 }]
],
error: [
[{ node: 'Error Handler', type: 'main', index: 0 }]
],
ai_tool: [
[{ node: 'Tool', type: 'main', index: 0 }]
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should validate all connection types
expect(result).toBeDefined();
expect(result.statistics.validConnections).toBe(3);
});
});
describe('SplitInBatches specific edge cases', () => {
it('should handle SplitInBatches with no connections', async () => {
// Use default mock that includes outputs for SplitInBatches
const workflow = {
name: 'Isolated SplitInBatches',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
}
],
connections: {}
};
const result = await validator.validateWorkflow(workflow as any);
// Should not produce SplitInBatches-specific warnings for isolated node
const splitWarnings = result.warnings.filter(w =>
w.message?.includes('SplitInBatches') ||
w.message?.includes('loop') ||
w.message?.includes('done')
);
expect(splitWarnings).toHaveLength(0);
});
it('should handle SplitInBatches with only one output connected', async () => {
// Use default mock that includes outputs for SplitInBatches
const workflow = {
name: 'Single Output SplitInBatches',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: 'Final Action',
type: 'n8n-nodes-base.emailSend',
position: [300, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[{ node: 'Final Action', type: 'main', index: 0 }], // Only done output connected
[] // Loop output empty
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should NOT warn about empty loop output (it's only a problem if loop connects to something but doesn't loop back)
// An empty loop output is valid - it just means no looping occurs
const loopWarnings = result.warnings.filter(w =>
w.message?.includes('loop') && w.message?.includes('connect back')
);
expect(loopWarnings).toHaveLength(0);
});
it('should handle SplitInBatches with both outputs to same node', async () => {
// Use default mock that includes outputs for SplitInBatches
const workflow = {
name: 'Same Target SplitInBatches',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: 'Multi Purpose',
type: 'n8n-nodes-base.set',
position: [300, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[{ node: 'Multi Purpose', type: 'main', index: 0 }], // Done -> Multi Purpose
[{ node: 'Multi Purpose', type: 'main', index: 0 }] // Loop -> Multi Purpose
]
},
'Multi Purpose': {
main: [
[{ node: 'Split In Batches', type: 'main', index: 0 }] // Loop back
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Both outputs go to same node which loops back - should be valid
// No warnings about loop back since it does connect back
const loopWarnings = result.warnings.filter(w =>
w.message?.includes('loop') && w.message?.includes('connect back')
);
expect(loopWarnings).toHaveLength(0);
});
it('should detect reversed outputs with processing node on done output', async () => {
// Use default mock that includes outputs for SplitInBatches
const workflow = {
name: 'Reversed SplitInBatches with Function Node',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: 'Process Function',
type: 'n8n-nodes-base.function',
position: [300, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[{ node: 'Process Function', type: 'main', index: 0 }], // Done -> Function (this is wrong)
[] // Loop output empty
]
},
'Process Function': {
main: [
[{ node: 'Split In Batches', type: 'main', index: 0 }] // Function connects back (indicates it should be on loop)
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should error about reversed outputs since function node on done output connects back
const reversedErrors = result.errors.filter(e =>
e.message?.includes('SplitInBatches outputs appear reversed')
);
expect(reversedErrors).toHaveLength(1);
});
it('should handle non-existent node type gracefully', async () => {
// Node doesn't exist in repository
mockNodeRepository.getNode.mockReturnValue(null);
const workflow = {
name: 'Unknown Node Type',
nodes: [
{
id: '1',
name: 'Unknown Node',
type: 'n8n-nodes-base.unknownNode',
position: [100, 100],
parameters: {}
}
],
connections: {}
};
const result = await validator.validateWorkflow(workflow as any);
// Should report unknown node type error
const unknownNodeErrors = result.errors.filter(e =>
e.message?.includes('Unknown node type')
);
expect(unknownNodeErrors).toHaveLength(1);
});
});
describe('Performance edge cases', () => {
it('should handle very large workflows efficiently', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.set',
properties: []
});
// Create workflow with 1000 nodes
const nodes = Array.from({ length: 1000 }, (_, i) => ({
id: `node${i}`,
name: `Node ${i}`,
type: 'n8n-nodes-base.set',
position: [100 + (i % 50) * 50, 100 + Math.floor(i / 50) * 50],
parameters: {}
}));
// Create simple linear connections
const connections: any = {};
for (let i = 0; i < 999; i++) {
connections[`Node ${i}`] = {
main: [[{ node: `Node ${i + 1}`, type: 'main', index: 0 }]]
};
}
const workflow = {
name: 'Large Workflow',
nodes,
connections
};
const startTime = Date.now();
const result = await validator.validateWorkflow(workflow as any);
const duration = Date.now() - startTime;
// Should complete within reasonable time (< 5 seconds)
expect(duration).toBeLessThan(5000);
expect(result).toBeDefined();
expect(result.statistics.totalNodes).toBe(1000);
});
it('should handle workflows with many SplitInBatches nodes', async () => {
// Use default mock that includes outputs for SplitInBatches
// Create 100 SplitInBatches nodes
const nodes = Array.from({ length: 100 }, (_, i) => ({
id: `split${i}`,
name: `Split ${i}`,
type: 'n8n-nodes-base.splitInBatches',
position: [100 + (i % 10) * 100, 100 + Math.floor(i / 10) * 100],
parameters: {}
}));
const connections: any = {};
// Each split connects to the next one
for (let i = 0; i < 99; i++) {
connections[`Split ${i}`] = {
main: [
[{ node: `Split ${i + 1}`, type: 'main', index: 0 }], // Done -> next split
[] // Empty loop
]
};
}
const workflow = {
name: 'Many SplitInBatches Workflow',
nodes,
connections
};
const result = await validator.validateWorkflow(workflow as any);
// Should validate all nodes without performance issues
expect(result).toBeDefined();
expect(result.statistics.totalNodes).toBe(100);
});
});
});

View File

@@ -223,7 +223,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
it('should error when nodes array is missing', async () => {
const workflow = { connections: {} } as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.valid).toBe(false);
expect(result.errors.some(e => e.message === 'Workflow must have a nodes array')).toBe(true);
@@ -232,7 +232,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
it('should error when connections object is missing', async () => {
const workflow = { nodes: [] } as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.valid).toBe(false);
expect(result.errors.some(e => e.message === 'Workflow must have a connections object')).toBe(true);
@@ -241,7 +241,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
it('should warn when workflow has no nodes', async () => {
const workflow = { nodes: [], connections: {} } as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.valid).toBe(true); // Empty workflows are valid but get a warning
expect(result.warnings).toHaveLength(1);
@@ -260,7 +260,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.valid).toBe(false);
expect(result.errors.some(e => e.message.includes('Single-node workflows are only valid for webhook endpoints'))).toBe(true);
@@ -279,7 +279,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.valid).toBe(true);
expect(result.warnings.some(w => w.message.includes('Webhook node has no connections'))).toBe(true);
@@ -306,7 +306,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.valid).toBe(false);
expect(result.errors.some(e => e.message.includes('Multi-node workflow has no connections'))).toBe(true);
@@ -333,7 +333,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Duplicate node name: "Webhook"'))).toBe(true);
});
@@ -359,7 +359,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Duplicate node ID: "1"'))).toBe(true);
});
@@ -392,7 +392,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.statistics.triggerNodes).toBe(3);
});
@@ -422,7 +422,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('Workflow has no trigger nodes'))).toBe(true);
});
@@ -449,7 +449,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.statistics.totalNodes).toBe(2);
expect(result.statistics.enabledNodes).toBe(1);
@@ -472,7 +472,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(mockNodeRepository.getNode).not.toHaveBeenCalled();
});
@@ -491,7 +491,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.valid).toBe(false);
expect(result.errors.some(e => e.message.includes('Invalid node type: "nodes-base.webhook"'))).toBe(true);
@@ -512,7 +512,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.valid).toBe(false);
expect(result.errors.some(e => e.message.includes('Unknown node type: "httpRequest"'))).toBe(true);
@@ -533,7 +533,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(mockNodeRepository.getNode).toHaveBeenCalledWith('n8n-nodes-base.webhook');
expect(mockNodeRepository.getNode).toHaveBeenCalledWith('nodes-base.webhook');
@@ -553,7 +553,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(mockNodeRepository.getNode).toHaveBeenCalledWith('@n8n/n8n-nodes-langchain.agent');
expect(mockNodeRepository.getNode).toHaveBeenCalledWith('nodes-langchain.agent');
@@ -574,7 +574,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Missing required property \'typeVersion\''))).toBe(true);
});
@@ -594,7 +594,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Invalid typeVersion: invalid'))).toBe(true);
});
@@ -614,7 +614,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('Outdated typeVersion: 1. Latest is 2'))).toBe(true);
});
@@ -634,7 +634,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('typeVersion 10 exceeds maximum supported version 2'))).toBe(true);
});
@@ -664,7 +664,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Missing required field: url'))).toBe(true);
expect(result.warnings.some(w => w.message.includes('Consider using HTTPS'))).toBe(true);
@@ -689,7 +689,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Failed to validate node: Validation error'))).toBe(true);
});
@@ -721,7 +721,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.statistics.validConnections).toBe(1);
expect(result.statistics.invalidConnections).toBe(0);
@@ -745,7 +745,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Connection from non-existent node: "NonExistent"'))).toBe(true);
expect(result.statistics.invalidConnections).toBe(1);
@@ -776,7 +776,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Connection uses node ID \'webhook-id\' instead of node name \'Webhook\''))).toBe(true);
});
@@ -799,7 +799,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Connection to non-existent node: "NonExistent"'))).toBe(true);
expect(result.statistics.invalidConnections).toBe(1);
@@ -830,7 +830,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Connection target uses node ID \'set-id\' instead of node name \'Set\''))).toBe(true);
});
@@ -861,7 +861,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('Connection to disabled node: "Set"'))).toBe(true);
});
@@ -891,7 +891,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.statistics.validConnections).toBe(1);
});
@@ -921,7 +921,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.statistics.validConnections).toBe(1);
});
@@ -953,7 +953,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('Community node "CustomTool" is being used as an AI tool'))).toBe(true);
});
@@ -990,7 +990,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('Node is not connected to any other nodes') && w.nodeName === 'Orphaned')).toBe(true);
});
@@ -1033,7 +1033,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Workflow contains a cycle'))).toBe(true);
});
@@ -1068,7 +1068,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.statistics.validConnections).toBe(1);
expect(result.valid).toBe(true);
@@ -1110,7 +1110,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(ExpressionValidator.validateNodeExpressions).toHaveBeenCalledWith(
expect.objectContaining({ values: expect.any(Object) }),
@@ -1146,7 +1146,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Expression error: Invalid expression syntax'))).toBe(true);
expect(result.warnings.some(w => w.message.includes('Expression warning: Deprecated variable usage'))).toBe(true);
@@ -1170,7 +1170,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(ExpressionValidator.validateNodeExpressions).not.toHaveBeenCalled();
});
@@ -1187,7 +1187,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
const workflow = builder.build() as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('Consider adding error handling'))).toBe(true);
});
@@ -1208,7 +1208,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
const workflow = builder.build() as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('Long linear chain detected'))).toBe(true);
});
@@ -1230,7 +1230,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('Missing credentials configuration for slackApi'))).toBe(true);
});
@@ -1249,7 +1249,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('AI Agent has no tools connected'))).toBe(true);
});
@@ -1279,7 +1279,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.suggestions.some(s => s.includes('N8N_COMMUNITY_PACKAGES_ALLOW_TOOL_USAGE'))).toBe(true);
});
@@ -1306,7 +1306,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Node-level properties onError, retryOnFail, credentials are in the wrong location'))).toBe(true);
expect(result.errors.some(e => e.details?.fix?.includes('Move these properties from node.parameters to the node level'))).toBe(true);
@@ -1327,7 +1327,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Invalid onError value: "invalidValue"'))).toBe(true);
});
@@ -1347,7 +1347,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('Using deprecated "continueOnFail: true"'))).toBe(true);
});
@@ -1368,7 +1368,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Cannot use both "continueOnFail" and "onError" properties'))).toBe(true);
});
@@ -1390,7 +1390,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('maxTries must be a positive number'))).toBe(true);
expect(result.errors.some(e => e.message.includes('waitBetweenTries must be a non-negative number'))).toBe(true);
@@ -1413,7 +1413,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('maxTries is set to 15'))).toBe(true);
expect(result.warnings.some(w => w.message.includes('waitBetweenTries is set to 400000ms'))).toBe(true);
@@ -1434,7 +1434,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('retryOnFail is enabled but maxTries is not specified'))).toBe(true);
});
@@ -1459,7 +1459,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('alwaysOutputData must be a boolean'))).toBe(true);
@@ -1484,7 +1484,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('executeOnce is enabled'))).toBe(true);
});
@@ -1512,7 +1512,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes(nodeInfo.message) && w.message.includes('without error handling'))).toBe(true);
}
@@ -1534,7 +1534,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('Both continueOnFail and retryOnFail are enabled'))).toBe(true);
});
@@ -1554,7 +1554,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.suggestions.some(s => s.includes('Consider enabling alwaysOutputData'))).toBe(true);
});
@@ -1569,7 +1569,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
const workflow = builder.build() as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.suggestions.some(s => s.includes('Most nodes lack error handling'))).toBe(true);
});
@@ -1589,7 +1589,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.suggestions.some(s => s.includes('Replace "continueOnFail: true" with "onError:'))).toBe(true);
});
@@ -1610,7 +1610,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.suggestions.some(s => s.includes('Add a trigger node'))).toBe(true);
});
@@ -1636,7 +1636,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {} // Missing connections
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.suggestions.some(s => s.includes('Example connection structure'))).toBe(true);
expect(result.suggestions.some(s => s.includes('Use node NAMES (not IDs) in connections'))).toBe(true);
@@ -1667,7 +1667,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.suggestions.some(s => s.includes('Add error handling'))).toBe(true);
});
@@ -1682,7 +1682,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
const workflow = builder.build() as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.suggestions.some(s => s.includes('Consider breaking this workflow into smaller sub-workflows'))).toBe(true);
});
@@ -1708,7 +1708,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.suggestions.some(s => s.includes('Consider using a Code node for complex data transformations'))).toBe(true);
});
@@ -1727,7 +1727,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.suggestions.some(s => s.includes('A minimal workflow needs'))).toBe(true);
});
@@ -1756,7 +1756,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
connections: {}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes(`Did you mean`) && e.message.includes(testCase.suggestion))).toBe(true);
}
@@ -1848,7 +1848,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
// Should have multiple errors
expect(result.valid).toBe(false);
@@ -1940,7 +1940,7 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
}
} as any;
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.valid).toBe(true);
expect(result.errors).toHaveLength(0);

View File

@@ -157,7 +157,7 @@ describe('WorkflowValidator - Edge Cases', () => {
nodes: [],
connections: {}
};
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.valid).toBe(true);
expect(result.warnings.some(w => w.message.includes('empty'))).toBe(true);
});
@@ -181,7 +181,7 @@ describe('WorkflowValidator - Edge Cases', () => {
const workflow = { nodes, connections };
const start = Date.now();
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
const duration = Date.now() - start;
expect(result).toBeDefined();
@@ -207,7 +207,7 @@ describe('WorkflowValidator - Edge Cases', () => {
}
};
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.statistics.invalidConnections).toBe(0);
});
@@ -228,7 +228,7 @@ describe('WorkflowValidator - Edge Cases', () => {
}
};
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.valid).toBe(true);
});
});
@@ -264,7 +264,7 @@ describe('WorkflowValidator - Edge Cases', () => {
connections: {}
};
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.length).toBeGreaterThan(0);
});
@@ -292,7 +292,7 @@ describe('WorkflowValidator - Edge Cases', () => {
}
};
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('self-referencing'))).toBe(true);
});
@@ -308,7 +308,7 @@ describe('WorkflowValidator - Edge Cases', () => {
}
};
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('non-existent'))).toBe(true);
});
@@ -324,7 +324,7 @@ describe('WorkflowValidator - Edge Cases', () => {
}
};
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.length).toBeGreaterThan(0);
});
@@ -341,7 +341,7 @@ describe('WorkflowValidator - Edge Cases', () => {
} as any
};
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
// Should still work as type and index can have defaults
expect(result.statistics.validConnections).toBeGreaterThan(0);
});
@@ -359,7 +359,7 @@ describe('WorkflowValidator - Edge Cases', () => {
}
};
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.some(e => e.message.includes('Invalid'))).toBe(true);
});
});
@@ -382,7 +382,7 @@ describe('WorkflowValidator - Edge Cases', () => {
}
};
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.valid).toBe(true);
});
@@ -395,7 +395,7 @@ describe('WorkflowValidator - Edge Cases', () => {
connections: {}
};
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.warnings.some(w => w.message.includes('very long'))).toBe(true);
});
});
@@ -479,7 +479,7 @@ describe('WorkflowValidator - Edge Cases', () => {
}
};
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.statistics.validConnections).toBeGreaterThan(0);
});
});
@@ -499,7 +499,7 @@ describe('WorkflowValidator - Edge Cases', () => {
}
};
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
expect(result.errors.length).toBeGreaterThan(0);
expect(result.statistics.validConnections).toBeGreaterThan(0);
});

View File

@@ -1,434 +0,0 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { WorkflowValidator } from '@/services/workflow-validator';
import { NodeRepository } from '@/database/node-repository';
import { EnhancedConfigValidator } from '@/services/enhanced-config-validator';
// Mock dependencies
vi.mock('@/database/node-repository');
vi.mock('@/services/enhanced-config-validator');
describe('WorkflowValidator - SplitInBatches Validation (Simplified)', () => {
let validator: WorkflowValidator;
let mockNodeRepository: any;
let mockNodeValidator: any;
beforeEach(() => {
vi.clearAllMocks();
mockNodeRepository = {
getNode: vi.fn()
};
mockNodeValidator = {
validateWithMode: vi.fn().mockReturnValue({
errors: [],
warnings: []
})
};
validator = new WorkflowValidator(mockNodeRepository, mockNodeValidator);
});
describe('SplitInBatches node detection', () => {
it('should identify SplitInBatches nodes in workflow', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const workflow = {
name: 'SplitInBatches Workflow',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: { batchSize: 10 }
},
{
id: '2',
name: 'Process Item',
type: 'n8n-nodes-base.set',
position: [300, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[], // Done output (0)
[{ node: 'Process Item', type: 'main', index: 0 }] // Loop output (1)
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should complete validation without crashing
expect(result).toBeDefined();
expect(result.valid).toBeDefined();
});
it('should handle SplitInBatches with processing node name patterns', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const processingNames = [
'Process Item',
'Transform Data',
'Handle Each',
'Function Node',
'Code Block'
];
for (const nodeName of processingNames) {
const workflow = {
name: 'Processing Pattern Test',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: nodeName,
type: 'n8n-nodes-base.function',
position: [300, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[{ node: nodeName, type: 'main', index: 0 }], // Processing node on Done output
[]
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should identify potential processing nodes
expect(result).toBeDefined();
}
});
it('should handle final processing node patterns', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const finalNames = [
'Final Summary',
'Send Email',
'Complete Notification',
'Final Report'
];
for (const nodeName of finalNames) {
const workflow = {
name: 'Final Pattern Test',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: nodeName,
type: 'n8n-nodes-base.emailSend',
position: [300, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[{ node: nodeName, type: 'main', index: 0 }], // Final node on Done output (correct)
[]
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should not warn about final nodes on done output
expect(result).toBeDefined();
}
});
});
describe('Connection validation', () => {
it('should validate connection indices', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const workflow = {
name: 'Connection Index Test',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: 'Target',
type: 'n8n-nodes-base.set',
position: [300, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[{ node: 'Target', type: 'main', index: -1 }] // Invalid negative index
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
const negativeIndexErrors = result.errors.filter(e =>
e.message?.includes('Invalid connection index -1')
);
expect(negativeIndexErrors.length).toBeGreaterThan(0);
});
it('should handle non-existent target nodes', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const workflow = {
name: 'Missing Target Test',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[{ node: 'NonExistentNode', type: 'main', index: 0 }]
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
const missingNodeErrors = result.errors.filter(e =>
e.message?.includes('non-existent node')
);
expect(missingNodeErrors.length).toBeGreaterThan(0);
});
});
describe('Self-referencing connections', () => {
it('should allow self-referencing for SplitInBatches nodes', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const workflow = {
name: 'Self Reference Test',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[],
[{ node: 'Split In Batches', type: 'main', index: 0 }] // Self-reference on loop output
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should not warn about self-reference for SplitInBatches
const selfRefWarnings = result.warnings.filter(w =>
w.message?.includes('self-referencing')
);
expect(selfRefWarnings).toHaveLength(0);
});
it('should warn about self-referencing for non-loop nodes', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.set',
properties: []
});
const workflow = {
name: 'Non-Loop Self Reference Test',
nodes: [
{
id: '1',
name: 'Set',
type: 'n8n-nodes-base.set',
position: [100, 100],
parameters: {}
}
],
connections: {
'Set': {
main: [
[{ node: 'Set', type: 'main', index: 0 }] // Self-reference on regular node
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should warn about self-reference for non-loop nodes
const selfRefWarnings = result.warnings.filter(w =>
w.message?.includes('self-referencing')
);
expect(selfRefWarnings.length).toBeGreaterThan(0);
});
});
describe('Output connection validation', () => {
it('should validate output connections for nodes with outputs', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.if',
outputs: [
{ displayName: 'True', description: 'Items that match condition' },
{ displayName: 'False', description: 'Items that do not match condition' }
],
outputNames: ['true', 'false'],
properties: []
});
const workflow = {
name: 'IF Node Test',
nodes: [
{
id: '1',
name: 'IF',
type: 'n8n-nodes-base.if',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: 'True Handler',
type: 'n8n-nodes-base.set',
position: [300, 50],
parameters: {}
},
{
id: '3',
name: 'False Handler',
type: 'n8n-nodes-base.set',
position: [300, 150],
parameters: {}
}
],
connections: {
'IF': {
main: [
[{ node: 'True Handler', type: 'main', index: 0 }], // True output (0)
[{ node: 'False Handler', type: 'main', index: 0 }] // False output (1)
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should validate without major errors
expect(result).toBeDefined();
expect(result.statistics.validConnections).toBe(2);
});
});
describe('Error handling', () => {
it('should handle nodes without outputs gracefully', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.httpRequest',
outputs: null,
outputNames: null,
properties: []
});
const workflow = {
name: 'No Outputs Test',
nodes: [
{
id: '1',
name: 'HTTP Request',
type: 'n8n-nodes-base.httpRequest',
position: [100, 100],
parameters: {}
}
],
connections: {}
};
const result = await validator.validateWorkflow(workflow as any);
// Should handle gracefully without crashing
expect(result).toBeDefined();
});
it('should handle unknown node types gracefully', async () => {
mockNodeRepository.getNode.mockReturnValue(null);
const workflow = {
name: 'Unknown Node Test',
nodes: [
{
id: '1',
name: 'Unknown',
type: 'n8n-nodes-base.unknown',
position: [100, 100],
parameters: {}
}
],
connections: {}
};
const result = await validator.validateWorkflow(workflow as any);
// Should report unknown node error
const unknownErrors = result.errors.filter(e =>
e.message?.includes('Unknown node type')
);
expect(unknownErrors.length).toBeGreaterThan(0);
});
});
});

View File

@@ -1,705 +0,0 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { WorkflowValidator } from '@/services/workflow-validator';
import { NodeRepository } from '@/database/node-repository';
import { EnhancedConfigValidator } from '@/services/enhanced-config-validator';
// Mock dependencies
vi.mock('@/database/node-repository');
vi.mock('@/services/enhanced-config-validator');
describe('WorkflowValidator - Loop Node Validation', () => {
let validator: WorkflowValidator;
let mockNodeRepository: any;
let mockNodeValidator: any;
beforeEach(() => {
vi.clearAllMocks();
mockNodeRepository = {
getNode: vi.fn()
};
mockNodeValidator = {
validateWithMode: vi.fn().mockReturnValue({
errors: [],
warnings: []
})
};
validator = new WorkflowValidator(mockNodeRepository, mockNodeValidator);
});
describe('validateSplitInBatchesConnection', () => {
const createWorkflow = (connections: any) => ({
name: 'Test Workflow',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: { batchSize: 10 }
},
{
id: '2',
name: 'Process Item',
type: 'n8n-nodes-base.set',
position: [300, 100],
parameters: {}
},
{
id: '3',
name: 'Final Summary',
type: 'n8n-nodes-base.emailSend',
position: [500, 100],
parameters: {}
}
],
connections
});
it('should detect reversed SplitInBatches connections (processing node on done output)', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
// Create a processing node with a name that matches the pattern (includes "process")
const workflow = {
name: 'Test Workflow',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: { batchSize: 10 }
},
{
id: '2',
name: 'Process Function', // Name matches processing pattern
type: 'n8n-nodes-base.function', // Type also matches processing pattern
position: [300, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[{ node: 'Process Function', type: 'main', index: 0 }], // Done output (wrong for processing)
[] // No loop connections
]
},
'Process Function': {
main: [
[{ node: 'Split In Batches', type: 'main', index: 0 }] // Loop back - confirms it's processing
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// The validator should detect the processing node name/type pattern and loop back
const reversedErrors = result.errors.filter(e =>
e.message?.includes('SplitInBatches outputs appear reversed')
);
expect(reversedErrors.length).toBeGreaterThanOrEqual(1);
});
it('should warn about processing node on done output without loop back', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
// Processing node connected to "done" output but no loop back
const workflow = createWorkflow({
'Split In Batches': {
main: [
[{ node: 'Process Item', type: 'main', index: 0 }], // Done output
[]
]
}
// No loop back from Process Item
});
const result = await validator.validateWorkflow(workflow as any);
expect(result.warnings).toContainEqual(
expect.objectContaining({
type: 'warning',
nodeId: '1',
nodeName: 'Split In Batches',
message: expect.stringContaining('connected to the "done" output (index 0) but appears to be a processing node')
})
);
});
it('should warn about final processing node on loop output', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
// Final summary node connected to "loop" output (index 1) - suspicious
const workflow = createWorkflow({
'Split In Batches': {
main: [
[],
[{ node: 'Final Summary', type: 'main', index: 0 }] // Loop output for final node
]
}
});
const result = await validator.validateWorkflow(workflow as any);
expect(result.warnings).toContainEqual(
expect.objectContaining({
type: 'warning',
nodeId: '1',
nodeName: 'Split In Batches',
message: expect.stringContaining('connected to the "loop" output (index 1) but appears to be a post-processing node')
})
);
});
it('should warn about loop output without loop back connection', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
// Processing node on loop output but doesn't connect back
const workflow = createWorkflow({
'Split In Batches': {
main: [
[],
[{ node: 'Process Item', type: 'main', index: 0 }] // Loop output
]
}
// Process Item doesn't connect back to Split In Batches
});
const result = await validator.validateWorkflow(workflow as any);
expect(result.warnings).toContainEqual(
expect.objectContaining({
type: 'warning',
nodeId: '1',
nodeName: 'Split In Batches',
message: expect.stringContaining('doesn\'t connect back to the SplitInBatches node')
})
);
});
it('should accept correct SplitInBatches connections', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
// Create a workflow with neutral node names that don't trigger patterns
const workflow = {
name: 'Test Workflow',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: { batchSize: 10 }
},
{
id: '2',
name: 'Data Node', // Neutral name, won't trigger processing pattern
type: 'n8n-nodes-base.set',
position: [300, 100],
parameters: {}
},
{
id: '3',
name: 'Output Node', // Neutral name, won't trigger post-processing pattern
type: 'n8n-nodes-base.noOp',
position: [500, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[{ node: 'Output Node', type: 'main', index: 0 }], // Done output -> neutral node
[{ node: 'Data Node', type: 'main', index: 0 }] // Loop output -> neutral node
]
},
'Data Node': {
main: [
[{ node: 'Split In Batches', type: 'main', index: 0 }] // Loop back
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should not have SplitInBatches-specific errors or warnings
const splitErrors = result.errors.filter(e =>
e.message?.includes('SplitInBatches') ||
e.message?.includes('loop') ||
e.message?.includes('done')
);
const splitWarnings = result.warnings.filter(w =>
w.message?.includes('SplitInBatches') ||
w.message?.includes('loop') ||
w.message?.includes('done')
);
expect(splitErrors).toHaveLength(0);
expect(splitWarnings).toHaveLength(0);
});
it('should handle complex loop structures', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const complexWorkflow = {
name: 'Complex Loop',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: 'Step A', // Neutral name
type: 'n8n-nodes-base.set',
position: [300, 50],
parameters: {}
},
{
id: '3',
name: 'Step B', // Neutral name
type: 'n8n-nodes-base.noOp',
position: [500, 50],
parameters: {}
},
{
id: '4',
name: 'Final Step', // More neutral name
type: 'n8n-nodes-base.set',
position: [300, 150],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[{ node: 'Final Step', type: 'main', index: 0 }], // Done -> Final (correct)
[{ node: 'Step A', type: 'main', index: 0 }] // Loop -> Processing (correct)
]
},
'Step A': {
main: [
[{ node: 'Step B', type: 'main', index: 0 }]
]
},
'Step B': {
main: [
[{ node: 'Split In Batches', type: 'main', index: 0 }] // Loop back (correct)
]
}
}
};
const result = await validator.validateWorkflow(complexWorkflow as any);
// Should accept this correct structure without warnings
const loopWarnings = result.warnings.filter(w =>
w.message?.includes('loop') || w.message?.includes('done')
);
expect(loopWarnings).toHaveLength(0);
});
it('should detect node type patterns for processing detection', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const testCases = [
{ type: 'n8n-nodes-base.function', name: 'Process Data', shouldWarn: true },
{ type: 'n8n-nodes-base.code', name: 'Transform Item', shouldWarn: true },
{ type: 'n8n-nodes-base.set', name: 'Handle Each', shouldWarn: true },
{ type: 'n8n-nodes-base.emailSend', name: 'Final Email', shouldWarn: false },
{ type: 'n8n-nodes-base.slack', name: 'Complete Notification', shouldWarn: false }
];
for (const testCase of testCases) {
const workflow = {
name: 'Pattern Test',
nodes: [
{
id: '1',
name: 'Split In Batches',
type: 'n8n-nodes-base.splitInBatches',
position: [100, 100],
parameters: {}
},
{
id: '2',
name: testCase.name,
type: testCase.type,
position: [300, 100],
parameters: {}
}
],
connections: {
'Split In Batches': {
main: [
[{ node: testCase.name, type: 'main', index: 0 }], // Connected to done (index 0)
[]
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
const hasProcessingWarning = result.warnings.some(w =>
w.message?.includes('appears to be a processing node')
);
if (testCase.shouldWarn) {
expect(hasProcessingWarning).toBe(true);
} else {
expect(hasProcessingWarning).toBe(false);
}
}
});
});
describe('checkForLoopBack method', () => {
it('should detect direct loop back connection', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const workflow = {
name: 'Direct Loop Back',
nodes: [
{ id: '1', name: 'Split In Batches', type: 'n8n-nodes-base.splitInBatches', position: [0, 0], parameters: {} },
{ id: '2', name: 'Process', type: 'n8n-nodes-base.set', position: [0, 0], parameters: {} }
],
connections: {
'Split In Batches': {
main: [[], [{ node: 'Process', type: 'main', index: 0 }]]
},
'Process': {
main: [
[{ node: 'Split In Batches', type: 'main', index: 0 }] // Direct loop back
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should not warn about missing loop back since it exists
const missingLoopBackWarnings = result.warnings.filter(w =>
w.message?.includes('doesn\'t connect back')
);
expect(missingLoopBackWarnings).toHaveLength(0);
});
it('should detect indirect loop back connection through multiple nodes', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const workflow = {
name: 'Indirect Loop Back',
nodes: [
{ id: '1', name: 'Split In Batches', type: 'n8n-nodes-base.splitInBatches', position: [0, 0], parameters: {} },
{ id: '2', name: 'Step1', type: 'n8n-nodes-base.set', position: [0, 0], parameters: {} },
{ id: '3', name: 'Step2', type: 'n8n-nodes-base.function', position: [0, 0], parameters: {} },
{ id: '4', name: 'Step3', type: 'n8n-nodes-base.code', position: [0, 0], parameters: {} }
],
connections: {
'Split In Batches': {
main: [[], [{ node: 'Step1', type: 'main', index: 0 }]]
},
'Step1': {
main: [
[{ node: 'Step2', type: 'main', index: 0 }]
]
},
'Step2': {
main: [
[{ node: 'Step3', type: 'main', index: 0 }]
]
},
'Step3': {
main: [
[{ node: 'Split In Batches', type: 'main', index: 0 }] // Indirect loop back
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should not warn about missing loop back since indirect loop exists
const missingLoopBackWarnings = result.warnings.filter(w =>
w.message?.includes('doesn\'t connect back')
);
expect(missingLoopBackWarnings).toHaveLength(0);
});
it('should respect max depth to prevent infinite recursion', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
// Create a very deep chain that would exceed depth limit
const nodes = [
{ id: '1', name: 'Split In Batches', type: 'n8n-nodes-base.splitInBatches', position: [0, 0], parameters: {} }
];
const connections: any = {
'Split In Batches': {
main: [[], [{ node: 'Node1', type: 'main', index: 0 }]]
}
};
// Create a chain of 60 nodes (exceeds default maxDepth of 50)
for (let i = 1; i <= 60; i++) {
nodes.push({
id: (i + 1).toString(),
name: `Node${i}`,
type: 'n8n-nodes-base.set',
position: [0, 0],
parameters: {}
});
if (i < 60) {
connections[`Node${i}`] = {
main: [[{ node: `Node${i + 1}`, type: 'main', index: 0 }]]
};
} else {
// Last node connects back to Split In Batches
connections[`Node${i}`] = {
main: [[{ node: 'Split In Batches', type: 'main', index: 0 }]]
};
}
}
const workflow = {
name: 'Deep Chain',
nodes,
connections
};
const result = await validator.validateWorkflow(workflow as any);
// Should warn about missing loop back because depth limit prevents detection
const missingLoopBackWarnings = result.warnings.filter(w =>
w.message?.includes('doesn\'t connect back')
);
expect(missingLoopBackWarnings).toHaveLength(1);
});
it('should handle circular references without infinite loops', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const workflow = {
name: 'Circular Reference',
nodes: [
{ id: '1', name: 'Split In Batches', type: 'n8n-nodes-base.splitInBatches', position: [0, 0], parameters: {} },
{ id: '2', name: 'NodeA', type: 'n8n-nodes-base.set', position: [0, 0], parameters: {} },
{ id: '3', name: 'NodeB', type: 'n8n-nodes-base.function', position: [0, 0], parameters: {} }
],
connections: {
'Split In Batches': {
main: [[], [{ node: 'NodeA', type: 'main', index: 0 }]]
},
'NodeA': {
main: [
[{ node: 'NodeB', type: 'main', index: 0 }]
]
},
'NodeB': {
main: [
[{ node: 'NodeA', type: 'main', index: 0 }] // Circular reference (doesn't connect back to Split)
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should complete without hanging and warn about missing loop back
const missingLoopBackWarnings = result.warnings.filter(w =>
w.message?.includes('doesn\'t connect back')
);
expect(missingLoopBackWarnings).toHaveLength(1);
});
});
describe('self-referencing connections', () => {
it('should allow self-referencing for SplitInBatches (loop back)', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const workflow = {
name: 'Self Reference Loop',
nodes: [
{ id: '1', name: 'Split In Batches', type: 'n8n-nodes-base.splitInBatches', position: [0, 0], parameters: {} }
],
connections: {
'Split In Batches': {
main: [
[],
[{ node: 'Split In Batches', type: 'main', index: 0 }] // Self-reference on loop output
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should not warn about self-reference for SplitInBatches
const selfReferenceWarnings = result.warnings.filter(w =>
w.message?.includes('self-referencing')
);
expect(selfReferenceWarnings).toHaveLength(0);
});
it('should warn about self-referencing for non-loop nodes', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.set',
properties: []
});
const workflow = {
name: 'Non-Loop Self Reference',
nodes: [
{ id: '1', name: 'Set', type: 'n8n-nodes-base.set', position: [0, 0], parameters: {} }
],
connections: {
'Set': {
main: [
[{ node: 'Set', type: 'main', index: 0 }] // Self-reference on regular node
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should warn about self-reference for non-loop nodes
const selfReferenceWarnings = result.warnings.filter(w =>
w.message?.includes('self-referencing')
);
expect(selfReferenceWarnings).toHaveLength(1);
});
});
describe('edge cases', () => {
it('should handle missing target node gracefully', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const workflow = {
name: 'Missing Target',
nodes: [
{ id: '1', name: 'Split In Batches', type: 'n8n-nodes-base.splitInBatches', position: [0, 0], parameters: {} }
],
connections: {
'Split In Batches': {
main: [
[],
[{ node: 'NonExistentNode', type: 'main', index: 0 }] // Target doesn't exist
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should have connection error for non-existent node
const connectionErrors = result.errors.filter(e =>
e.message?.includes('non-existent node')
);
expect(connectionErrors).toHaveLength(1);
});
it('should handle empty connections gracefully', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const workflow = {
name: 'Empty Connections',
nodes: [
{ id: '1', name: 'Split In Batches', type: 'n8n-nodes-base.splitInBatches', position: [0, 0], parameters: {} }
],
connections: {
'Split In Batches': {
main: [
[], // Empty done output
[] // Empty loop output
]
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should not crash and should not have SplitInBatches-specific errors
expect(result).toBeDefined();
});
it('should handle null/undefined connection arrays', async () => {
mockNodeRepository.getNode.mockReturnValue({
nodeType: 'nodes-base.splitInBatches',
properties: []
});
const workflow = {
name: 'Null Connections',
nodes: [
{ id: '1', name: 'Split In Batches', type: 'n8n-nodes-base.splitInBatches', position: [0, 0], parameters: {} }
],
connections: {
'Split In Batches': {
main: [
null, // Null done output
undefined // Undefined loop output
] as any
}
}
};
const result = await validator.validateWorkflow(workflow as any);
// Should handle gracefully without crashing
expect(result).toBeDefined();
});
});
});

View File

@@ -77,7 +77,7 @@ describe('WorkflowValidator - Simple Unit Tests', () => {
};
// Act
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
// Assert
expect(result.valid).toBe(true);
@@ -113,7 +113,7 @@ describe('WorkflowValidator - Simple Unit Tests', () => {
};
// Act
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
// Assert
expect(result.valid).toBe(false);
@@ -154,7 +154,7 @@ describe('WorkflowValidator - Simple Unit Tests', () => {
};
// Act
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
// Assert
expect(result.valid).toBe(false);
@@ -229,7 +229,7 @@ describe('WorkflowValidator - Simple Unit Tests', () => {
};
// Act
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
// Assert
expect(result.valid).toBe(true);
@@ -297,7 +297,7 @@ describe('WorkflowValidator - Simple Unit Tests', () => {
};
// Act
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
// Assert
expect(result.valid).toBe(false);
@@ -386,7 +386,7 @@ describe('WorkflowValidator - Simple Unit Tests', () => {
};
// Act
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
// Assert
expect(result.valid).toBe(false);
@@ -438,7 +438,7 @@ describe('WorkflowValidator - Simple Unit Tests', () => {
};
// Act
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
// Assert
expect(result.warnings.some(w => w.message.includes('Outdated typeVersion'))).toBe(true);
@@ -471,7 +471,7 @@ describe('WorkflowValidator - Simple Unit Tests', () => {
};
// Act
const result = await validator.validateWorkflow(workflow as any);
const result = await validator.validateWorkflow(workflow);
// Assert
expect(result.valid).toBe(false);

View File

@@ -1,410 +0,0 @@
/**
* Test suite for validation system fixes
* Covers issues #58, #68, #70, #73
*/
import { describe, test, expect, beforeAll, afterAll } from 'vitest';
import { WorkflowValidator } from '../../src/services/workflow-validator';
import { EnhancedConfigValidator } from '../../src/services/enhanced-config-validator';
import { ToolValidation, Validator, ValidationError } from '../../src/utils/validation-schemas';
describe('Validation System Fixes', () => {
let workflowValidator: WorkflowValidator;
let mockNodeRepository: any;
beforeAll(async () => {
// Initialize test environment
process.env.NODE_ENV = 'test';
// Mock repository for testing
mockNodeRepository = {
getNode: (nodeType: string) => {
if (nodeType === 'nodes-base.webhook' || nodeType === 'n8n-nodes-base.webhook') {
return {
nodeType: 'nodes-base.webhook',
displayName: 'Webhook',
properties: [
{ name: 'path', required: true, displayName: 'Path' },
{ name: 'httpMethod', required: true, displayName: 'HTTP Method' }
]
};
}
if (nodeType === 'nodes-base.set' || nodeType === 'n8n-nodes-base.set') {
return {
nodeType: 'nodes-base.set',
displayName: 'Set',
properties: [
{ name: 'values', required: false, displayName: 'Values' }
]
};
}
return null;
}
} as any;
workflowValidator = new WorkflowValidator(mockNodeRepository, EnhancedConfigValidator);
});
afterAll(() => {
delete process.env.NODE_ENV;
});
describe('Issue #73: validate_node_minimal crashes without input validation', () => {
test('should handle empty config in validation schemas', () => {
// Test the validation schema handles empty config
const result = ToolValidation.validateNodeMinimal({
nodeType: 'nodes-base.webhook',
config: undefined
});
expect(result).toBeDefined();
expect(result.valid).toBe(false);
expect(result.errors.length).toBeGreaterThan(0);
expect(result.errors[0].field).toBe('config');
});
test('should handle null config in validation schemas', () => {
const result = ToolValidation.validateNodeMinimal({
nodeType: 'nodes-base.webhook',
config: null
});
expect(result).toBeDefined();
expect(result.valid).toBe(false);
expect(result.errors.length).toBeGreaterThan(0);
expect(result.errors[0].field).toBe('config');
});
test('should accept valid config object', () => {
const result = ToolValidation.validateNodeMinimal({
nodeType: 'nodes-base.webhook',
config: { path: '/webhook', httpMethod: 'POST' }
});
expect(result).toBeDefined();
expect(result.valid).toBe(true);
expect(result.errors).toHaveLength(0);
});
});
describe('Issue #58: validate_node_operation crashes on nested input', () => {
test('should handle invalid nodeType gracefully', () => {
expect(() => {
EnhancedConfigValidator.validateWithMode(
undefined as any,
{ resource: 'channel', operation: 'create' },
[],
'operation',
'ai-friendly'
);
}).toThrow(Error);
});
test('should handle null nodeType gracefully', () => {
expect(() => {
EnhancedConfigValidator.validateWithMode(
null as any,
{ resource: 'channel', operation: 'create' },
[],
'operation',
'ai-friendly'
);
}).toThrow(Error);
});
test('should handle non-string nodeType gracefully', () => {
expect(() => {
EnhancedConfigValidator.validateWithMode(
{ type: 'nodes-base.slack' } as any,
{ resource: 'channel', operation: 'create' },
[],
'operation',
'ai-friendly'
);
}).toThrow(Error);
});
test('should handle valid nodeType properly', () => {
const result = EnhancedConfigValidator.validateWithMode(
'nodes-base.set',
{ values: {} },
[],
'operation',
'ai-friendly'
);
expect(result).toBeDefined();
expect(typeof result.valid).toBe('boolean');
});
});
describe('Issue #70: Profile settings not respected', () => {
test('should pass profile parameter to all validation phases', async () => {
const workflow = {
nodes: [
{
id: '1',
name: 'Webhook',
type: 'n8n-nodes-base.webhook',
position: [100, 200],
parameters: { path: '/test', httpMethod: 'POST' },
typeVersion: 1
},
{
id: '2',
name: 'Set',
type: 'n8n-nodes-base.set',
position: [300, 200],
parameters: { values: {} },
typeVersion: 1
}
],
connections: {
'Webhook': {
main: [[{ node: 'Set', type: 'main', index: 0 }]]
}
}
};
const result = await workflowValidator.validateWorkflow(workflow, {
validateNodes: true,
validateConnections: true,
validateExpressions: true,
profile: 'minimal'
});
expect(result).toBeDefined();
expect(result.valid).toBe(true);
// In minimal profile, should have fewer warnings/errors - just check it's reasonable
expect(result.warnings.length).toBeLessThanOrEqual(5);
});
test('should filter out sticky notes from validation', async () => {
const workflow = {
nodes: [
{
id: '1',
name: 'Webhook',
type: 'n8n-nodes-base.webhook',
position: [100, 200],
parameters: { path: '/test', httpMethod: 'POST' },
typeVersion: 1
},
{
id: '2',
name: 'Sticky Note',
type: 'n8n-nodes-base.stickyNote',
position: [300, 100],
parameters: { content: 'This is a note' },
typeVersion: 1
}
],
connections: {}
};
const result = await workflowValidator.validateWorkflow(workflow);
expect(result).toBeDefined();
expect(result.statistics.totalNodes).toBe(1); // Only webhook, sticky note excluded
expect(result.statistics.enabledNodes).toBe(1);
});
test('should allow legitimate loops in cycle detection', async () => {
const workflow = {
nodes: [
{
id: '1',
name: 'Manual Trigger',
type: 'n8n-nodes-base.manualTrigger',
position: [100, 200],
parameters: {},
typeVersion: 1
},
{
id: '2',
name: 'SplitInBatches',
type: 'n8n-nodes-base.splitInBatches',
position: [300, 200],
parameters: { batchSize: 1 },
typeVersion: 1
},
{
id: '3',
name: 'Set',
type: 'n8n-nodes-base.set',
position: [500, 200],
parameters: { values: {} },
typeVersion: 1
}
],
connections: {
'Manual Trigger': {
main: [[{ node: 'SplitInBatches', type: 'main', index: 0 }]]
},
'SplitInBatches': {
main: [
[{ node: 'Set', type: 'main', index: 0 }], // Done output
[{ node: 'Set', type: 'main', index: 0 }] // Loop output
]
},
'Set': {
main: [[{ node: 'SplitInBatches', type: 'main', index: 0 }]] // Loop back
}
}
};
const result = await workflowValidator.validateWorkflow(workflow);
expect(result).toBeDefined();
// Should not report cycle error for legitimate SplitInBatches loop
const cycleErrors = result.errors.filter(e => e.message.includes('cycle'));
expect(cycleErrors).toHaveLength(0);
});
});
describe('Issue #68: Better error recovery suggestions', () => {
test('should provide recovery suggestions for invalid node types', async () => {
const workflow = {
nodes: [
{
id: '1',
name: 'Invalid Node',
type: 'invalid-node-type',
position: [100, 200],
parameters: {},
typeVersion: 1
}
],
connections: {}
};
const result = await workflowValidator.validateWorkflow(workflow);
expect(result).toBeDefined();
expect(result.valid).toBe(false);
expect(result.suggestions.length).toBeGreaterThan(0);
// Should contain recovery suggestions
const recoveryStarted = result.suggestions.some(s => s.includes('🔧 RECOVERY'));
expect(recoveryStarted).toBe(true);
});
test('should provide recovery suggestions for connection errors', async () => {
const workflow = {
nodes: [
{
id: '1',
name: 'Webhook',
type: 'n8n-nodes-base.webhook',
position: [100, 200],
parameters: { path: '/test', httpMethod: 'POST' },
typeVersion: 1
}
],
connections: {
'Webhook': {
main: [[{ node: 'NonExistentNode', type: 'main', index: 0 }]]
}
}
};
const result = await workflowValidator.validateWorkflow(workflow);
expect(result).toBeDefined();
expect(result.valid).toBe(false);
expect(result.suggestions.length).toBeGreaterThan(0);
// Should contain connection recovery suggestions
const connectionRecovery = result.suggestions.some(s =>
s.includes('Connection errors detected') || s.includes('connection')
);
expect(connectionRecovery).toBe(true);
});
test('should provide workflow for multiple errors', async () => {
const workflow = {
nodes: [
{
id: '1',
name: 'Invalid Node 1',
type: 'invalid-type-1',
position: [100, 200],
parameters: {}
// Missing typeVersion
},
{
id: '2',
name: 'Invalid Node 2',
type: 'invalid-type-2',
position: [300, 200],
parameters: {}
// Missing typeVersion
},
{
id: '3',
name: 'Invalid Node 3',
type: 'invalid-type-3',
position: [500, 200],
parameters: {}
// Missing typeVersion
}
],
connections: {
'Invalid Node 1': {
main: [[{ node: 'NonExistent', type: 'main', index: 0 }]]
}
}
};
const result = await workflowValidator.validateWorkflow(workflow);
expect(result).toBeDefined();
expect(result.valid).toBe(false);
expect(result.errors.length).toBeGreaterThan(3);
// Should provide step-by-step recovery workflow
const workflowSuggestion = result.suggestions.some(s =>
s.includes('SUGGESTED WORKFLOW') && s.includes('Too many errors detected')
);
expect(workflowSuggestion).toBe(true);
});
});
describe('Enhanced Input Validation', () => {
test('should validate tool parameters with schemas', () => {
// Test validate_node_operation parameters
const validationResult = ToolValidation.validateNodeOperation({
nodeType: 'nodes-base.webhook',
config: { path: '/test' },
profile: 'ai-friendly'
});
expect(validationResult.valid).toBe(true);
expect(validationResult.errors).toHaveLength(0);
});
test('should reject invalid parameters', () => {
const validationResult = ToolValidation.validateNodeOperation({
nodeType: 123, // Invalid type
config: 'not an object', // Invalid type
profile: 'invalid-profile' // Invalid enum value
});
expect(validationResult.valid).toBe(false);
expect(validationResult.errors.length).toBeGreaterThan(0);
});
test('should format validation errors properly', () => {
const validationResult = ToolValidation.validateNodeOperation({
nodeType: null,
config: null
});
const errorMessage = Validator.formatErrors(validationResult, 'validate_node_operation');
expect(errorMessage).toContain('validate_node_operation: Validation failed:');
expect(errorMessage).toContain('nodeType');
expect(errorMessage).toContain('config');
});
});
});