65 lines
2.6 KiB
JavaScript
65 lines
2.6 KiB
JavaScript
require('dotenv').config();
|
|
const customerDb = require('./services/customerDb');
|
|
|
|
// Mock process.env.NO_ADMIN for testing purposes if not set or to ensure test consistency
|
|
// We'll use a dummy admin number for this test authentication
|
|
const TEST_ADMIN_NUMBER = '628123456789'; // Format used in code logic check
|
|
process.env.NO_ADMIN = TEST_ADMIN_NUMBER;
|
|
|
|
async function testAdminLogic() {
|
|
console.log("Testing Admin Logic...");
|
|
|
|
// 1. Simulate Message from Admin
|
|
const mockMessage = {
|
|
from: '628123456789@c.us', // Check against NO_ADMIN
|
|
body: 'Cek 0812345678' // Search intent
|
|
};
|
|
|
|
console.log(`\n[Input] Sender: ${mockMessage.from}, Message: "${mockMessage.body}"`);
|
|
|
|
// Logic replicated from index.js for testing isolation
|
|
let customerContext = null;
|
|
let isAdmin = false;
|
|
|
|
const adminNumbers = (process.env.NO_ADMIN || '').split(',').map(n => n.trim());
|
|
const senderNumber = mockMessage.from.replace('@c.us', '');
|
|
|
|
if (adminNumbers.some(admin => senderNumber.endsWith(admin.replace(/^0|^62/, '')))) {
|
|
isAdmin = true;
|
|
}
|
|
|
|
console.log(`[Status] Is Admin? ${isAdmin ? '✅ YES' : '❌ NO'}`);
|
|
|
|
if (isAdmin) {
|
|
const searchPattern = /(\d{9,})|cek|cari|info/i;
|
|
if (searchPattern.test(mockMessage.body)) {
|
|
let query = mockMessage.body.replace(/cek|cari|info/gi, '').trim();
|
|
console.log(`[Action] Searching for: "${query}"`);
|
|
|
|
// Call actual DB service
|
|
// Note: This relies on the DB having the dummy data or at least connecting
|
|
// We will search for a number that might NOT exist, but we check if the function runs
|
|
// To make it pass successfully on the empty DB, we'll search for 'Budi' or just expect 'Not Found' but no error
|
|
|
|
try {
|
|
const searchResults = await customerDb.searchCustomer(query);
|
|
console.log(`[DB Result] Found ${searchResults.length} matches.`);
|
|
|
|
if (searchResults.length > 0) {
|
|
customerContext = `[ADMIN MODE - HASIL PENCARIAN]\n`;
|
|
searchResults.forEach((c, i) => {
|
|
customerContext += `${i + 1}. Nama: ${c.name}\n`;
|
|
});
|
|
console.log("[Context Generated] \n" + customerContext);
|
|
} else {
|
|
console.log("[Context Generated] [ADMIN MODE] Pencarian tidak ditemukan (Expected if DB is empty or no match).");
|
|
}
|
|
} catch (err) {
|
|
console.error("DB Error:", err);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
testAdminLogic().then(() => process.exit());
|