/** * Test Edge Runtime compatibility for audit logging */ // Test Edge Runtime detection console.log("Testing Edge Runtime compatibility...\n"); // Simulate Edge Runtime environment const originalEdgeRuntime = global.EdgeRuntime; const originalNextRuntime = process.env.NEXT_RUNTIME; console.log("1. Testing in simulated Edge Runtime environment..."); global.EdgeRuntime = "edge"; process.env.NEXT_RUNTIME = "edge"; // Import the audit logging functions const { logAuditEvent, getAuditLogs, AUDIT_ACTIONS, RESOURCE_TYPES } = await import("./src/lib/auditLog.js"); // Test logging in Edge Runtime logAuditEvent({ action: AUDIT_ACTIONS.PROJECT_VIEW, userId: "test-user", resourceType: RESOURCE_TYPES.PROJECT, resourceId: "test-project", details: { test: "edge runtime test" }, }); // Test querying in Edge Runtime const logs = getAuditLogs({ limit: 10 }); console.log(`Queried logs in Edge Runtime: ${logs.length} results`); console.log("2. Testing in simulated Node.js Runtime environment..."); // Restore Node.js environment delete global.EdgeRuntime; delete process.env.NEXT_RUNTIME; // Test logging in Node.js Runtime try { logAuditEvent({ action: AUDIT_ACTIONS.PROJECT_CREATE, userId: "test-user", resourceType: RESOURCE_TYPES.PROJECT, resourceId: "test-project-2", details: { test: "nodejs runtime test" }, }); console.log("Node.js runtime logging: ✅ Success"); } catch (error) { console.log("Node.js runtime logging: ❌ Error:", error.message); } // Test querying in Node.js Runtime try { const nodeLogs = getAuditLogs({ limit: 10 }); console.log( `Node.js runtime querying: ✅ Success (${nodeLogs.length} results)` ); } catch (error) { console.log("Node.js runtime querying: ❌ Error:", error.message); } // Restore original environment if (originalEdgeRuntime !== undefined) { global.EdgeRuntime = originalEdgeRuntime; } else { delete global.EdgeRuntime; } if (originalNextRuntime !== undefined) { process.env.NEXT_RUNTIME = originalNextRuntime; } else { delete process.env.NEXT_RUNTIME; } console.log("\n✅ Edge Runtime compatibility test completed!"); console.log("\nKey points:"); console.log( "- Edge Runtime: Logs to console, returns empty arrays for queries" ); console.log("- Node.js Runtime: Full database functionality"); console.log('- API routes are configured with runtime: "nodejs"'); console.log("- Middleware avoids database operations"); console.log("- Error handling prevents runtime crashes");