const fs = require('fs');
const path = require('path');
console.log('🏦 Testing Bank Leumi Account Selection Logic\n');
// Mock the exact Bank Leumi data structure from /users/plain-sub-users API
const bankLeumiCustomerData = {
customerDisplayName: "Bank Leumi",
divisionData: [
{
accountName: "Reseller-1",
accountKey: "22676",
divisionId: 139,
awsAccountId: "696314371547",
accountType: "Shared",
description: "Primary Shared Account"
},
{
accountName: "Allcloud-AWS-Reseller-329 - BL Test Env",
accountKey: "24223",
divisionId: 1,
description: "Test Environment Account"
}
]
};
function testAccountSelection(query, expectedAccountKey) {
console.log(`🔍 Testing query: "${query}"`);
console.log(` Expected account key: ${expectedAccountKey}\n`);
const queryLower = query.toLowerCase();
const queryWords = queryLower.split(/[\s-]+/).filter(w => w.length > 2);
console.log(` Query words: [${queryWords.join(', ')}]`);
let selectedAccount = null;
let bestMatchScore = 0;
let matchDetails = [];
// Test account selection within Bank Leumi customer
bankLeumiCustomerData.divisionData.forEach((account, index) => {
console.log(`\n Account ${index + 1}: "${account.accountName}" (key: ${account.accountKey})`);
const accountNameLower = account.accountName.toLowerCase();
const accountWords = accountNameLower.split(/[\s-]+/).filter(w => w.length > 2);
console.log(` Account words: [${accountWords.join(', ')}]`);
// Check for word matches
const matchingWords = accountWords.filter(accountWord =>
queryWords.some(queryWord =>
queryWord.includes(accountWord) ||
accountWord.includes(queryWord) ||
queryWord === accountWord
)
);
console.log(` Matching words: [${matchingWords.join(', ')}]`);
// Apply the three matching conditions from our detection logic
const fullNameMatch = queryLower.includes(accountNameLower);
const wordCountMatch = matchingWords.length >= Math.min(2, accountWords.length);
const partialMatch = accountNameLower.split(/[\s-]+/).some(part => part.length > 3 && queryLower.includes(part));
console.log(` Full name match: ${fullNameMatch}`);
console.log(` Word count match (${matchingWords.length} >= min(2, ${accountWords.length})): ${wordCountMatch}`);
console.log(` Partial match: ${partialMatch}`);
const isMatch = fullNameMatch || wordCountMatch || partialMatch;
const matchScore = accountWords.length > 0 ? matchingWords.length / accountWords.length : 0;
console.log(` Match score: ${matchScore.toFixed(2)}`);
console.log(` RESULT: ${isMatch ? '✅ MATCH' : '❌ NO MATCH'}`);
matchDetails.push({
account,
isMatch,
matchScore,
matchingWords,
fullNameMatch,
wordCountMatch,
partialMatch
});
if (isMatch && matchScore > bestMatchScore) {
selectedAccount = account;
bestMatchScore = matchScore;
}
});
console.log(`\n🎯 FINAL SELECTION:`);
if (selectedAccount) {
console.log(` Selected: "${selectedAccount.accountName}" (key: ${selectedAccount.accountKey})`);
console.log(` Match score: ${bestMatchScore.toFixed(2)}`);
if (selectedAccount.accountKey === expectedAccountKey) {
console.log(` ✅ CORRECT! Selected expected account key ${expectedAccountKey}`);
} else {
console.log(` ❌ WRONG! Expected ${expectedAccountKey}, got ${selectedAccount.accountKey}`);
console.log(` 🚨 This is the BUG we need to fix!`);
}
} else {
console.log(` ❌ NO ACCOUNT SELECTED`);
console.log(` 🚨 This would fall back to default account`);
}
return { selectedAccount, matchDetails, bestMatchScore };
}
console.log('=' .repeat(80));
console.log('🧪 Testing Different Query Patterns\n');
// Test Case 1: Should select BL Test Env (24223)
console.log('TEST 1: Bank Leumi Test Environment Query');
console.log('=' .repeat(50));
testAccountSelection("show me Bank Leumi Test Env costs last 3 months", "24223");
console.log('\n\n');
// Test Case 2: Should select BL Test Env (24223)
console.log('TEST 2: Different Test Environment Query');
console.log('=' .repeat(50));
testAccountSelection("Bank Leumi test environment costs", "24223");
console.log('\n\n');
// Test Case 3: Should select Reseller-1 (22676) - general query
console.log('TEST 3: General Bank Leumi Query (should default to primary)');
console.log('=' .repeat(50));
testAccountSelection("show me Bank Leumi costs", "22676");
console.log('\n\n');
// Test Case 4: Should select BL Test Env (24223)
console.log('TEST 4: Explicit BL Test Env Query');
console.log('=' .repeat(50));
testAccountSelection("BL Test Env monthly costs", "24223");
console.log('\n\n');
function analyzeIssue() {
console.log('🔬 ANALYSIS: Why "Test Env" might not match "BL Test Env"\n');
const query = "Bank Leumi Test Env costs";
const accountName = "Allcloud-AWS-Reseller-329 - BL Test Env";
const queryWords = query.toLowerCase().split(/[\s-]+/).filter(w => w.length > 2);
const accountWords = accountName.toLowerCase().split(/[\s-]+/).filter(w => w.length > 2);
console.log(`Query words: [${queryWords.join(', ')}]`);
console.log(`Account words: [${accountWords.join(', ')}]`);
console.log('\nWord-by-word matching:');
queryWords.forEach(qWord => {
console.log(` "${qWord}" matches:`);
accountWords.forEach(aWord => {
const matches = qWord.includes(aWord) || aWord.includes(qWord) || qWord === aWord;
console.log(` "${aWord}": ${matches ? '✅' : '❌'}`);
});
});
const matchingWords = accountWords.filter(accountWord =>
queryWords.some(queryWord =>
queryWord.includes(accountWord) ||
accountWord.includes(queryWord) ||
queryWord === accountWord
)
);
console.log(`\nMatching words found: [${matchingWords.join(', ')}]`);
console.log(`Match count: ${matchingWords.length}`);
console.log(`Required matches: ${Math.min(2, accountWords.length)}`);
console.log(`Word count match: ${matchingWords.length >= Math.min(2, accountWords.length)}`);
// Check individual conditions
const fullNameMatch = query.toLowerCase().includes(accountName.toLowerCase());
const partialMatch = accountName.toLowerCase().split(/[\s-]+/).some(part =>
part.length > 3 && query.toLowerCase().includes(part)
);
console.log(`\nOther match conditions:`);
console.log(`Full name match: ${fullNameMatch}`);
console.log(`Partial match: ${partialMatch}`);
const finalMatch = fullNameMatch || (matchingWords.length >= Math.min(2, accountWords.length)) || partialMatch;
console.log(`\nFinal match result: ${finalMatch ? '✅ SHOULD MATCH' : '❌ NO MATCH'}`);
}
analyzeIssue();
console.log('\n💡 POTENTIAL SOLUTIONS:');
console.log('1. Lower the word match threshold for "test" + "env" combinations');
console.log('2. Add special handling for "test env" → "bl test env" mappings');
console.log('3. Improve partial word matching for abbreviated forms like "BL"');
console.log('4. Add fuzzy matching for test environment keywords');