export const ACTIVITY_LIMIT = 500; export const ACTIVITY_STATES = ['active', 'hidden', 'unfocused', 'outside', 'closed', 'finished']; // Clues of outside help (AI side panels, extensions, pasted code), recorded alongside the journal. export const SIGNAL_LIMIT = 200; export const SIGNAL_KINDS = ['paste', 'drop', 'copy', 'insertion', 'synthetic', 'panel', 'extension']; const SIGNAL_LABELS = { paste: 'collage', drop: 'texte déposé (glisser-déposer)', copy: 'copie depuis l’évaluation', insertion: 'bloc de texte inséré sans frappe', synthetic: 'modification par un script ou une extension', panel: 'panneau latéral du navigateur ouvert / fermé', extension: 'élément ajouté à la page par une extension' }; export function activitySummary(log, until = Date.now()) { const periods = []; let away; for (const event of log?.events || []) { if (event.state === 'active' || event.state === 'finished') { if (away) { periods.push({ ...away, end: event.at, durationMs: Math.max(0, event.at - away.start) }); away = null; } } else if (!away) away = { start: event.at, reasons: [event.state] }; else if (!away.reasons.includes(event.state)) away.reasons.push(event.state); } if (away) periods.push({ ...away, end: null, durationMs: Math.max(0, until - away.start) }); return { periods, count: periods.length, totalMs: periods.reduce((sum, item) => sum + item.durationMs, 0) }; } export function validateActivity(value, now = Date.now()) { const invalid = () => { throw Object.assign(new Error('invalid_activity'), { status: 400, code: 'invalid_activity' }); }; if (!value || !/^[a-f\d-]{36}$/i.test(value.id) || !Number.isSafeInteger(value.revision) || value.revision < 1 || !Number.isSafeInteger(value.dropped) || value.dropped < 0 || !Array.isArray(value.events) || !value.events.length || value.events.length > ACTIVITY_LIMIT) invalid(); let previous = 0; const events = value.events.map(event => { if (!event || !ACTIVITY_STATES.includes(event.state) || !Number.isSafeInteger(event.at) || event.at < previous || event.at < 0 || event.at > now + 300000) invalid(); previous = event.at; return { at: event.at, state: event.state }; }); const signals = value.signals ?? [], signalsDropped = value.signalsDropped ?? 0; if (!Array.isArray(signals) || signals.length > SIGNAL_LIMIT || !Number.isSafeInteger(signalsDropped) || signalsDropped < 0) invalid(); return { id: value.id, revision: value.revision, dropped: value.dropped, events, signalsDropped, signals: signals.map(signal => { if (!signal || !SIGNAL_KINDS.includes(signal.kind) || !Number.isSafeInteger(signal.at) || signal.at < 0 || signal.at > now + 300000 || typeof signal.detail !== 'string' || signal.detail.length > 120) invalid(); return { at: signal.at, kind: signal.kind, detail: signal.detail }; }) }; } export function activityReport(records, until = Date.now()) { const labels = { hidden: 'onglet masqué / fenêtre réduite', unfocused: 'perte de focus', outside: 'évaluation quittée', closed: 'page quittée / rechargée' }; const lines = ['JOURNAL DE VISIBILITÉ — horodatages UTC', 'Signaux déclarés par le navigateur, non certifiés : ils ne prouvent pas une fraude.', 'Les durées en cours sont calculées jusqu’à ce rapport. Une fermeture brutale peut ne pas être signalée.', 'Les sessions / onglets sont présentés séparément ; leurs durées peuvent se chevaucher.', 'Les signaux d’aide extérieure possible (IA du navigateur, extensions, collages) sont des indices à vérifier, pas des preuves.', '']; if (!records.length) lines.push('Aucun journal disponible.'); for (const record of records) { const log = record.activity; const summary = activitySummary(log, until); lines.push(`${record.student} — examen ${record.examId} — session ${log.id}`, `Dernière réception serveur : ${new Date(record.receivedAt).toISOString()}`, `${summary.count} sortie(s), ${(summary.totalMs / 1000).toFixed(1)} s cumulées.`); if (log.dropped) lines.push(`Journal partiel : ${log.dropped} ancien(s) événement(s) non conservé(s).`); for (const period of summary.periods) lines.push( `${new Date(period.start).toISOString()} → ${period.end === null ? 'retour non observé' : new Date(period.end).toISOString()} | ${(period.durationMs / 1000).toFixed(1)} s | ${period.reasons.map(reason => labels[reason]).join(', ')}`); const signals = log.signals || []; if (signals.length || log.signalsDropped) lines.push(`${signals.length + (log.signalsDropped || 0)} signal(aux) d’aide extérieure possible :`); if (log.signalsDropped) lines.push(`Signaux partiels : ${log.signalsDropped} ancien(s) signal(aux) non conservé(s).`); for (const signal of signals) lines.push(`${new Date(signal.at).toISOString()} | ${SIGNAL_LABELS[signal.kind]} | ${signal.detail}`); lines.push(''); } return lines.join('\n'); }