feat: add comprehensive performance benchmark tracking system
- Create benchmark test suites for critical operations: - Node loading performance - Database query performance - Search operations performance - Validation performance - MCP tool execution performance - Add GitHub Actions workflow for benchmark tracking: - Runs on push to main and PRs - Uses github-action-benchmark for historical tracking - Comments on PRs with performance results - Alerts on >10% performance regressions - Stores results in GitHub Pages - Create benchmark infrastructure: - Custom Vitest benchmark configuration - JSON reporter for CI results - Result formatter for github-action-benchmark - Performance threshold documentation - Add supporting utilities: - SQLiteStorageService for benchmark database setup - MCPEngine wrapper for testing MCP tools - Test factories for generating benchmark data - Enhanced NodeRepository with benchmark methods - Document benchmark system: - Comprehensive benchmark guide in docs/BENCHMARKS.md - Performance thresholds in .github/BENCHMARK_THRESHOLDS.md - README for benchmarks directory - Integration with existing test suite The benchmark system will help monitor performance over time and catch regressions before they reach production. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
153
tests/unit/__mocks__/README.md
Normal file
153
tests/unit/__mocks__/README.md
Normal file
@@ -0,0 +1,153 @@
|
||||
# n8n-nodes-base Mock
|
||||
|
||||
This directory contains comprehensive mocks for n8n packages used in unit tests.
|
||||
|
||||
## n8n-nodes-base Mock
|
||||
|
||||
The `n8n-nodes-base.ts` mock provides a complete testing infrastructure for code that depends on n8n nodes.
|
||||
|
||||
### Features
|
||||
|
||||
1. **Pre-configured Node Types**
|
||||
- `webhook` - Trigger node with webhook functionality
|
||||
- `httpRequest` - HTTP request node with mock responses
|
||||
- `slack` - Slack integration with all resources and operations
|
||||
- `function` - JavaScript code execution node
|
||||
- `noOp` - Pass-through utility node
|
||||
- `merge` - Data stream merging node
|
||||
- `if` - Conditional branching node
|
||||
- `switch` - Multi-output routing node
|
||||
|
||||
2. **Flexible Mock Behavior**
|
||||
- Override node execution logic
|
||||
- Customize node descriptions
|
||||
- Add custom nodes dynamically
|
||||
- Reset all mocks between tests
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```typescript
|
||||
import { vi } from 'vitest';
|
||||
|
||||
// Mock the module
|
||||
vi.mock('n8n-nodes-base', () => import('../__mocks__/n8n-nodes-base'));
|
||||
|
||||
// In your test
|
||||
import { getNodeTypes, mockNodeBehavior, resetAllMocks } from '../__mocks__/n8n-nodes-base';
|
||||
|
||||
describe('Your test', () => {
|
||||
beforeEach(() => {
|
||||
resetAllMocks();
|
||||
});
|
||||
|
||||
it('should get node description', () => {
|
||||
const registry = getNodeTypes();
|
||||
const slackNode = registry.getByName('slack');
|
||||
|
||||
expect(slackNode?.description.name).toBe('slack');
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
### Advanced Usage
|
||||
|
||||
#### Override Node Behavior
|
||||
|
||||
```typescript
|
||||
mockNodeBehavior('httpRequest', {
|
||||
execute: async function(this: IExecuteFunctions) {
|
||||
return [[{ json: { custom: 'response' } }]];
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
#### Add Custom Nodes
|
||||
|
||||
```typescript
|
||||
import { registerMockNode } from '../__mocks__/n8n-nodes-base';
|
||||
|
||||
const customNode = {
|
||||
description: {
|
||||
displayName: 'Custom Node',
|
||||
name: 'customNode',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
description: 'A custom test node',
|
||||
defaults: { name: 'Custom' },
|
||||
inputs: ['main'],
|
||||
outputs: ['main'],
|
||||
properties: []
|
||||
},
|
||||
execute: async function() {
|
||||
return [[{ json: { result: 'custom' } }]];
|
||||
}
|
||||
};
|
||||
|
||||
registerMockNode('customNode', customNode);
|
||||
```
|
||||
|
||||
#### Mock Execution Context
|
||||
|
||||
```typescript
|
||||
const mockContext = {
|
||||
getInputData: vi.fn(() => [{ json: { test: 'data' } }]),
|
||||
getNodeParameter: vi.fn((name: string) => {
|
||||
const params = {
|
||||
method: 'POST',
|
||||
url: 'https://api.example.com'
|
||||
};
|
||||
return params[name];
|
||||
}),
|
||||
getCredentials: vi.fn(async () => ({ apiKey: 'test-key' })),
|
||||
helpers: {
|
||||
returnJsonArray: vi.fn(),
|
||||
httpRequest: vi.fn()
|
||||
}
|
||||
};
|
||||
|
||||
const result = await node.execute.call(mockContext);
|
||||
```
|
||||
|
||||
### Mock Structure
|
||||
|
||||
Each mock node implements the `INodeType` interface with:
|
||||
|
||||
- `description`: Complete node metadata including properties, inputs/outputs, credentials
|
||||
- `execute`: Mock implementation for regular nodes (returns `INodeExecutionData[][]`)
|
||||
- `webhook`: Mock implementation for trigger nodes (returns webhook data)
|
||||
|
||||
### Testing Patterns
|
||||
|
||||
1. **Unit Testing Node Logic**
|
||||
```typescript
|
||||
const node = registry.getByName('slack');
|
||||
const result = await node.execute.call(mockContext);
|
||||
expect(result[0][0].json.ok).toBe(true);
|
||||
```
|
||||
|
||||
2. **Testing Node Properties**
|
||||
```typescript
|
||||
const node = registry.getByName('httpRequest');
|
||||
const methodProp = node.description.properties.find(p => p.name === 'method');
|
||||
expect(methodProp.options).toHaveLength(6);
|
||||
```
|
||||
|
||||
3. **Testing Conditional Nodes**
|
||||
```typescript
|
||||
const ifNode = registry.getByName('if');
|
||||
const [trueOutput, falseOutput] = await ifNode.execute.call(mockContext);
|
||||
expect(trueOutput).toHaveLength(2);
|
||||
expect(falseOutput).toHaveLength(1);
|
||||
```
|
||||
|
||||
### Utilities
|
||||
|
||||
- `resetAllMocks()` - Clear all mock function calls
|
||||
- `mockNodeBehavior(name, overrides)` - Override specific node behavior
|
||||
- `registerMockNode(name, node)` - Add new mock nodes
|
||||
- `getNodeTypes()` - Get the node registry with `getByName` and `getByNameAndVersion`
|
||||
|
||||
### See Also
|
||||
|
||||
- `tests/unit/examples/using-n8n-nodes-base-mock.test.ts` - Complete usage examples
|
||||
- `tests/unit/__mocks__/n8n-nodes-base.test.ts` - Mock test coverage
|
||||
224
tests/unit/__mocks__/n8n-nodes-base.test.ts
Normal file
224
tests/unit/__mocks__/n8n-nodes-base.test.ts
Normal file
@@ -0,0 +1,224 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { getNodeTypes, mockNodeBehavior, resetAllMocks, registerMockNode } from './n8n-nodes-base';
|
||||
|
||||
describe('n8n-nodes-base mock', () => {
|
||||
beforeEach(() => {
|
||||
resetAllMocks();
|
||||
});
|
||||
|
||||
describe('getNodeTypes', () => {
|
||||
it('should return node types registry', () => {
|
||||
const registry = getNodeTypes();
|
||||
expect(registry).toBeDefined();
|
||||
expect(registry.getByName).toBeDefined();
|
||||
expect(registry.getByNameAndVersion).toBeDefined();
|
||||
});
|
||||
|
||||
it('should retrieve webhook node', () => {
|
||||
const registry = getNodeTypes();
|
||||
const webhookNode = registry.getByName('webhook');
|
||||
|
||||
expect(webhookNode).toBeDefined();
|
||||
expect(webhookNode?.description.name).toBe('webhook');
|
||||
expect(webhookNode?.description.group).toContain('trigger');
|
||||
expect(webhookNode?.webhook).toBeDefined();
|
||||
});
|
||||
|
||||
it('should retrieve httpRequest node', () => {
|
||||
const registry = getNodeTypes();
|
||||
const httpNode = registry.getByName('httpRequest');
|
||||
|
||||
expect(httpNode).toBeDefined();
|
||||
expect(httpNode?.description.name).toBe('httpRequest');
|
||||
expect(httpNode?.description.version).toBe(3);
|
||||
expect(httpNode?.execute).toBeDefined();
|
||||
});
|
||||
|
||||
it('should retrieve slack node', () => {
|
||||
const registry = getNodeTypes();
|
||||
const slackNode = registry.getByName('slack');
|
||||
|
||||
expect(slackNode).toBeDefined();
|
||||
expect(slackNode?.description.credentials).toHaveLength(1);
|
||||
expect(slackNode?.description.credentials?.[0].name).toBe('slackApi');
|
||||
});
|
||||
});
|
||||
|
||||
describe('node execution', () => {
|
||||
it('should execute webhook node', async () => {
|
||||
const registry = getNodeTypes();
|
||||
const webhookNode = registry.getByName('webhook');
|
||||
|
||||
const mockContext = {
|
||||
getWebhookName: vi.fn(() => 'default'),
|
||||
getBodyData: vi.fn(() => ({ test: 'data' })),
|
||||
getHeaderData: vi.fn(() => ({ 'content-type': 'application/json' })),
|
||||
getQueryData: vi.fn(() => ({ query: 'param' })),
|
||||
getRequestObject: vi.fn(),
|
||||
getResponseObject: vi.fn(),
|
||||
helpers: {
|
||||
returnJsonArray: vi.fn((data) => [{ json: data }]),
|
||||
},
|
||||
};
|
||||
|
||||
const result = await webhookNode?.webhook?.call(mockContext);
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result.workflowData).toBeDefined();
|
||||
expect(result.workflowData[0]).toHaveLength(1);
|
||||
expect(result.workflowData[0][0].json).toMatchObject({
|
||||
headers: { 'content-type': 'application/json' },
|
||||
params: { query: 'param' },
|
||||
body: { test: 'data' },
|
||||
});
|
||||
});
|
||||
|
||||
it('should execute httpRequest node', async () => {
|
||||
const registry = getNodeTypes();
|
||||
const httpNode = registry.getByName('httpRequest');
|
||||
|
||||
const mockContext = {
|
||||
getInputData: vi.fn(() => [{ json: { test: 'input' } }]),
|
||||
getNodeParameter: vi.fn((name: string) => {
|
||||
if (name === 'method') return 'POST';
|
||||
if (name === 'url') return 'https://api.example.com';
|
||||
return '';
|
||||
}),
|
||||
helpers: {
|
||||
returnJsonArray: vi.fn((data) => [{ json: data }]),
|
||||
httpRequest: vi.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
const result = await httpNode?.execute?.call(mockContext);
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]).toHaveLength(1);
|
||||
expect(result[0][0].json).toMatchObject({
|
||||
statusCode: 200,
|
||||
body: {
|
||||
success: true,
|
||||
method: 'POST',
|
||||
url: 'https://api.example.com',
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('mockNodeBehavior', () => {
|
||||
it('should override node execution behavior', async () => {
|
||||
const customExecute = vi.fn(async function() {
|
||||
return [[{ json: { custom: 'response' } }]];
|
||||
});
|
||||
|
||||
mockNodeBehavior('httpRequest', {
|
||||
execute: customExecute,
|
||||
});
|
||||
|
||||
const registry = getNodeTypes();
|
||||
const httpNode = registry.getByName('httpRequest');
|
||||
|
||||
const mockContext = {
|
||||
getInputData: vi.fn(() => []),
|
||||
getNodeParameter: vi.fn(),
|
||||
};
|
||||
|
||||
const result = await httpNode?.execute?.call(mockContext);
|
||||
|
||||
expect(customExecute).toHaveBeenCalled();
|
||||
expect(result).toEqual([[{ json: { custom: 'response' } }]]);
|
||||
});
|
||||
|
||||
it('should override node description', () => {
|
||||
mockNodeBehavior('slack', {
|
||||
description: {
|
||||
displayName: 'Custom Slack',
|
||||
version: 3,
|
||||
},
|
||||
});
|
||||
|
||||
const registry = getNodeTypes();
|
||||
const slackNode = registry.getByName('slack');
|
||||
|
||||
expect(slackNode?.description.displayName).toBe('Custom Slack');
|
||||
expect(slackNode?.description.version).toBe(3);
|
||||
expect(slackNode?.description.name).toBe('slack'); // Original preserved
|
||||
});
|
||||
});
|
||||
|
||||
describe('registerMockNode', () => {
|
||||
it('should register custom node', () => {
|
||||
const customNode = {
|
||||
description: {
|
||||
displayName: 'Custom Node',
|
||||
name: 'customNode',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
description: 'A custom test node',
|
||||
defaults: { name: 'Custom' },
|
||||
inputs: ['main'],
|
||||
outputs: ['main'],
|
||||
properties: [],
|
||||
},
|
||||
execute: vi.fn(async function() {
|
||||
return [[{ json: { custom: true } }]];
|
||||
}),
|
||||
};
|
||||
|
||||
registerMockNode('customNode', customNode);
|
||||
|
||||
const registry = getNodeTypes();
|
||||
const retrievedNode = registry.getByName('customNode');
|
||||
|
||||
expect(retrievedNode).toBe(customNode);
|
||||
expect(retrievedNode?.description.name).toBe('customNode');
|
||||
});
|
||||
});
|
||||
|
||||
describe('conditional nodes', () => {
|
||||
it('should execute if node with two outputs', async () => {
|
||||
const registry = getNodeTypes();
|
||||
const ifNode = registry.getByName('if');
|
||||
|
||||
const mockContext = {
|
||||
getInputData: vi.fn(() => [
|
||||
{ json: { value: 1 } },
|
||||
{ json: { value: 2 } },
|
||||
{ json: { value: 3 } },
|
||||
{ json: { value: 4 } },
|
||||
]),
|
||||
getNodeParameter: vi.fn(),
|
||||
};
|
||||
|
||||
const result = await ifNode?.execute?.call(mockContext);
|
||||
|
||||
expect(result).toHaveLength(2); // true and false outputs
|
||||
expect(result[0]).toHaveLength(2); // even indices
|
||||
expect(result[1]).toHaveLength(2); // odd indices
|
||||
});
|
||||
|
||||
it('should execute switch node with multiple outputs', async () => {
|
||||
const registry = getNodeTypes();
|
||||
const switchNode = registry.getByName('switch');
|
||||
|
||||
const mockContext = {
|
||||
getInputData: vi.fn(() => [
|
||||
{ json: { value: 1 } },
|
||||
{ json: { value: 2 } },
|
||||
{ json: { value: 3 } },
|
||||
{ json: { value: 4 } },
|
||||
]),
|
||||
getNodeParameter: vi.fn(),
|
||||
};
|
||||
|
||||
const result = await switchNode?.execute?.call(mockContext);
|
||||
|
||||
expect(result).toHaveLength(4); // 4 outputs
|
||||
expect(result[0]).toHaveLength(1); // item 0
|
||||
expect(result[1]).toHaveLength(1); // item 1
|
||||
expect(result[2]).toHaveLength(1); // item 2
|
||||
expect(result[3]).toHaveLength(1); // item 3
|
||||
});
|
||||
});
|
||||
});
|
||||
655
tests/unit/__mocks__/n8n-nodes-base.ts
Normal file
655
tests/unit/__mocks__/n8n-nodes-base.ts
Normal file
@@ -0,0 +1,655 @@
|
||||
import { vi } from 'vitest';
|
||||
|
||||
// Mock types that match n8n-workflow
|
||||
interface INodeExecutionData {
|
||||
json: any;
|
||||
binary?: any;
|
||||
pairedItem?: any;
|
||||
}
|
||||
|
||||
interface IExecuteFunctions {
|
||||
getInputData(): INodeExecutionData[];
|
||||
getNodeParameter(parameterName: string, itemIndex: number, fallbackValue?: any): any;
|
||||
getCredentials(type: string): Promise<any>;
|
||||
helpers: {
|
||||
returnJsonArray(data: any): INodeExecutionData[];
|
||||
httpRequest(options: any): Promise<any>;
|
||||
webhook(): any;
|
||||
};
|
||||
}
|
||||
|
||||
interface IWebhookFunctions {
|
||||
getWebhookName(): string;
|
||||
getBodyData(): any;
|
||||
getHeaderData(): any;
|
||||
getQueryData(): any;
|
||||
getRequestObject(): any;
|
||||
getResponseObject(): any;
|
||||
helpers: {
|
||||
returnJsonArray(data: any): INodeExecutionData[];
|
||||
};
|
||||
}
|
||||
|
||||
interface INodeTypeDescription {
|
||||
displayName: string;
|
||||
name: string;
|
||||
group: string[];
|
||||
version: number;
|
||||
description: string;
|
||||
defaults: { name: string };
|
||||
inputs: string[];
|
||||
outputs: string[];
|
||||
credentials?: any[];
|
||||
webhooks?: any[];
|
||||
properties: any[];
|
||||
icon?: string;
|
||||
subtitle?: string;
|
||||
}
|
||||
|
||||
interface INodeType {
|
||||
description: INodeTypeDescription;
|
||||
execute?(this: IExecuteFunctions): Promise<INodeExecutionData[][]>;
|
||||
webhook?(this: IWebhookFunctions): Promise<any>;
|
||||
trigger?(this: any): Promise<void>;
|
||||
poll?(this: any): Promise<INodeExecutionData[][] | null>;
|
||||
}
|
||||
|
||||
// Base mock node implementation
|
||||
class BaseMockNode implements INodeType {
|
||||
description: INodeTypeDescription;
|
||||
execute: any;
|
||||
webhook: any;
|
||||
|
||||
constructor(description: INodeTypeDescription, execute?: any, webhook?: any) {
|
||||
this.description = description;
|
||||
this.execute = execute ? vi.fn(execute) : undefined;
|
||||
this.webhook = webhook ? vi.fn(webhook) : undefined;
|
||||
}
|
||||
}
|
||||
|
||||
// Mock implementations for each node type
|
||||
const mockWebhookNode = new BaseMockNode(
|
||||
{
|
||||
displayName: 'Webhook',
|
||||
name: 'webhook',
|
||||
group: ['trigger'],
|
||||
version: 1,
|
||||
description: 'Starts the workflow when a webhook is called',
|
||||
defaults: { name: 'Webhook' },
|
||||
inputs: [],
|
||||
outputs: ['main'],
|
||||
webhooks: [
|
||||
{
|
||||
name: 'default',
|
||||
httpMethod: '={{$parameter["httpMethod"]}}',
|
||||
path: '={{$parameter["path"]}}',
|
||||
responseMode: '={{$parameter["responseMode"]}}',
|
||||
}
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Path',
|
||||
name: 'path',
|
||||
type: 'string',
|
||||
default: 'webhook',
|
||||
required: true,
|
||||
description: 'The path to listen on',
|
||||
},
|
||||
{
|
||||
displayName: 'HTTP Method',
|
||||
name: 'httpMethod',
|
||||
type: 'options',
|
||||
default: 'GET',
|
||||
options: [
|
||||
{ name: 'GET', value: 'GET' },
|
||||
{ name: 'POST', value: 'POST' },
|
||||
{ name: 'PUT', value: 'PUT' },
|
||||
{ name: 'DELETE', value: 'DELETE' },
|
||||
{ name: 'HEAD', value: 'HEAD' },
|
||||
{ name: 'PATCH', value: 'PATCH' },
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Response Mode',
|
||||
name: 'responseMode',
|
||||
type: 'options',
|
||||
default: 'onReceived',
|
||||
options: [
|
||||
{ name: 'On Received', value: 'onReceived' },
|
||||
{ name: 'Last Node', value: 'lastNode' },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
undefined,
|
||||
async function webhook(this: IWebhookFunctions) {
|
||||
const returnData: INodeExecutionData[] = [];
|
||||
returnData.push({
|
||||
json: {
|
||||
headers: this.getHeaderData(),
|
||||
params: this.getQueryData(),
|
||||
body: this.getBodyData(),
|
||||
}
|
||||
});
|
||||
return {
|
||||
workflowData: [returnData],
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
const mockHttpRequestNode = new BaseMockNode(
|
||||
{
|
||||
displayName: 'HTTP Request',
|
||||
name: 'httpRequest',
|
||||
group: ['transform'],
|
||||
version: 3,
|
||||
description: 'Makes an HTTP request and returns the response',
|
||||
defaults: { name: 'HTTP Request' },
|
||||
inputs: ['main'],
|
||||
outputs: ['main'],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Method',
|
||||
name: 'method',
|
||||
type: 'options',
|
||||
default: 'GET',
|
||||
options: [
|
||||
{ name: 'GET', value: 'GET' },
|
||||
{ name: 'POST', value: 'POST' },
|
||||
{ name: 'PUT', value: 'PUT' },
|
||||
{ name: 'DELETE', value: 'DELETE' },
|
||||
{ name: 'HEAD', value: 'HEAD' },
|
||||
{ name: 'PATCH', value: 'PATCH' },
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'URL',
|
||||
name: 'url',
|
||||
type: 'string',
|
||||
default: '',
|
||||
required: true,
|
||||
placeholder: 'https://example.com',
|
||||
},
|
||||
{
|
||||
displayName: 'Authentication',
|
||||
name: 'authentication',
|
||||
type: 'options',
|
||||
default: 'none',
|
||||
options: [
|
||||
{ name: 'None', value: 'none' },
|
||||
{ name: 'Basic Auth', value: 'basicAuth' },
|
||||
{ name: 'Digest Auth', value: 'digestAuth' },
|
||||
{ name: 'Header Auth', value: 'headerAuth' },
|
||||
{ name: 'OAuth1', value: 'oAuth1' },
|
||||
{ name: 'OAuth2', value: 'oAuth2' },
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Response Format',
|
||||
name: 'responseFormat',
|
||||
type: 'options',
|
||||
default: 'json',
|
||||
options: [
|
||||
{ name: 'JSON', value: 'json' },
|
||||
{ name: 'String', value: 'string' },
|
||||
{ name: 'File', value: 'file' },
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Option',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Body Content Type',
|
||||
name: 'bodyContentType',
|
||||
type: 'options',
|
||||
default: 'json',
|
||||
options: [
|
||||
{ name: 'JSON', value: 'json' },
|
||||
{ name: 'Form Data', value: 'formData' },
|
||||
{ name: 'Form URL Encoded', value: 'form-urlencoded' },
|
||||
{ name: 'Raw', value: 'raw' },
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Headers',
|
||||
name: 'headers',
|
||||
type: 'fixedCollection',
|
||||
default: {},
|
||||
typeOptions: {
|
||||
multipleValues: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Query Parameters',
|
||||
name: 'queryParameters',
|
||||
type: 'fixedCollection',
|
||||
default: {},
|
||||
typeOptions: {
|
||||
multipleValues: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
async function execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const returnData: INodeExecutionData[] = [];
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const method = this.getNodeParameter('method', i) as string;
|
||||
const url = this.getNodeParameter('url', i) as string;
|
||||
|
||||
// Mock response
|
||||
const response = {
|
||||
statusCode: 200,
|
||||
headers: {},
|
||||
body: { success: true, method, url },
|
||||
};
|
||||
|
||||
returnData.push({
|
||||
json: response,
|
||||
});
|
||||
}
|
||||
|
||||
return [returnData];
|
||||
}
|
||||
);
|
||||
|
||||
const mockSlackNode = new BaseMockNode(
|
||||
{
|
||||
displayName: 'Slack',
|
||||
name: 'slack',
|
||||
group: ['output'],
|
||||
version: 2,
|
||||
description: 'Send messages to Slack',
|
||||
defaults: { name: 'Slack' },
|
||||
inputs: ['main'],
|
||||
outputs: ['main'],
|
||||
credentials: [
|
||||
{
|
||||
name: 'slackApi',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
default: 'message',
|
||||
options: [
|
||||
{ name: 'Channel', value: 'channel' },
|
||||
{ name: 'Message', value: 'message' },
|
||||
{ name: 'User', value: 'user' },
|
||||
{ name: 'File', value: 'file' },
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['message'],
|
||||
},
|
||||
},
|
||||
default: 'post',
|
||||
options: [
|
||||
{ name: 'Post', value: 'post' },
|
||||
{ name: 'Update', value: 'update' },
|
||||
{ name: 'Delete', value: 'delete' },
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Channel',
|
||||
name: 'channel',
|
||||
type: 'options',
|
||||
typeOptions: {
|
||||
loadOptionsMethod: 'getChannels',
|
||||
},
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['message'],
|
||||
operation: ['post'],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Text',
|
||||
name: 'text',
|
||||
type: 'string',
|
||||
typeOptions: {
|
||||
alwaysOpenEditWindow: true,
|
||||
},
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['message'],
|
||||
operation: ['post'],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
async function execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const returnData: INodeExecutionData[] = [];
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const resource = this.getNodeParameter('resource', i) as string;
|
||||
const operation = this.getNodeParameter('operation', i) as string;
|
||||
|
||||
// Mock response
|
||||
const response = {
|
||||
ok: true,
|
||||
channel: this.getNodeParameter('channel', i, '') as string,
|
||||
ts: Date.now().toString(),
|
||||
message: {
|
||||
text: this.getNodeParameter('text', i, '') as string,
|
||||
},
|
||||
};
|
||||
|
||||
returnData.push({
|
||||
json: response,
|
||||
});
|
||||
}
|
||||
|
||||
return [returnData];
|
||||
}
|
||||
);
|
||||
|
||||
const mockFunctionNode = new BaseMockNode(
|
||||
{
|
||||
displayName: 'Function',
|
||||
name: 'function',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
description: 'Execute custom JavaScript code',
|
||||
defaults: { name: 'Function' },
|
||||
inputs: ['main'],
|
||||
outputs: ['main'],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'JavaScript Code',
|
||||
name: 'functionCode',
|
||||
type: 'string',
|
||||
typeOptions: {
|
||||
alwaysOpenEditWindow: true,
|
||||
codeAutocomplete: 'function',
|
||||
editor: 'code',
|
||||
rows: 10,
|
||||
},
|
||||
default: 'return items;',
|
||||
description: 'JavaScript code to execute',
|
||||
},
|
||||
],
|
||||
},
|
||||
async function execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const functionCode = this.getNodeParameter('functionCode', 0) as string;
|
||||
|
||||
// Simple mock - just return items
|
||||
return [items];
|
||||
}
|
||||
);
|
||||
|
||||
const mockNoOpNode = new BaseMockNode(
|
||||
{
|
||||
displayName: 'No Operation',
|
||||
name: 'noOp',
|
||||
group: ['utility'],
|
||||
version: 1,
|
||||
description: 'Does nothing',
|
||||
defaults: { name: 'No Op' },
|
||||
inputs: ['main'],
|
||||
outputs: ['main'],
|
||||
properties: [],
|
||||
},
|
||||
async function execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
return [this.getInputData()];
|
||||
}
|
||||
);
|
||||
|
||||
const mockMergeNode = new BaseMockNode(
|
||||
{
|
||||
displayName: 'Merge',
|
||||
name: 'merge',
|
||||
group: ['transform'],
|
||||
version: 2,
|
||||
description: 'Merge multiple data streams',
|
||||
defaults: { name: 'Merge' },
|
||||
inputs: ['main', 'main'],
|
||||
outputs: ['main'],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Mode',
|
||||
name: 'mode',
|
||||
type: 'options',
|
||||
default: 'append',
|
||||
options: [
|
||||
{ name: 'Append', value: 'append' },
|
||||
{ name: 'Merge By Index', value: 'mergeByIndex' },
|
||||
{ name: 'Merge By Key', value: 'mergeByKey' },
|
||||
{ name: 'Multiplex', value: 'multiplex' },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
async function execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const mode = this.getNodeParameter('mode', 0) as string;
|
||||
|
||||
// Mock merge - just return first input
|
||||
return [this.getInputData(0)];
|
||||
}
|
||||
);
|
||||
|
||||
const mockIfNode = new BaseMockNode(
|
||||
{
|
||||
displayName: 'IF',
|
||||
name: 'if',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
description: 'Conditional logic',
|
||||
defaults: { name: 'IF' },
|
||||
inputs: ['main'],
|
||||
outputs: ['main', 'main'],
|
||||
outputNames: ['true', 'false'],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Conditions',
|
||||
name: 'conditions',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValues: true,
|
||||
},
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
name: 'string',
|
||||
displayName: 'String',
|
||||
values: [
|
||||
{
|
||||
displayName: 'Value 1',
|
||||
name: 'value1',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
default: 'equals',
|
||||
options: [
|
||||
{ name: 'Equals', value: 'equals' },
|
||||
{ name: 'Not Equals', value: 'notEquals' },
|
||||
{ name: 'Contains', value: 'contains' },
|
||||
{ name: 'Not Contains', value: 'notContains' },
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Value 2',
|
||||
name: 'value2',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
async function execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const trueItems: INodeExecutionData[] = [];
|
||||
const falseItems: INodeExecutionData[] = [];
|
||||
|
||||
// Mock condition - split 50/50
|
||||
items.forEach((item, index) => {
|
||||
if (index % 2 === 0) {
|
||||
trueItems.push(item);
|
||||
} else {
|
||||
falseItems.push(item);
|
||||
}
|
||||
});
|
||||
|
||||
return [trueItems, falseItems];
|
||||
}
|
||||
);
|
||||
|
||||
const mockSwitchNode = new BaseMockNode(
|
||||
{
|
||||
displayName: 'Switch',
|
||||
name: 'switch',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
description: 'Route items based on conditions',
|
||||
defaults: { name: 'Switch' },
|
||||
inputs: ['main'],
|
||||
outputs: ['main', 'main', 'main', 'main'],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Mode',
|
||||
name: 'mode',
|
||||
type: 'options',
|
||||
default: 'expression',
|
||||
options: [
|
||||
{ name: 'Expression', value: 'expression' },
|
||||
{ name: 'Rules', value: 'rules' },
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Output',
|
||||
name: 'output',
|
||||
type: 'options',
|
||||
displayOptions: {
|
||||
show: {
|
||||
mode: ['expression'],
|
||||
},
|
||||
},
|
||||
default: 'all',
|
||||
options: [
|
||||
{ name: 'All', value: 'all' },
|
||||
{ name: 'First Match', value: 'firstMatch' },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
async function execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
|
||||
// Mock routing - distribute evenly across outputs
|
||||
const outputs: INodeExecutionData[][] = [[], [], [], []];
|
||||
items.forEach((item, index) => {
|
||||
outputs[index % 4].push(item);
|
||||
});
|
||||
|
||||
return outputs;
|
||||
}
|
||||
);
|
||||
|
||||
// Node registry
|
||||
const nodeRegistry = new Map<string, INodeType>([
|
||||
['webhook', mockWebhookNode],
|
||||
['httpRequest', mockHttpRequestNode],
|
||||
['slack', mockSlackNode],
|
||||
['function', mockFunctionNode],
|
||||
['noOp', mockNoOpNode],
|
||||
['merge', mockMergeNode],
|
||||
['if', mockIfNode],
|
||||
['switch', mockSwitchNode],
|
||||
]);
|
||||
|
||||
// Export mock functions
|
||||
export const getNodeTypes = vi.fn(() => ({
|
||||
getByName: vi.fn((name: string) => nodeRegistry.get(name)),
|
||||
getByNameAndVersion: vi.fn((name: string, version: number) => nodeRegistry.get(name)),
|
||||
}));
|
||||
|
||||
// Export individual node classes for direct import
|
||||
export const Webhook = mockWebhookNode;
|
||||
export const HttpRequest = mockHttpRequestNode;
|
||||
export const Slack = mockSlackNode;
|
||||
export const Function = mockFunctionNode;
|
||||
export const NoOp = mockNoOpNode;
|
||||
export const Merge = mockMergeNode;
|
||||
export const If = mockIfNode;
|
||||
export const Switch = mockSwitchNode;
|
||||
|
||||
// Test utility to override node behavior
|
||||
export const mockNodeBehavior = (nodeName: string, overrides: Partial<INodeType>) => {
|
||||
const existingNode = nodeRegistry.get(nodeName);
|
||||
if (!existingNode) {
|
||||
throw new Error(`Node ${nodeName} not found in registry`);
|
||||
}
|
||||
|
||||
const updatedNode = new BaseMockNode(
|
||||
{ ...existingNode.description, ...overrides.description },
|
||||
overrides.execute || existingNode.execute,
|
||||
overrides.webhook || existingNode.webhook
|
||||
);
|
||||
|
||||
nodeRegistry.set(nodeName, updatedNode);
|
||||
return updatedNode;
|
||||
};
|
||||
|
||||
// Test utility to reset all mocks
|
||||
export const resetAllMocks = () => {
|
||||
getNodeTypes.mockClear();
|
||||
nodeRegistry.forEach((node) => {
|
||||
if (node.execute && vi.isMockFunction(node.execute)) {
|
||||
node.execute.mockClear();
|
||||
}
|
||||
if (node.webhook && vi.isMockFunction(node.webhook)) {
|
||||
node.webhook.mockClear();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Test utility to add custom nodes
|
||||
export const registerMockNode = (name: string, node: INodeType) => {
|
||||
nodeRegistry.set(name, node);
|
||||
};
|
||||
|
||||
// Export default for require() compatibility
|
||||
export default {
|
||||
getNodeTypes,
|
||||
Webhook,
|
||||
HttpRequest,
|
||||
Slack,
|
||||
Function,
|
||||
NoOp,
|
||||
Merge,
|
||||
If,
|
||||
Switch,
|
||||
mockNodeBehavior,
|
||||
resetAllMocks,
|
||||
registerMockNode,
|
||||
};
|
||||
227
tests/unit/examples/using-n8n-nodes-base-mock.test.ts
Normal file
227
tests/unit/examples/using-n8n-nodes-base-mock.test.ts
Normal file
@@ -0,0 +1,227 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { getNodeTypes, mockNodeBehavior, resetAllMocks } from '../__mocks__/n8n-nodes-base';
|
||||
|
||||
// Example service that uses n8n-nodes-base
|
||||
class WorkflowService {
|
||||
async getNodeDescription(nodeName: string) {
|
||||
const nodeTypes = getNodeTypes();
|
||||
const node = nodeTypes.getByName(nodeName);
|
||||
return node?.description;
|
||||
}
|
||||
|
||||
async executeNode(nodeName: string, context: any) {
|
||||
const nodeTypes = getNodeTypes();
|
||||
const node = nodeTypes.getByName(nodeName);
|
||||
|
||||
if (!node?.execute) {
|
||||
throw new Error(`Node ${nodeName} does not have an execute method`);
|
||||
}
|
||||
|
||||
return node.execute.call(context);
|
||||
}
|
||||
|
||||
async validateSlackMessage(channel: string, text: string) {
|
||||
if (!channel || !text) {
|
||||
throw new Error('Channel and text are required');
|
||||
}
|
||||
|
||||
const nodeTypes = getNodeTypes();
|
||||
const slackNode = nodeTypes.getByName('slack');
|
||||
|
||||
if (!slackNode) {
|
||||
throw new Error('Slack node not found');
|
||||
}
|
||||
|
||||
// Check if required properties exist
|
||||
const channelProp = slackNode.description.properties.find(p => p.name === 'channel');
|
||||
const textProp = slackNode.description.properties.find(p => p.name === 'text');
|
||||
|
||||
return !!(channelProp && textProp);
|
||||
}
|
||||
}
|
||||
|
||||
// Mock the module at the top level
|
||||
vi.mock('n8n-nodes-base', () => ({
|
||||
getNodeTypes: vi.fn(() => {
|
||||
const { getNodeTypes } = require('../__mocks__/n8n-nodes-base');
|
||||
return getNodeTypes();
|
||||
})
|
||||
}));
|
||||
|
||||
describe('WorkflowService with n8n-nodes-base mock', () => {
|
||||
let service: WorkflowService;
|
||||
|
||||
beforeEach(() => {
|
||||
resetAllMocks();
|
||||
service = new WorkflowService();
|
||||
});
|
||||
|
||||
describe('getNodeDescription', () => {
|
||||
it('should get webhook node description', async () => {
|
||||
const description = await service.getNodeDescription('webhook');
|
||||
|
||||
expect(description).toBeDefined();
|
||||
expect(description?.name).toBe('webhook');
|
||||
expect(description?.group).toContain('trigger');
|
||||
expect(description?.webhooks).toBeDefined();
|
||||
});
|
||||
|
||||
it('should get httpRequest node description', async () => {
|
||||
const description = await service.getNodeDescription('httpRequest');
|
||||
|
||||
expect(description).toBeDefined();
|
||||
expect(description?.name).toBe('httpRequest');
|
||||
expect(description?.version).toBe(3);
|
||||
|
||||
const methodProp = description?.properties.find(p => p.name === 'method');
|
||||
expect(methodProp).toBeDefined();
|
||||
expect(methodProp?.options).toHaveLength(6);
|
||||
});
|
||||
});
|
||||
|
||||
describe('executeNode', () => {
|
||||
it('should execute httpRequest node with custom response', async () => {
|
||||
// Override the httpRequest node behavior for this test
|
||||
mockNodeBehavior('httpRequest', {
|
||||
execute: vi.fn(async function(this: any) {
|
||||
const url = this.getNodeParameter('url', 0);
|
||||
return [[{
|
||||
json: {
|
||||
statusCode: 200,
|
||||
url,
|
||||
customData: 'mocked response'
|
||||
}
|
||||
}]];
|
||||
})
|
||||
});
|
||||
|
||||
const mockContext = {
|
||||
getInputData: vi.fn(() => [{ json: { input: 'data' } }]),
|
||||
getNodeParameter: vi.fn((name: string) => {
|
||||
if (name === 'url') return 'https://test.com/api';
|
||||
return '';
|
||||
})
|
||||
};
|
||||
|
||||
const result = await service.executeNode('httpRequest', mockContext);
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result[0][0].json).toMatchObject({
|
||||
statusCode: 200,
|
||||
url: 'https://test.com/api',
|
||||
customData: 'mocked response'
|
||||
});
|
||||
});
|
||||
|
||||
it('should execute slack node and track calls', async () => {
|
||||
const mockContext = {
|
||||
getInputData: vi.fn(() => [{ json: { message: 'test' } }]),
|
||||
getNodeParameter: vi.fn((name: string, index: number) => {
|
||||
const params: Record<string, string> = {
|
||||
resource: 'message',
|
||||
operation: 'post',
|
||||
channel: '#general',
|
||||
text: 'Hello from test!'
|
||||
};
|
||||
return params[name] || '';
|
||||
}),
|
||||
getCredentials: vi.fn(async () => ({ token: 'mock-token' }))
|
||||
};
|
||||
|
||||
const result = await service.executeNode('slack', mockContext);
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result[0][0].json).toMatchObject({
|
||||
ok: true,
|
||||
channel: '#general',
|
||||
message: {
|
||||
text: 'Hello from test!'
|
||||
}
|
||||
});
|
||||
|
||||
// Verify the mock was called
|
||||
expect(mockContext.getNodeParameter).toHaveBeenCalledWith('channel', 0, '');
|
||||
expect(mockContext.getNodeParameter).toHaveBeenCalledWith('text', 0, '');
|
||||
});
|
||||
|
||||
it('should throw error for non-executable node', async () => {
|
||||
// Create a trigger-only node
|
||||
mockNodeBehavior('webhook', {
|
||||
execute: undefined // Remove execute method
|
||||
});
|
||||
|
||||
await expect(
|
||||
service.executeNode('webhook', {})
|
||||
).rejects.toThrow('Node webhook does not have an execute method');
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateSlackMessage', () => {
|
||||
it('should validate slack message parameters', async () => {
|
||||
const isValid = await service.validateSlackMessage('#general', 'Hello');
|
||||
expect(isValid).toBe(true);
|
||||
});
|
||||
|
||||
it('should throw error for missing parameters', async () => {
|
||||
await expect(
|
||||
service.validateSlackMessage('', 'Hello')
|
||||
).rejects.toThrow('Channel and text are required');
|
||||
|
||||
await expect(
|
||||
service.validateSlackMessage('#general', '')
|
||||
).rejects.toThrow('Channel and text are required');
|
||||
});
|
||||
|
||||
it('should handle missing slack node', async () => {
|
||||
// Override getNodeTypes to return undefined for slack
|
||||
const getNodeTypes = vi.fn(() => ({
|
||||
getByName: vi.fn((name: string) => {
|
||||
if (name === 'slack') return undefined;
|
||||
return null;
|
||||
}),
|
||||
getByNameAndVersion: vi.fn()
|
||||
}));
|
||||
|
||||
vi.mocked(require('n8n-nodes-base').getNodeTypes).mockImplementation(getNodeTypes);
|
||||
|
||||
await expect(
|
||||
service.validateSlackMessage('#general', 'Hello')
|
||||
).rejects.toThrow('Slack node not found');
|
||||
});
|
||||
});
|
||||
|
||||
describe('complex workflow scenarios', () => {
|
||||
it('should handle if node branching', async () => {
|
||||
const mockContext = {
|
||||
getInputData: vi.fn(() => [
|
||||
{ json: { status: 'active' } },
|
||||
{ json: { status: 'inactive' } },
|
||||
{ json: { status: 'active' } },
|
||||
]),
|
||||
getNodeParameter: vi.fn()
|
||||
};
|
||||
|
||||
const result = await service.executeNode('if', mockContext);
|
||||
|
||||
expect(result).toHaveLength(2); // true and false branches
|
||||
expect(result[0]).toHaveLength(2); // items at index 0 and 2
|
||||
expect(result[1]).toHaveLength(1); // item at index 1
|
||||
});
|
||||
|
||||
it('should handle merge node combining inputs', async () => {
|
||||
const mockContext = {
|
||||
getInputData: vi.fn((inputIndex?: number) => {
|
||||
if (inputIndex === 0) return [{ json: { source: 'input1' } }];
|
||||
if (inputIndex === 1) return [{ json: { source: 'input2' } }];
|
||||
return [{ json: { source: 'input1' } }];
|
||||
}),
|
||||
getNodeParameter: vi.fn(() => 'append')
|
||||
};
|
||||
|
||||
const result = await service.executeNode('merge', mockContext);
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result[0]).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
170
tests/unit/test-env-example.test.ts
Normal file
170
tests/unit/test-env-example.test.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
/**
|
||||
* Example test demonstrating test environment configuration usage
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
||||
import {
|
||||
getTestConfig,
|
||||
getTestTimeout,
|
||||
isFeatureEnabled,
|
||||
isTestMode
|
||||
} from '@tests/setup/test-env';
|
||||
import {
|
||||
withEnvOverrides,
|
||||
createTestDatabasePath,
|
||||
getMockApiUrl,
|
||||
measurePerformance,
|
||||
createTestLogger,
|
||||
waitForCondition
|
||||
} from '@tests/helpers/env-helpers';
|
||||
|
||||
describe('Test Environment Configuration Example', () => {
|
||||
const config = getTestConfig();
|
||||
const logger = createTestLogger('test-env-example');
|
||||
|
||||
beforeAll(() => {
|
||||
logger.info('Test suite starting with configuration:', {
|
||||
environment: config.nodeEnv,
|
||||
database: config.database.path,
|
||||
apiUrl: config.api.url
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
logger.info('Test suite completed');
|
||||
});
|
||||
|
||||
it('should be in test mode', () => {
|
||||
expect(isTestMode()).toBe(true);
|
||||
expect(config.nodeEnv).toBe('test');
|
||||
expect(config.isTest).toBe(true);
|
||||
});
|
||||
|
||||
it('should have proper database configuration', () => {
|
||||
expect(config.database.path).toBeDefined();
|
||||
expect(config.database.rebuildOnStart).toBe(false);
|
||||
expect(config.database.seedData).toBe(true);
|
||||
});
|
||||
|
||||
it('should have mock API configuration', () => {
|
||||
expect(config.api.url).toMatch(/mock-api/);
|
||||
expect(config.api.key).toBe('test-api-key-12345');
|
||||
});
|
||||
|
||||
it('should respect test timeouts', { timeout: getTestTimeout('unit') }, async () => {
|
||||
const timeout = getTestTimeout('unit');
|
||||
expect(timeout).toBe(5000);
|
||||
|
||||
// Simulate async operation
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
});
|
||||
|
||||
it('should support environment overrides', () => {
|
||||
const originalLogLevel = config.logging.level;
|
||||
|
||||
const result = withEnvOverrides({
|
||||
LOG_LEVEL: 'debug',
|
||||
DEBUG: 'true'
|
||||
}, () => {
|
||||
const newConfig = getTestConfig();
|
||||
expect(newConfig.logging.level).toBe('debug');
|
||||
expect(newConfig.logging.debug).toBe(true);
|
||||
return 'success';
|
||||
});
|
||||
|
||||
expect(result).toBe('success');
|
||||
expect(config.logging.level).toBe(originalLogLevel);
|
||||
});
|
||||
|
||||
it('should generate unique test database paths', () => {
|
||||
const path1 = createTestDatabasePath('feature1');
|
||||
const path2 = createTestDatabasePath('feature1');
|
||||
|
||||
if (path1 !== ':memory:') {
|
||||
expect(path1).not.toBe(path2);
|
||||
expect(path1).toMatch(/test-feature1-\d+-\w+\.db$/);
|
||||
}
|
||||
});
|
||||
|
||||
it('should construct mock API URLs', () => {
|
||||
const baseUrl = getMockApiUrl();
|
||||
const endpointUrl = getMockApiUrl('/nodes');
|
||||
|
||||
expect(baseUrl).toBe(config.api.url);
|
||||
expect(endpointUrl).toBe(`${config.api.url}/nodes`);
|
||||
});
|
||||
|
||||
it.skipIf(!isFeatureEnabled('mockExternalApis'))('should check feature flags', () => {
|
||||
expect(config.features.mockExternalApis).toBe(true);
|
||||
expect(isFeatureEnabled('mockExternalApis')).toBe(true);
|
||||
});
|
||||
|
||||
it('should measure performance', async () => {
|
||||
const measure = measurePerformance('test-operation');
|
||||
|
||||
// Simulate some work
|
||||
measure.mark('start-processing');
|
||||
await new Promise(resolve => setTimeout(resolve, 50));
|
||||
measure.mark('mid-processing');
|
||||
await new Promise(resolve => setTimeout(resolve, 50));
|
||||
|
||||
const results = measure.end();
|
||||
|
||||
expect(results.total).toBeGreaterThan(100);
|
||||
expect(results.marks['start-processing']).toBeLessThan(results.marks['mid-processing']);
|
||||
});
|
||||
|
||||
it('should wait for conditions', async () => {
|
||||
let counter = 0;
|
||||
const incrementCounter = setInterval(() => counter++, 100);
|
||||
|
||||
try {
|
||||
await waitForCondition(
|
||||
() => counter >= 3,
|
||||
{
|
||||
timeout: 1000,
|
||||
interval: 50,
|
||||
message: 'Counter did not reach 3'
|
||||
}
|
||||
);
|
||||
|
||||
expect(counter).toBeGreaterThanOrEqual(3);
|
||||
} finally {
|
||||
clearInterval(incrementCounter);
|
||||
}
|
||||
});
|
||||
|
||||
it('should have proper logging configuration', () => {
|
||||
expect(config.logging.level).toBe('error');
|
||||
expect(config.logging.debug).toBe(false);
|
||||
expect(config.logging.showStack).toBe(true);
|
||||
|
||||
// Logger should respect configuration
|
||||
logger.debug('This should not appear in test output');
|
||||
logger.error('This should appear in test output');
|
||||
});
|
||||
|
||||
it('should have performance thresholds', () => {
|
||||
expect(config.performance.thresholds.apiResponse).toBe(100);
|
||||
expect(config.performance.thresholds.dbQuery).toBe(50);
|
||||
expect(config.performance.thresholds.nodeParse).toBe(200);
|
||||
});
|
||||
|
||||
it('should disable caching and rate limiting in tests', () => {
|
||||
expect(config.cache.enabled).toBe(false);
|
||||
expect(config.cache.ttl).toBe(0);
|
||||
expect(config.rateLimiting.max).toBe(0);
|
||||
expect(config.rateLimiting.window).toBe(0);
|
||||
});
|
||||
|
||||
it('should configure test paths', () => {
|
||||
expect(config.paths.fixtures).toBe('./tests/fixtures');
|
||||
expect(config.paths.data).toBe('./tests/data');
|
||||
expect(config.paths.snapshots).toBe('./tests/__snapshots__');
|
||||
});
|
||||
|
||||
it('should support MSW configuration', () => {
|
||||
expect(config.mocking.msw.enabled).toBe(true);
|
||||
expect(config.mocking.msw.apiDelay).toBe(0);
|
||||
});
|
||||
});
|
||||
399
tests/unit/utils/database-utils.test.ts
Normal file
399
tests/unit/utils/database-utils.test.ts
Normal file
@@ -0,0 +1,399 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import {
|
||||
createTestDatabase,
|
||||
seedTestNodes,
|
||||
seedTestTemplates,
|
||||
createTestNode,
|
||||
createTestTemplate,
|
||||
resetDatabase,
|
||||
createDatabaseSnapshot,
|
||||
restoreDatabaseSnapshot,
|
||||
loadFixtures,
|
||||
dbHelpers,
|
||||
createMockDatabaseAdapter,
|
||||
withTransaction,
|
||||
measureDatabaseOperation,
|
||||
TestDatabase
|
||||
} from '../../utils/database-utils';
|
||||
|
||||
describe('Database Utils', () => {
|
||||
let testDb: TestDatabase;
|
||||
|
||||
afterEach(async () => {
|
||||
if (testDb) {
|
||||
await testDb.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
describe('createTestDatabase', () => {
|
||||
it('should create an in-memory database by default', async () => {
|
||||
testDb = await createTestDatabase();
|
||||
|
||||
expect(testDb.adapter).toBeDefined();
|
||||
expect(testDb.nodeRepository).toBeDefined();
|
||||
expect(testDb.templateRepository).toBeDefined();
|
||||
expect(testDb.path).toBe(':memory:');
|
||||
});
|
||||
|
||||
it('should create a file-based database when requested', async () => {
|
||||
const dbPath = path.join(__dirname, '../../temp/test-file.db');
|
||||
testDb = await createTestDatabase({ inMemory: false, dbPath });
|
||||
|
||||
expect(testDb.path).toBe(dbPath);
|
||||
expect(fs.existsSync(dbPath)).toBe(true);
|
||||
});
|
||||
|
||||
it('should initialize schema when requested', async () => {
|
||||
testDb = await createTestDatabase({ initSchema: true });
|
||||
|
||||
// Verify tables exist
|
||||
const tables = testDb.adapter
|
||||
.prepare("SELECT name FROM sqlite_master WHERE type='table'")
|
||||
.all() as { name: string }[];
|
||||
|
||||
const tableNames = tables.map(t => t.name);
|
||||
expect(tableNames).toContain('nodes');
|
||||
expect(tableNames).toContain('templates');
|
||||
});
|
||||
|
||||
it('should skip schema initialization when requested', async () => {
|
||||
testDb = await createTestDatabase({ initSchema: false });
|
||||
|
||||
// Verify tables don't exist (SQLite has internal tables, so check for our specific tables)
|
||||
const tables = testDb.adapter
|
||||
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name IN ('nodes', 'templates')")
|
||||
.all() as { name: string }[];
|
||||
|
||||
expect(tables.length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('seedTestNodes', () => {
|
||||
beforeEach(async () => {
|
||||
testDb = await createTestDatabase();
|
||||
});
|
||||
|
||||
it('should seed default test nodes', async () => {
|
||||
const nodes = await seedTestNodes(testDb.nodeRepository);
|
||||
|
||||
expect(nodes).toHaveLength(3);
|
||||
expect(nodes[0].nodeType).toBe('nodes-base.httpRequest');
|
||||
expect(nodes[1].nodeType).toBe('nodes-base.webhook');
|
||||
expect(nodes[2].nodeType).toBe('nodes-base.slack');
|
||||
});
|
||||
|
||||
it('should seed custom nodes along with defaults', async () => {
|
||||
const customNodes = [
|
||||
{ nodeType: 'nodes-base.custom1', displayName: 'Custom 1' },
|
||||
{ nodeType: 'nodes-base.custom2', displayName: 'Custom 2' }
|
||||
];
|
||||
|
||||
const nodes = await seedTestNodes(testDb.nodeRepository, customNodes);
|
||||
|
||||
expect(nodes).toHaveLength(5); // 3 default + 2 custom
|
||||
expect(nodes[3].nodeType).toBe('nodes-base.custom1');
|
||||
expect(nodes[4].nodeType).toBe('nodes-base.custom2');
|
||||
});
|
||||
|
||||
it('should save nodes to database', async () => {
|
||||
await seedTestNodes(testDb.nodeRepository);
|
||||
|
||||
const count = dbHelpers.countRows(testDb.adapter, 'nodes');
|
||||
expect(count).toBe(3);
|
||||
|
||||
const httpNode = testDb.nodeRepository.getNode('nodes-base.httpRequest');
|
||||
expect(httpNode).toBeDefined();
|
||||
expect(httpNode.displayName).toBe('HTTP Request');
|
||||
});
|
||||
});
|
||||
|
||||
describe('seedTestTemplates', () => {
|
||||
beforeEach(async () => {
|
||||
testDb = await createTestDatabase();
|
||||
});
|
||||
|
||||
it('should seed default test templates', async () => {
|
||||
const templates = await seedTestTemplates(testDb.templateRepository);
|
||||
|
||||
expect(templates).toHaveLength(2);
|
||||
expect(templates[0].name).toBe('Simple HTTP Workflow');
|
||||
expect(templates[1].name).toBe('Webhook to Slack');
|
||||
});
|
||||
|
||||
it('should seed custom templates', async () => {
|
||||
const customTemplates = [
|
||||
{ id: 100, name: 'Custom Template' }
|
||||
];
|
||||
|
||||
const templates = await seedTestTemplates(testDb.templateRepository, customTemplates);
|
||||
|
||||
expect(templates).toHaveLength(3);
|
||||
expect(templates[2].id).toBe(100);
|
||||
expect(templates[2].name).toBe('Custom Template');
|
||||
});
|
||||
});
|
||||
|
||||
describe('createTestNode', () => {
|
||||
it('should create a node with defaults', () => {
|
||||
const node = createTestNode();
|
||||
|
||||
expect(node.nodeType).toBe('nodes-base.test');
|
||||
expect(node.displayName).toBe('Test Node');
|
||||
expect(node.style).toBe('programmatic');
|
||||
expect(node.isAITool).toBe(false);
|
||||
});
|
||||
|
||||
it('should override defaults', () => {
|
||||
const node = createTestNode({
|
||||
nodeType: 'nodes-base.custom',
|
||||
displayName: 'Custom Node',
|
||||
isAITool: true
|
||||
});
|
||||
|
||||
expect(node.nodeType).toBe('nodes-base.custom');
|
||||
expect(node.displayName).toBe('Custom Node');
|
||||
expect(node.isAITool).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('resetDatabase', () => {
|
||||
beforeEach(async () => {
|
||||
testDb = await createTestDatabase();
|
||||
});
|
||||
|
||||
it('should clear all data and reinitialize schema', async () => {
|
||||
// Add some data
|
||||
await seedTestNodes(testDb.nodeRepository);
|
||||
await seedTestTemplates(testDb.templateRepository);
|
||||
|
||||
// Verify data exists
|
||||
expect(dbHelpers.countRows(testDb.adapter, 'nodes')).toBe(3);
|
||||
expect(dbHelpers.countRows(testDb.adapter, 'templates')).toBe(2);
|
||||
|
||||
// Reset database
|
||||
await resetDatabase(testDb.adapter);
|
||||
|
||||
// Verify data is cleared
|
||||
expect(dbHelpers.countRows(testDb.adapter, 'nodes')).toBe(0);
|
||||
expect(dbHelpers.countRows(testDb.adapter, 'templates')).toBe(0);
|
||||
|
||||
// Verify tables still exist
|
||||
const tables = testDb.adapter
|
||||
.prepare("SELECT name FROM sqlite_master WHERE type='table'")
|
||||
.all() as { name: string }[];
|
||||
|
||||
const tableNames = tables.map(t => t.name);
|
||||
expect(tableNames).toContain('nodes');
|
||||
expect(tableNames).toContain('templates');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Database Snapshots', () => {
|
||||
beforeEach(async () => {
|
||||
testDb = await createTestDatabase();
|
||||
});
|
||||
|
||||
it('should create and restore database snapshot', async () => {
|
||||
// Seed initial data
|
||||
await seedTestNodes(testDb.nodeRepository);
|
||||
await seedTestTemplates(testDb.templateRepository);
|
||||
|
||||
// Create snapshot
|
||||
const snapshot = await createDatabaseSnapshot(testDb.adapter);
|
||||
|
||||
expect(snapshot.metadata.nodeCount).toBe(3);
|
||||
expect(snapshot.metadata.templateCount).toBe(2);
|
||||
expect(snapshot.nodes).toHaveLength(3);
|
||||
expect(snapshot.templates).toHaveLength(2);
|
||||
|
||||
// Clear database
|
||||
await resetDatabase(testDb.adapter);
|
||||
expect(dbHelpers.countRows(testDb.adapter, 'nodes')).toBe(0);
|
||||
|
||||
// Restore from snapshot
|
||||
await restoreDatabaseSnapshot(testDb.adapter, snapshot);
|
||||
|
||||
// Verify data is restored
|
||||
expect(dbHelpers.countRows(testDb.adapter, 'nodes')).toBe(3);
|
||||
expect(dbHelpers.countRows(testDb.adapter, 'templates')).toBe(2);
|
||||
|
||||
const httpNode = testDb.nodeRepository.getNode('nodes-base.httpRequest');
|
||||
expect(httpNode).toBeDefined();
|
||||
expect(httpNode.displayName).toBe('HTTP Request');
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadFixtures', () => {
|
||||
beforeEach(async () => {
|
||||
testDb = await createTestDatabase();
|
||||
});
|
||||
|
||||
it('should load fixtures from JSON file', async () => {
|
||||
// Create a temporary fixture file
|
||||
const fixturePath = path.join(__dirname, '../../temp/test-fixtures.json');
|
||||
const fixtures = {
|
||||
nodes: [
|
||||
createTestNode({ nodeType: 'nodes-base.fixture1' }),
|
||||
createTestNode({ nodeType: 'nodes-base.fixture2' })
|
||||
],
|
||||
templates: [
|
||||
createTestTemplate({ id: 1000, name: 'Fixture Template' })
|
||||
]
|
||||
};
|
||||
|
||||
// Ensure directory exists
|
||||
const dir = path.dirname(fixturePath);
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
fs.writeFileSync(fixturePath, JSON.stringify(fixtures, null, 2));
|
||||
|
||||
// Load fixtures
|
||||
await loadFixtures(testDb.adapter, fixturePath);
|
||||
|
||||
// Verify data was loaded
|
||||
expect(dbHelpers.countRows(testDb.adapter, 'nodes')).toBe(2);
|
||||
expect(dbHelpers.countRows(testDb.adapter, 'templates')).toBe(1);
|
||||
|
||||
expect(dbHelpers.nodeExists(testDb.adapter, 'nodes-base.fixture1')).toBe(true);
|
||||
expect(dbHelpers.nodeExists(testDb.adapter, 'nodes-base.fixture2')).toBe(true);
|
||||
|
||||
// Cleanup
|
||||
fs.unlinkSync(fixturePath);
|
||||
});
|
||||
});
|
||||
|
||||
describe('dbHelpers', () => {
|
||||
beforeEach(async () => {
|
||||
testDb = await createTestDatabase();
|
||||
await seedTestNodes(testDb.nodeRepository);
|
||||
});
|
||||
|
||||
it('should count rows correctly', () => {
|
||||
const count = dbHelpers.countRows(testDb.adapter, 'nodes');
|
||||
expect(count).toBe(3);
|
||||
});
|
||||
|
||||
it('should check if node exists', () => {
|
||||
expect(dbHelpers.nodeExists(testDb.adapter, 'nodes-base.httpRequest')).toBe(true);
|
||||
expect(dbHelpers.nodeExists(testDb.adapter, 'nodes-base.nonexistent')).toBe(false);
|
||||
});
|
||||
|
||||
it('should get all node types', () => {
|
||||
const nodeTypes = dbHelpers.getAllNodeTypes(testDb.adapter);
|
||||
expect(nodeTypes).toHaveLength(3);
|
||||
expect(nodeTypes).toContain('nodes-base.httpRequest');
|
||||
expect(nodeTypes).toContain('nodes-base.webhook');
|
||||
expect(nodeTypes).toContain('nodes-base.slack');
|
||||
});
|
||||
|
||||
it('should clear table', () => {
|
||||
expect(dbHelpers.countRows(testDb.adapter, 'nodes')).toBe(3);
|
||||
|
||||
dbHelpers.clearTable(testDb.adapter, 'nodes');
|
||||
|
||||
expect(dbHelpers.countRows(testDb.adapter, 'nodes')).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('createMockDatabaseAdapter', () => {
|
||||
it('should create a mock adapter with all required methods', () => {
|
||||
const mockAdapter = createMockDatabaseAdapter();
|
||||
|
||||
expect(mockAdapter.prepare).toBeDefined();
|
||||
expect(mockAdapter.exec).toBeDefined();
|
||||
expect(mockAdapter.close).toBeDefined();
|
||||
expect(mockAdapter.pragma).toBeDefined();
|
||||
expect(mockAdapter.transaction).toBeDefined();
|
||||
expect(mockAdapter.checkFTS5Support).toBeDefined();
|
||||
|
||||
// Test that methods are mocked
|
||||
expect(vi.isMockFunction(mockAdapter.prepare)).toBe(true);
|
||||
expect(vi.isMockFunction(mockAdapter.exec)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('withTransaction', () => {
|
||||
beforeEach(async () => {
|
||||
testDb = await createTestDatabase();
|
||||
});
|
||||
|
||||
it('should rollback transaction for testing', async () => {
|
||||
// Insert a node
|
||||
await seedTestNodes(testDb.nodeRepository, [
|
||||
{ nodeType: 'nodes-base.transaction-test' }
|
||||
]);
|
||||
|
||||
const initialCount = dbHelpers.countRows(testDb.adapter, 'nodes');
|
||||
|
||||
// Try to insert in a transaction that will rollback
|
||||
const result = await withTransaction(testDb.adapter, async () => {
|
||||
testDb.nodeRepository.saveNode(createTestNode({
|
||||
nodeType: 'nodes-base.should-rollback'
|
||||
}));
|
||||
|
||||
// Verify it was inserted within transaction
|
||||
const midCount = dbHelpers.countRows(testDb.adapter, 'nodes');
|
||||
expect(midCount).toBe(initialCount + 1);
|
||||
|
||||
return 'test-result';
|
||||
});
|
||||
|
||||
// Transaction should have rolled back
|
||||
expect(result).toBeNull();
|
||||
const finalCount = dbHelpers.countRows(testDb.adapter, 'nodes');
|
||||
expect(finalCount).toBe(initialCount);
|
||||
});
|
||||
});
|
||||
|
||||
describe('measureDatabaseOperation', () => {
|
||||
beforeEach(async () => {
|
||||
testDb = await createTestDatabase();
|
||||
});
|
||||
|
||||
it('should measure operation duration', async () => {
|
||||
const duration = await measureDatabaseOperation('test operation', async () => {
|
||||
await seedTestNodes(testDb.nodeRepository);
|
||||
});
|
||||
|
||||
expect(duration).toBeGreaterThan(0);
|
||||
expect(duration).toBeLessThan(1000); // Should be fast
|
||||
});
|
||||
});
|
||||
|
||||
describe('Integration Tests', () => {
|
||||
it('should handle complex database operations', async () => {
|
||||
testDb = await createTestDatabase({ enableFTS5: true });
|
||||
|
||||
// Seed initial data
|
||||
const nodes = await seedTestNodes(testDb.nodeRepository);
|
||||
const templates = await seedTestTemplates(testDb.templateRepository);
|
||||
|
||||
// Create snapshot
|
||||
const snapshot = await createDatabaseSnapshot(testDb.adapter);
|
||||
|
||||
// Add more data
|
||||
await seedTestNodes(testDb.nodeRepository, [
|
||||
{ nodeType: 'nodes-base.extra1' },
|
||||
{ nodeType: 'nodes-base.extra2' }
|
||||
]);
|
||||
|
||||
expect(dbHelpers.countRows(testDb.adapter, 'nodes')).toBe(5);
|
||||
|
||||
// Restore snapshot
|
||||
await restoreDatabaseSnapshot(testDb.adapter, snapshot);
|
||||
|
||||
// Should be back to original state
|
||||
expect(dbHelpers.countRows(testDb.adapter, 'nodes')).toBe(3);
|
||||
|
||||
// Test FTS5 if supported
|
||||
if (testDb.adapter.checkFTS5Support()) {
|
||||
// FTS5 operations would go here
|
||||
expect(true).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user