test: tests unitaires socketio-adapter (8 tests, bun test)
- Injection socket factory pour testabilité (2e param constructeur) - Tests : état initial, connect, login_error, connect_error, reconnecting, reconnexion, logoff, onStateChange - Script test ajouté dans package.json
This commit is contained in:
@@ -8,11 +8,28 @@
|
||||
const io = require('socket.io-client');
|
||||
|
||||
class SocketIOAdapter {
|
||||
constructor(serverUrl) {
|
||||
constructor(serverUrl, socketFactory = null) {
|
||||
this.serverUrl = serverUrl;
|
||||
this._socketFactory = socketFactory || io;
|
||||
this.socket = null;
|
||||
this._state = 'disconnected'; // disconnected, connecting, connected, error
|
||||
this._state = 'disconnected'; // disconnected, connecting, connected, reconnecting, error
|
||||
this._eventHandlers = new Map();
|
||||
this._onStateChange = null; // callback pour notifier main.js des changements d'etat
|
||||
}
|
||||
|
||||
/**
|
||||
* Enregistrer un callback de changement d'etat.
|
||||
* @param {function(string)} callback - recoit le nouvel etat
|
||||
*/
|
||||
onStateChange(callback) {
|
||||
this._onStateChange = callback;
|
||||
}
|
||||
|
||||
_setState(state) {
|
||||
this._state = state;
|
||||
if (this._onStateChange) {
|
||||
this._onStateChange(state);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -22,9 +39,9 @@ class SocketIOAdapter {
|
||||
*/
|
||||
connect(accessCode, password, terminal) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._state = 'connecting';
|
||||
this._setState('connecting');
|
||||
|
||||
this.socket = io(this.serverUrl, {
|
||||
this.socket = this._socketFactory(this.serverUrl, {
|
||||
auth: { access_code: accessCode, password, terminal },
|
||||
transports: ['websocket'],
|
||||
reconnection: true,
|
||||
@@ -39,7 +56,7 @@ class SocketIOAdapter {
|
||||
this.socket.once('login_ok', (data) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
this._state = 'connected';
|
||||
this._setState('connected');
|
||||
resolve(data);
|
||||
});
|
||||
|
||||
@@ -47,7 +64,7 @@ class SocketIOAdapter {
|
||||
this.socket.once('login_error', (data) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
this._state = 'error';
|
||||
this._setState('error');
|
||||
this.socket.disconnect();
|
||||
reject(new Error(data.message || 'Authentification refusee'));
|
||||
});
|
||||
@@ -56,23 +73,41 @@ class SocketIOAdapter {
|
||||
this.socket.on('connect_error', (err) => {
|
||||
if (!settled) {
|
||||
settled = true;
|
||||
this._state = 'error';
|
||||
this._setState('error');
|
||||
this.socket.disconnect();
|
||||
reject(new Error(err.message || 'Connexion refusee'));
|
||||
}
|
||||
// Apres login reussi : reconnexion auto geree par socket.io
|
||||
// Apres login reussi : reconnexion auto, on passe en 'reconnecting'
|
||||
});
|
||||
|
||||
// Timeout de connexion initiale (15s)
|
||||
setTimeout(() => {
|
||||
if (!settled) {
|
||||
settled = true;
|
||||
this._state = 'error';
|
||||
this._setState('error');
|
||||
this.socket.disconnect();
|
||||
reject(new Error('Timeout de connexion au serveur'));
|
||||
}
|
||||
}, 15000);
|
||||
|
||||
// === Handlers de reconnexion (actifs apres le premier login) ===
|
||||
|
||||
// Deconnexion temporaire → passer en 'reconnecting'
|
||||
this.socket.on('disconnect', (reason) => {
|
||||
if (this._state === 'connected') {
|
||||
// Deconnexion temporaire, socket.io va retenter
|
||||
this._setState('reconnecting');
|
||||
}
|
||||
});
|
||||
|
||||
// Reconnexion reussie → le serveur recoit un nouveau connect avec les memes auth
|
||||
// et emet 'login_ok' (reprise de session)
|
||||
this.socket.on('login_ok', (data) => {
|
||||
if (settled && this._state === 'reconnecting') {
|
||||
this._setState('connected');
|
||||
}
|
||||
});
|
||||
|
||||
// Restaurer les handlers enregistres avant connect
|
||||
this._eventHandlers.forEach((handler, event) => {
|
||||
this.socket.on(event, handler);
|
||||
@@ -87,13 +122,13 @@ class SocketIOAdapter {
|
||||
logoff() {
|
||||
return new Promise((resolve) => {
|
||||
if (!this.socket || !this.socket.connected) {
|
||||
this._state = 'disconnected';
|
||||
this._setState('disconnected');
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
this.socket.once('logout_ok', () => {
|
||||
this._state = 'disconnected';
|
||||
this._setState('disconnected');
|
||||
resolve();
|
||||
});
|
||||
|
||||
@@ -104,7 +139,7 @@ class SocketIOAdapter {
|
||||
if (this.socket) {
|
||||
this.socket.disconnect();
|
||||
}
|
||||
this._state = 'disconnected';
|
||||
this._setState('disconnected');
|
||||
resolve();
|
||||
}, 5000);
|
||||
});
|
||||
@@ -117,7 +152,7 @@ class SocketIOAdapter {
|
||||
if (this.socket) {
|
||||
this.socket.disconnect();
|
||||
}
|
||||
this._state = 'disconnected';
|
||||
this._setState('disconnected');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user