95 lines
3.7 KiB
JavaScript
95 lines
3.7 KiB
JavaScript
// Test script for Batch Management API
|
||
// Run with: node test-api.js
|
||
|
||
const BASE_URL = 'http://localhost:4444/api';
|
||
|
||
async function testAPI() {
|
||
console.log('🚀 Testing Batch Management API\n');
|
||
|
||
// Test 1: Create Batch
|
||
console.log('1️⃣ Creating a new batch...');
|
||
try {
|
||
const now = new Date();
|
||
const timestamp = now.getFullYear() +
|
||
String(now.getMonth() + 1).padStart(2, '0') +
|
||
String(now.getDate()).padStart(2, '0') +
|
||
String(now.getHours()).padStart(2, '0') +
|
||
String(now.getMinutes()).padStart(2, '0') +
|
||
String(now.getSeconds()).padStart(2, '0');
|
||
|
||
const createResponse = await fetch(`${BASE_URL}/batch/save`, {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({
|
||
batch_name: `TEST_BATCH_${timestamp}`,
|
||
items: [
|
||
{ mpn: 'CN_M378A5143DB0-CPB_1620', sn: 'U03A00062074544398' },
|
||
{ mpn: 'CN_M378A5143DB0-CPB_1620', sn: 'U03A00062074544399' },
|
||
{ mpn: 'CN_M378A5143DB0-CPB_1620', sn: 'U03A00062074544400' }
|
||
],
|
||
items_mix: [
|
||
{ mpn: 'CN_M378A5143DB0-CPB_1621', sn: 'U03A00062074544403' },
|
||
{ mpn: 'CN_M378A5143DB0-CPB_1622', sn: 'U03A00062074544404' }
|
||
]
|
||
})
|
||
});
|
||
|
||
const createData = await createResponse.json();
|
||
console.log('✅ Batch created:', createData);
|
||
console.log(` - Batch ID: ${createData.batch_id}`);
|
||
console.log(` - Items inserted: ${createData.inserted_items}`);
|
||
console.log(` - Mixed items inserted: ${createData.inserted_mix_items}\n`);
|
||
|
||
const batchId = createData.batch_id;
|
||
|
||
// Test 2: Get All Batches
|
||
console.log('2️⃣ Getting all batches...');
|
||
const getAllResponse = await fetch(`${BASE_URL}/batch/get-all?page=1&limit=10`);
|
||
const getAllData = await getAllResponse.json();
|
||
console.log(`✅ Found ${getAllData.total} batches`);
|
||
console.log(` - Page ${getAllData.page} of ${getAllData.totalPages}\n`);
|
||
|
||
// Test 3: Get Single Batch
|
||
console.log(`3️⃣ Getting batch #${batchId}...`);
|
||
const getSingleResponse = await fetch(`${BASE_URL}/batch/get/${batchId}`);
|
||
const getSingleData = await getSingleResponse.json();
|
||
console.log('✅ Batch details:');
|
||
console.log(` - Batch Name: ${getSingleData.batch_name}`);
|
||
console.log(` - Items: ${getSingleData.items.length}`);
|
||
console.log(` - Mixed Items: ${getSingleData.items_mix.length}`);
|
||
console.log(` - Created At: ${getSingleData.createdAt}\n`);
|
||
|
||
// Test 4: Search Items
|
||
console.log('4️⃣ Searching for items with SN containing "544398"...');
|
||
const searchResponse = await fetch(`${BASE_URL}/items/search?q=544398`);
|
||
const searchData = await searchResponse.json();
|
||
console.log(`✅ Found ${searchData.results.length} items`);
|
||
if (searchData.results.length > 0) {
|
||
console.log(' First result:', searchData.results[0]);
|
||
}
|
||
console.log();
|
||
|
||
// Test 5: Search Batches
|
||
console.log('5️⃣ Searching for batches with "TEST" in name...');
|
||
const searchBatchResponse = await fetch(`${BASE_URL}/batch/get-all?search=TEST`);
|
||
const searchBatchData = await searchBatchResponse.json();
|
||
console.log(`✅ Found ${searchBatchData.total} batches matching "TEST"\n`);
|
||
|
||
// Test 6: Delete Batch
|
||
console.log(`6️⃣ Deleting batch #${batchId}...`);
|
||
const deleteResponse = await fetch(`${BASE_URL}/batch/delete/${batchId}`, {
|
||
method: 'DELETE'
|
||
});
|
||
const deleteData = await deleteResponse.json();
|
||
console.log('✅ Batch deleted:', deleteData);
|
||
console.log(` - Deleted count: ${deleteData.deleted}\n`);
|
||
|
||
console.log('🎉 All tests completed successfully!');
|
||
|
||
} catch (error) {
|
||
console.error('❌ Test failed:', error.message);
|
||
}
|
||
}
|
||
|
||
// Run tests
|
||
testAPI(); |