105 lines
4.0 KiB
JavaScript
105 lines
4.0 KiB
JavaScript
/**
|
|
* Test LaborInput Work Breakdown Endpoint
|
|
* Tester at work breakdown endpoint virker som LaborInput.js forventer
|
|
*/
|
|
|
|
const axios = require('axios');
|
|
|
|
const API_BASE = 'http://localhost:4031';
|
|
|
|
async function testWorkBreakdown() {
|
|
console.log('🔨 Testing LaborInput Work Breakdown Endpoint...\n');
|
|
|
|
// Test 1: Valid case number (should return work breakdown)
|
|
console.log('Test 1: Valid case number (e.g., 1050)');
|
|
try {
|
|
const response = await axios.get(`${API_BASE}/api/ordrestyring/case/1050/work-breakdown`);
|
|
|
|
if (response.data.success) {
|
|
console.log('✅ SUCCESS');
|
|
console.log(' - Work breakdown entries:', response.data.work_breakdown?.length || 0);
|
|
console.log(' - Total hours:', response.data.total_hours);
|
|
console.log(' - Total sales price:', response.data.total_sales_price);
|
|
|
|
if (response.data.work_breakdown && response.data.work_breakdown.length > 0) {
|
|
console.log(' - Sample entry:', JSON.stringify(response.data.work_breakdown[0], null, 2));
|
|
}
|
|
} else {
|
|
console.log('❌ FAIL: Response not successful');
|
|
}
|
|
} catch (error) {
|
|
if (error.response?.status === 404) {
|
|
console.log('⚠️ 404: Case 1050 not found (expected if case doesn\'t exist)');
|
|
} else {
|
|
console.error('❌ ERROR:', error.message);
|
|
}
|
|
}
|
|
|
|
console.log('\n---\n');
|
|
|
|
// Test 2: Invalid case number (should return 404)
|
|
console.log('Test 2: Invalid case number (999999)');
|
|
try {
|
|
const response = await axios.get(`${API_BASE}/api/ordrestyring/case/999999/work-breakdown`);
|
|
console.log('❌ FAIL: Should have returned 404');
|
|
} catch (error) {
|
|
if (error.response?.status === 404) {
|
|
console.log('✅ SUCCESS: Returns 404 as expected');
|
|
} else {
|
|
console.error('❌ ERROR:', error.message);
|
|
}
|
|
}
|
|
|
|
console.log('\n---\n');
|
|
|
|
// Test 3: Find a real case number and test it
|
|
console.log('Test 3: Finding a real case number from calendar...');
|
|
try {
|
|
const calendarResponse = await axios.get(`${API_BASE}/api/calendar?limit=50`);
|
|
|
|
if (calendarResponse.data.success && calendarResponse.data.data.length > 0) {
|
|
// Find first entry with a case number
|
|
const entryWithCase = calendarResponse.data.data.find(h => h.case?.caseNumber);
|
|
|
|
if (entryWithCase) {
|
|
const caseNumber = entryWithCase.case.caseNumber;
|
|
console.log(` - Found case number: ${caseNumber}`);
|
|
console.log(` - Case name: ${entryWithCase.case.projectName || 'N/A'}`);
|
|
|
|
// Test work breakdown for this case
|
|
const workResponse = await axios.get(`${API_BASE}/api/ordrestyring/case/${caseNumber}/work-breakdown`);
|
|
|
|
if (workResponse.data.success) {
|
|
console.log('✅ SUCCESS');
|
|
console.log(' - Work breakdown entries:', workResponse.data.work_breakdown?.length || 0);
|
|
console.log(' - Total hours:', workResponse.data.total_hours);
|
|
console.log(' - Case info:', workResponse.data.case_number, '-', workResponse.data.description);
|
|
|
|
// Check format matches LaborInput expectations
|
|
if (workResponse.data.work_breakdown && workResponse.data.work_breakdown.length > 0) {
|
|
const firstEntry = workResponse.data.work_breakdown[0];
|
|
const hasRequiredFields = firstEntry.category && firstEntry.hours !== undefined;
|
|
|
|
if (hasRequiredFields) {
|
|
console.log(' ✅ Format matches LaborInput.js expectations');
|
|
console.log(' Sample:', JSON.stringify(firstEntry, null, 2));
|
|
} else {
|
|
console.log(' ❌ Missing required fields (category, hours)');
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
console.log('⚠️ No calendar entries with case numbers found');
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('❌ ERROR:', error.message);
|
|
}
|
|
|
|
console.log('\n===================================');
|
|
console.log('✅ Work Breakdown Tests Complete!');
|
|
console.log('===================================\n');
|
|
}
|
|
|
|
testWorkBreakdown();
|