Files
btpxpress-frontend/scripts/test-api.js
2025-10-13 05:29:32 +02:00

104 lines
3.5 KiB
JavaScript

// Script de test pour vérifier la connectivité API et créer des données de test
const API_BASE_URL = 'http://localhost:8080/api/v1';
async function testAPI() {
console.log('🚀 Test de connectivité API BTPXpress...\n');
try {
// Test 1: Vérifier la santé de l'API
console.log('1. Test de santé de l\'API...');
const healthResponse = await fetch(`${API_BASE_URL}/health`);
console.log(` Status: ${healthResponse.status}`);
// Test 2: Lister les chantiers
console.log('\n2. Test GET /api/v1/chantiers...');
const chantiersResponse = await fetch(`${API_BASE_URL}/chantiers`);
console.log(` Status: ${chantiersResponse.status}`);
const chantiers = await chantiersResponse.json();
console.log(` Nombre de chantiers: ${chantiers.length}`);
// Test 3: Lister les clients
console.log('\n3. Test GET /api/clients...');
const clientsResponse = await fetch(`${API_BASE_URL}/clients`);
console.log(` Status: ${clientsResponse.status}`);
const clients = await clientsResponse.json();
console.log(` Nombre de clients: ${clients.length}`);
// Test 4: Créer un client de test si aucun n'existe
if (clients.length === 0) {
console.log('\n4. Création d\'un client de test...');
const clientData = {
nom: 'Client Test',
email: 'test@btpxpress.dev',
telephone: '0123456789',
adresse: '123 Rue de Test',
ville: 'Paris',
codePostal: '75001',
typeClient: 'PARTICULIER',
actif: true
};
const createClientResponse = await fetch(`${API_BASE_URL}/clients`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(clientData)
});
console.log(` Status: ${createClientResponse.status}`);
if (createClientResponse.ok) {
const newClient = await createClientResponse.json();
console.log(` Client créé avec ID: ${newClient.id}`);
}
}
// Test 5: Créer un chantier de test si aucun n'existe
if (chantiers.length === 0 && clients.length > 0) {
console.log('\n5. Création d\'un chantier de test...');
const chantierData = {
nom: 'Chantier Test',
description: 'Chantier de démonstration',
adresse: '456 Avenue de Test',
ville: 'Lyon',
codePostal: '69001',
clientId: clients[0].id,
statut: 'EN_PREPARATION',
typeChantier: 'CONSTRUCTION_NEUVE',
dateDebutPrevue: new Date().toISOString(),
dateFinPrevue: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString(),
budgetPrevisionnel: 50000,
pourcentageAvancement: 0,
actif: true
};
const createChantierResponse = await fetch(`${API_BASE_URL}/chantiers`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(chantierData)
});
console.log(` Status: ${createChantierResponse.status}`);
if (createChantierResponse.ok) {
const newChantier = await createChantierResponse.json();
console.log(` Chantier créé avec ID: ${newChantier.id}`);
}
}
console.log('\n✅ Tests API terminés avec succès !');
} catch (error) {
console.error('\n❌ Erreur lors des tests API:', error);
}
}
// Exécuter les tests si le script est appelé directement
if (typeof window === 'undefined') {
testAPI();
}
module.exports = { testAPI };