Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 1x 1x 1x 1x 12x 12x 6x 6x 12x 12x 1x 1x 7x 7x 7x 7x 6x 7x 6x 6x 6x 6x 7x 1x 1x 5x 7x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 7x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 4x 1x 1x 3x 3x 3x 3x 3x 1x 1x 1x 3x 3x 3x 3x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 2x 3x 1x 1x 1x 3x 1x 1x 1x 1x 9x 1x 1x 1x 8x 9x 9x 9x 9x 9x 9x 8x 9x 9x 9x 2x 2x 2x 2x 1x 1x 9x 9x 9x 1x 9x 9x 9x 1x 9x 9x 9x 1x 1x 2x 2x 2x 2x 1x 1x 8x 8x 8x 8x 2x 2x 8x 8x 8x 2x 2x 8x 1x 1x 8x 1x 1x 8x 1x 1x 8x 1x 1x 8x 8x 1x 1x 1x 1x 6x 4x 4x 6x 2x 2x 2x 2x 6x 6x 1x 1x 4x 4x 4x 4x 2x 2x 2x 2x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 1x 1x 1x 1x 5x 5x 5x 3x 3x 3x 5x 1x 1x 1x 1x | /**
* DashboardCore - Core logic extracted from main.js for testability.
*
* This module contains the testable business logic:
* - Authentication (login, logout, checkAuthStatus)
* - API calls with auth headers
* - WebSocket message handling
* - Data management (hosts, tasks, schedules)
*
* The main.js file imports this module and adds DOM-specific functionality.
*/
/**
* Core dashboard functionality that can be tested without DOM dependencies.
*/
export class DashboardCore {
constructor(options = {}) {
// Allow injection of dependencies for testing
this.apiBase = options.apiBase || (typeof window !== 'undefined' ? window.location.origin : 'http://localhost');
this.storage = options.storage || (typeof localStorage !== 'undefined' ? localStorage : null);
this.fetchFn = options.fetch || (typeof fetch !== 'undefined' ? fetch : null);
this.WebSocketClass = options.WebSocket || (typeof WebSocket !== 'undefined' ? WebSocket : null);
// Authentication state
this.accessToken = this.storage?.getItem('accessToken') || null;
this.currentUser = null;
this.setupRequired = false;
// Data state
this.hosts = [];
this.tasks = [];
this.schedules = [];
this.logs = [];
this.alerts = [];
this.alertsUnread = 0;
// WebSocket
this.ws = null;
// Callbacks for UI updates (set by main.js)
this.onHostsUpdated = null;
this.onTasksUpdated = null;
this.onSchedulesUpdated = null;
this.onAlertsUpdated = null;
this.onAuthStateChanged = null;
this.onNotification = null;
}
// ===== AUTHENTICATION =====
getAuthHeaders() {
const headers = { 'Content-Type': 'application/json' };
if (this.accessToken) {
headers['Authorization'] = `Bearer ${this.accessToken}`;
}
return headers;
}
async checkAuthStatus() {
try {
const response = await this.fetchFn(`${this.apiBase}/api/auth/status`, {
headers: this.getAuthHeaders()
});
if (!response.ok) {
return false;
}
const data = await response.json();
this.setupRequired = data.setup_required;
if (data.setup_required) {
return false;
}
if (data.authenticated && data.user) {
this.currentUser = data.user;
this.onAuthStateChanged?.({ authenticated: true, user: data.user });
return true;
}
return false;
} catch (error) {
console.error('Auth status check failed:', error);
return false;
}
}
async login(username, password) {
try {
const response = await this.fetchFn(`${this.apiBase}/api/auth/login/json`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password })
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Login failed');
}
const data = await response.json();
this.accessToken = data.access_token;
this.storage?.setItem('accessToken', data.access_token);
await this.checkAuthStatus();
this.onNotification?.('Connexion réussie', 'success');
return true;
} catch (error) {
console.error('Login failed:', error);
this.onNotification?.(error.message, 'error');
return false;
}
}
logout() {
this.accessToken = null;
this.currentUser = null;
this.storage?.removeItem('accessToken');
if (this.ws) {
this.ws.close();
this.ws = null;
}
this.onAuthStateChanged?.({ authenticated: false, user: null });
this.onNotification?.('Déconnexion réussie', 'success');
}
// ===== API CALLS =====
async apiCall(endpoint, options = {}) {
const url = `${this.apiBase}${endpoint}`;
const defaultOptions = {
headers: this.getAuthHeaders()
};
try {
const response = await this.fetchFn(url, { ...defaultOptions, ...options });
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return await response.json();
} catch (error) {
console.error(`API Error (${endpoint}):`, error);
throw error;
}
}
// ===== WEBSOCKET =====
connectWebSocket(wsUrl = null) {
if (!this.WebSocketClass) {
console.warn('WebSocket not available');
return;
}
const url = wsUrl || this._getDefaultWsUrl();
try {
this.ws = new this.WebSocketClass(url);
this.ws.onopen = () => {
console.log('WebSocket connected');
};
this.ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
this.handleWebSocketMessage(data);
} catch (e) {
console.error('WebSocket message parse error:', e);
}
};
this.ws.onclose = () => {
console.log('WebSocket closed');
};
this.ws.onerror = (error) => {
console.error('WebSocket error:', error);
};
} catch (error) {
console.error('WebSocket connection failed:', error);
}
}
_getDefaultWsUrl() {
if (typeof window === 'undefined') {
return 'ws://localhost/ws';
}
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
return `${protocol}//${window.location.host}/ws`;
}
handleWebSocketMessage(message) {
const { type, data } = message;
switch (type) {
case 'host_updated':
this.updateHostInList(data);
break;
case 'task_started':
case 'task_updated':
case 'task_completed':
this.handleTaskUpdate(data);
break;
case 'schedule_completed':
this.updateScheduleStatus(data);
break;
case 'alert_created':
this.handleAlertCreated(data);
break;
case 'hosts_synced':
this.onHostsUpdated?.();
break;
default:
// Unknown message type - ignore
break;
}
}
// ===== DATA MANAGEMENT =====
updateHostInList(hostData) {
if (!hostData?.id) return;
const idx = this.hosts.findIndex(h => h.id === hostData.id);
if (idx >= 0) {
this.hosts[idx] = { ...this.hosts[idx], ...hostData };
} else {
this.hosts.push(hostData);
}
this.onHostsUpdated?.();
}
handleTaskUpdate(taskData) {
if (!taskData?.id) return;
const idx = this.tasks.findIndex(t => t.id === taskData.id);
if (idx >= 0) {
this.tasks[idx] = { ...this.tasks[idx], ...taskData };
} else {
this.tasks.unshift(taskData);
}
this.onTasksUpdated?.();
}
updateScheduleStatus(scheduleData) {
if (!scheduleData?.schedule_id) return;
const idx = this.schedules.findIndex(s => s.id === scheduleData.schedule_id);
if (idx >= 0) {
this.schedules[idx].last_status = scheduleData.status;
this.schedules[idx].last_run_at = scheduleData.completed_at;
}
this.onSchedulesUpdated?.();
}
handleAlertCreated(alert) {
if (!alert) return;
this.alerts = [alert, ...this.alerts].slice(0, 200);
this.alertsUnread++;
this.onAlertsUpdated?.();
}
// ===== UTILITY =====
escapeHtml(text) {
if (!text) return '';
const div = typeof document !== 'undefined' ? document.createElement('div') : null;
if (div) {
div.textContent = text;
return div.innerHTML;
}
// Fallback for non-DOM environments
return String(text)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
}
// Export for both ESM and CommonJS
export default DashboardCore;
|