mirror of
https://github.com/czlonkowski/n8n-mcp.git
synced 2026-02-06 13:33:11 +00:00
feat: add operation and resource validation with intelligent suggestions
- Added OperationSimilarityService for validating operations with "Did you mean...?" suggestions - Added ResourceSimilarityService for validating resources with plural/singular detection - Implements Levenshtein distance algorithm for typo detection - Pattern matching for common operation/resource mistakes - 5-minute cache with automatic cleanup to prevent memory leaks - Confidence scoring (30% minimum threshold) for suggestion quality - Resource-aware operation filtering for contextual suggestions - Safe JSON parsing with ValidationServiceError for proper error handling - Type guards for safe property access - Performance optimizations with early termination - Comprehensive test coverage (37 new tests) - Integration tested with n8n-mcp-tester agent Example use cases: - "listFiles" → suggests "search" for Google Drive - "files" → suggests singular "file" - "flie" → suggests "file" (typo correction) - "downlod" → suggests "download" 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
421
tests/unit/services/enhanced-config-validator-operations.test.ts
Normal file
421
tests/unit/services/enhanced-config-validator-operations.test.ts
Normal file
@@ -0,0 +1,421 @@
|
||||
/**
|
||||
* Tests for EnhancedConfigValidator operation and resource validation
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { EnhancedConfigValidator } from '../../../src/services/enhanced-config-validator';
|
||||
import { NodeRepository } from '../../../src/database/node-repository';
|
||||
import { createTestDatabase } from '../../utils/database-utils';
|
||||
|
||||
describe('EnhancedConfigValidator - Operation and Resource Validation', () => {
|
||||
let repository: NodeRepository;
|
||||
let testDb: any;
|
||||
|
||||
beforeEach(async () => {
|
||||
testDb = await createTestDatabase();
|
||||
repository = testDb.nodeRepository;
|
||||
|
||||
// Initialize similarity services
|
||||
EnhancedConfigValidator.initializeSimilarityServices(repository);
|
||||
|
||||
// Add Google Drive test node
|
||||
const googleDriveNode = {
|
||||
nodeType: 'nodes-base.googleDrive',
|
||||
packageName: 'n8n-nodes-base',
|
||||
displayName: 'Google Drive',
|
||||
description: 'Access Google Drive',
|
||||
category: 'transform',
|
||||
style: 'declarative',
|
||||
isAITool: false,
|
||||
isTrigger: false,
|
||||
isWebhook: false,
|
||||
isVersioned: true,
|
||||
version: 1,
|
||||
properties: [
|
||||
{
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
required: true,
|
||||
options: [
|
||||
{ value: 'file', name: 'File' },
|
||||
{ value: 'folder', name: 'Folder' },
|
||||
{ value: 'fileFolder', name: 'File & Folder' }
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['file']
|
||||
}
|
||||
},
|
||||
options: [
|
||||
{ value: 'copy', name: 'Copy' },
|
||||
{ value: 'delete', name: 'Delete' },
|
||||
{ value: 'download', name: 'Download' },
|
||||
{ value: 'list', name: 'List' },
|
||||
{ value: 'share', name: 'Share' },
|
||||
{ value: 'update', name: 'Update' },
|
||||
{ value: 'upload', name: 'Upload' }
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['folder']
|
||||
}
|
||||
},
|
||||
options: [
|
||||
{ value: 'create', name: 'Create' },
|
||||
{ value: 'delete', name: 'Delete' },
|
||||
{ value: 'share', name: 'Share' }
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['fileFolder']
|
||||
}
|
||||
},
|
||||
options: [
|
||||
{ value: 'search', name: 'Search' }
|
||||
]
|
||||
}
|
||||
],
|
||||
operations: [],
|
||||
credentials: []
|
||||
};
|
||||
|
||||
repository.saveNode(googleDriveNode);
|
||||
|
||||
// Add Slack test node
|
||||
const slackNode = {
|
||||
nodeType: 'nodes-base.slack',
|
||||
packageName: 'n8n-nodes-base',
|
||||
displayName: 'Slack',
|
||||
description: 'Send messages to Slack',
|
||||
category: 'communication',
|
||||
style: 'declarative',
|
||||
isAITool: false,
|
||||
isTrigger: false,
|
||||
isWebhook: false,
|
||||
isVersioned: true,
|
||||
version: 2,
|
||||
properties: [
|
||||
{
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
required: true,
|
||||
options: [
|
||||
{ value: 'channel', name: 'Channel' },
|
||||
{ value: 'message', name: 'Message' },
|
||||
{ value: 'user', name: 'User' }
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['message']
|
||||
}
|
||||
},
|
||||
options: [
|
||||
{ value: 'send', name: 'Send' },
|
||||
{ value: 'update', name: 'Update' },
|
||||
{ value: 'delete', name: 'Delete' }
|
||||
]
|
||||
}
|
||||
],
|
||||
operations: [],
|
||||
credentials: []
|
||||
};
|
||||
|
||||
repository.saveNode(slackNode);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
// Clean up database
|
||||
if (testDb) {
|
||||
await testDb.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
describe('Invalid Operations', () => {
|
||||
it('should detect invalid operation "listFiles" for Google Drive', () => {
|
||||
const config = {
|
||||
resource: 'fileFolder',
|
||||
operation: 'listFiles'
|
||||
};
|
||||
|
||||
const node = repository.getNode('nodes-base.googleDrive');
|
||||
const result = EnhancedConfigValidator.validateWithMode(
|
||||
'nodes-base.googleDrive',
|
||||
config,
|
||||
node.properties,
|
||||
'operation',
|
||||
'ai-friendly'
|
||||
);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
|
||||
// Should have an error for invalid operation
|
||||
const operationError = result.errors.find(e => e.property === 'operation');
|
||||
expect(operationError).toBeDefined();
|
||||
expect(operationError!.message).toContain('Invalid operation "listFiles"');
|
||||
expect(operationError!.message).toContain('Did you mean');
|
||||
expect(operationError!.fix).toContain('search'); // Should suggest 'search' for fileFolder resource
|
||||
});
|
||||
|
||||
it('should provide suggestions for typos in operations', () => {
|
||||
const config = {
|
||||
resource: 'file',
|
||||
operation: 'downlod' // Typo: missing 'a'
|
||||
};
|
||||
|
||||
const node = repository.getNode('nodes-base.googleDrive');
|
||||
const result = EnhancedConfigValidator.validateWithMode(
|
||||
'nodes-base.googleDrive',
|
||||
config,
|
||||
node.properties,
|
||||
'operation',
|
||||
'ai-friendly'
|
||||
);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
|
||||
const operationError = result.errors.find(e => e.property === 'operation');
|
||||
expect(operationError).toBeDefined();
|
||||
expect(operationError!.message).toContain('Did you mean "download"');
|
||||
});
|
||||
|
||||
it('should list valid operations for the resource', () => {
|
||||
const config = {
|
||||
resource: 'folder',
|
||||
operation: 'upload' // Invalid for folder resource
|
||||
};
|
||||
|
||||
const node = repository.getNode('nodes-base.googleDrive');
|
||||
const result = EnhancedConfigValidator.validateWithMode(
|
||||
'nodes-base.googleDrive',
|
||||
config,
|
||||
node.properties,
|
||||
'operation',
|
||||
'ai-friendly'
|
||||
);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
|
||||
const operationError = result.errors.find(e => e.property === 'operation');
|
||||
expect(operationError).toBeDefined();
|
||||
expect(operationError!.fix).toContain('Valid operations for resource "folder"');
|
||||
expect(operationError!.fix).toContain('create');
|
||||
expect(operationError!.fix).toContain('delete');
|
||||
expect(operationError!.fix).toContain('share');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Invalid Resources', () => {
|
||||
it('should detect plural resource "files" and suggest singular', () => {
|
||||
const config = {
|
||||
resource: 'files', // Should be 'file'
|
||||
operation: 'list'
|
||||
};
|
||||
|
||||
const node = repository.getNode('nodes-base.googleDrive');
|
||||
const result = EnhancedConfigValidator.validateWithMode(
|
||||
'nodes-base.googleDrive',
|
||||
config,
|
||||
node.properties,
|
||||
'operation',
|
||||
'ai-friendly'
|
||||
);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
|
||||
const resourceError = result.errors.find(e => e.property === 'resource');
|
||||
expect(resourceError).toBeDefined();
|
||||
expect(resourceError!.message).toContain('Invalid resource "files"');
|
||||
expect(resourceError!.message).toContain('Did you mean "file"');
|
||||
expect(resourceError!.fix).toContain('Use singular');
|
||||
});
|
||||
|
||||
it('should suggest similar resources for typos', () => {
|
||||
const config = {
|
||||
resource: 'flie', // Typo
|
||||
operation: 'download'
|
||||
};
|
||||
|
||||
const node = repository.getNode('nodes-base.googleDrive');
|
||||
const result = EnhancedConfigValidator.validateWithMode(
|
||||
'nodes-base.googleDrive',
|
||||
config,
|
||||
node.properties,
|
||||
'operation',
|
||||
'ai-friendly'
|
||||
);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
|
||||
const resourceError = result.errors.find(e => e.property === 'resource');
|
||||
expect(resourceError).toBeDefined();
|
||||
expect(resourceError!.message).toContain('Did you mean "file"');
|
||||
});
|
||||
|
||||
it('should list valid resources when no match found', () => {
|
||||
const config = {
|
||||
resource: 'document', // Not a valid resource
|
||||
operation: 'create'
|
||||
};
|
||||
|
||||
const node = repository.getNode('nodes-base.googleDrive');
|
||||
const result = EnhancedConfigValidator.validateWithMode(
|
||||
'nodes-base.googleDrive',
|
||||
config,
|
||||
node.properties,
|
||||
'operation',
|
||||
'ai-friendly'
|
||||
);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
|
||||
const resourceError = result.errors.find(e => e.property === 'resource');
|
||||
expect(resourceError).toBeDefined();
|
||||
expect(resourceError!.fix).toContain('Valid resources:');
|
||||
expect(resourceError!.fix).toContain('file');
|
||||
expect(resourceError!.fix).toContain('folder');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Combined Resource and Operation Validation', () => {
|
||||
it('should validate both resource and operation together', () => {
|
||||
const config = {
|
||||
resource: 'files', // Invalid: should be singular
|
||||
operation: 'listFiles' // Invalid: should be 'list' or 'search'
|
||||
};
|
||||
|
||||
const node = repository.getNode('nodes-base.googleDrive');
|
||||
const result = EnhancedConfigValidator.validateWithMode(
|
||||
'nodes-base.googleDrive',
|
||||
config,
|
||||
node.properties,
|
||||
'operation',
|
||||
'ai-friendly'
|
||||
);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.errors.length).toBeGreaterThanOrEqual(2);
|
||||
|
||||
// Should have error for resource
|
||||
const resourceError = result.errors.find(e => e.property === 'resource');
|
||||
expect(resourceError).toBeDefined();
|
||||
expect(resourceError!.message).toContain('files');
|
||||
|
||||
// Should have error for operation
|
||||
const operationError = result.errors.find(e => e.property === 'operation');
|
||||
expect(operationError).toBeDefined();
|
||||
expect(operationError!.message).toContain('listFiles');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Slack Node Validation', () => {
|
||||
it('should suggest "send" instead of "sendMessage"', () => {
|
||||
const config = {
|
||||
resource: 'message',
|
||||
operation: 'sendMessage' // Common mistake
|
||||
};
|
||||
|
||||
const node = repository.getNode('nodes-base.slack');
|
||||
const result = EnhancedConfigValidator.validateWithMode(
|
||||
'nodes-base.slack',
|
||||
config,
|
||||
node.properties,
|
||||
'operation',
|
||||
'ai-friendly'
|
||||
);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
|
||||
const operationError = result.errors.find(e => e.property === 'operation');
|
||||
expect(operationError).toBeDefined();
|
||||
expect(operationError!.message).toContain('Did you mean "send"');
|
||||
});
|
||||
|
||||
it('should suggest singular "channel" instead of "channels"', () => {
|
||||
const config = {
|
||||
resource: 'channels', // Should be singular
|
||||
operation: 'create'
|
||||
};
|
||||
|
||||
const node = repository.getNode('nodes-base.slack');
|
||||
const result = EnhancedConfigValidator.validateWithMode(
|
||||
'nodes-base.slack',
|
||||
config,
|
||||
node.properties,
|
||||
'operation',
|
||||
'ai-friendly'
|
||||
);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
|
||||
const resourceError = result.errors.find(e => e.property === 'resource');
|
||||
expect(resourceError).toBeDefined();
|
||||
expect(resourceError!.message).toContain('Did you mean "channel"');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Valid Configurations', () => {
|
||||
it('should accept valid Google Drive configuration', () => {
|
||||
const config = {
|
||||
resource: 'file',
|
||||
operation: 'download'
|
||||
};
|
||||
|
||||
const node = repository.getNode('nodes-base.googleDrive');
|
||||
const result = EnhancedConfigValidator.validateWithMode(
|
||||
'nodes-base.googleDrive',
|
||||
config,
|
||||
node.properties,
|
||||
'operation',
|
||||
'ai-friendly'
|
||||
);
|
||||
|
||||
// Should not have errors for resource or operation
|
||||
const resourceError = result.errors.find(e => e.property === 'resource');
|
||||
const operationError = result.errors.find(e => e.property === 'operation');
|
||||
expect(resourceError).toBeUndefined();
|
||||
expect(operationError).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should accept valid Slack configuration', () => {
|
||||
const config = {
|
||||
resource: 'message',
|
||||
operation: 'send'
|
||||
};
|
||||
|
||||
const node = repository.getNode('nodes-base.slack');
|
||||
const result = EnhancedConfigValidator.validateWithMode(
|
||||
'nodes-base.slack',
|
||||
config,
|
||||
node.properties,
|
||||
'operation',
|
||||
'ai-friendly'
|
||||
);
|
||||
|
||||
// Should not have errors for resource or operation
|
||||
const resourceError = result.errors.find(e => e.property === 'resource');
|
||||
const operationError = result.errors.find(e => e.property === 'operation');
|
||||
expect(resourceError).toBeUndefined();
|
||||
expect(operationError).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
234
tests/unit/services/operation-similarity-service.test.ts
Normal file
234
tests/unit/services/operation-similarity-service.test.ts
Normal file
@@ -0,0 +1,234 @@
|
||||
/**
|
||||
* Tests for OperationSimilarityService
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { OperationSimilarityService } from '../../../src/services/operation-similarity-service';
|
||||
import { NodeRepository } from '../../../src/database/node-repository';
|
||||
import { createTestDatabase } from '../../utils/database-utils';
|
||||
|
||||
describe('OperationSimilarityService', () => {
|
||||
let service: OperationSimilarityService;
|
||||
let repository: NodeRepository;
|
||||
let testDb: any;
|
||||
|
||||
beforeEach(async () => {
|
||||
testDb = await createTestDatabase();
|
||||
repository = testDb.nodeRepository;
|
||||
service = new OperationSimilarityService(repository);
|
||||
|
||||
// Add test node with operations
|
||||
const testNode = {
|
||||
nodeType: 'nodes-base.googleDrive',
|
||||
packageName: 'n8n-nodes-base',
|
||||
displayName: 'Google Drive',
|
||||
description: 'Access Google Drive',
|
||||
category: 'transform',
|
||||
style: 'declarative',
|
||||
isAITool: false,
|
||||
isTrigger: false,
|
||||
isWebhook: false,
|
||||
isVersioned: true,
|
||||
version: 1,
|
||||
properties: [
|
||||
{
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
options: [
|
||||
{ value: 'file', name: 'File' },
|
||||
{ value: 'folder', name: 'Folder' },
|
||||
{ value: 'drive', name: 'Shared Drive' },
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['file']
|
||||
}
|
||||
},
|
||||
options: [
|
||||
{ value: 'copy', name: 'Copy' },
|
||||
{ value: 'delete', name: 'Delete' },
|
||||
{ value: 'download', name: 'Download' },
|
||||
{ value: 'list', name: 'List' },
|
||||
{ value: 'share', name: 'Share' },
|
||||
{ value: 'update', name: 'Update' },
|
||||
{ value: 'upload', name: 'Upload' }
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['folder']
|
||||
}
|
||||
},
|
||||
options: [
|
||||
{ value: 'create', name: 'Create' },
|
||||
{ value: 'delete', name: 'Delete' },
|
||||
{ value: 'share', name: 'Share' }
|
||||
]
|
||||
}
|
||||
],
|
||||
operations: [],
|
||||
credentials: []
|
||||
};
|
||||
|
||||
repository.saveNode(testNode);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
if (testDb) {
|
||||
await testDb.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
describe('findSimilarOperations', () => {
|
||||
it('should find exact match', () => {
|
||||
const suggestions = service.findSimilarOperations(
|
||||
'nodes-base.googleDrive',
|
||||
'download',
|
||||
'file'
|
||||
);
|
||||
|
||||
expect(suggestions).toHaveLength(0); // No suggestions for valid operation
|
||||
});
|
||||
|
||||
it('should suggest similar operations for typos', () => {
|
||||
const suggestions = service.findSimilarOperations(
|
||||
'nodes-base.googleDrive',
|
||||
'downlod',
|
||||
'file'
|
||||
);
|
||||
|
||||
expect(suggestions.length).toBeGreaterThan(0);
|
||||
expect(suggestions[0].value).toBe('download');
|
||||
expect(suggestions[0].confidence).toBeGreaterThan(0.8);
|
||||
});
|
||||
|
||||
it('should handle common mistakes with patterns', () => {
|
||||
const suggestions = service.findSimilarOperations(
|
||||
'nodes-base.googleDrive',
|
||||
'uploadFile',
|
||||
'file'
|
||||
);
|
||||
|
||||
expect(suggestions.length).toBeGreaterThan(0);
|
||||
expect(suggestions[0].value).toBe('upload');
|
||||
expect(suggestions[0].reason).toContain('instead of');
|
||||
});
|
||||
|
||||
it('should filter operations by resource', () => {
|
||||
const suggestions = service.findSimilarOperations(
|
||||
'nodes-base.googleDrive',
|
||||
'upload',
|
||||
'folder'
|
||||
);
|
||||
|
||||
// Upload is not valid for folder resource
|
||||
expect(suggestions).toBeDefined();
|
||||
expect(suggestions.find(s => s.value === 'upload')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return empty array for node not found', () => {
|
||||
const suggestions = service.findSimilarOperations(
|
||||
'nodes-base.nonexistent',
|
||||
'operation',
|
||||
undefined
|
||||
);
|
||||
|
||||
expect(suggestions).toEqual([]);
|
||||
});
|
||||
|
||||
it('should handle operations without resource filtering', () => {
|
||||
const suggestions = service.findSimilarOperations(
|
||||
'nodes-base.googleDrive',
|
||||
'updat', // Missing 'e' at the end
|
||||
undefined
|
||||
);
|
||||
|
||||
expect(suggestions.length).toBeGreaterThan(0);
|
||||
expect(suggestions[0].value).toBe('update');
|
||||
});
|
||||
});
|
||||
|
||||
describe('similarity calculation', () => {
|
||||
it('should rank exact matches highest', () => {
|
||||
const suggestions = service.findSimilarOperations(
|
||||
'nodes-base.googleDrive',
|
||||
'delete',
|
||||
'file'
|
||||
);
|
||||
|
||||
expect(suggestions).toHaveLength(0); // Exact match, no suggestions needed
|
||||
});
|
||||
|
||||
it('should rank substring matches high', () => {
|
||||
const suggestions = service.findSimilarOperations(
|
||||
'nodes-base.googleDrive',
|
||||
'del',
|
||||
'file'
|
||||
);
|
||||
|
||||
expect(suggestions.length).toBeGreaterThan(0);
|
||||
const deleteSuggestion = suggestions.find(s => s.value === 'delete');
|
||||
expect(deleteSuggestion).toBeDefined();
|
||||
expect(deleteSuggestion!.confidence).toBeGreaterThanOrEqual(0.7);
|
||||
});
|
||||
|
||||
it('should detect common variations', () => {
|
||||
const suggestions = service.findSimilarOperations(
|
||||
'nodes-base.googleDrive',
|
||||
'getData',
|
||||
'file'
|
||||
);
|
||||
|
||||
expect(suggestions.length).toBeGreaterThan(0);
|
||||
// Should suggest 'download' or similar
|
||||
});
|
||||
});
|
||||
|
||||
describe('caching', () => {
|
||||
it('should cache results for repeated queries', () => {
|
||||
// First call
|
||||
const suggestions1 = service.findSimilarOperations(
|
||||
'nodes-base.googleDrive',
|
||||
'downlod',
|
||||
'file'
|
||||
);
|
||||
|
||||
// Second call with same params
|
||||
const suggestions2 = service.findSimilarOperations(
|
||||
'nodes-base.googleDrive',
|
||||
'downlod',
|
||||
'file'
|
||||
);
|
||||
|
||||
expect(suggestions1).toEqual(suggestions2);
|
||||
});
|
||||
|
||||
it('should clear cache when requested', () => {
|
||||
// Add to cache
|
||||
service.findSimilarOperations(
|
||||
'nodes-base.googleDrive',
|
||||
'test',
|
||||
'file'
|
||||
);
|
||||
|
||||
// Clear cache
|
||||
service.clearCache();
|
||||
|
||||
// This would fetch fresh data (behavior is the same, just uncached)
|
||||
const suggestions = service.findSimilarOperations(
|
||||
'nodes-base.googleDrive',
|
||||
'test',
|
||||
'file'
|
||||
);
|
||||
|
||||
expect(suggestions).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
288
tests/unit/services/resource-similarity-service.test.ts
Normal file
288
tests/unit/services/resource-similarity-service.test.ts
Normal file
@@ -0,0 +1,288 @@
|
||||
/**
|
||||
* Tests for ResourceSimilarityService
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { ResourceSimilarityService } from '../../../src/services/resource-similarity-service';
|
||||
import { NodeRepository } from '../../../src/database/node-repository';
|
||||
import { createTestDatabase } from '../../utils/database-utils';
|
||||
|
||||
describe('ResourceSimilarityService', () => {
|
||||
let service: ResourceSimilarityService;
|
||||
let repository: NodeRepository;
|
||||
let testDb: any;
|
||||
|
||||
beforeEach(async () => {
|
||||
testDb = await createTestDatabase();
|
||||
repository = testDb.nodeRepository;
|
||||
service = new ResourceSimilarityService(repository);
|
||||
|
||||
// Add test node with resources
|
||||
const testNode = {
|
||||
nodeType: 'nodes-base.googleDrive',
|
||||
packageName: 'n8n-nodes-base',
|
||||
displayName: 'Google Drive',
|
||||
description: 'Access Google Drive',
|
||||
category: 'transform',
|
||||
style: 'declarative',
|
||||
isAITool: false,
|
||||
isTrigger: false,
|
||||
isWebhook: false,
|
||||
isVersioned: true,
|
||||
version: 1,
|
||||
properties: [
|
||||
{
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
options: [
|
||||
{ value: 'file', name: 'File' },
|
||||
{ value: 'folder', name: 'Folder' },
|
||||
{ value: 'drive', name: 'Shared Drive' },
|
||||
{ value: 'fileFolder', name: 'File & Folder' }
|
||||
]
|
||||
}
|
||||
],
|
||||
operations: [],
|
||||
credentials: []
|
||||
};
|
||||
|
||||
repository.saveNode(testNode);
|
||||
|
||||
// Add Slack node for testing different patterns
|
||||
const slackNode = {
|
||||
nodeType: 'nodes-base.slack',
|
||||
packageName: 'n8n-nodes-base',
|
||||
displayName: 'Slack',
|
||||
description: 'Send messages to Slack',
|
||||
category: 'communication',
|
||||
style: 'declarative',
|
||||
isAITool: false,
|
||||
isTrigger: false,
|
||||
isWebhook: false,
|
||||
isVersioned: true,
|
||||
version: 2,
|
||||
properties: [
|
||||
{
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
options: [
|
||||
{ value: 'channel', name: 'Channel' },
|
||||
{ value: 'message', name: 'Message' },
|
||||
{ value: 'user', name: 'User' },
|
||||
{ value: 'file', name: 'File' },
|
||||
{ value: 'star', name: 'Star' }
|
||||
]
|
||||
}
|
||||
],
|
||||
operations: [],
|
||||
credentials: []
|
||||
};
|
||||
|
||||
repository.saveNode(slackNode);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
if (testDb) {
|
||||
await testDb.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
describe('findSimilarResources', () => {
|
||||
it('should find exact match', () => {
|
||||
const suggestions = service.findSimilarResources(
|
||||
'nodes-base.googleDrive',
|
||||
'file',
|
||||
5
|
||||
);
|
||||
|
||||
expect(suggestions).toHaveLength(0); // No suggestions for valid resource
|
||||
});
|
||||
|
||||
it('should suggest singular form for plural input', () => {
|
||||
const suggestions = service.findSimilarResources(
|
||||
'nodes-base.googleDrive',
|
||||
'files',
|
||||
5
|
||||
);
|
||||
|
||||
expect(suggestions.length).toBeGreaterThan(0);
|
||||
expect(suggestions[0].value).toBe('file');
|
||||
expect(suggestions[0].confidence).toBeGreaterThanOrEqual(0.9);
|
||||
expect(suggestions[0].reason).toContain('singular');
|
||||
});
|
||||
|
||||
it('should suggest singular form for folders', () => {
|
||||
const suggestions = service.findSimilarResources(
|
||||
'nodes-base.googleDrive',
|
||||
'folders',
|
||||
5
|
||||
);
|
||||
|
||||
expect(suggestions.length).toBeGreaterThan(0);
|
||||
expect(suggestions[0].value).toBe('folder');
|
||||
expect(suggestions[0].confidence).toBeGreaterThanOrEqual(0.9);
|
||||
});
|
||||
|
||||
it('should handle typos with Levenshtein distance', () => {
|
||||
const suggestions = service.findSimilarResources(
|
||||
'nodes-base.googleDrive',
|
||||
'flie',
|
||||
5
|
||||
);
|
||||
|
||||
expect(suggestions.length).toBeGreaterThan(0);
|
||||
expect(suggestions[0].value).toBe('file');
|
||||
expect(suggestions[0].confidence).toBeGreaterThan(0.7);
|
||||
});
|
||||
|
||||
it('should handle combined resources', () => {
|
||||
const suggestions = service.findSimilarResources(
|
||||
'nodes-base.googleDrive',
|
||||
'fileAndFolder',
|
||||
5
|
||||
);
|
||||
|
||||
expect(suggestions.length).toBeGreaterThan(0);
|
||||
// Should suggest 'fileFolder' (the actual combined resource)
|
||||
const fileFolderSuggestion = suggestions.find(s => s.value === 'fileFolder');
|
||||
expect(fileFolderSuggestion).toBeDefined();
|
||||
});
|
||||
|
||||
it('should return empty array for node not found', () => {
|
||||
const suggestions = service.findSimilarResources(
|
||||
'nodes-base.nonexistent',
|
||||
'resource',
|
||||
5
|
||||
);
|
||||
|
||||
expect(suggestions).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('plural/singular detection', () => {
|
||||
it('should handle regular plurals (s)', () => {
|
||||
const suggestions = service.findSimilarResources(
|
||||
'nodes-base.slack',
|
||||
'channels',
|
||||
5
|
||||
);
|
||||
|
||||
expect(suggestions.length).toBeGreaterThan(0);
|
||||
expect(suggestions[0].value).toBe('channel');
|
||||
});
|
||||
|
||||
it('should handle plural ending in es', () => {
|
||||
const suggestions = service.findSimilarResources(
|
||||
'nodes-base.slack',
|
||||
'messages',
|
||||
5
|
||||
);
|
||||
|
||||
expect(suggestions.length).toBeGreaterThan(0);
|
||||
expect(suggestions[0].value).toBe('message');
|
||||
});
|
||||
|
||||
it('should handle plural ending in ies', () => {
|
||||
// Test with a hypothetical 'entities' -> 'entity' conversion
|
||||
const suggestions = service.findSimilarResources(
|
||||
'nodes-base.googleDrive',
|
||||
'entities',
|
||||
5
|
||||
);
|
||||
|
||||
// Should not crash and provide some suggestions
|
||||
expect(suggestions).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('node-specific patterns', () => {
|
||||
it('should apply Google Drive specific patterns', () => {
|
||||
const suggestions = service.findSimilarResources(
|
||||
'nodes-base.googleDrive',
|
||||
'sharedDrives',
|
||||
5
|
||||
);
|
||||
|
||||
expect(suggestions.length).toBeGreaterThan(0);
|
||||
const driveSuggestion = suggestions.find(s => s.value === 'drive');
|
||||
expect(driveSuggestion).toBeDefined();
|
||||
});
|
||||
|
||||
it('should apply Slack specific patterns', () => {
|
||||
const suggestions = service.findSimilarResources(
|
||||
'nodes-base.slack',
|
||||
'users',
|
||||
5
|
||||
);
|
||||
|
||||
expect(suggestions.length).toBeGreaterThan(0);
|
||||
expect(suggestions[0].value).toBe('user');
|
||||
});
|
||||
});
|
||||
|
||||
describe('similarity calculation', () => {
|
||||
it('should rank exact matches highest', () => {
|
||||
const suggestions = service.findSimilarResources(
|
||||
'nodes-base.googleDrive',
|
||||
'file',
|
||||
5
|
||||
);
|
||||
|
||||
expect(suggestions).toHaveLength(0); // Exact match, no suggestions
|
||||
});
|
||||
|
||||
it('should rank substring matches high', () => {
|
||||
const suggestions = service.findSimilarResources(
|
||||
'nodes-base.googleDrive',
|
||||
'fil',
|
||||
5
|
||||
);
|
||||
|
||||
expect(suggestions.length).toBeGreaterThan(0);
|
||||
const fileSuggestion = suggestions.find(s => s.value === 'file');
|
||||
expect(fileSuggestion).toBeDefined();
|
||||
expect(fileSuggestion!.confidence).toBeGreaterThanOrEqual(0.7);
|
||||
});
|
||||
});
|
||||
|
||||
describe('caching', () => {
|
||||
it('should cache results for repeated queries', () => {
|
||||
// First call
|
||||
const suggestions1 = service.findSimilarResources(
|
||||
'nodes-base.googleDrive',
|
||||
'files',
|
||||
5
|
||||
);
|
||||
|
||||
// Second call with same params
|
||||
const suggestions2 = service.findSimilarResources(
|
||||
'nodes-base.googleDrive',
|
||||
'files',
|
||||
5
|
||||
);
|
||||
|
||||
expect(suggestions1).toEqual(suggestions2);
|
||||
});
|
||||
|
||||
it('should clear cache when requested', () => {
|
||||
// Add to cache
|
||||
service.findSimilarResources(
|
||||
'nodes-base.googleDrive',
|
||||
'test',
|
||||
5
|
||||
);
|
||||
|
||||
// Clear cache
|
||||
service.clearCache();
|
||||
|
||||
// This would fetch fresh data (behavior is the same, just uncached)
|
||||
const suggestions = service.findSimilarResources(
|
||||
'nodes-base.googleDrive',
|
||||
'test',
|
||||
5
|
||||
);
|
||||
|
||||
expect(suggestions).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user