Files
coolify/tests/Feature/TrafficAnalytics/AnalyticsFormattingTest.php
T

58 lines
2.4 KiB
PHP

<?php
// Presentation helpers for the analytics views (items: flags, referrer favicons,
// device capitalization).
it('maps woothee device categories to friendly capitalized labels', function () {
expect(deviceLabel('pc'))->toBe('Desktop');
expect(deviceLabel('smartphone'))->toBe('Mobile');
expect(deviceLabel('mobilephone'))->toBe('Mobile');
expect(deviceLabel('crawler'))->toBe('Bot');
expect(deviceLabel('tablet'))->toBe('Tablet');
expect(deviceLabel(''))->toBe('Unknown');
expect(deviceLabel(null))->toBe('Unknown');
});
it('extracts a bare host from referer URLs and bare hosts, dropping www', function () {
expect(refererHost('https://www.google.com/search?q=x'))->toBe('google.com');
expect(refererHost('http://news.ycombinator.com'))->toBe('news.ycombinator.com');
expect(refererHost('example.com'))->toBe('example.com');
expect(refererHost(''))->toBeNull();
expect(refererHost(null))->toBeNull();
});
it('groups referer breakdown rows by normalized host', function () {
expect(groupRefererBreakdownRows([
['value' => 'https://www.example.com/first', 'requests' => 7, 'bytesOut' => 700],
['value' => 'http://example.com/second', 'requests' => 3, 'bytesOut' => 300],
['value' => 'https://other.example/path', 'requests' => 5, 'bytesOut' => 500],
]))->toBe([
['value' => 'example.com', 'requests' => 10, 'bytesOut' => 1000],
['value' => 'other.example', 'requests' => 5, 'bytesOut' => 500],
]);
});
it('builds a duckduckgo favicon url for a host', function () {
expect(refererFaviconUrl('example.com'))->toBe('https://icons.duckduckgo.com/ip3/example.com.ico');
});
it('builds a flagcdn image url for valid ISO codes only', function () {
expect(countryFlagUrl('US'))->toBe('https://flagcdn.com/24x18/us.png');
expect(countryFlagUrl('de', '48x36'))->toBe('https://flagcdn.com/48x36/de.png');
expect(countryFlagUrl('ZZZ'))->toBeNull();
expect(countryFlagUrl(''))->toBeNull();
expect(countryFlagUrl(null))->toBeNull();
});
it('renders ampersands in breakdown empty-state descriptions', function () {
$html = view('livewire.traffic._breakdown-section', [
'dimension' => 'agent',
'label' => 'AI agents & bots',
'rows' => [],
])->render();
expect($html)
->toContain('No ai agents &amp; bots data for the selected range.')
->not->toContain('No ai agents &amp;amp; bots data for the selected range.');
});