#!/usr/bin/env node /* eslint-disable no-console, unused-imports/no-unused-vars */ /** * Data Engine API Test Suite * * Comprehensive testing script for all Data Engine API endpoints including: * - CRUD operations for all resources * - Pagination and filtering * - Search capabilities * - Tag hierarchies * - Document vector search * * Usage: * node test-data-engine-api.js [options] * * Options: * --setup Interactive setup to save API key and base URL * --api-key= API key for authentication * --base-url= Base URL (default: http://localhost:4000/api/v1/data-engine) * --resource= Test only specific resource (domains, urls, calls, tags, documents, tickets, chats) * --verbose Show detailed request/response info * * Examples: * node test-data-engine-api.js --setup * node test-data-engine-api.js * node test-data-engine-api.js --resource=domains --verbose */ const https = require('https') const http = require('http') const { URL } = require('url') const fs = require('fs') const path = require('path') const readline = require('readline') // ===================================================================== // Environment Variable Loading // ===================================================================== function loadEnvFile(filePath) { try { if (!fs.existsSync(filePath)) return const content = fs.readFileSync(filePath, 'utf-8') const lines = content.split('\n') for (const line of lines) { const trimmed = line.trim() // Skip empty lines and comments if (!trimmed || trimmed.startsWith('#')) continue const match = trimmed.match(/^([^=]+)=(.*)$/) if (match) { const key = match[1].trim() let value = match[2].trim() // Remove quotes if present if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) { value = value.slice(1, -1) } // Only set if not already set if (!process.env[key]) { process.env[key] = value } } } } catch (error) { // Silently fail if .env file can't be read } } // Load .env file - try multiple locations // 1. Try project root (when script is in scripts/ subdirectory) const projectRoot = path.resolve(__dirname, '..') const envPath = path.join(projectRoot, '.env') loadEnvFile(envPath) // 2. Try current working directory (when running from project root) const cwdEnvPath = path.join(process.cwd(), '.env') loadEnvFile(cwdEnvPath) // 3. Try scripts directory const scriptsEnvPath = path.join(__dirname, '.env') loadEnvFile(scriptsEnvPath) // ===================================================================== // Configuration // ===================================================================== const config = { baseUrl: process.env.DATA_ENGINE_BASE_URL || 'http://localhost:4000/api/v1/data-engine', apiKey: process.env.DATA_ENGINE_API_KEY || '', verbose: false, testResource: null, // Test all resources by default setupMode: false, debug: false, useM2M: false, // Use M2M authentication instead of API key m2mToken: null, // Cached M2M token noCleanup: false // Skip cleanup (keep test data for verification) } // Parse command line arguments process.argv.slice(2).forEach((arg) => { if (arg === '--setup') { config.setupMode = true } else if (arg.startsWith('--api-key=')) { config.apiKey = arg.split('=')[1] } else if (arg.startsWith('--base-url=')) { config.baseUrl = arg.split('=')[1] } else if (arg.startsWith('--resource=')) { config.testResource = arg.split('=')[1] } else if (arg === '--verbose') { config.verbose = true } else if (arg === '--debug') { config.debug = true config.verbose = true } else if (arg === '--m2m') { config.useM2M = true } else if (arg === '--no-cleanup') { config.noCleanup = true } else if (arg === '--help' || arg === '-h') { console.log(` Data Engine API Test Suite Usage: node test-data-engine-api.js [options] Options: --setup Interactive setup to save API key and base URL to .env --api-key= API key for authentication --base-url= Base URL (default: http://localhost:4000/api/v1/data-engine) --resource= Test only specific resource --verbose Show detailed request/response info --debug Enable debug mode (shows raw responses, enables verbose) --m2m Use Auth0 M2M authentication (requires M2M env vars) --no-cleanup Skip cleanup (keep test data for manual verification) --help, -h Show this help message Resources: domains, urls, calls, tags, documents, tickets, chats Examples: # First time setup (saves to .env file) node test-data-engine-api.js --setup # Run all tests with API key node test-data-engine-api.js # Run all tests with M2M authentication node test-data-engine-api.js --m2m # Test specific resource with verbose output node test-data-engine-api.js --resource=domains --verbose # Keep test data for verification (no cleanup) node test-data-engine-api.js --no-cleanup # Test against custom domain node test-data-engine-api.js --base-url=https://your-domain.com/api/v1/data-engine `) process.exit(0) } }) // ===================================================================== // Setup Mode - Interactive Configuration // ===================================================================== async function setupConfiguration() { const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) const question = (prompt) => new Promise((resolve) => { rl.question(prompt, resolve) }) console.log('\n╔════════════════════════════════════════════════════════════╗') console.log('║ Data Engine API Test Suite - Setup ║') console.log('╚════════════════════════════════════════════════════════════╝\n') console.log('This will save your configuration to the project .env file.\n') // Get API Key const currentKey = process.env.DATA_ENGINE_API_KEY || '' const keyPrompt = currentKey ? `API Key [current: ${currentKey.substring(0, 10)}...]: ` : 'API Key (required): ' let apiKey = await question(keyPrompt) apiKey = apiKey.trim() || currentKey if (!apiKey) { console.error('\n❌ Error: API key is required') rl.close() process.exit(1) } // Get Base URL const currentUrl = process.env.DATA_ENGINE_BASE_URL || 'http://localhost:3000/api/v1/data-engine' const urlPrompt = `Base URL [${currentUrl}]: ` let baseUrl = await question(urlPrompt) baseUrl = baseUrl.trim() || currentUrl rl.close() // Save to .env file const envPath = path.join(projectRoot, '.env') let envContent = '' // Read existing .env if it exists if (fs.existsSync(envPath)) { envContent = fs.readFileSync(envPath, 'utf-8') } // Update or add the variables const updateEnvVar = (content, key, value) => { const regex = new RegExp(`^${key}=.*$`, 'm') const line = `${key}="${value}"` if (regex.test(content)) { return content.replace(regex, line) } else { // Add to end with proper newline return content + (content.endsWith('\n') ? '' : '\n') + line + '\n' } } envContent = updateEnvVar(envContent, 'DATA_ENGINE_API_KEY', apiKey) envContent = updateEnvVar(envContent, 'DATA_ENGINE_BASE_URL', baseUrl) try { fs.writeFileSync(envPath, envContent, 'utf-8') console.log('\n✅ Configuration saved to .env file') console.log(` API Key: ${apiKey.substring(0, 10)}...`) console.log(` Base URL: ${baseUrl}`) console.log('\n💡 You can now run tests with: node test-data-engine-api.js\n') process.exit(0) } catch (error) { console.error('\n❌ Error saving configuration:', error.message) console.log('\nYou can manually add these to your .env file:') console.log(`DATA_ENGINE_API_KEY="${apiKey}"`) console.log(`DATA_ENGINE_BASE_URL="${baseUrl}"`) process.exit(1) } } // If in setup mode, run setup and exit if (config.setupMode) { setupConfiguration() return } // Validate configuration if (!config.useM2M && !config.apiKey) { console.error('❌ Error: Authentication required') console.error('\n Option 1: Run setup to save API key configuration') console.error(' node test-data-engine-api.js --setup') console.error('\n Option 2: Set environment variable') console.error(' export DATA_ENGINE_API_KEY="your-key"') console.error('\n Option 3: Use command line option') console.error(' node test-data-engine-api.js --api-key=your-key') console.error('\n Option 4: Use M2M authentication (requires Auth0 env vars)') console.error(' node test-data-engine-api.js --m2m\n') process.exit(1) } // Validate M2M configuration if using M2M if (config.useM2M) { const requiredVars = ['AUTH0_ISSUER_BASE_URL', 'DATA_SIDEKICK_CLIENT_ID', 'DATA_SIDEKICK_CLIENT_SECRET'] const missing = requiredVars.filter((v) => !process.env[v]) if (missing.length > 0) { console.error('❌ Error: M2M authentication requires the following environment variables:') missing.forEach((v) => console.error(` - ${v}`)) console.error('\n Add these to your .env file or set as environment variables\n') process.exit(1) } } // ===================================================================== // HTTP Helper Functions // ===================================================================== /** * Get Auth0 M2M token using client credentials */ async function getM2MToken() { // Return cached token if available if (config.m2mToken) { return config.m2mToken } try { const tokenUrl = `${process.env.AUTH0_ISSUER_BASE_URL}/oauth/token` const payload = JSON.stringify({ grant_type: 'client_credentials', client_id: process.env.DATA_SIDEKICK_CLIENT_ID, client_secret: process.env.DATA_SIDEKICK_CLIENT_SECRET, audience: process.env.DATA_SIDEKICK_AUDIENCE || 'https://data-sidekick-api' }) return new Promise((resolve, reject) => { const url = new URL(tokenUrl) const isHttps = url.protocol === 'https:' const client = isHttps ? https : http const options = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(payload) } } const req = client.request(url, options, (res) => { let data = '' res.on('data', (chunk) => { data += chunk }) res.on('end', () => { try { const parsed = JSON.parse(data) if (res.statusCode === 200 && parsed.access_token) { config.m2mToken = parsed.access_token console.log('✅ M2M token obtained successfully') resolve(parsed.access_token) } else { reject({ status: res.statusCode, message: parsed.error_description || parsed.error || 'Failed to get M2M token', data: parsed }) } } catch (error) { reject({ status: res.statusCode, message: 'Failed to parse token response', raw: data }) } }) }) req.on('error', (error) => { reject({ message: 'Network error getting M2M token', error: error.message }) }) req.write(payload) req.end() }) } catch (error) { console.error('❌ Failed to get M2M token:', error) throw error } } async function makeRequest(method, path, body = null) { // Get auth token (M2M or API key) let authToken = config.apiKey if (config.useM2M) { authToken = await getM2MToken() } return new Promise((resolve, reject) => { // Ensure path starts with / const cleanPath = path.startsWith('/') ? path : `/${path}` // Build full URL properly const fullUrl = config.baseUrl + cleanPath const url = new URL(fullUrl) const isHttps = url.protocol === 'https:' const client = isHttps ? https : http const options = { method, headers: { Authorization: `Bearer ${authToken}`, 'Content-Type': 'application/json' } } if (config.verbose) { console.log(`\n → ${method} ${fullUrl}`) if (body) console.log(` → Body: ${JSON.stringify(body, null, 2)}`) } const req = client.request(url, options, (res) => { let data = '' res.on('data', (chunk) => { data += chunk }) res.on('end', () => { try { const parsed = data ? JSON.parse(data) : {} if (config.verbose) { console.log(` ← Status: ${res.statusCode}`) console.log(` ← Response: ${JSON.stringify(parsed, null, 2)}`) } if (res.statusCode >= 200 && res.statusCode < 300) { resolve(parsed) } else { reject({ status: res.statusCode, message: parsed.message || parsed.error || 'Request failed', data: parsed }) } } catch (error) { // Show raw response when JSON parsing fails const preview = data.length > 500 ? data.substring(0, 500) + '...' : data if (config.debug) { console.log('\n🐛 DEBUG: JSON Parse Error') console.log(`Status: ${res.statusCode}`) console.log(`Content-Type: ${res.headers['content-type']}`) console.log(`Raw Response (first 1000 chars):`) console.log(data.substring(0, 1000)) console.log('─'.repeat(60)) } reject({ status: res.statusCode, message: 'Failed to parse response as JSON', raw: data, preview: preview, contentType: res.headers['content-type'] }) } }) }) req.on('error', (error) => { reject({ message: 'Network error', error: error.message }) }) if (body) { req.write(JSON.stringify(body)) } req.end() }) } // ===================================================================== // Test Result Tracking // ===================================================================== const results = { passed: 0, failed: 0, skipped: 0, tests: [] } function logTest(name, status, details = '') { const icons = { pass: '✅', fail: '❌', skip: '⏭️' } const icon = icons[status] || '❓' console.log(` ${icon} ${name}${details ? ': ' + details : ''}`) results[status === 'pass' ? 'passed' : status === 'fail' ? 'failed' : 'skipped']++ results.tests.push({ name, status, details }) } // ===================================================================== // Resource-Specific Test Functions // ===================================================================== // --------------------- DOMAINS --------------------- async function testDomains() { console.log('\n📦 Testing Domains API') console.log('─'.repeat(60)) let domainId = null try { // Test: Create domain with all fields const domainInput = { domain_name: `test-${Date.now()}.example.com`, is_valid: true, meta_title: 'Test Domain Title', meta_description: 'This is a test domain for API testing', custom_data: { source: 'automated_test', test_run: Date.now(), notes: 'Created by test suite' }, metadata: { source_system: 'test_suite', created_by: 'test@example.com' } } const created = await makeRequest('POST', '/domains', domainInput) domainId = created.id logTest('Create domain with all fields', 'pass', `ID: ${domainId}`) // Test: Get domain by ID const fetched = await makeRequest('GET', `/domains/${domainId}`) if (fetched.id === domainId && fetched.domain_name === domainInput.domain_name) { logTest('Get domain by ID', 'pass') } else { logTest('Get domain by ID', 'fail', 'Data mismatch') } // Test: Update domain (all fields) const updateAll = { meta_title: 'Updated Test Domain', is_valid: false, custom_data: { source: 'updated', updated_at: new Date().toISOString() }, metadata: { last_updated_by: 'test-update@example.com', last_updated_from: 'test_suite' } } const updated = await makeRequest('PUT', `/domains/${domainId}`, updateAll) if (updated.meta_title === updateAll.meta_title) { logTest('Update domain (all fields)', 'pass') } else { logTest('Update domain (all fields)', 'fail') } // Test: Update domain (partial) const updatePartial = { meta_title: 'Partially Updated Title' } const partialUpdated = await makeRequest('PUT', `/domains/${domainId}`, updatePartial) if (partialUpdated.meta_title === updatePartial.meta_title) { logTest('Update domain (partial)', 'pass') } else { logTest('Update domain (partial)', 'fail') } // Test: List domains with pagination const list = await makeRequest('GET', '/domains?page=0&pageSize=10') if (list.domains && Array.isArray(list.domains)) { logTest('List domains with pagination', 'pass', `Found ${list.total} total`) } else { logTest('List domains with pagination', 'fail') } // Test: List domains with search const searchTerm = domainInput.domain_name.substring(0, 10) const searchResults = await makeRequest('GET', `/domains?searchTerm=${searchTerm}`) logTest('List domains with search', 'pass', `Found ${searchResults.domains?.length || 0} results`) // Test: List domains with filters const filtered = await makeRequest('GET', '/domains?isValid=false') logTest('List domains with filters', 'pass') if (config.noCleanup) { logTest('Delete domain', 'skip', `Skipped - data kept for verification (ID: ${domainId})`) logTest('Verify deletion', 'skip', 'Skipped - cleanup disabled') } else { // Test: Delete domain await makeRequest('DELETE', `/domains/${domainId}`) domainId = null logTest('Delete domain', 'pass') // Verify deletion try { await makeRequest('GET', `/domains/${created.id}`) logTest('Verify deletion', 'fail', 'Domain still exists') } catch (error) { if (error.status === 404) { logTest('Verify deletion', 'pass') } else { logTest('Verify deletion', 'fail', error.message) } } } } catch (error) { const errorMsg = error.message || 'Unknown error' const errorDetails = error.preview ? `\n Response preview: ${error.preview}\n Content-Type: ${error.contentType}` : '' logTest('Domain tests', 'fail', errorMsg + errorDetails) // Cleanup if (domainId && !config.noCleanup) { try { await makeRequest('DELETE', `/domains/${domainId}`) } catch (e) { // Ignore cleanup errors } } } } // --------------------- URLS --------------------- async function testUrls() { console.log('\n🔗 Testing URLs API') console.log('─'.repeat(60)) let urlId = null let domainId = null try { // Create a domain first const domain = await makeRequest('POST', '/domains', { domain_name: `url-test-${Date.now()}.example.com`, is_valid: true }) domainId = domain.id // Test: Create URL with all fields const urlInput = { domain_id: domainId, domain_name: domain.domain_name, url: `https://${domain.domain_name}/test-page-${Date.now()}`, http_status: 200, status_text: 'OK', content_type: 'text/html', page_title: 'Test Page Title', meta_description: 'Test page meta description', canonical_url: `https://${domain.domain_name}/canonical`, custom_data: { load_time: 150, word_count: 500 }, metadata: { source_system: 'test_suite', created_by: 'test@example.com' } } const created = await makeRequest('POST', '/urls', urlInput) urlId = created.id logTest('Create URL with all fields', 'pass', `ID: ${urlId}`) // Test: Get URL by ID const fetched = await makeRequest('GET', `/urls/${urlId}`) if (fetched.id === urlId) { logTest('Get URL by ID', 'pass') } else { logTest('Get URL by ID', 'fail') } // Test: Update URL (all fields) const updateAll = { page_title: 'Updated Page Title', http_status: 404, custom_data: { load_time: 200, updated: true }, metadata: { last_updated_by: 'test-update@example.com' } } await makeRequest('PUT', `/urls/${urlId}`, updateAll) logTest('Update URL (all fields)', 'pass') // Test: Update URL (partial) const updatePartial = { page_title: 'Partially Updated Title' } await makeRequest('PUT', `/urls/${urlId}`, updatePartial) logTest('Update URL (partial)', 'pass') // Test: List URLs with pagination const list = await makeRequest('GET', '/urls?page=0&pageSize=5') logTest('List URLs with pagination', 'pass', `Found ${list.total} total`) // Test: List URLs with status filter const statusFilter = await makeRequest('GET', '/urls?statusFilter=[200,404]') logTest('List URLs with status filter', 'pass') if (config.noCleanup) { logTest('Delete URL', 'skip', `Skipped - data kept for verification (ID: ${urlId})`) } else { // Test: Delete URL await makeRequest('DELETE', `/urls/${urlId}`) urlId = null logTest('Delete URL', 'pass') // Cleanup domain await makeRequest('DELETE', `/domains/${domainId}`) domainId = null } } catch (error) { logTest('URL tests', 'fail', error.message || JSON.stringify(error)) // Cleanup if (!config.noCleanup) { if (urlId) { try { await makeRequest('DELETE', `/urls/${urlId}`) } catch (e) { // Cleanup error ignored } } if (domainId) { try { await makeRequest('DELETE', `/domains/${domainId}`) } catch (e) { // Cleanup error ignored } } } } } // --------------------- CALLS --------------------- async function testCalls() { console.log('\n📞 Testing Calls API') console.log('─'.repeat(60)) let callId = null try { // Test: Create call with all fields const callInput = { transcript: "Agent: Hello, how can I help you today?\nCaller: I have a question about my order.\nAgent: I'd be happy to help with that.", transcript_json: { speakers: ['Agent', 'Caller'], segments: [ { speaker: 'Agent', text: 'Hello, how can I help you today?', timestamp: 0.0 }, { speaker: 'Caller', text: 'I have a question about my order.', timestamp: 2.5 }, { speaker: 'Agent', text: "I'd be happy to help with that.", timestamp: 5.0 } ] }, recording_url: 'https://example.com/recordings/test-123.mp3', duration: 180, call_datetime: new Date().toISOString(), sentiment_score: 7.5, summary: 'Customer inquiry about order status', custom_data: { employee_name: 'John Doe', caller_name: 'Jane Smith', call_type: 'inbound', department: 'support' }, ai_analysis: { topics: ['order', 'inquiry'], intent: 'support' }, ai_coaching: { suggestions: ['Great empathy', 'Could ask more clarifying questions'] }, metadata: { source_system: 'test_suite', created_by: 'test@example.com' } } const created = await makeRequest('POST', '/calls', callInput) callId = created.id logTest('Create call with all fields', 'pass', `ID: ${callId}`) // Test: Get call by ID const fetched = await makeRequest('GET', `/calls/${callId}`) if (fetched.id === callId) { logTest('Get call by ID', 'pass') } else { logTest('Get call by ID', 'fail') } // Test: Update call (all fields) const updateAll = { sentiment_score: 8.0, summary: 'Updated summary', custom_data: { employee_name: 'John Doe', caller_name: 'Jane Smith', call_type: 'inbound', resolution: 'resolved' }, metadata: { last_updated_by: 'test-update@example.com' } } await makeRequest('PUT', `/calls/${callId}`, updateAll) logTest('Update call (all fields)', 'pass') // Test: Update call (partial) const updatePartial = { sentiment_score: 9.0 } await makeRequest('PUT', `/calls/${callId}`, updatePartial) logTest('Update call (partial)', 'pass') // Test: List calls with pagination const list = await makeRequest('GET', '/calls?page=0&pageSize=10') logTest('List calls with pagination', 'pass', `Found ${list.total} total`) // Test: List calls with filters const filtered = await makeRequest('GET', '/calls?sentimentMin=7.0&sentimentMax=10.0&hasTranscript=true') logTest('List calls with sentiment filter', 'pass') // Test: List calls with date range // SKIPPED: Data-sidekick API has a bug parsing date parameters // Error: time zone "zt23:59:59.999z" not recognized // TODO: Re-enable once data-sidekick fixes date parsing // const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(); // const tomorrow = new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(); // const dateFiltered = await makeRequest('GET', `/calls?dateFrom=${encodeURIComponent(yesterday)}&dateTo=${encodeURIComponent(tomorrow)}`); logTest('List calls with date range', 'skip', 'Skipped - data-sidekick date parsing bug') if (config.noCleanup) { logTest('Delete call', 'skip', `Skipped - data kept for verification (ID: ${callId})`) } else { // Test: Delete call await makeRequest('DELETE', `/calls/${callId}`) callId = null logTest('Delete call', 'pass') } } catch (error) { logTest('Call tests', 'fail', error.message || JSON.stringify(error)) // Cleanup if (callId && !config.noCleanup) { try { await makeRequest('DELETE', `/calls/${callId}`) } catch (e) { // Cleanup error ignored } } } } // --------------------- TAGS --------------------- async function testTags() { console.log('\n🏷️ Testing Tags API') console.log('─'.repeat(60)) let tagId = null let parentTagId = null try { // Test: Create parent tag const parentTagInput = { slug: `parent-tag-${Date.now()}`, label: 'Parent Tag', description: 'This is a parent tag for testing', color: '#FF5733', shade: '#FF8C66', parent_id: null, metadata: { source_system: 'test_suite', created_by: 'test@example.com' } } const parentTag = await makeRequest('POST', '/tags', parentTagInput) parentTagId = parentTag.id logTest('Create parent tag', 'pass', `ID: ${parentTagId}`) // Test: Create child tag const childTagInput = { slug: `child-tag-${Date.now()}`, label: 'Child Tag', description: 'This is a child tag for testing', color: '#33FF57', shade: '#66FF8C', parent_id: parentTagId, metadata: { source_system: 'test_suite', created_by: 'test@example.com' } } const childTag = await makeRequest('POST', '/tags', childTagInput) tagId = childTag.id logTest('Create child tag', 'pass', `ID: ${tagId}`) // Test: Get tag by ID const fetched = await makeRequest('GET', `/tags/${tagId}`) if (fetched.id === tagId) { logTest('Get tag by ID', 'pass') } else { logTest('Get tag by ID', 'fail') } // Test: Update tag (tags don't have metadata column) const update = { label: 'Updated Child Tag', description: 'Updated description', color: '#5733FF', shade: '#8C66FF' } await makeRequest('PUT', `/tags/${tagId}`, update) logTest('Update tag', 'pass') // Test: List all tags const list = await makeRequest('GET', '/tags') if (list.tags && Array.isArray(list.tags)) { logTest('List all tags', 'pass', `Found ${list.tags.length} tags`) } else { logTest('List all tags', 'fail') } // Test: Get tag hierarchy const hierarchy = await makeRequest('GET', '/tags/hierarchy') logTest('Get tag hierarchy', 'pass') if (config.noCleanup) { logTest('Delete child tag', 'skip', `Skipped - data kept for verification (ID: ${tagId})`) logTest('Delete parent tag', 'skip', `Skipped - data kept for verification (ID: ${parentTagId})`) } else { // Test: Delete tags await makeRequest('DELETE', `/tags/${tagId}`) tagId = null logTest('Delete child tag', 'pass') await makeRequest('DELETE', `/tags/${parentTagId}`) parentTagId = null logTest('Delete parent tag', 'pass') } } catch (error) { logTest('Tag tests', 'fail', error.message || JSON.stringify(error)) // Cleanup if (!config.noCleanup) { if (tagId) { try { await makeRequest('DELETE', `/tags/${tagId}`) } catch (e) { // Cleanup error ignored } } if (parentTagId) { try { await makeRequest('DELETE', `/tags/${parentTagId}`) } catch (e) { // Cleanup error ignored } } } } } // --------------------- DOCUMENTS --------------------- async function testDocuments() { console.log('\n📄 Testing Documents API') console.log('─'.repeat(60)) // SKIPPED: Data-sidekick API has a bug with store_id column // Error: Could not find the 'store_id' column of 'aai_documents' // We're NOT sending store_id, but the API code tries to use it logTest('Document tests', 'skip', 'Skipped - data-sidekick store_id column bug') /* Commented out until store_id bug is fixed let documentId = null try { // Test: Create document with embedding (don't send store_id - column doesn't exist) const documentInput = { content: 'This is a test document for the Data Engine API. It contains sample content for vector search testing.', embedding: Array.from({ length: 10 }, () => Math.random()), metadata: { title: 'Test Document', source: 'test_suite', category: 'testing', author: 'test@example.com' } } const created = await makeRequest('POST', '/documents', documentInput) documentId = created.id logTest('Create document with embedding', 'pass', `ID: ${documentId}`) // Test: Get document by ID const fetched = await makeRequest('GET', `/documents/${documentId}`) if (fetched.id === documentId) { logTest('Get document by ID', 'pass') } else { logTest('Get document by ID', 'fail') } // Test: Update document (all fields) const updateAll = { content: 'Updated document content with more information', metadata: { title: 'Updated Test Document', source: 'test_suite', updated_at: new Date().toISOString() } } await makeRequest('PUT', `/documents/${documentId}`, updateAll) logTest('Update document (all fields)', 'pass') // Test: Update document (partial) const updatePartial = { metadata: { title: 'Partially Updated Document' } } await makeRequest('PUT', `/documents/${documentId}`, updatePartial) logTest('Update document (partial)', 'pass') // Test: List documents with pagination const list = await makeRequest('GET', '/documents?page=0&pageSize=10') logTest('List documents with pagination', 'pass', `Found ${list.total} total`) // Test: Search documents (vector search) const searchInput = { query_embedding: Array.from({ length: 10 }, () => Math.random()), match_threshold: 0.5, match_count: 5 } const searchResults = await makeRequest('POST', '/documents/search', searchInput) logTest('Search documents (vector search)', 'pass', `Found ${searchResults.count} results`) // Test: Delete document await makeRequest('DELETE', `/documents/${documentId}`) documentId = null logTest('Delete document', 'pass') } catch (error) { logTest('Document tests', 'fail', error.message || JSON.stringify(error)) // Cleanup if (documentId) { try { await makeRequest('DELETE', `/documents/${documentId}`) } catch (e) { // Cleanup error ignored } } } */ } // --------------------- TICKETS --------------------- async function testTickets() { console.log('\n🎫 Testing Tickets API') console.log('─'.repeat(60)) let ticketId = null try { // Test: Create ticket with all fields (including required created_by) const ticketInput = { title: `Test Ticket ${Date.now()}`, description: 'This is a comprehensive test ticket with all fields populated', status: 'open', priority: 'high', assigned_to: 'support@example.com', ticket_type: 'email', escalated: false, tags_array: ['urgent', 'customer', 'test'], created_by: 'test@example.com' } const created = await makeRequest('POST', '/tickets', ticketInput) ticketId = created.id logTest('Create ticket with all fields', 'pass', `ID: ${ticketId}`) // Test: Get ticket by ID const fetched = await makeRequest('GET', `/tickets/${ticketId}`) if (fetched.id === ticketId) { logTest('Get ticket by ID', 'pass') } else { logTest('Get ticket by ID', 'fail') } // Test: Update ticket (all fields - no metadata) const updateAll = { status: 'in_progress', priority: 'urgent', assigned_to: 'senior-support@example.com', tags_array: ['urgent', 'escalated', 'in-progress'] } await makeRequest('PUT', `/tickets/${ticketId}`, updateAll) logTest('Update ticket (all fields)', 'pass') // Test: Update ticket (partial) const updatePartial = { status: 'resolved' } await makeRequest('PUT', `/tickets/${ticketId}`, updatePartial) logTest('Update ticket (partial)', 'pass') // Test: List tickets with pagination const list = await makeRequest('GET', '/tickets?page=0&pageSize=10') logTest('List tickets with pagination', 'pass', `Found ${list.total} total`) // Test: List tickets with filters const filtered = await makeRequest('GET', '/tickets?status=resolved&escalated=false') logTest('List tickets with status filter', 'pass') // Test: List tickets with tags filter const tagsParam = encodeURIComponent('["urgent", "customer"]') const tagFiltered = await makeRequest('GET', `/tickets?tags=${tagsParam}`) logTest('List tickets with tags filter', 'pass') if (config.noCleanup) { logTest('Delete ticket', 'skip', `Skipped - data kept for verification (ID: ${ticketId})`) } else { // Test: Delete ticket await makeRequest('DELETE', `/tickets/${ticketId}`) ticketId = null logTest('Delete ticket', 'pass') } } catch (error) { logTest('Ticket tests', 'fail', error.message || JSON.stringify(error)) // Cleanup if (ticketId && !config.noCleanup) { try { await makeRequest('DELETE', `/tickets/${ticketId}`) } catch (e) { // Cleanup error ignored } } } } // --------------------- CHATS --------------------- async function testChats() { console.log('\n💬 Testing Chats API') console.log('─'.repeat(60)) // SKIPPED: Data-sidekick API has wrong column name // Error: Could not find the 'ai_response' column of 'chat_logs' // We send 'bot_response', but API tries to insert into 'ai_response' logTest('Chat tests', 'skip', 'Skipped - data-sidekick ai_response column bug') /* Commented out until ai_response bug is fixed let chatId = null try { // Test: Create chat with all fields (use bot_response instead of ai_response) const chatInput = { chatbot_name: 'Test Support Bot', ai_model: 'gpt-4', conversation_id: `conv-${Date.now()}`, user_message: 'Hello, I need help with my account', bot_response: "Hello! I'd be happy to help you with your account. What specific issue are you experiencing?", sentiment_score: 7.5, resolution_status: 'in_progress', tags_array: ['support', 'account', 'greeting'] } const created = await makeRequest('POST', '/chats', chatInput) chatId = created.id logTest('Create chat with all fields', 'pass', `ID: ${chatId}`) // Test: Get chat by ID const fetched = await makeRequest('GET', `/chats/${chatId}`) if (fetched.id === chatId) { logTest('Get chat by ID', 'pass') } else { logTest('Get chat by ID', 'fail') } // Test: Update chat (all fields - no metadata) const updateAll = { sentiment_score: 9.0, resolution_status: 'resolved', tags_array: ['support', 'account', 'resolved'] } await makeRequest('PUT', `/chats/${chatId}`, updateAll) logTest('Update chat (all fields)', 'pass') // Test: Update chat (partial) const updatePartial = { sentiment_score: 8.5 } await makeRequest('PUT', `/chats/${chatId}`, updatePartial) logTest('Update chat (partial)', 'pass') // Test: List chats with pagination const list = await makeRequest('GET', '/chats?page=0&pageSize=10') logTest('List chats with pagination', 'pass', `Found ${list.total} total`) // Test: List chats with filters const filtered = await makeRequest('GET', '/chats?chatbotName=Test Support Bot&sentimentMin=7.0') logTest('List chats with filters', 'pass') // Test: Delete chat await makeRequest('DELETE', `/chats/${chatId}`) chatId = null logTest('Delete chat', 'pass') } catch (error) { logTest('Chat tests', 'fail', error.message || JSON.stringify(error)) // Cleanup if (chatId) { try { await makeRequest('DELETE', `/chats/${chatId}`) } catch (e) { // Cleanup error ignored } } } */ } // ===================================================================== // Main Test Runner // ===================================================================== async function testConnectivity() { console.log('\n🔌 Testing connectivity...') try { // Try to hit the base URL to see if it exists const response = await makeRequest('GET', '/domains?pageSize=1') console.log('✅ API is reachable and responding') return true } catch (error) { console.log('❌ API connectivity test failed') console.log(` Status: ${error.status || 'unknown'}`) console.log(` Message: ${error.message}`) if (error.preview) { console.log('\n📄 Response preview:') console.log(error.preview) console.log(`\n Content-Type: ${error.contentType || 'unknown'}`) } if (error.status === 301 || error.status === 302) { console.log('\n💡 Server is redirecting (301/302). Possible issues:') console.log(' 1. Wrong port - Check if server is on port 3000 instead of 4000') console.log(' 2. Wrong protocol - Try https:// instead of http://') console.log('\n Try:') console.log(' --base-url=http://localhost:3000/api/v1/data-engine') if (error.raw && error.raw.includes('localhost:3000')) { console.log('\n ⚠️ Detected redirect to port 3000!') console.log(' Use: --base-url=http://localhost:3000/api/v1/data-engine') } } else if (error.status === 404) { console.log('\n💡 The endpoint returned 404. Possible issues:') console.log(' 1. The Data Engine API is not deployed on this server') console.log(' 2. The base URL is incorrect') console.log(' 3. The routes are not registered') console.log('\n Try checking:') console.log(` - Is the server running at ${config.baseUrl}?`) console.log(' - Is the /data-engine route mounted in packages/server/src/routes/index.ts?') } else if (error.contentType && error.contentType.includes('html')) { console.log('\n💡 Received HTML instead of JSON. Possible issues:') console.log(' 1. Wrong base URL (hitting a web page instead of API)') console.log(' 2. Server error page being returned') console.log(' 3. Authentication redirecting to login page') } return false } } async function runTests() { console.log('\n╔════════════════════════════════════════════════════════════╗') console.log('║ Data Engine API Comprehensive Test Suite ║') console.log('╚════════════════════════════════════════════════════════════╝') console.log(`\n🔧 Configuration:`) console.log(` Base URL: ${config.baseUrl}`) if (config.useM2M) { console.log(` Auth Method: Auth0 M2M (Client Credentials)`) console.log(` Client ID: ${process.env.DATA_SIDEKICK_CLIENT_ID?.substring(0, 10)}...`) } else { console.log(` Auth Method: API Key`) console.log(` API Key: ${config.apiKey.substring(0, 10)}...`) } console.log(` Verbose: ${config.verbose}`) console.log(` Debug: ${config.debug}`) console.log(` Cleanup: ${config.noCleanup ? '❌ Disabled (data will persist)' : '✅ Enabled'}`) if (config.testResource) { console.log(` Testing: ${config.testResource} only`) } // Test connectivity first const isConnected = await testConnectivity() if (!isConnected) { console.log('\n❌ Cannot proceed with tests - API is not reachable') console.log(' Fix the connectivity issues above and try again\n') process.exit(1) } const testSuite = { domains: testDomains, urls: testUrls, calls: testCalls, tags: testTags, documents: testDocuments, tickets: testTickets, chats: testChats } // Run tests if (config.testResource) { if (testSuite[config.testResource]) { await testSuite[config.testResource]() } else { console.error(`\n❌ Unknown resource: ${config.testResource}`) console.error(` Valid resources: ${Object.keys(testSuite).join(', ')}`) process.exit(1) } } else { // Run all tests for (const [name, testFn] of Object.entries(testSuite)) { await testFn() } } // Print summary console.log('\n╔════════════════════════════════════════════════════════════╗') console.log('║ Test Summary ║') console.log('╚════════════════════════════════════════════════════════════╝') console.log(`\n ✅ Passed: ${results.passed}`) console.log(` ❌ Failed: ${results.failed}`) console.log(` ⏭️ Skipped: ${results.skipped}`) console.log(` 📊 Total: ${results.passed + results.failed + results.skipped}`) if (results.failed > 0) { console.log('\n❌ Failed Tests:') results.tests.filter((t) => t.status === 'fail').forEach((t) => console.log(` - ${t.name}: ${t.details}`)) process.exit(1) } else { console.log('\n🎉 All tests passed!') if (config.noCleanup) { console.log('\n💡 Note: Test data was NOT deleted (--no-cleanup mode)') console.log(' You can now verify the data in your database or via the API') console.log(' To clean up manually, run tests again without --no-cleanup') } process.exit(0) } } // ===================================================================== // Execute // ===================================================================== runTests().catch((error) => { console.error('\n💥 Unhandled error:', error) process.exit(1) })