Add goals repository and configurable rules engine

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-15 08:44:52 -08:00
1 parent 939193f258
commit 7155396312
19 files changed
+882

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use StreamGoals\Rules\EventStore;
final class FakeEventStore implements EventStore {
public array $events = array(); // "rule:donation" => [delta, reversed]
public function record( int $ruleId, int $donationId, int $delta ): bool {
$k = "$ruleId:$donationId";
if ( isset( $this->events[ $k ] ) ) {
return false;
}
$this->events[ $k ] = array( $delta, false );
return true;
}
public function markReversed( int $ruleId, int $donationId ): ?int {
$k = "$ruleId:$donationId";
if ( ! isset( $this->events[ $k ] ) || $this->events[ $k ][1] ) {
return null;
}
$this->events[ $k ][1] = true;
return $this->events[ $k ][0];
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use StreamGoals\Rules\RuleStore;
final class FakeRuleStore implements RuleStore {
public function __construct( public array $rules = array() ) {}
public function enabledForGoal( int $goalId ): array {
return array_values( array_filter( $this->rules, static fn( $r ) => $r->goalId === $goalId ) );
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use StreamGoals\Rules\StateRepository;
final class FakeStateRepository implements StateRepository {
public array $data = array();
public function getInt( int $goalId, string $key, int $default = 0 ): int {
return $this->data[ "$goalId:$key" ] ?? $default;
}
public function setInt( int $goalId, string $key, int $value ): void {
$this->data[ "$goalId:$key" ] = $value;
}
}
+137
View File
@@ -0,0 +1,137 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Rules\Donation;
use StreamGoals\Rules\Engine;
use StreamGoals\Rules\RuleDef;
final class RulesEngineTest extends TestCase {
private FakeStateRepository $state;
private FakeEventStore $events;
private int $now = 1000;
private array $fired = array();
private function engine( array $rules ): Engine {
$this->state = new FakeStateRepository();
$this->events = new FakeEventStore();
return new Engine(
new FakeRuleStore( $rules ),
$this->events,
$this->state,
fn(): int => $this->now,
function ( string $e, RuleDef $r, Donation $d, bool $rev ): void {
$this->fired[] = array( $e, $d->id, $rev );
}
);
}
private function d( int $id, int $base, string $cur = 'USD' ): Donation {
return new Donation( $id, 1, $base, 0, $base, $cur );
}
private function timer( array $extra = array() ): RuleDef {
return new RuleDef( 1, 1, 'add_time', $extra + array( 'seconds_per_unit' => 60, 'unit_minor' => 100 ) );
}
private function counter( array $extra = array() ): RuleDef {
return new RuleDef( 2, 1, 'counter', $extra + array( 'step' => 1, 'per_minor' => 1000, 'carry_over' => true ) );
}
public function test_apply_persists_timer_counter_and_events(): void {
$e = $this->engine( array( $this->timer(), $this->counter() ) );
$e->applyDonation( $this->d( 10, 2500 ) );
$this->assertSame( 1000 + 1500, $this->state->getInt( 1, 'timer_ends_at' ) );
$this->assertSame( 2, $this->state->getInt( 1, 'counter:2' ) );
$this->assertSame( 500, $this->state->getInt( 1, 'carry:2' ) );
$this->assertSame( 1500, $this->events->events['1:10'][0] );
}
public function test_replay_is_idempotent(): void {
$e = $this->engine( array( $this->timer(), $this->counter() ) );
$d = $this->d( 10, 2500 );
$e->applyDonation( $d );
$snapshot = $this->state->data;
$e->applyDonation( $d );
$e->applyDonation( $d );
$this->assertSame( $snapshot, $this->state->data );
}
public function test_expired_timer_restarts_from_now(): void {
$e = $this->engine( array( $this->timer() ) );
$this->state->setInt( 1, 'timer_ends_at', 10 );
$e->applyDonation( $this->d( 10, 100 ) );
$this->assertSame( 1060, $this->state->getInt( 1, 'timer_ends_at' ) );
}
public function test_running_timer_extends(): void {
$e = $this->engine( array( $this->timer() ) );
$this->state->setInt( 1, 'timer_ends_at', 5000 );
$e->applyDonation( $this->d( 10, 100 ) );
$this->assertSame( 5060, $this->state->getInt( 1, 'timer_ends_at' ) );
}
public function test_max_total_across_donations(): void {
$e = $this->engine( array( $this->timer( array( 'max_total_seconds' => 100 ) ) ) );
$e->applyDonation( $this->d( 1, 100 ) );
$e->applyDonation( $this->d( 2, 100 ) );
$this->assertSame( 1000 + 100, $this->state->getInt( 1, 'timer_ends_at' ) );
}
public function test_reversal_undoes_and_is_not_double_applied(): void {
$e = $this->engine( array( $this->timer(), $this->counter() ) );
$d = $this->d( 10, 2500 );
$e->applyDonation( $d );
$e->reverseDonation( $d );
$e->reverseDonation( $d );
$this->assertSame( 1000, $this->state->getInt( 1, 'timer_ends_at' ) );
$this->assertSame( 0, $this->state->getInt( 1, 'counter:2' ) );
$this->assertSame( 0, $this->state->getInt( 1, 'carry:2' ) );
}
public function test_reversal_never_below_floor(): void {
$e = $this->engine( array( $this->timer(), $this->counter() ) );
$d = $this->d( 10, 2500 );
$e->applyDonation( $d );
$this->state->setInt( 1, 'counter:2', 1 ); // already lower than the delta
$this->now = 5000; // timer long expired
$e->reverseDonation( $d );
$this->assertSame( 0, $this->state->getInt( 1, 'counter:2' ) );
$this->assertSame( 2500, $this->state->getInt( 1, 'timer_ends_at' ) ); // untouched, not pushed below
}
public function test_reverse_on_refund_false_is_honored(): void {
$e = $this->engine( array( $this->counter( array( 'reverse_on_refund' => false ) ) ) );
$d = $this->d( 10, 2000 );
$e->applyDonation( $d );
$e->reverseDonation( $d );
$this->assertSame( 2, $this->state->getInt( 1, 'counter:2' ) );
}
public function test_reverse_unapplied_donation_is_noop(): void {
$e = $this->engine( array( $this->counter() ) );
$this->state->setInt( 1, 'counter:2', 5 );
$e->reverseDonation( $this->d( 99, 5000 ) );
$this->assertSame( 5, $this->state->getInt( 1, 'counter:2' ) );
}
public function test_bonus_fires_once_and_zero_decimal_currency(): void {
$bonus = new RuleDef( 3, 1, 'bonus', array( 'min_minor' => 5000, 'event' => 'big_donation' ) );
$e = $this->engine( array( $bonus, $this->counter( array( 'per_minor' => 1000 ) ) ) );
$d = $this->d( 7, 6000, 'JPY' );
$e->applyDonation( $d );
$e->applyDonation( $d );
$this->assertSame( array( array( 'big_donation', 7, false ) ), $this->fired );
$this->assertSame( 6, $this->state->getInt( 1, 'counter:2' ) );
}
public function test_bad_config_does_not_block_other_rules(): void {
$bad = new RuleDef( 9, 1, 'add_time', array( 'seconds_per_unit' => 1, 'unit_minor' => 0 ) );
$e = $this->engine( array( $bad, $this->counter() ) );
$e->applyDonation( $this->d( 1, 1000 ) );
$this->assertSame( 1, $this->state->getInt( 1, 'counter:2' ) );
}
}
+82
View File
@@ -0,0 +1,82 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Rules\AddTimeRule;
use StreamGoals\Rules\BonusRule;
use StreamGoals\Rules\CounterRule;
use StreamGoals\Rules\Donation;
final class RulesPureTest extends TestCase {
private function d( int $base, int $fee = 0, string $cur = 'USD' ): Donation {
return new Donation( 1, 1, $base, $fee, $base + $fee, $cur );
}
public function test_counter_carry_over(): void {
$r = new CounterRule( 1, 1000 );
$a = $r->apply( $this->d( 2500 ), 0, 0 );
$this->assertSame( array( 2, 2, 500 ), array( $a['delta'], $a['counter'], $a['carry'] ) );
$b = $r->apply( $this->d( 500 ), $a['counter'], $a['carry'] );
$this->assertSame( array( 1, 3, 0 ), array( $b['delta'], $b['counter'], $b['carry'] ) );
}
public function test_counter_without_carry_discards_remainder(): void {
$a = ( new CounterRule( 1, 1000, false ) )->apply( $this->d( 2500 ), 0, 700 );
$this->assertSame( array( 2, 0 ), array( $a['delta'], $a['carry'] ) );
}
public function test_counter_reverse_floors_at_zero_and_rolls_carry(): void {
$r = new CounterRule( 1, 1000 );
$x = $r->reverse( $this->d( 2500 ), 2, 1, 500 );
$this->assertSame( array( 0, 0 ), array( $x['counter'], $x['carry'] ) );
}
public function test_add_time_proportional_and_caps(): void {
$r = new AddTimeRule( 60, 100, 300, 400 );
$this->assertSame( 150, ( new AddTimeRule( 60, 100 ) )->apply( $this->d( 250 ), 0, 0, 1000 )['delta'] );
$this->assertSame( 300, $r->apply( $this->d( 10000 ), 0, 0, 1000 )['delta'] ); // per-donation cap
$this->assertSame( 100, $r->apply( $this->d( 10000 ), 0, 300, 1000 )['delta'] ); // total cap
$this->assertSame( 0, $r->apply( $this->d( 10000 ), 0, 400, 1000 )['delta'] );
}
public function test_add_time_expired_starts_from_now_and_running_extends(): void {
$r = new AddTimeRule( 60, 100 );
$this->assertSame( 1000 + 120, $r->apply( $this->d( 200 ), 500, 0, 1000 )['ends_at'] );
$this->assertSame( 2000 + 120, $r->apply( $this->d( 200 ), 2000, 0, 1000 )['ends_at'] );
}
public function test_add_time_reverse_floors_at_now(): void {
$r = new AddTimeRule( 60, 100 );
$this->assertSame( 1000, $r->reverse( 500, 1200, 500, 1000 )['ends_at'] );
$this->assertSame( 500, $r->reverse( 100, 500, 100, 1000 )['ends_at'] ); // expired: untouched
}
public function test_basis_base_vs_total(): void {
$d = $this->d( 1000, 100 );
$this->assertSame( 600, ( new AddTimeRule( 60, 100, null, null, 'base' ) )->apply( $d, 0, 0, 0 )['delta'] );
$this->assertSame( 660, ( new AddTimeRule( 60, 100, null, null, 'total' ) )->apply( $d, 0, 0, 0 )['delta'] );
$this->assertSame( 1, ( new CounterRule( 1, 1100, true, 'total' ) )->apply( $d, 0, 0 )['delta'] );
$this->assertSame( 0, ( new CounterRule( 1, 1100, true, 'base' ) )->apply( $d, 0, 0 )['delta'] );
}
public function test_zero_decimal_currency(): void {
// 500 JPY = 500 minor; 1 minute per 100 yen.
$d = $this->d( 500, 0, 'JPY' );
$this->assertSame( 300, ( new AddTimeRule( 60, 100 ) )->apply( $d, 0, 0, 0 )['delta'] );
$this->assertSame( 5, ( new CounterRule( 1, 100 ) )->apply( $d, 0, 0 )['delta'] );
}
public function test_bonus_threshold(): void {
$r = new BonusRule( 5000, 'big_donation' );
$this->assertSame( 0, $r->apply( $this->d( 4999 ) ) );
$this->assertSame( 1, $r->apply( $this->d( 5000 ) ) );
}
public function test_invalid_config_throws(): void {
$this->expectException( \InvalidArgumentException::class );
AddTimeRule::fromConfig( array( 'seconds_per_unit' => 1, 'unit_minor' => 0 ) );
}
}