Add OBS overlays (goal, timer, counter, alerts) with ETag polling
Align timer state key with rules engine (timer_ends_at). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7155396312
commit
65082fb2f5
10 files changed
+815
No files matched your search
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use StreamGoals\Overlay\OverlayDataSource;
|
||||
use StreamGoals\Overlay\OverlayService;
|
||||
use StreamGoals\Overlay\Payloads;
|
||||
use StreamGoals\Overlay\Style;
|
||||
|
||||
final class OverlayPayloadsTest extends TestCase {
|
||||
|
||||
private function don( int $id, array $o = array() ): array {
|
||||
return array_merge( array( 'id' => $id, 'base_minor' => 500, 'currency' => 'USD', 'donor_name' => 'Alice Real',
|
||||
'is_anonymous' => 0, 'message' => 'hi', 'status' => 'completed', 'moderation' => 'approved' ), $o );
|
||||
}
|
||||
|
||||
public function test_percent_clamped(): void {
|
||||
$this->assertSame( 100, Payloads::percent( 5000, 1000 ) );
|
||||
$this->assertSame( 0, Payloads::percent( -5, 1000 ) );
|
||||
$this->assertSame( 0, Payloads::percent( 100, 0 ) );
|
||||
$this->assertSame( 33, Payloads::percent( 1, 3 ) );
|
||||
$g = Payloads::goal( array( 'title' => 'T', 'target_minor' => 1000, 'currency' => 'USD' ), 123456 );
|
||||
$this->assertSame( 100, $g['percent'] );
|
||||
$this->assertSame( '$1,234.56', $g['raised_display'] );
|
||||
}
|
||||
|
||||
public function test_money_zero_decimal_and_unknown_currency(): void {
|
||||
$this->assertSame( '¥1,500', Payloads::money( 1500, 'JPY' ) );
|
||||
$this->assertSame( 'CHF 10.00', Payloads::money( 1000, 'CHF' ) );
|
||||
}
|
||||
|
||||
public function test_goal_none(): void {
|
||||
$this->assertFalse( Payloads::goal( null, 0 )['active'] );
|
||||
}
|
||||
|
||||
public function test_timer_running_and_paused(): void {
|
||||
$t = Payloads::timer( '1000', null, 940 );
|
||||
$this->assertSame( array( 1000, 940, 0, false ), array( $t['ends_at'], $t['server_now'], $t['remaining_seconds'], $t['paused'] ) );
|
||||
$p = Payloads::timer( '1000', '300', 940 );
|
||||
$this->assertTrue( $p['paused'] );
|
||||
$this->assertSame( 300, $p['remaining_seconds'] );
|
||||
$this->assertFalse( Payloads::timer( null, null, 940 )['active'] );
|
||||
$this->assertSame( 0, Payloads::timer( '2024-01-01 00:00:00', null, 5 )['ends_at'] - 1704067200 );
|
||||
}
|
||||
|
||||
public function test_counter(): void {
|
||||
$this->assertSame( array( 'label' => 'Pies', 'value' => 3 ), Payloads::counter( 'Pies', 3 ) );
|
||||
}
|
||||
|
||||
public function test_alerts_mask_anonymous_and_hide_unapproved(): void {
|
||||
$rows = array(
|
||||
$this->don( 1 ),
|
||||
$this->don( 2, array( 'is_anonymous' => 1, 'donor_name' => 'Secret Sam' ) ),
|
||||
$this->don( 3, array( 'moderation' => 'pending', 'donor_name' => 'Pending Pat' ) ),
|
||||
$this->don( 4, array( 'moderation' => 'rejected' ) ),
|
||||
$this->don( 5, array( 'status' => 'refunded' ) ),
|
||||
$this->don( 6, array( 'donor_name' => ' ' ) ),
|
||||
);
|
||||
$out = Payloads::alerts( $rows, 0 );
|
||||
$json = json_encode( $out );
|
||||
$this->assertStringNotContainsString( 'Secret Sam', $json );
|
||||
$this->assertStringNotContainsString( 'Pending Pat', $json );
|
||||
$this->assertSame( array( 1, 2, 6 ), array_column( $out['alerts'], 'id' ) );
|
||||
$this->assertSame( 'Anonymous', $out['alerts'][1]['name'] );
|
||||
$this->assertSame( 'Anonymous', $out['alerts'][2]['name'] );
|
||||
$this->assertSame( 'Alice Real', $out['alerts'][0]['name'] );
|
||||
$this->assertSame( '$5.00', $out['alerts'][0]['amount_display'] );
|
||||
$this->assertArrayNotHasKey( 'donor_name', $out['alerts'][0] );
|
||||
}
|
||||
|
||||
public function test_alerts_since_filter_and_baseline(): void {
|
||||
$rows = array( $this->don( 7 ), $this->don( 8 ), $this->don( 9, array( 'moderation' => 'pending' ) ) );
|
||||
$out = Payloads::alerts( $rows, 7 );
|
||||
$this->assertSame( array( 8 ), array_column( $out['alerts'], 'id' ) );
|
||||
$this->assertSame( 8, $out['last_id'] );
|
||||
$base = Payloads::alerts( $rows, null );
|
||||
$this->assertSame( array(), $base['alerts'] );
|
||||
$this->assertSame( 8, $base['last_id'] );
|
||||
$this->assertSame( 20, Payloads::alerts( array(), 20 )['last_id'] );
|
||||
}
|
||||
|
||||
public function test_alerts_limit_does_not_skip_rows(): void {
|
||||
$rows = array( $this->don( 1 ), $this->don( 2 ), $this->don( 3 ) );
|
||||
$out = Payloads::alerts( $rows, 0, 2 );
|
||||
$this->assertSame( 2, $out['last_id'] );
|
||||
}
|
||||
|
||||
public function test_etag_stable_ignores_server_now_and_changes_with_data(): void {
|
||||
$a = Payloads::timer( '1000', null, 100 );
|
||||
$b = Payloads::timer( '1000', null, 101 );
|
||||
$this->assertSame( Payloads::etag( $a ), Payloads::etag( $b ) );
|
||||
$this->assertNotSame( Payloads::etag( $a ), Payloads::etag( Payloads::timer( '1060', null, 101 ) ) );
|
||||
$g = Payloads::goal( array( 'title' => 'T', 'target_minor' => 100, 'currency' => 'USD' ), 10 );
|
||||
$this->assertSame( Payloads::etag( $g ), Payloads::etag( $g ) );
|
||||
}
|
||||
|
||||
public function test_style_sanitizes(): void {
|
||||
$s = Style::fromQuery( array( 'theme' => 'bogus', 'accent' => '#FF00AA', 'color' => 'red;}</style>', 'bg' => '11223344',
|
||||
'font' => 'x;}body{', 'scale' => '99' ) );
|
||||
$this->assertSame( 'dark', $s['theme'] );
|
||||
$this->assertSame( array( '--sg-accent' => '#ff00aa', '--sg-bg' => '#11223344', '--sg-scale' => '3' ), $s['vars'] );
|
||||
$f = Style::fromQuery( array( 'font' => 'Comic Neue', 'theme' => 'NEON' ) );
|
||||
$this->assertSame( 'neon', $f['theme'] );
|
||||
$this->assertStringStartsWith( '"Comic Neue", ', $f['vars']['--sg-font'] );
|
||||
$this->assertArrayHasKey( '--sg-font', Style::fromQuery( array( 'font' => 'mono' ) )['vars'] );
|
||||
$this->assertSame( array(), Style::fromQuery( array( 'accent' => array( 'x' ) ) )['vars'] );
|
||||
}
|
||||
|
||||
public function test_service_routes_widgets(): void {
|
||||
$src = new class implements OverlayDataSource {
|
||||
public function activeGoal(): ?array { return array( 'id' => 1, 'title' => 'Goal', 'target_minor' => 1000, 'currency' => 'USD' ); }
|
||||
public function raisedMinor( array $goal ): int { return 250; }
|
||||
public function stateValue( int $goalId, string $key ): ?string { return 'timer_ends_at' === $key ? '2000' : null; }
|
||||
public function counter( int $goalId ): array { return array( 'label' => 'Pies', 'value' => 4 ); }
|
||||
public function donations( ?int $sinceId, int $limit ): array { return array( array( 'id' => 3, 'base_minor' => 100, 'currency' => 'USD', 'donor_name' => 'Z', 'is_anonymous' => 1, 'message' => '', 'status' => 'completed', 'moderation' => 'approved' ) ); }
|
||||
};
|
||||
$svc = new OverlayService( $src );
|
||||
$this->assertSame( 25, $svc->payload( 'goal', 1500 )['percent'] );
|
||||
$this->assertSame( 2000, $svc->payload( 'timer', 1500 )['ends_at'] );
|
||||
$this->assertSame( 4, $svc->payload( 'counter', 1 )['value'] );
|
||||
$this->assertSame( 'Anonymous', $svc->payload( 'alerts', 1, 0 )['alerts'][0]['name'] );
|
||||
$this->assertNull( $svc->payload( 'nope', 1 ) );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user