89 lines
3.6 KiB
PHP
89 lines
3.6 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/app/bootstrap.php';
|
|
|
|
use App\Core\Database;
|
|
use App\Models\ConversationState;
|
|
use App\Core\Flows\ConversationFlowEngine;
|
|
|
|
echo "=== Starting Conversation Flow Engine Mock Test ===\n";
|
|
|
|
// 1. Ensure table exists
|
|
echo "Ensuring conversation_states table exists...\n";
|
|
ConversationState::ensureTableExists();
|
|
echo "Table ensured.\n";
|
|
|
|
// 2. Fetch a company and session to mock with
|
|
$company = Database::selectOne("SELECT * FROM companies LIMIT 1");
|
|
if (!$company) {
|
|
echo "No company found in database. Creating a mock company...\n";
|
|
$companyId = Database::insert("INSERT INTO companies (name) VALUES (?)", ["Mock Test Company"]);
|
|
$company = Database::selectOne("SELECT * FROM companies WHERE id = ?", [$companyId]);
|
|
}
|
|
echo "Using Company ID: {$company['id']} ({$company['name']})\n";
|
|
|
|
$session = Database::selectOne("SELECT * FROM whatsapp_sessions WHERE company_id = ? LIMIT 1", [$company['id']]);
|
|
if (!$session) {
|
|
echo "No WhatsApp session found for company. Creating a mock session...\n";
|
|
$sessionId = Database::insert(
|
|
"INSERT INTO whatsapp_sessions (company_id, name, session_key, status) VALUES (?, ?, ?, ?)",
|
|
[$company['id'], "Test Session", "mock_test_session_" . rand(100, 999), "connected"]
|
|
);
|
|
$session = Database::selectOne("SELECT * FROM whatsapp_sessions WHERE id = ?", [$sessionId]);
|
|
}
|
|
echo "Using WhatsApp Session Key: {$session['session_key']}\n";
|
|
|
|
// Clean existing states for test phone
|
|
$testPhone = "+962799999999";
|
|
$hash = \App\Core\Security::blindIndex($testPhone);
|
|
Database::execute("DELETE FROM conversation_states WHERE company_id = ? AND contact_phone_hash = ?", [$company['id'], $hash]);
|
|
echo "Cleaned any old state for test phone: {$testPhone}\n";
|
|
|
|
// Helper to simulate incoming message
|
|
function simulateIncoming(array $session, string $phone, string $body) {
|
|
echo "\n--- SIMULATING INCOMING: '$body' from $phone ---\n";
|
|
|
|
// We will bypass actual network calls by mocking the Curl response inside ConversationFlowEngine's sendReply or using a local test double.
|
|
// Wait, ConversationFlowEngine calls WHATSAPP_GATEWAY_URL to send messages. In our local test environment, that gateway might not be active,
|
|
// or it might return error. To prevent Curl from throwing or blocking, we can set WHATSAPP_GATEWAY_URL to a mock, or we can check the result.
|
|
// Let's see what happens when we process:
|
|
$msgData = [
|
|
'phone' => $phone,
|
|
'body' => $body,
|
|
'id' => 'msg_' . uniqid()
|
|
];
|
|
|
|
$handled = ConversationFlowEngine::processMessage($session, $msgData);
|
|
echo "Handled by Engine: " . ($handled ? "YES" : "NO") . "\n";
|
|
|
|
// Print current state in DB
|
|
$state = ConversationState::findActive($session['company_id'], $phone);
|
|
if ($state) {
|
|
echo "Current DB State: Step = '{$state['current_step']}', Context = '{$state['context_data']}'\n";
|
|
} else {
|
|
echo "Current DB State: [No Active Flow / Finished]\n";
|
|
}
|
|
}
|
|
|
|
// 3. Run test cases
|
|
// Case A: Message not triggering anything
|
|
simulateIncoming($session, $testPhone, "مرحبا");
|
|
|
|
// Case B: Trigger keyword "اختبار"
|
|
simulateIncoming($session, $testPhone, "اختبار");
|
|
|
|
// Case C: Provide name "احمد"
|
|
simulateIncoming($session, $testPhone, "احمد");
|
|
|
|
// Case D: Provide invalid rating "10"
|
|
simulateIncoming($session, $testPhone, "10");
|
|
|
|
// Case E: Provide valid rating "5"
|
|
simulateIncoming($session, $testPhone, "5");
|
|
|
|
// Case F: Start again and test cancellation
|
|
simulateIncoming($session, $testPhone, "اختبار");
|
|
simulateIncoming($session, $testPhone, "إلغاء");
|
|
|
|
echo "\n=== Mock Test Complete ===\n";
|