helpyourneighbour/test-dispute-flow.js

103 lines
3 KiB
JavaScript
Raw Permalink Normal View History

const { DisputeService } = require('./backend/src/disputes/dispute-service');
const { DB } = require('./backend/src/db');
// Mock database for testing
class MockDB {
constructor() {
this.queries = [];
}
async query(sql, params) {
this.queries.push({ sql, params });
// Return mock results based on SQL
if (sql.includes('INSERT INTO disputes')) {
return { insertId: 1 };
}
if (sql.includes('SELECT * FROM disputes WHERE id = ?')) {
return [[{
id: 1,
deal_id: 123,
opened_by_user_id: 456,
status: 'open',
reason_code: 'NO_SHOW',
summary: 'Test dispute',
requested_outcome: 'refund',
created_at: '2026-03-19T14:00:00Z',
updated_at: '2026-03-19T14:00:00Z'
}]];
}
if (sql.includes('SELECT * FROM dispute_events WHERE dispute_id = ?')) {
return [[]];
}
return [];
}
}
// Test the dispute service
async function testDisputeService() {
console.log('Testing Dispute Service...');
const mockDB = new MockDB();
const disputeService = new DisputeService(mockDB);
try {
// Test creating a dispute
console.log('1. Testing createDispute...');
const dispute = await disputeService.createDispute({
deal_id: 123,
opened_by_user_id: 456,
reason_code: 'NO_SHOW',
summary: 'Test dispute',
requested_outcome: 'refund'
});
console.log('✓ Dispute created successfully');
console.log('Dispute ID:', dispute.id);
// Test getting a dispute
console.log('2. Testing getDisputeById...');
const retrievedDispute = await disputeService.getDisputeById(1);
console.log('✓ Dispute retrieved successfully');
console.log('Status:', retrievedDispute.status);
// Test updating status
console.log('3. Testing updateDisputeStatus...');
await disputeService.updateDisputeStatus(1, 'evidence', 456);
console.log('✓ Status updated successfully');
// Test adding evidence
console.log('4. Testing addEvidence...');
await disputeService.addEvidence(1, 456, { file: 'test.jpg' });
console.log('✓ Evidence added successfully');
// Test resolving dispute
console.log('5. Testing resolveDispute...');
await disputeService.resolveDispute(1, 456, 'refund', 'User did not show up');
console.log('✓ Dispute resolved successfully');
// Test getting events
console.log('6. Testing getDisputeEvents...');
const events = await disputeService.getDisputeEvents(1);
console.log('✓ Events retrieved successfully');
console.log('Number of events:', events.length);
console.log('\n🎉 All tests passed!');
} catch (error) {
console.error('❌ Test failed:', error.message);
return false;
}
return true;
}
// Run the test
testDisputeService().then(success => {
if (success) {
console.log('\n✅ All tests completed successfully');
} else {
console.log('\n❌ Some tests failed');
process.exit(1);
}
});