56 lines
2.2 KiB
PHP
56 lines
2.2 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(); }
|
||
|
|
} );
|
||
|
|
|
||
|
|
$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'] );
|
||
|
|
}
|
||
|
|
}
|