#!/usr/bin/env node /** * Smoke test de connectivite WMS. * * npm test -> teste le profil DEFAULT_WMS_PROFILE * npm test -- LIMAGRAIN -> teste le profil nomme * npm test -- --all -> teste tous les profils declares dans WMS_PROFILES * * Verifie, pour chaque profil : chargement du profil, OAuth, QueryExecute, * QueryScalarExecute et l'API AD. N'ecrit rien dans le WMS. */ const path = require('path'); require('dotenv').config({ path: path.join(__dirname, '..', '.env') }); const profileManager = require('../src/config/profile-manager'); const apiService = require('../src/services/api-service').getInstance(); function ok(label, detail) { console.log(` OK ${label}${detail ? ` - ${detail}` : ''}`); } function ko(label, error) { console.log(` FAIL ${label} - ${error.message}`); } async function step(label, fn) { try { ok(label, await fn()); return true; } catch (error) { ko(label, error); return false; } } async function testProfile(name) { console.log(`\n=== Profil ${name} ===`); profileManager.switchTo(name); const profile = profileManager.getCurrent(); console.log(` host=${profile.host} tenant=${profile.tenant} saas=${profile.saas}`); let passed = 0; const total = 4; if (await step('OAuth', async () => { await apiService.authenticate(); return `token obtenu (age max ~${process.env.TOKEN_MAX_AGE || 1190}s)`; })) passed++; if (await step('QueryExecute', async () => { const rows = await apiService.executeQuery('Context.Products.OrderBy(z => z.Id)', { take: 1 }); return `${Array.isArray(rows) ? rows.length : 0} ligne(s)`; })) passed++; if (await step('QueryScalarExecute', async () => { const count = await apiService.executeScalarQuery('Context.Products.Count()'); return `${count} produit(s)`; })) passed++; if (await step('AD API (Validator)', async () => { const res = await apiService.post('/Validator/GetByApplication', [profile.application, profile.tenant, 10, 0], true); return `${res?.entities?.length ?? 0} element(s)`; })) passed++; console.log(` -> ${passed}/${total} tests reussis`); return passed === total; } async function main() { profileManager.loadProfiles(); const available = profileManager.listProfiles(); if (available.length === 0) { console.error('Aucun profil charge. Verifiez WMS_PROFILES et _HOST/USERNAME/PASSWORD/TENANT dans .env'); process.exit(1); } const arg = process.argv[2]; let targets; if (arg === '--all') { targets = available; } else if (arg) { targets = [arg]; } else { targets = [profileManager.getCurrentName() || available[0]]; } let allOk = true; for (const name of targets) { try { allOk = (await testProfile(name)) && allOk; } catch (error) { console.log(`\n=== Profil ${name} ===\n FAIL ${error.message}`); allOk = false; } } console.log(`\n${allOk ? 'Tous les profils testes sont operationnels.' : 'Au moins un test a echoue (voir ci-dessus).'}`); process.exit(allOk ? 0 : 1); } main();