feat: implement integration testing foundation (Phase 1)

Complete implementation of Phase 1 foundation for n8n API integration tests.
Establishes core utilities, fixtures, and infrastructure for testing all 17 n8n API handlers against real n8n instance.

Changes:
- Add integration test environment configuration to .env.example
- Create comprehensive test utilities infrastructure:
  * credentials.ts: Environment-aware credential management (local .env vs CI secrets)
  * n8n-client.ts: Singleton API client wrapper with health checks
  * test-context.ts: Resource tracking and automatic cleanup
  * cleanup-helpers.ts: Multi-level cleanup strategies (orphaned, age-based, tag-based)
  * fixtures.ts: 6 pre-built workflow templates (webhook, HTTP, multi-node, error handling, AI, expressions)
  * factories.ts: Dynamic node/workflow builders with 15+ factory functions
  * webhook-workflows.ts: Webhook workflow configs and setup instructions

- Add npm scripts:
  * test:integration:n8n: Run n8n API integration tests
  * test:cleanup:orphans: Clean up orphaned test resources

- Create cleanup script for CI/manual use

Documentation:
- Add comprehensive integration testing plan (550 lines)
- Add Phase 1 completion summary with lessons learned

Key Features:
- Automatic credential detection (CI vs local)
- Multi-level cleanup (test, suite, CI, orphan)
- 6 workflow fixtures covering common scenarios
- 15+ factory functions for dynamic test data
- Support for 4 HTTP methods (GET, POST, PUT, DELETE) via pre-activated webhook workflows
- TypeScript-first with full type safety
- Comprehensive error handling with helpful messages

Total: ~1,520 lines of production-ready code + 650 lines of documentation

Ready for Phase 2: Workflow creation tests

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
czlonkowski
2025-10-03 13:12:42 +02:00
parent fe59688e03
commit 2305aaab9e
18 changed files with 10956 additions and 1 deletions

View File

@@ -0,0 +1,63 @@
/**
* Pre-configured n8n API Client for Integration Tests
*
* Provides a singleton API client instance configured with test credentials.
* Automatically loads credentials from .env (local) or GitHub secrets (CI).
*/
import { N8nApiClient } from '../../../../src/services/n8n-api-client';
import { getN8nCredentials, validateCredentials } from './credentials';
let client: N8nApiClient | null = null;
/**
* Get or create the test n8n API client
*
* Creates a singleton instance configured with credentials from
* the environment. Validates that required credentials are present.
*
* @returns Configured N8nApiClient instance
* @throws Error if credentials are missing or invalid
*
* @example
* const client = getTestN8nClient();
* const workflow = await client.createWorkflow({ ... });
*/
export function getTestN8nClient(): N8nApiClient {
if (!client) {
const creds = getN8nCredentials();
validateCredentials(creds);
client = new N8nApiClient({
baseUrl: creds.url,
apiKey: creds.apiKey
});
}
return client;
}
/**
* Reset the test client instance
*
* Forces recreation of the client on next call to getTestN8nClient().
* Useful for testing or when credentials change.
*/
export function resetTestN8nClient(): void {
client = null;
}
/**
* Check if the n8n API is accessible
*
* Performs a health check to verify API connectivity.
*
* @returns true if API is accessible, false otherwise
*/
export async function isN8nApiAccessible(): Promise<boolean> {
try {
const client = getTestN8nClient();
await client.healthCheck();
return true;
} catch {
return false;
}
}