Files
wp-donations/tests/Unit/OverlayBuildTest.php
shadowdaoandClaude Sonnet 5.5 afa768fa42
Test and Release / test (push) Successful in 1m17s
Test and Release / release (push) Successful in 5s
Overlays refresh themselves after updates; media URLs versioned; frameless (transparent) alerts
- Overlay\Build id (plugin version + admin epoch) in X-SG-Build; overlay.js reloads once on mismatch (loop-guarded); 'Refresh all overlays now'
- Media URLs carry ?v=<mtime>
- Alert option 'Frameless' (schema v5): no box so transparent PNG/GIF/WebP and VP9-alpha WebM show through; admin preview with checkerboard and ffmpeg help

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 13:41:18 -08:00

77 lines
4.9 KiB
PHP

<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Alerts\Media;
use StreamGoals\Overlay\Build;
final class OverlayBuildTest extends TestCase {
public function test_build_id_combines_version_and_epoch_and_is_safe(): void {
$this->assertSame( '0.1.0-0', Build::id( '0.1.0', false ) );
$this->assertSame( '0.1.0-7', Build::id( '0.1.0', '7' ) );
$this->assertSame( '1.2x-0', Build::id( '1.2"<x>', 'abc' ), 'junk characters are stripped, junk epoch is 0' );
$this->assertSame( '0-0', Build::id( '', null ) );
$this->assertNotSame( Build::id( '0.1.0', 1 ), Build::id( '0.1.1', 1 ) );
$this->assertNotSame( Build::id( '0.1.0', 1 ), Build::id( '0.1.0', 2 ) );
}
public function test_headers_carry_only_the_build_and_never_touch_the_etag(): void {
$this->assertSame( array( 'X-SG-Build' => '0.1.0-3' ), Build::headers( '0.1.0-3' ) );
$src = (string) file_get_contents( dirname( __DIR__, 2 ) . '/includes/Rest/OverlayController.php' );
$this->assertStringContainsString( 'Build::headers( Build::current() )', $src );
$this->assertStringContainsString( 'Payloads::etag( $payload, $service->etagSalt(', $src, 'ETag is still computed from the payload only' );
}
public function test_epoch_bump_rules(): void {
$this->assertSame( 1000, Build::nextEpoch( 0, 1000 ) );
$this->assertSame( 1000, Build::nextEpoch( false, 1000 ) );
$this->assertSame( 1000, Build::nextEpoch( '999', 1000 ) );
$this->assertSame( 2001, Build::nextEpoch( 2000, 1000 ), 'strictly increasing even if the clock went back' );
$this->assertSame( 1000, Build::nextEpoch( '-5', 1000 ) );
$this->assertSame( 1000, Build::nextEpoch( array( 9 ), 1000 ) );
$this->assertSame( 0, Build::epochInt( 1.5 ) );
$this->assertSame( 0, Build::epochInt( '99999999999999999999' ) );
}
public function test_refresh_handler_is_guarded_registered_and_option_is_not_autoloaded(): void {
$root = dirname( __DIR__, 2 ) . '/includes/';
$page = (string) file_get_contents( $root . 'Admin/OverlaysPage.php' );
$start = strpos( $page, 'function handleRefreshAll' );
$this->assertNotFalse( $start );
$this->assertStringContainsString( "Admin::guard( 'sg_refresh_overlays' )", substr( $page, $start, 200 ) );
$this->assertStringContainsString( "'sg_refresh_overlays' => array( OverlaysPage::class, 'handleRefreshAll' )", (string) file_get_contents( $root . 'Admin/Admin.php' ) );
$this->assertStringContainsString( 'Reloads every open OBS source. Done automatically after a plugin update.', $page );
$this->assertStringContainsString( ', false );', substr( (string) file_get_contents( $root . 'Overlay/Build.php' ), (int) strpos( (string) file_get_contents( $root . 'Overlay/Build.php' ), 'update_option' ) ) );
$this->assertStringContainsString( 'data-build=', (string) file_get_contents( $root . 'Overlay/Router.php' ) );
}
public function test_media_version_suffix_builder(): void {
$this->assertSame( 'https://x.test/a.png?v=123', Media::withVersion( 'https://x.test/a.png', 123 ) );
$this->assertSame( 'https://x.test/a.png?q=1&v=123', Media::withVersion( 'https://x.test/a.png?q=1', 123 ) );
$this->assertSame( 'https://x.test/a.png?v=123', Media::withVersion( 'https://x.test/a.png?v=5', 123 ), 'replaces an old v' );
$this->assertSame( 'https://x.test/a.png?q=1&v=123', Media::withVersion( 'https://x.test/a.png?v=5&q=1', 123 ) );
$this->assertSame( 'https://x.test/a.png?v=' . strtotime( '2024-05-06 07:08:09 UTC' ), Media::withVersion( 'https://x.test/a.png', 0, '2024-05-06 07:08:09' ), 'falls back to post_modified_gmt' );
$this->assertSame( 'https://x.test/a.png?v=50', Media::withVersion( 'https://x.test/a.png', 50, '2024-05-06 07:08:09' ), 'file mtime wins' );
$this->assertSame( 'https://x.test/a.png', Media::withVersion( 'https://x.test/a.png', 0, '0000-00-00 00:00:00x' ) );
$this->assertSame( 'https://x.test/a.png', Media::withVersion( 'https://x.test/a.png', 0 ) );
$this->assertSame( 'https://x.test/a.png#f', Media::withVersion( 'https://x.test/a.png#f', 9 ) );
$this->assertNotNull( Media::safeUrl( Media::withVersion( 'https://x.test/a.png', 123 ) ) );
}
public function test_installer_has_frameless_column_and_version_five(): void {
$src = (string) file_get_contents( dirname( __DIR__, 2 ) . '/includes/Core/Installer.php' );
$this->assertSame( 1, preg_match( "/DB_VERSION = '(\\d+)'/", $src, $m ) );
$this->assertGreaterThanOrEqual( 5, (int) $m[1] );
$this->assertMatchesRegularExpression( '/CREATE TABLE \{\$p\}alert_defs \([^;]*frameless TINYINT\(1\) NOT NULL DEFAULT 0,/s', $src );
}
public function test_overlay_js_checks_build_on_every_response_including_304(): void {
$js = (string) file_get_contents( dirname( __DIR__, 2 ) . '/assets/overlay/overlay.js' );
$this->assertLessThan( strpos( $js, 'r.status === 304' ), strpos( $js, 'checkBuild(r)' ), 'build is compared before the 304 shortcut' );
$this->assertStringContainsString( 'X-SG-Build', $js );
}
}