|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SciProfiler\Tests; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use SciProfiler\Config; |
| 9 | +use SciProfiler\ProfileResult; |
| 10 | +use SciProfiler\Reporter\HtmlReporter; |
| 11 | +use SciProfiler\Reporter\JsonReporter; |
| 12 | + |
| 13 | +/** |
| 14 | + * Detailed tests for HtmlReporter rendering branches. |
| 15 | + */ |
| 16 | +final class HtmlReporterDetailTest extends TestCase |
| 17 | +{ |
| 18 | + private string $outputDir; |
| 19 | + |
| 20 | + protected function setUp(): void |
| 21 | + { |
| 22 | + $this->outputDir = sys_get_temp_dir() . '/sci-html-detail-' . uniqid(); |
| 23 | + } |
| 24 | + |
| 25 | + protected function tearDown(): void |
| 26 | + { |
| 27 | + if (is_dir($this->outputDir)) { |
| 28 | + $files = glob($this->outputDir . '/*'); |
| 29 | + if ($files !== false) { |
| 30 | + array_map('unlink', $files); |
| 31 | + } |
| 32 | + rmdir($this->outputDir); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private function seedJsonl(array $entries): Config |
| 37 | + { |
| 38 | + $config = new Config(outputDir: $this->outputDir); |
| 39 | + $json = new JsonReporter(); |
| 40 | + foreach ($entries as $entry) { |
| 41 | + $json->report($entry, $config); |
| 42 | + } |
| 43 | + return $config; |
| 44 | + } |
| 45 | + |
| 46 | + private function makeEntry( |
| 47 | + float $sci, |
| 48 | + string $script = '/app/index.php', |
| 49 | + string $uri = '/page', |
| 50 | + string $method = 'GET', |
| 51 | + int $responseCode = 200, |
| 52 | + float $peakMb = 4.0, |
| 53 | + ): ProfileResult { |
| 54 | + return new ProfileResult( |
| 55 | + collectorMetrics: [ |
| 56 | + 'time' => ['wall_time_ms' => 50.0, 'wall_time_sec' => 0.05], |
| 57 | + 'memory' => ['memory_peak_mb' => $peakMb], |
| 58 | + 'request' => [ |
| 59 | + 'method' => $method, |
| 60 | + 'uri' => $uri, |
| 61 | + 'script_filename' => $script, |
| 62 | + 'response_code' => $responseCode, |
| 63 | + 'input_bytes' => 0, |
| 64 | + 'output_bytes' => 1024, |
| 65 | + ], |
| 66 | + 'config' => [ |
| 67 | + 'device_power_watts' => 18.0, |
| 68 | + 'grid_carbon_intensity' => 332.0, |
| 69 | + 'embodied_carbon' => 211000.0, |
| 70 | + 'device_lifetime_hours' => 11680.0, |
| 71 | + 'machine_description' => 'Test', |
| 72 | + ], |
| 73 | + ], |
| 74 | + sciMetrics: ['sci_mgco2eq' => $sci], |
| 75 | + timestamp: '2026-01-01T00:00:00+00:00', |
| 76 | + profileId: bin2hex(random_bytes(8)), |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + private function getDashboard(Config $config): string |
| 81 | + { |
| 82 | + $html = new HtmlReporter(); |
| 83 | + $html->report($this->makeEntry(0.1), $config); |
| 84 | + return (string) file_get_contents($this->outputDir . '/dashboard.html'); |
| 85 | + } |
| 86 | + |
| 87 | + // ── Delta trend (first-half vs second-half) ── |
| 88 | + |
| 89 | + public function testDeltaWorseWithFourEntries(): void |
| 90 | + { |
| 91 | + // First 2: low SCI, last 2: high SCI → worse trend |
| 92 | + $config = $this->seedJsonl([ |
| 93 | + $this->makeEntry(0.1, '/app/test.php'), |
| 94 | + $this->makeEntry(0.1, '/app/test.php'), |
| 95 | + $this->makeEntry(0.5, '/app/test.php'), |
| 96 | + $this->makeEntry(0.5, '/app/test.php'), |
| 97 | + ]); |
| 98 | + |
| 99 | + $content = $this->getDashboard($config); |
| 100 | + $this->assertStringContainsString('worse', $content); |
| 101 | + $this->assertStringContainsString('▲', $content); |
| 102 | + } |
| 103 | + |
| 104 | + public function testDeltaBetterWithFourEntries(): void |
| 105 | + { |
| 106 | + $config = $this->seedJsonl([ |
| 107 | + $this->makeEntry(1.0, '/app/test.php'), |
| 108 | + $this->makeEntry(1.0, '/app/test.php'), |
| 109 | + $this->makeEntry(0.3, '/app/test.php'), |
| 110 | + $this->makeEntry(0.3, '/app/test.php'), |
| 111 | + ]); |
| 112 | + |
| 113 | + $content = $this->getDashboard($config); |
| 114 | + $this->assertStringContainsString('better', $content); |
| 115 | + $this->assertStringContainsString('▼', $content); |
| 116 | + } |
| 117 | + |
| 118 | + public function testDeltaStableWithinThreshold(): void |
| 119 | + { |
| 120 | + $config = $this->seedJsonl([ |
| 121 | + $this->makeEntry(1.00, '/app/test.php'), |
| 122 | + $this->makeEntry(1.00, '/app/test.php'), |
| 123 | + $this->makeEntry(1.02, '/app/test.php'), |
| 124 | + $this->makeEntry(1.02, '/app/test.php'), |
| 125 | + ]); |
| 126 | + |
| 127 | + $content = $this->getDashboard($config); |
| 128 | + $this->assertStringContainsString('stable', $content); |
| 129 | + } |
| 130 | + |
| 131 | + public function testNoDeltaInPerScriptWithThreeEntries(): void |
| 132 | + { |
| 133 | + // Only 3 entries for one script → n < 4 → no delta in per-script summary |
| 134 | + $config = $this->seedJsonl([ |
| 135 | + $this->makeEntry(0.1, '/app/test.php'), |
| 136 | + $this->makeEntry(0.5, '/app/test.php'), |
| 137 | + $this->makeEntry(0.9, '/app/test.php'), |
| 138 | + ]); |
| 139 | + |
| 140 | + $content = $this->getDashboard($config); |
| 141 | + $this->assertStringContainsString('test.php', $content); |
| 142 | + |
| 143 | + // Extract the per-script table section (between "Per-Script Summary" and "Recent Requests") |
| 144 | + $perScriptStart = strpos($content, 'Per-Script Summary'); |
| 145 | + $recentStart = strpos($content, 'Recent Requests'); |
| 146 | + $perScriptSection = substr($content, $perScriptStart, $recentStart - $perScriptStart); |
| 147 | + |
| 148 | + // Per-script section should NOT have delta indicators (n=3 < 4) |
| 149 | + $this->assertStringNotContainsString('class="delta worse"', $perScriptSection); |
| 150 | + $this->assertStringNotContainsString('class="delta better"', $perScriptSection); |
| 151 | + $this->assertStringNotContainsString('class="delta stable"', $perScriptSection); |
| 152 | + } |
| 153 | + |
| 154 | + public function testNoDeltaWhenFirstHalfAvgIsZero(): void |
| 155 | + { |
| 156 | + $config = $this->seedJsonl([ |
| 157 | + $this->makeEntry(0.0, '/app/test.php'), |
| 158 | + $this->makeEntry(0.0, '/app/test.php'), |
| 159 | + $this->makeEntry(5.0, '/app/test.php'), |
| 160 | + $this->makeEntry(5.0, '/app/test.php'), |
| 161 | + ]); |
| 162 | + |
| 163 | + $content = $this->getDashboard($config); |
| 164 | + // avgFirst = 0 → division guard → no delta shown |
| 165 | + $this->assertStringNotContainsString('class="delta worse"', $content); |
| 166 | + } |
| 167 | + |
| 168 | + // ── Status badges ── |
| 169 | + |
| 170 | + public function testStatusBadge200IsOk(): void |
| 171 | + { |
| 172 | + $config = $this->seedJsonl([ |
| 173 | + $this->makeEntry(0.1, responseCode: 200), |
| 174 | + ]); |
| 175 | + |
| 176 | + $content = $this->getDashboard($config); |
| 177 | + $this->assertStringContainsString('badge ok', $content); |
| 178 | + $this->assertStringContainsString('200', $content); |
| 179 | + } |
| 180 | + |
| 181 | + public function testStatusBadge301IsRedir(): void |
| 182 | + { |
| 183 | + $config = $this->seedJsonl([ |
| 184 | + $this->makeEntry(0.1, responseCode: 301), |
| 185 | + ]); |
| 186 | + |
| 187 | + $content = $this->getDashboard($config); |
| 188 | + $this->assertStringContainsString('badge redir', $content); |
| 189 | + } |
| 190 | + |
| 191 | + public function testStatusBadge500IsErr(): void |
| 192 | + { |
| 193 | + $config = $this->seedJsonl([ |
| 194 | + $this->makeEntry(0.1, responseCode: 500), |
| 195 | + ]); |
| 196 | + |
| 197 | + $content = $this->getDashboard($config); |
| 198 | + $this->assertStringContainsString('badge err', $content); |
| 199 | + } |
| 200 | + |
| 201 | + public function testCliBadgeNotShown(): void |
| 202 | + { |
| 203 | + $config = $this->seedJsonl([ |
| 204 | + $this->makeEntry(0.1, method: 'CLI', responseCode: 0), |
| 205 | + ]); |
| 206 | + |
| 207 | + $content = $this->getDashboard($config); |
| 208 | + $this->assertStringContainsString('CLI', $content); |
| 209 | + $this->assertStringNotContainsString('badge ok', $content); |
| 210 | + $this->assertStringNotContainsString('badge err', $content); |
| 211 | + } |
| 212 | + |
| 213 | + // ── HTML escaping ── |
| 214 | + |
| 215 | + public function testEscHandlesHtmlInjection(): void |
| 216 | + { |
| 217 | + $config = $this->seedJsonl([ |
| 218 | + $this->makeEntry(0.1, uri: '/<script>alert(1)</script>'), |
| 219 | + ]); |
| 220 | + |
| 221 | + $content = $this->getDashboard($config); |
| 222 | + $this->assertStringNotContainsString('<script>alert', $content); |
| 223 | + $this->assertStringContainsString('<script>', $content); |
| 224 | + } |
| 225 | + |
| 226 | + // ── Script path display ── |
| 227 | + |
| 228 | + public function testScriptFilenameShownWhenDifferentFromUri(): void |
| 229 | + { |
| 230 | + $config = $this->seedJsonl([ |
| 231 | + $this->makeEntry(0.1, script: '/var/www/public/index.php', uri: '/dashboard'), |
| 232 | + ]); |
| 233 | + |
| 234 | + $content = $this->getDashboard($config); |
| 235 | + // URI displayed as main text, script_filename as <small> |
| 236 | + $this->assertStringContainsString('/dashboard', $content); |
| 237 | + $this->assertStringContainsString('index.php', $content); |
| 238 | + } |
| 239 | + |
| 240 | + public function testScriptShortenedInPerScriptTable(): void |
| 241 | + { |
| 242 | + $config = $this->seedJsonl([ |
| 243 | + $this->makeEntry(0.1, script: '/var/www/long/nested/path/to/index.php'), |
| 244 | + ]); |
| 245 | + |
| 246 | + $content = $this->getDashboard($config); |
| 247 | + // Should show "to/index.php" not the full path |
| 248 | + $this->assertStringContainsString('to/index.php', $content); |
| 249 | + } |
| 250 | + |
| 251 | + // ── Detail row delta marks ── |
| 252 | + |
| 253 | + public function testDetailRowDeltaMarks(): void |
| 254 | + { |
| 255 | + // Three entries with increasing then decreasing SCI |
| 256 | + $config = $this->seedJsonl([ |
| 257 | + $this->makeEntry(0.1), |
| 258 | + $this->makeEntry(0.5), // big increase from 0.1 |
| 259 | + $this->makeEntry(0.1), // big decrease from 0.5 |
| 260 | + ]); |
| 261 | + |
| 262 | + $content = $this->getDashboard($config); |
| 263 | + // Should contain both better and worse delta marks in detail rows |
| 264 | + $this->assertStringContainsString('delta worse', $content); |
| 265 | + $this->assertStringContainsString('delta better', $content); |
| 266 | + } |
| 267 | + |
| 268 | + public function testDetailRowNoDeltaWhenPrevSciIsZero(): void |
| 269 | + { |
| 270 | + $config = $this->seedJsonl([ |
| 271 | + $this->makeEntry(0.0), |
| 272 | + $this->makeEntry(5.0), |
| 273 | + ]); |
| 274 | + |
| 275 | + $content = $this->getDashboard($config); |
| 276 | + // prevSci is 0 → no delta mark (division guard) |
| 277 | + // The detail table is reversed, so 5.0 comes first (no prev), then 0.0 (prev=5.0 → delta shown) |
| 278 | + // This test verifies no crash occurs with zero values |
| 279 | + $this->assertStringContainsString('SCI Profiler Dashboard', $content); |
| 280 | + } |
| 281 | + |
| 282 | + // ── Ring buffer boundary ── |
| 283 | + |
| 284 | + public function testDashboardWith201Entries(): void |
| 285 | + { |
| 286 | + $config = new Config(outputDir: $this->outputDir); |
| 287 | + $json = new JsonReporter(); |
| 288 | + |
| 289 | + for ($i = 0; $i < 201; $i++) { |
| 290 | + $json->report($this->makeEntry(0.1 + $i * 0.001), $config); |
| 291 | + } |
| 292 | + |
| 293 | + $content = $this->getDashboard($config); |
| 294 | + $this->assertStringContainsString('SCI Profiler Dashboard', $content); |
| 295 | + $this->assertStringContainsString('Per-Script Summary', $content); |
| 296 | + } |
| 297 | +} |
0 commit comments