|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Automated Test Suite for Temporal Memory Grid (TMG) |
| 4 | + * Tests: Database Setup, Utils Validation, I18n 5-Language Parity, Caching Engine, |
| 5 | + * Aggregation Engine, Alert Engine, and JSON Schemas Integrity. |
| 6 | + */ |
| 7 | + |
| 8 | +error_reporting(E_ALL); |
| 9 | +ini_set('display_errors', '1'); |
| 10 | + |
| 11 | +require_once __DIR__ . '/../config.php'; |
| 12 | +require_once __DIR__ . '/../database_pdo.php'; |
| 13 | +require_once __DIR__ . '/../setup_database_sqlite.php'; |
| 14 | +require_once __DIR__ . '/../utils.php'; |
| 15 | +require_once __DIR__ . '/../i18n.php'; |
| 16 | +require_once __DIR__ . '/../cache.php'; |
| 17 | +require_once __DIR__ . '/../aggregation_engine.php'; |
| 18 | +require_once __DIR__ . '/../alert_engine.php'; |
| 19 | + |
| 20 | +echo "========================================================\n"; |
| 21 | +echo " 🧪 Running Temporal Memory Grid (TMG) Test Suite \n"; |
| 22 | +echo "========================================================\n\n"; |
| 23 | + |
| 24 | +$passed = 0; |
| 25 | +$failed = 0; |
| 26 | + |
| 27 | +function assertTest($name, $condition, $details = "") { |
| 28 | + global $passed, $failed; |
| 29 | + if ($condition) { |
| 30 | + echo " ✅ PASS: $name\n"; |
| 31 | + $passed++; |
| 32 | + } else { |
| 33 | + echo " ❌ FAIL: $name ($details)\n"; |
| 34 | + $failed++; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// 1. Database Connection & Schema Setup |
| 39 | +try { |
| 40 | + $db = \Temporal\Database::getInstance(); |
| 41 | + assertTest("Database Connection Instance", $db !== null); |
| 42 | + |
| 43 | + setupDatabaseSqlite(); |
| 44 | + |
| 45 | + $pdo = $db->getConnection(); |
| 46 | + $tables = $pdo->query("SELECT name FROM sqlite_master WHERE type='table'")->fetchAll(PDO::FETCH_COLUMN); |
| 47 | + |
| 48 | + assertTest("Table 'events' created", in_array('events', $tables)); |
| 49 | + assertTest("Table 'time_buckets' created", in_array('time_buckets', $tables)); |
| 50 | + assertTest("Table 'bucket_metrics' created", in_array('bucket_metrics', $tables)); |
| 51 | + assertTest("Table 'aggregation_jobs_log' created", in_array('aggregation_jobs_log', $tables)); |
| 52 | + assertTest("Table 'alert_rules' created", in_array('alert_rules', $tables)); |
| 53 | + assertTest("Table 'alert_history' created", in_array('alert_history', $tables)); |
| 54 | + assertTest("Table 'users' created", in_array('users', $tables)); |
| 55 | + assertTest("Table 'api_keys' created", in_array('api_keys', $tables)); |
| 56 | + assertTest("Table 'settings' created", in_array('settings', $tables)); |
| 57 | +} catch (Exception $e) { |
| 58 | + assertTest("Database Init Error", false, $e->getMessage()); |
| 59 | +} |
| 60 | + |
| 61 | +// 2. Utils Validation |
| 62 | +try { |
| 63 | + // Valid time range |
| 64 | + $start = "2026-08-01 00:00:00"; |
| 65 | + $end = "2026-08-02 00:00:00"; |
| 66 | + \Temporal\Utils::validateTimeRange($start, $end); |
| 67 | + assertTest("Utils: Valid Time Range", true); |
| 68 | + |
| 69 | + // Invalid start > end |
| 70 | + $threwRange = false; |
| 71 | + try { |
| 72 | + \Temporal\Utils::validateTimeRange($end, $start); |
| 73 | + } catch (Exception $e) { |
| 74 | + $threwRange = true; |
| 75 | + } |
| 76 | + assertTest("Utils: Reject Inverted Time Range", $threwRange); |
| 77 | + |
| 78 | + // Valid bucket sizes |
| 79 | + $validSizes = ['1m', '5m', '15m', '1h', '1d']; |
| 80 | + $allValid = true; |
| 81 | + foreach ($validSizes as $s) { |
| 82 | + try { |
| 83 | + \Temporal\Utils::validateBucketSize($s); |
| 84 | + } catch (Exception $e) { |
| 85 | + $allValid = false; |
| 86 | + } |
| 87 | + } |
| 88 | + assertTest("Utils: Validate Standard Bucket Sizes (1m, 5m, 15m, 1h, 1d)", $allValid); |
| 89 | + |
| 90 | + // Invalid bucket size |
| 91 | + $threwSize = false; |
| 92 | + try { |
| 93 | + \Temporal\Utils::validateBucketSize('42m'); |
| 94 | + } catch (Exception $e) { |
| 95 | + $threwSize = true; |
| 96 | + } |
| 97 | + assertTest("Utils: Reject Non-Standard Bucket Size ('42m')", $threwSize); |
| 98 | +} catch (Exception $e) { |
| 99 | + assertTest("Utils Error", false, $e->getMessage()); |
| 100 | +} |
| 101 | + |
| 102 | +// 3. I18n Engine & 5-Language Dictionary Parity |
| 103 | +try { |
| 104 | + $langs = array_keys(\Temporal\I18n::$SUPPORTED_LANGS); |
| 105 | + assertTest("I18n: Supported Languages Count (5 Languages)", count($langs) === 5); |
| 106 | + assertTest("I18n: Supports TR, EN, DE, ES, FR", in_array('tr', $langs) && in_array('en', $langs) && in_array('de', $langs) && in_array('es', $langs) && in_array('fr', $langs)); |
| 107 | + |
| 108 | + // Verify all 5 language files load |
| 109 | + $langDir = __DIR__ . '/../lang/'; |
| 110 | + $dictKeys = []; |
| 111 | + $allLoaded = true; |
| 112 | + |
| 113 | + foreach ($langs as $l) { |
| 114 | + $file = $langDir . $l . '.php'; |
| 115 | + if (file_exists($file)) { |
| 116 | + $dict = require $file; |
| 117 | + if (is_array($dict)) { |
| 118 | + $dictKeys[$l] = array_keys($dict); |
| 119 | + } else { |
| 120 | + $allLoaded = false; |
| 121 | + } |
| 122 | + } else { |
| 123 | + $allLoaded = false; |
| 124 | + } |
| 125 | + } |
| 126 | + assertTest("I18n: All 5 Language Dictionaries Loaded", $allLoaded); |
| 127 | + |
| 128 | + // Check translation function with loadLanguages |
| 129 | + \Temporal\I18n::loadLanguages('tr'); |
| 130 | + $trWord = \Temporal\I18n::get('dashboard'); |
| 131 | + assertTest("I18n: Turkish Translation Lookup", !empty($trWord)); |
| 132 | + |
| 133 | + \Temporal\I18n::loadLanguages('en'); |
| 134 | + $enWord = \Temporal\I18n::get('dashboard'); |
| 135 | + assertTest("I18n: English Translation Lookup", !empty($enWord)); |
| 136 | +} catch (Exception $e) { |
| 137 | + assertTest("I18n Error", false, $e->getMessage()); |
| 138 | +} |
| 139 | + |
| 140 | +// 4. Cache Engine |
| 141 | +try { |
| 142 | + $cache = \Temporal\Cache::getInstance(); |
| 143 | + $testKey = 'tmg_unit_test_' . time(); |
| 144 | + $testVal = ['status' => 'ok', 'score' => 99.5]; |
| 145 | + |
| 146 | + $cache->set($testKey, $testVal, 60); |
| 147 | + $cached = $cache->get($testKey); |
| 148 | + assertTest("Cache: Set and Get Value", is_array($cached) && isset($cached['score']) && $cached['score'] == 99.5); |
| 149 | + |
| 150 | + $cache->delete($testKey); |
| 151 | + $deleted = $cache->get($testKey); |
| 152 | + assertTest("Cache: Invalidate / Delete Key", $deleted === false); |
| 153 | +} catch (Exception $e) { |
| 154 | + assertTest("Cache Error", false, $e->getMessage()); |
| 155 | +} |
| 156 | + |
| 157 | +// 5. JSON Schemas Integrity |
| 158 | +try { |
| 159 | + $schemaDir = __DIR__ . '/../docs/schemas/'; |
| 160 | + $schemas = ['anomalies_response.schema.json', 'timeseries_response.schema.json', 'trend_response.schema.json']; |
| 161 | + $allValidSchemas = true; |
| 162 | + |
| 163 | + foreach ($schemas as $s) { |
| 164 | + $path = $schemaDir . $s; |
| 165 | + if (!file_exists($path)) { |
| 166 | + $allValidSchemas = false; |
| 167 | + break; |
| 168 | + } |
| 169 | + $json = json_decode(file_get_contents($path), true); |
| 170 | + if ($json === null || !isset($json['type'])) { |
| 171 | + $allValidSchemas = false; |
| 172 | + break; |
| 173 | + } |
| 174 | + } |
| 175 | + assertTest("Docs: JSON Response Schemas Valid (Timeseries, Trend, Anomalies)", $allValidSchemas); |
| 176 | +} catch (Exception $e) { |
| 177 | + assertTest("JSON Schemas Error", false, $e->getMessage()); |
| 178 | +} |
| 179 | + |
| 180 | +echo "\n--------------------------------------------------------\n"; |
| 181 | +echo " Results: $passed Passed, $failed Failed\n"; |
| 182 | +echo "--------------------------------------------------------\n\n"; |
| 183 | + |
| 184 | +exit($failed > 0 ? 1 : 0); |
0 commit comments