Files
2meet-data-optimizer/tests/unit/AutoPromoterDriftTest.php
T
wpdev 9d1a7c5591 feat(2meet-data-optimizer): throttle shadow_read diffs per key and promote on clean drift checks
- The shadow diff logger records at most 20 rows per entity type + key per hour across all objects, on top of the existing per-object 5-minute window: a key stale on every object (WooCommerce total_sales, written by direct SQL) no longer logs a row per product read.
- The auto-promoter's third condition is now TMDO_Drift_Check::clean_since(): every backfillable group of the entity checked within 2 days (sampled or full) with no drift; no record, no promotion. The shadow diff count no longer gates it (the filter's 4th argument is passed as 0). The Promotion Guard still refuses HivePress / WooCommerce sites.
- Reason classification was dropped (decision 2026-09-27): it needs a schema change and drift-check already reports miss / diff / extra per key.
- Verify: ShadowDiffThrottleTest, AutoPromoterDriftTest (RED before); dev36 simulated user shadow_read: clean_since yes, promotion still refused by the guard, mode unchanged.
- PR: PR-075
2026-09-27 00:48:18 +08:00

79 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* TMDO_Auto_Promoter promotes shadow_read → aeav_only only when the last drift
* check of every group of the entity found nothing, within the last two days
* (2.2 PR-075; decision 2026-09-27). No record means no promotion.
*/
class AutoPromoterDriftTest extends TestCase {
private const TEN_DAYS_AGO = 864000;
public static function setUpBeforeClass(): void {
require_once TMDO_PATH . 'includes/engine/class-tmdo-auto-promoter.php';
}
protected function setUp(): void {
ini_set( 'error_log', '/dev/null' );
$GLOBALS['_wp_options'] = array( 'wpdo_bridge_modes' => array( 'user' => 'shadow_read' ) );
$GLOBALS['_wp_filter_callbacks'] = array();
$GLOBALS['_wp_current_user_can'] = array( 'manage_options' => true );
TMDO_Mode_Manager::reset_cache();
TMDO_Entity_Registry::init();
TMDO_Entity_Registry::register_adapter( 'user', new TMDO_Adapter_User() );
TMDO_Entity_Registry::register_group( 'user', 'ap_one', array( array( 'key' => 'ap_a', 'type' => 'text' ) ) );
TMDO_Entity_Registry::register_group( 'user', 'ap_two', array( array( 'key' => 'ap_b', 'type' => 'text' ) ) );
}
protected function tearDown(): void {
TMDO_Entity_Registry::init();
TMDO_Mode_Manager::reset_cache();
}
private function drift( string $group, int $diff, int $age = 0 ): void {
$GLOBALS['_wp_options'][ TMDO_Drift_Check::OPTION ]['user'][ $group ] = array(
'miss' => 0,
'diff' => $diff,
'extra' => 0,
'sample' => TMDO_Drift_Check::CRON_SAMPLE,
'checked_at' => gmdate( 'Y-m-d H:i:s', time() - $age ),
);
}
private function check(): string {
return TMDO_Auto_Promoter::check_entity( 'user', 7, array( 'user' => time() - self::TEN_DAYS_AGO ) );
}
public function test_promotes_when_every_group_checked_clean_recently(): void {
$this->drift( 'ap_one', 0 );
$this->drift( 'ap_two', 0 );
$this->assertSame( 'promoted', $this->check() );
$this->assertSame( 'aeav_only', TMDO_Mode_Manager::get( 'user' ) );
}
public function test_drift_in_one_group_blocks_promotion(): void {
$this->drift( 'ap_one', 0 );
$this->drift( 'ap_two', 1 );
$this->assertSame( 'drift_not_clean', $this->check() );
$this->assertSame( 'shadow_read', TMDO_Mode_Manager::get( 'user' ) );
}
public function test_a_group_never_checked_blocks_promotion(): void {
$this->drift( 'ap_one', 0 );
$this->assertSame( 'drift_not_clean', $this->check() );
}
public function test_a_check_older_than_two_days_blocks_promotion(): void {
$this->drift( 'ap_one', 0 );
$this->drift( 'ap_two', 0, 3 * DAY_IN_SECONDS );
$this->assertSame( 'drift_not_clean', $this->check() );
}
}