203 lines
6.2 KiB
JavaScript
203 lines
6.2 KiB
JavaScript
#!/usr/bin/env node
|
||
|
||
/**
|
||
* Test Enhanced Job Matching Implementation
|
||
* Tests the complete job matching workflow with actual database integration
|
||
*/
|
||
|
||
const mongoose = require('mongoose');
|
||
const { SatLocLogParser } = require('./helpers/satloc_log_parser');
|
||
const SatLocApplicationProcessor = require('./helpers/satloc_application_processor');
|
||
|
||
// Mock database records for testing
|
||
async function setupTestData() {
|
||
// Import models
|
||
const Vehicle = require('./model/vehicle');
|
||
const Job = require('./model/job');
|
||
const JobAssignment = require('./model/job_assignment');
|
||
const User = require('./model/user');
|
||
|
||
console.log('📋 Setting up test data...');
|
||
|
||
// Create test vehicle
|
||
const testVehicle = await Vehicle.create({
|
||
name: 'Test Aircraft N619LF',
|
||
aircraftId: 'N619LF',
|
||
tailNumber: 'N619LF',
|
||
serialNumber: 'SN619LF',
|
||
vehicleType: 'aircraft',
|
||
active: true,
|
||
createdDate: new Date()
|
||
});
|
||
|
||
// Create test user/pilot
|
||
const testUser = await User.create({
|
||
name: 'Test Pilot Lerch',
|
||
email: 'lerch@test.com',
|
||
active: true,
|
||
partnerInfo: {
|
||
partnerAircraftId: 'N619LF',
|
||
partnerId: 'SATLOC'
|
||
},
|
||
createdDate: new Date()
|
||
});
|
||
|
||
// Create test job matching the SatLoc job ID
|
||
const testJob = await Job.create({
|
||
name: 'Field 213150 Spray Job',
|
||
description: 'Test job for SatLoc job ID 213150',
|
||
satlocJobId: '213150', // This should match the SatLoc log
|
||
active: true,
|
||
createdDate: new Date()
|
||
});
|
||
|
||
// Create job assignment
|
||
const testAssignment = await JobAssignment.create({
|
||
jobId: testJob._id,
|
||
userId: testUser._id,
|
||
vehicleId: testVehicle._id,
|
||
status: 'UPLOADED', // Use string instead of constant for this test
|
||
active: true,
|
||
createdDate: new Date()
|
||
});
|
||
|
||
console.log(`✅ Created test vehicle: ${testVehicle._id}`);
|
||
console.log(`✅ Created test user: ${testUser._id}`);
|
||
console.log(`✅ Created test job: ${testJob._id} (${testJob.name})`);
|
||
console.log(`✅ Created test assignment: ${testAssignment._id}`);
|
||
|
||
return {
|
||
vehicle: testVehicle,
|
||
user: testUser,
|
||
job: testJob,
|
||
assignment: testAssignment
|
||
};
|
||
}
|
||
|
||
async function cleanupTestData(testData) {
|
||
console.log('\n🧹 Cleaning up test data...');
|
||
|
||
const { vehicle, user, job, assignment } = testData;
|
||
|
||
await JobAssignment.deleteOne({ _id: assignment._id });
|
||
await Job.deleteOne({ _id: job._id });
|
||
await User.deleteOne({ _id: user._id });
|
||
await Vehicle.deleteOne({ _id: vehicle._id });
|
||
|
||
console.log('✅ Test data cleaned up');
|
||
}
|
||
|
||
async function testEnhancedJobMatching() {
|
||
console.log('=== Enhanced Job Matching Test ===\n');
|
||
|
||
try {
|
||
// Connect to database
|
||
await mongoose.connect('mongodb://localhost:27017/test_agmission');
|
||
console.log('📡 Connected to test database\n');
|
||
|
||
// Setup test data
|
||
const testData = await setupTestData();
|
||
|
||
// Test file with known job information
|
||
const testFile = './test-logs/Liquid_IF2_G4.log';
|
||
|
||
console.log('\n🔍 Testing Enhanced Job Matching...');
|
||
console.log(`📁 Test file: ${testFile}`);
|
||
|
||
// Step 1: Quick parse to extract job information
|
||
console.log('\n1️⃣ Extracting job information from SatLoc log...');
|
||
const parser = new SatLocLogParser({ outputAllRecords: true });
|
||
const parseResult = await parser.parseFile(testFile);
|
||
|
||
if (!parseResult.success) {
|
||
throw new Error(`Parse failed: ${parseResult.error}`);
|
||
}
|
||
|
||
// Extract job information
|
||
const satlocJobIds = new Set();
|
||
let aircraftInfo = null;
|
||
|
||
parseResult.records.forEach(record => {
|
||
if (record.recordType === 120 && record.jobId) {
|
||
satlocJobIds.add(record.jobId);
|
||
}
|
||
if (record.recordType === 100 && !aircraftInfo) {
|
||
aircraftInfo = {
|
||
aircraftId: record.aircraftId,
|
||
pilotName: record.pilotName
|
||
};
|
||
}
|
||
});
|
||
|
||
console.log(` ✅ Aircraft ID: ${aircraftInfo?.aircraftId}`);
|
||
console.log(` ✅ SatLoc Job IDs: ${Array.from(satlocJobIds).join(', ')}`);
|
||
|
||
// Step 2: Test the enhanced job matching
|
||
console.log('\n2️⃣ Testing Application Processor job matching...');
|
||
const processor = new SatLocApplicationProcessor();
|
||
|
||
const contextData = {
|
||
userId: testData.user._id,
|
||
jobId: null, // Let the processor find the job
|
||
uploadedDate: new Date(),
|
||
meta: {
|
||
source: 'enhanced_job_matching_test',
|
||
originalFilename: 'Liquid_IF2_G4.log'
|
||
}
|
||
};
|
||
|
||
// Process with enhanced job matching
|
||
const result = await processor.processLogFile(
|
||
{ filePath: testFile },
|
||
contextData
|
||
);
|
||
|
||
console.log(' ✅ Processing completed');
|
||
console.log(` 📊 Applications created: ${result.applications ? result.applications.length : 1}`);
|
||
console.log(` 📋 Total details: ${result.totalDetails}`);
|
||
|
||
// Step 3: Verify job matching results
|
||
console.log('\n3️⃣ Verifying job matching results...');
|
||
|
||
if (result.applications) {
|
||
// Multi-job result
|
||
result.applications.forEach((app, index) => {
|
||
console.log(` App ${index + 1}: ${app._id}`);
|
||
console.log(` Job ID: ${app.jobId}`);
|
||
console.log(` Status: ${app.status}`);
|
||
});
|
||
} else if (result.application) {
|
||
// Single job result
|
||
console.log(` Application: ${result.application._id}`);
|
||
console.log(` Job ID: ${result.application.jobId}`);
|
||
console.log(` Status: ${result.application.status}`);
|
||
}
|
||
|
||
// Step 4: Check if job mapping was created
|
||
console.log('\n4️⃣ Checking job mapping...');
|
||
const Job = require('./model/job');
|
||
const updatedJob = await Job.findById(testData.job._id);
|
||
|
||
if (updatedJob.satlocJobId) {
|
||
console.log(` ✅ Job mapping exists: ${updatedJob.satlocJobId}`);
|
||
console.log(` 📝 Job name: ${updatedJob.name}`);
|
||
} else {
|
||
console.log(' ⚠️ No job mapping found');
|
||
}
|
||
|
||
// Cleanup test data
|
||
await cleanupTestData(testData);
|
||
|
||
console.log('\n🎉 Enhanced job matching test completed successfully!');
|
||
|
||
} catch (error) {
|
||
console.error('❌ Test failed:', error.message);
|
||
console.error(error.stack);
|
||
} finally {
|
||
await mongoose.disconnect();
|
||
console.log('📡 Disconnected from database');
|
||
}
|
||
}
|
||
|
||
// Run the test
|
||
testEnhancedJobMatching().catch(console.error); |