v0.3.2 — adopt estate CI: PHPUnit suite + reusable pipeline + governance workflows
- Convert 17 hand-rolled tests to PHPUnit TestCases (tests: stubs pattern) P0 security tests preserved: key-leak/encryption (KeyStoreTest), capture-allowlist/executor-path sentinel (CaptureWriterTest) - Add phpunit.xml.dist + update tests/bootstrap.php (Composer autoloader first) - Add phpunit/phpunit ^9.6 + yoast/phpunit-polyfills ^2.0 to require-dev - composer test -> vendor/bin/phpunit --configuration phpunit.xml.dist - ci.yml: thin caller of peptide-e2e reusable CI (tests: stubs, has_js: true, workflow_call trigger, permissions: contents: write) - deploy.yml: estate pattern caller of peptide-e2e deploy-app.yml@main (target_path: wp-content/plugins/pr-vision, gated on reusable CI) Pull-deploy on Hostinger stays as break-glass - Add cto-review.yml (Haiku-only, A/B panel dropped 2026-06-12) - Add main-push-audit.yml (direct-push tripwire) Agent-Session: claude-adopt-ci-20260616
This commit is contained in:
158
tests/unit/ProbeRunnerTest.php
Normal file
158
tests/unit/ProbeRunnerTest.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for PRV_Probe_Runner: budget-cap abort, run-lock, model health.
|
||||
*
|
||||
* @package PrVision
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PRV_Mock_Ledger extends PRV_Cost_Ledger {
|
||||
public int $afford_count = 0;
|
||||
public int $no_afford_count = 0;
|
||||
public int $exhaust_after = PHP_INT_MAX;
|
||||
|
||||
public function can_afford( float $estimated ): bool {
|
||||
$total = $this->afford_count + $this->no_afford_count;
|
||||
if ( $total >= $this->exhaust_after ) {
|
||||
$this->no_afford_count++;
|
||||
return false;
|
||||
}
|
||||
$this->afford_count++;
|
||||
return true;
|
||||
}
|
||||
public function update_row_cost( int $row_id, float $cost ): bool { return true; }
|
||||
public function get_month_to_date_usd(): float { return 0.0; }
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers PRV_Probe_Runner
|
||||
*/
|
||||
class ProbeRunnerTest extends TestCase {
|
||||
|
||||
private array $sonar_row;
|
||||
private string $cited_body;
|
||||
|
||||
protected function setUp(): void {
|
||||
prv_test_reset();
|
||||
if ( ! defined( 'PRV_OPENROUTER_API_KEY' ) ) {
|
||||
define( 'PRV_OPENROUTER_API_KEY', 'sk-or-test-key' );
|
||||
}
|
||||
if ( ! defined( 'PRV_CF_ACCOUNT_ID' ) ) {
|
||||
define( 'PRV_CF_ACCOUNT_ID', '' );
|
||||
}
|
||||
if ( ! defined( 'PRV_CF_GATEWAY_ID' ) ) {
|
||||
define( 'PRV_CF_GATEWAY_ID', '' );
|
||||
}
|
||||
$this->sonar_row = [ 'id' => 'mdl_t1', 'slug' => 'perplexity/sonar', 'provider' => 'perplexity', 'enabled' => true, 'note' => '', 'health_status' => 'unknown', 'health_probed' => 0, 'health_errors' => 0, 'health_run_id' => null ];
|
||||
$this->cited_body = json_encode( [ 'choices' => [ [ 'message' => [ 'content' => 'BPC-157 info.' ] ] ], 'citations' => [ 'https://peptiderepo.com/bpc-157/', 'https://examine.com' ], 'usage' => [ 'total_tokens' => 100 ] ] );
|
||||
}
|
||||
|
||||
private function setupRun( array $model_rows, array $peptides = null, array $intents = null ): void {
|
||||
PRV_Model_Registry::run_migration_v2();
|
||||
PRV_Config_Version::maybe_seed_initial_version();
|
||||
$GLOBALS['prv_test_state']['options']['prv_models'] = $model_rows;
|
||||
$GLOBALS['prv_test_state']['options'][ PRV_Model_Registry::VERSION_KEY ] = PRV_Model_Registry::SCHEMA_VERSION;
|
||||
$GLOBALS['prv_test_state']['options']['prv_peptides'] = $peptides ?? [ [ 'slug' => 'bpc-157', 'label' => 'BPC-157' ] ];
|
||||
$GLOBALS['prv_test_state']['options']['prv_prompt_intents'] = $intents ?? [ 'what is {peptide}' ];
|
||||
}
|
||||
|
||||
public function test_basic_run_1x1x1(): void {
|
||||
$this->setupRun( [ $this->sonar_row ] );
|
||||
$GLOBALS['prv_test_state']['remote_posts'] = [ [ 'response' => [ 'code' => 200 ], 'body' => $this->cited_body ] ];
|
||||
|
||||
$ledger = new PRV_Mock_Ledger();
|
||||
$counts = ( new PRV_Probe_Runner( $ledger ) )->run();
|
||||
|
||||
$this->assertSame( 1, $counts['probed'] );
|
||||
$this->assertSame( 0, $counts['skipped_budget'] );
|
||||
$this->assertSame( 0, $counts['skipped_error'] );
|
||||
$this->assertSame( 1, $ledger->afford_count );
|
||||
$this->assertNotEmpty( $counts['run_id'] );
|
||||
$this->assertFalse( $counts['truncated'] );
|
||||
}
|
||||
|
||||
public function test_budget_cap_abort(): void {
|
||||
$this->setupRun(
|
||||
[ $this->sonar_row ],
|
||||
[ [ 'slug' => 'bpc-157', 'label' => 'BPC-157' ], [ 'slug' => 'tb-500', 'label' => 'TB-500' ], [ 'slug' => 'mk-677', 'label' => 'MK-677' ] ]
|
||||
);
|
||||
$GLOBALS['prv_test_state']['remote_posts'] = array_fill( 0, 3, [ 'response' => [ 'code' => 200 ], 'body' => $this->cited_body ] );
|
||||
|
||||
$exh = new PRV_Mock_Ledger();
|
||||
$exh->exhaust_after = 1;
|
||||
$counts = ( new PRV_Probe_Runner( $exh ) )->run();
|
||||
|
||||
$this->assertSame( 1, $counts['probed'] );
|
||||
$this->assertGreaterThanOrEqual( 2, $counts['skipped_budget'] );
|
||||
$this->assertSame( 0, $counts['skipped_error'] );
|
||||
$this->assertTrue( $counts['truncated'] );
|
||||
}
|
||||
|
||||
public function test_http_error_counts_as_skipped_error(): void {
|
||||
$this->setupRun( [ $this->sonar_row ] );
|
||||
$GLOBALS['prv_test_state']['remote_posts'] = array_fill( 0, 3, [ 'response' => [ 'code' => 500 ], 'body' => 'Error' ] );
|
||||
|
||||
$counts = ( new PRV_Probe_Runner( new PRV_Mock_Ledger() ) )->run();
|
||||
$this->assertSame( 0, $counts['probed'] );
|
||||
$this->assertGreaterThanOrEqual( 1, $counts['skipped_error'] );
|
||||
}
|
||||
|
||||
public function test_wp_error_counts_as_skipped_error(): void {
|
||||
$this->setupRun( [ $this->sonar_row ] );
|
||||
$wp_error = new WP_Error( 'http_request_failed', 'cURL error 28' );
|
||||
$GLOBALS['prv_test_state']['remote_posts'] = [ $wp_error, $wp_error, $wp_error ];
|
||||
|
||||
$counts = ( new PRV_Probe_Runner( new PRV_Mock_Ledger() ) )->run();
|
||||
$this->assertSame( 0, $counts['probed'] );
|
||||
$this->assertGreaterThanOrEqual( 1, $counts['skipped_error'] );
|
||||
}
|
||||
|
||||
public function test_run_returns_all_required_keys(): void {
|
||||
$this->setupRun( [ $this->sonar_row ] );
|
||||
$GLOBALS['prv_test_state']['remote_posts'] = [ [ 'response' => [ 'code' => 200 ], 'body' => $this->cited_body ] ];
|
||||
|
||||
$counts = ( new PRV_Probe_Runner( new PRV_Mock_Ledger() ) )->run();
|
||||
foreach ( [ 'probed', 'skipped_budget', 'skipped_error', 'truncated', 'run_id' ] as $key ) {
|
||||
$this->assertArrayHasKey( $key, $counts );
|
||||
}
|
||||
}
|
||||
|
||||
public function test_run_lock_refused_when_locked(): void {
|
||||
$this->setupRun( [ $this->sonar_row ] );
|
||||
PRV_Run_Lock::acquire();
|
||||
|
||||
$ledger = new PRV_Mock_Ledger();
|
||||
$counts = ( new PRV_Probe_Runner( $ledger ) )->run();
|
||||
|
||||
$this->assertSame( 0, $counts['probed'] );
|
||||
$this->assertSame( -1, $counts['skipped_error'] );
|
||||
$this->assertSame( 0, $ledger->afford_count );
|
||||
PRV_Run_Lock::release();
|
||||
}
|
||||
|
||||
public function test_health_updated_after_successful_run(): void {
|
||||
$this->setupRun( [ $this->sonar_row ] );
|
||||
$GLOBALS['prv_test_state']['remote_posts'] = [ [ 'response' => [ 'code' => 200 ], 'body' => $this->cited_body ] ];
|
||||
|
||||
( new PRV_Probe_Runner( new PRV_Mock_Ledger() ) )->run();
|
||||
|
||||
foreach ( PRV_Model_Registry::get_all() as $m ) {
|
||||
if ( 'perplexity/sonar' === $m['slug'] ) {
|
||||
$this->assertSame( 'healthy', $m['health_status'] );
|
||||
$this->assertSame( 1, $m['health_probed'] );
|
||||
$this->assertSame( 0, $m['health_errors'] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function test_lock_released_after_successful_run(): void {
|
||||
$this->setupRun( [ $this->sonar_row ] );
|
||||
$GLOBALS['prv_test_state']['remote_posts'] = [ [ 'response' => [ 'code' => 200 ], 'body' => $this->cited_body ] ];
|
||||
|
||||
( new PRV_Probe_Runner( new PRV_Mock_Ledger() ) )->run();
|
||||
$this->assertFalse( PRV_Run_Lock::is_locked() );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user