All files / lib/evolution timeline.ts

41.81% Statements 23/55
100% Branches 1/1
16.66% Functions 1/6
41.81% Lines 23/55

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 881x                 1x   1x 1x                       1x   1x 1x                     1x   1x 1x                             1x 1x   1x   1x 1x                       1x   1x 1x 6x 6x   1x   1x 1x      
// lib/evolution/timeline.ts
 
export interface EvolutionSnapshot {
  t: string;
  stability: number;
  adaptationRate: number;
  directive: string;
}
 
/**
 * يبني الخط الزمني الحالي للنظام من البيانات المخزنة محليًا
 */
export function buildTimeline(): EvolutionSnapshot[] {
  try {
    const raw = localStorage.getItem('EVOLUTION_HISTORY');
    if (!raw) return [];
    const data = JSON.parse(raw);
    if (!Array.isArray(data)) return [];
    return data.slice(-20);
  } catch {
    return [];
  }
}
 
/**
 * يضيف نقطة جديدة إلى الخط الزمني
 */
export function addSnapshot(snapshot: EvolutionSnapshot) {
  try {
    const key = 'EVOLUTION_HISTORY';
    const prev = JSON.parse(localStorage.getItem(key) || '[]');
    prev.push(snapshot);
    localStorage.setItem(key, JSON.stringify(prev.slice(-30)));
  } catch (err) {
    console.warn('⚠️ [Timeline] Failed to append snapshot', err);
  }
}
 
/**
 * يحذف الخط الزمني (لأغراض الاختبار)
 */
export function clearTimeline() {
  localStorage.removeItem('EVOLUTION_HISTORY');
}
 
/**
 * @file timeline.ts
 * مسؤول عن حفظ وتتبع استقرار النظام (stability) عبر الزمن
 * ويُستخدم في التحليل الزمني والـ meta forecast.
 */
 
export interface TimelineEntry {
  stability: number; // 0 to 1
  timestamp: string;
}
 
// ذاكرة داخلية مؤقتة (mocked for tests)
let timelineHistory: TimelineEntry[] = [];
 
/**
 * إضافة نقطة جديدة للتاريخ الزمني
 */
export function addTimelinePoint(stability: number): void {
  timelineHistory.push({
    stability: Math.max(0, Math.min(1, stability)), // clamp
    timestamp: new Date().toISOString(),
  });
 
  // الحفاظ على آخر 100 نقطة فقط
  if (timelineHistory.length > 100) {
    timelineHistory = timelineHistory.slice(-100);
  }
}
 
/**
 * استرجاع التاريخ الزمني الحالي
 */
export function getTimelineHistory(): TimelineEntry[] {
  return timelineHistory;
}
 
/**
 * مسح التاريخ (للاختبارات)
 */
export function clearTimelineHistory(): void {
  timelineHistory = [];
}