Files
wp-donations/tests/Unit/EngineToOverlayIntegrationTest.php
shadowdaoandClaude Sonnet 5.5 c7f2dcffeb
Test and Release / test (push) Successful in 1m14s
Test and Release / release (push) Successful in 4s
Make bonus rules visible: highlighted BONUS line on the alerts overlay, streamgoals_bonus action
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 12:07:06 -08:00

109 lines
4.7 KiB
PHP

<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Overlay\OverlayDataSource;
use StreamGoals\Overlay\OverlayService;
use StreamGoals\Rules\Donation;
use StreamGoals\Rules\Engine;
use StreamGoals\Rules\RuleDef;
/**
* Guards the seam between the rules engine's sg_state keys and what the overlay reads.
* (A past mismatch of timer_ends_at vs ends_at would have shown a dead timer.)
*/
final class EngineToOverlayIntegrationTest extends TestCase {
public function test_donation_moves_timer_and_counter_overlays_and_refund_reverses(): void {
$state = new FakeStateRepository();
$now = 10000;
$engine = new Engine(
new FakeRuleStore( array(
new RuleDef( 1, 1, 'add_time', array( 'seconds_per_unit' => 60, 'unit_minor' => 100 ) ),
new RuleDef( 2, 1, 'counter', array( 'step' => 1, 'per_minor' => 1000, 'carry_over' => true, 'label' => 'Pies' ) ),
) ),
new FakeEventStore(),
$state,
fn(): int => $now
);
$svc = new OverlayService( new class( $state ) implements OverlayDataSource {
public function __construct( private FakeStateRepository $s ) {}
public function activeGoal(): ?array { return array( 'id' => 1, 'title' => 'G', 'target_minor' => 5000, 'currency' => 'USD' ); }
public function raisedMinor( array $goal ): int { return 0; }
public function stateValue( int $goalId, string $key ): ?string {
$v = $this->s->getInt( $goalId, $key, -1 );
return -1 === $v ? null : (string) $v;
}
public function counter( int $goalId ): array { return array( 'label' => 'Pies', 'value' => $this->s->getInt( $goalId, 'counter:2' ) ); }
public function donations( ?int $sinceId, int $limit ): array { return array(); }
public function bonusesFor( array $donationIds ): array { return array(); }
} );
$d = new Donation( 7, 1, 2500, 0, 2500, 'USD' ); // $25 -> +25 min, counter +2
$engine->applyDonation( $d );
$this->assertSame( $now + 1500, $svc->payload( 'timer', $now )['ends_at'] );
$this->assertSame( 2, $svc->payload( 'counter', $now )['value'] );
$engine->applyDonation( $d ); // webhook replay must not double count
$this->assertSame( $now + 1500, $svc->payload( 'timer', $now )['ends_at'] );
$engine->reverseDonation( $d );
$this->assertSame( 0, $svc->payload( 'counter', $now )['value'] );
}
private function bonusOverlay( FakeEventStore $events, array $donations ): OverlayService {
return new OverlayService( new class( $events, $donations ) implements OverlayDataSource {
public function __construct( private FakeEventStore $ev, private array $rows ) {}
public function activeGoal(): ?array { return array( 'id' => 1, 'title' => 'G', 'target_minor' => 5000, 'currency' => 'USD' ); }
public function raisedMinor( array $goal ): int { return 0; }
public function stateValue( int $goalId, string $key ): ?string { return null; }
public function counter( int $goalId ): array { return array( 'label' => '', 'value' => 0 ); }
public function donations( ?int $sinceId, int $limit ): array { return $this->rows; }
public function bonusesFor( array $donationIds ): array {
$out = array();
foreach ( $this->ev->events as $k => $e ) {
[ , $did ] = array_map( 'intval', explode( ':', $k ) );
if ( 1 === $e[0] && ! $e[1] && in_array( $did, $donationIds, true ) ) {
$out[ $did ][] = 'big_donation';
}
}
return $out;
}
} );
}
public function test_bonus_over_threshold_visible_in_alerts_below_is_not_and_refund_removes(): void {
$events = new FakeEventStore();
$fired = array();
$engine = new Engine(
new FakeRuleStore( array( new RuleDef( 3, 1, 'bonus', array( 'min_minor' => 5000, 'event' => 'big_donation' ) ) ) ),
$events,
new FakeStateRepository(),
null,
function ( string $event, RuleDef $r, Donation $d, bool $rev ) use ( &$fired ): void {
$fired[] = array( $event, $d->id, $rev );
}
);
$big = new Donation( 10, 1, 5000, 0, 5000, 'USD' );
$small = new Donation( 11, 1, 4999, 0, 4999, 'USD' );
$engine->applyDonation( $big );
$engine->applyDonation( $small );
$this->assertSame( array( array( 'big_donation', 10, false ) ), $fired );
$row = static fn( int $id ): array => array( 'id' => $id, 'base_minor' => 5000, 'currency' => 'USD', 'donor_name' => 'A',
'is_anonymous' => 0, 'message' => '', 'status' => 'completed', 'moderation' => 'approved' );
$svc = $this->bonusOverlay( $events, array( $row( 10 ), $row( 11 ) ) );
$alerts = $svc->payload( 'alerts', 1, 0 )['alerts'];
$this->assertSame( array( 'Big donation' ), $alerts[0]['bonuses'] );
$this->assertSame( array(), $alerts[1]['bonuses'] );
$engine->reverseDonation( $big );
$this->assertSame( array(), $svc->payload( 'alerts', 1, 0 )['alerts'][0]['bonuses'] );
$this->assertSame( true, $fired[1][2] );
}
}