peptide-geo-monitor v0.1.0: internal AI-visibility dashboard (#1)
New internal plugin: weekly LLM probes of core peptides (Perplexity sonar + GPT/Gemini-search via OpenRouter/CF gateway) -> custom table -> wp-admin trendline + standings. Provider interface + cost ledger w/ hard $5/mo cap + collector/panel seam (AI-visibility only). v1 = AI-visibility only. QA: APPROVE-WITH-P2-NOTES — qa-reviews/peptide-geo-monitor/2026-06-13-f488f4a966cbb9e771d3abc20daee3fa49074520.md (P2 follow-ups: ltrim www display bug, PHPCS mandatory, dead cache) Agent-Session: geomonitor v0.1.0 merge
This commit is contained in:
290
tests/bootstrap.php
Normal file
290
tests/bootstrap.php
Normal file
@@ -0,0 +1,290 @@
|
||||
<?php
|
||||
/**
|
||||
* Minimal test bootstrap — stubs all WordPress functions used by the plugin
|
||||
* so classes can be exercised in plain PHP without a WP install.
|
||||
*
|
||||
* Pattern mirrors peptide-repo-core's bootstrap (flat PHP, no PHPUnit).
|
||||
*
|
||||
* @package PeptideGeoMonitor
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/* ── Constants ────────────────────────────────────────────────────────── */
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
define( 'ABSPATH', __DIR__ . '/' );
|
||||
}
|
||||
|
||||
define( 'PGM_VERSION', '0.1.0' );
|
||||
define( 'PGM_PLUGIN_FILE', __DIR__ . '/../peptide-geo-monitor.php' );
|
||||
define( 'PGM_PLUGIN_DIR', realpath( __DIR__ . '/..' ) . '/' );
|
||||
define( 'PGM_PLUGIN_URL', 'http://example.test/wp-content/plugins/peptide-geo-monitor/' );
|
||||
define( 'PGM_SCHEMA_VERSION', 1 );
|
||||
define( 'PGM_MAX_RETRIES', 3 );
|
||||
define( 'PGM_API_TIMEOUT_SECONDS', 60 );
|
||||
define( 'PGM_RETRY_BASE_DELAY_SECONDS', 2 );
|
||||
define( 'PGM_DEFAULT_MONTHLY_BUDGET_USD', 5.0 );
|
||||
define( 'PGM_CRON_HOOK', 'pgm_weekly_probe' );
|
||||
define( 'PGM_TARGET_DOMAIN', 'peptiderepo.com' );
|
||||
|
||||
/* ── Global test state ─────────────────────────────────────────────────── */
|
||||
|
||||
$GLOBALS['pgm_test_state'] = [
|
||||
'options' => [],
|
||||
'actions' => [],
|
||||
'wpdb_insert_id' => 1,
|
||||
'wpdb_results' => [],
|
||||
'wpdb_var' => null,
|
||||
'cron_events' => [],
|
||||
'remote_posts' => [],
|
||||
];
|
||||
|
||||
function pgm_test_reset(): void {
|
||||
$GLOBALS['pgm_test_state'] = [
|
||||
'options' => [],
|
||||
'actions' => [],
|
||||
'wpdb_insert_id' => 1,
|
||||
'wpdb_results' => [],
|
||||
'wpdb_var' => null,
|
||||
'cron_events' => [],
|
||||
'remote_posts' => [],
|
||||
];
|
||||
PGM_Collector_Registry::reset_for_testing();
|
||||
}
|
||||
|
||||
/* ── WordPress function stubs ──────────────────────────────────────────── */
|
||||
|
||||
function get_option( string $name, $default = false ) {
|
||||
return $GLOBALS['pgm_test_state']['options'][ $name ] ?? $default;
|
||||
}
|
||||
|
||||
function update_option( string $name, $value ): bool {
|
||||
$GLOBALS['pgm_test_state']['options'][ $name ] = $value;
|
||||
return true;
|
||||
}
|
||||
|
||||
function add_option( string $name, $value = '' ): bool {
|
||||
if ( ! isset( $GLOBALS['pgm_test_state']['options'][ $name ] ) ) {
|
||||
$GLOBALS['pgm_test_state']['options'][ $name ] = $value;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function add_action( string $hook, $callback, int $priority = 10, int $accepted_args = 1 ): bool {
|
||||
$GLOBALS['pgm_test_state']['actions'][] = compact( 'hook', 'callback', 'priority' );
|
||||
return true;
|
||||
}
|
||||
|
||||
function remove_action( string $hook, $callback, int $priority = 10 ): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
function add_menu_page( ...$args ): void {}
|
||||
|
||||
function wp_schedule_event( int $timestamp, string $recurrence, string $hook ): void {
|
||||
$GLOBALS['pgm_test_state']['cron_events'][ $hook ] = $timestamp;
|
||||
}
|
||||
|
||||
function wp_next_scheduled( string $hook ) {
|
||||
return $GLOBALS['pgm_test_state']['cron_events'][ $hook ] ?? false;
|
||||
}
|
||||
|
||||
function wp_unschedule_event( int $timestamp, string $hook ): void {
|
||||
unset( $GLOBALS['pgm_test_state']['cron_events'][ $hook ] );
|
||||
}
|
||||
|
||||
function wp_clear_scheduled_hook( string $hook ): void {
|
||||
unset( $GLOBALS['pgm_test_state']['cron_events'][ $hook ] );
|
||||
}
|
||||
|
||||
function wp_rand( int $min = 0, int $max = 0 ): int {
|
||||
return random_int( $min ?: 0, $max ?: PHP_INT_MAX );
|
||||
}
|
||||
|
||||
function current_time( string $type, bool $gmt = false ): string {
|
||||
return gmdate( 'Y-m-d H:i:s' );
|
||||
}
|
||||
|
||||
function home_url( string $path = '' ): string {
|
||||
return 'http://example.test' . $path;
|
||||
}
|
||||
|
||||
function __( string $text, string $domain = 'default' ): string {
|
||||
return $text;
|
||||
}
|
||||
|
||||
function esc_html( string $text ): string {
|
||||
return htmlspecialchars( $text, ENT_QUOTES );
|
||||
}
|
||||
|
||||
function esc_html__( string $text, string $domain = 'default' ): string {
|
||||
return esc_html( $text );
|
||||
}
|
||||
|
||||
function esc_js( string $text ): string {
|
||||
return addslashes( $text );
|
||||
}
|
||||
|
||||
function esc_url( string $url ): string {
|
||||
return $url;
|
||||
}
|
||||
|
||||
function wp_json_encode( $data, int $flags = 0 ): string {
|
||||
return json_encode( $data, $flags ) ?: 'null';
|
||||
}
|
||||
|
||||
function wp_parse_url( string $url, int $component = -1 ) {
|
||||
return parse_url( $url, $component );
|
||||
}
|
||||
|
||||
function absint( $value ): int {
|
||||
return abs( (int) $value );
|
||||
}
|
||||
|
||||
function sanitize_text_field( string $str ): string {
|
||||
return trim( $str );
|
||||
}
|
||||
|
||||
function current_user_can( string $cap ): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
function wp_die( $message = '', $code = 0 ): void {
|
||||
throw new \RuntimeException( is_string( $message ) ? $message : 'wp_die' );
|
||||
}
|
||||
|
||||
function wp_verify_nonce( string $nonce, string $action ): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
function wp_nonce_field( string $action, string $name ): string {
|
||||
return '';
|
||||
}
|
||||
|
||||
function submit_button( string $text, string $type = 'primary', string $name = 'submit', bool $wrap = true ): void {}
|
||||
|
||||
function add_query_arg( array $args, string $url ): string {
|
||||
return $url . '?' . http_build_query( $args );
|
||||
}
|
||||
|
||||
function admin_url( string $path ): string {
|
||||
return 'http://example.test/wp-admin/' . ltrim( $path, '/' );
|
||||
}
|
||||
|
||||
function wp_safe_redirect( string $url ): void {}
|
||||
|
||||
function is_wp_error( $thing ): bool {
|
||||
return $thing instanceof WP_Error;
|
||||
}
|
||||
|
||||
function wp_remote_post( string $url, array $args = array() ) {
|
||||
// Return from test queue if set, else a generic success.
|
||||
$queue = &$GLOBALS['pgm_test_state']['remote_posts'];
|
||||
if ( ! empty( $queue ) ) {
|
||||
return array_shift( $queue );
|
||||
}
|
||||
return array( 'response' => array( 'code' => 200 ), 'body' => '{"choices":[{"message":{"content":"test"}}],"citations":[],"usage":{"total_tokens":100}}' );
|
||||
}
|
||||
|
||||
function wp_remote_retrieve_response_code( $response ): int {
|
||||
return (int) ( $response['response']['code'] ?? 200 );
|
||||
}
|
||||
|
||||
function wp_remote_retrieve_body( $response ): string {
|
||||
return (string) ( $response['body'] ?? '' );
|
||||
}
|
||||
|
||||
function wp_remote_retrieve_header( $response, string $header ): string {
|
||||
return (string) ( $response['headers'][ $header ] ?? '' );
|
||||
}
|
||||
|
||||
class WP_Error {
|
||||
public function __construct( private string $code = '', private string $message = '' ) {}
|
||||
public function get_error_message(): string { return $this->message; }
|
||||
}
|
||||
|
||||
/* ── Minimal $wpdb stub ────────────────────────────────────────────────── */
|
||||
|
||||
class stdClass_wpdb {
|
||||
public string $prefix = 'wp_';
|
||||
public int $insert_id = 0;
|
||||
public array $_results = [];
|
||||
public mixed $_var = null;
|
||||
|
||||
public function get_charset_collate(): string {
|
||||
return 'DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci';
|
||||
}
|
||||
public function prepare( string $query, ...$args ): string {
|
||||
return vsprintf( str_replace( '%s', "'%s'", $query ), array_map( 'addslashes', $args ) );
|
||||
}
|
||||
public function query( string $sql ): bool { return true; }
|
||||
public function insert( string $table, array $data, array $format = [] ): int {
|
||||
$this->insert_id = $GLOBALS['pgm_test_state']['wpdb_insert_id']++;
|
||||
return 1;
|
||||
}
|
||||
public function update( string $table, array $data, array $where, array $data_format = [], array $where_format = [] ): int {
|
||||
return 1;
|
||||
}
|
||||
public function get_results( string $query, string $output = 'OBJECT' ): array {
|
||||
return is_array( $GLOBALS['pgm_test_state']['wpdb_results'] ) ? $GLOBALS['pgm_test_state']['wpdb_results'] : [];
|
||||
}
|
||||
public function get_var( string $query ): mixed {
|
||||
return $GLOBALS['pgm_test_state']['wpdb_var'];
|
||||
}
|
||||
}
|
||||
|
||||
$wpdb = new stdClass_wpdb();
|
||||
|
||||
function dbDelta( string $sql ): array { return []; }
|
||||
|
||||
/* ── Test assertion helpers ────────────────────────────────────────────── */
|
||||
|
||||
$GLOBALS['pgm_test_report'] = [ 'pass' => 0, 'fail' => 0, 'failures' => [] ];
|
||||
|
||||
function pgm_assert( bool $condition, string $label ): void {
|
||||
if ( $condition ) {
|
||||
$GLOBALS['pgm_test_report']['pass']++;
|
||||
echo " PASS: {$label}\n";
|
||||
} else {
|
||||
$GLOBALS['pgm_test_report']['fail']++;
|
||||
$GLOBALS['pgm_test_report']['failures'][] = $label;
|
||||
echo " FAIL: {$label}\n";
|
||||
}
|
||||
}
|
||||
|
||||
function pgm_assert_equals( $expected, $actual, string $label ): void {
|
||||
$pass = ( $expected === $actual );
|
||||
pgm_assert( $pass, $label . ( $pass ? '' : ' — expected ' . var_export( $expected, true ) . ', got ' . var_export( $actual, true ) ) );
|
||||
}
|
||||
|
||||
function pgm_assert_throws( callable $fn, string $exception_class, string $label ): void {
|
||||
try {
|
||||
$fn();
|
||||
pgm_assert( false, $label . ' — expected ' . $exception_class . ' but no exception thrown' );
|
||||
} catch ( \Throwable $e ) {
|
||||
pgm_assert( $e instanceof $exception_class, $label . ' — got ' . get_class( $e ) );
|
||||
}
|
||||
}
|
||||
|
||||
function pgm_test_summary(): int {
|
||||
$r = $GLOBALS['pgm_test_report'];
|
||||
echo "\n---\n";
|
||||
echo "Totals: {$r['pass']} passed, {$r['fail']} failed\n";
|
||||
if ( $r['fail'] > 0 ) {
|
||||
echo "Failures:\n";
|
||||
foreach ( $r['failures'] as $f ) {
|
||||
echo " - {$f}\n";
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ── Load autoloader + all plugin classes ──────────────────────────────── */
|
||||
|
||||
require_once __DIR__ . '/../includes/core/class-pgm-autoloader.php';
|
||||
PGM_Autoloader::register();
|
||||
require_once __DIR__ . '/../includes/providers/interface-pgm-probe-provider.php';
|
||||
require_once __DIR__ . '/../includes/collector/interface-pgm-data-collector.php';
|
||||
require_once __DIR__ . '/../includes/panel/interface-pgm-dashboard-panel.php';
|
||||
81
tests/unit/test-citation-detector.php
Normal file
81
tests/unit/test-citation-detector.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for PGM_Citation_Detector: domain extraction and cite detection.
|
||||
*
|
||||
* @package PeptideGeoMonitor
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../bootstrap.php';
|
||||
|
||||
echo "=== PGM Citation Detector Tests ===\n";
|
||||
|
||||
// ── Helpers ──────────────────────────────────────────────────────────────
|
||||
|
||||
$detector = new PGM_Citation_Detector();
|
||||
|
||||
// ── Test: parse plain URL strings ────────────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$domains = $detector->parse_domains( [
|
||||
'https://peptiderepo.com/bpc-157/',
|
||||
'https://www.examine.com/supplements/bpc-157/',
|
||||
'https://pubmed.ncbi.nlm.nih.gov/12345',
|
||||
] );
|
||||
|
||||
pgm_assert( in_array( 'peptiderepo.com', $domains, true ), 'parse_domains: peptiderepo.com extracted from URL' );
|
||||
pgm_assert( in_array( 'examine.com', $domains, true ), 'parse_domains: www. stripped from examine.com' );
|
||||
pgm_assert( in_array( 'pubmed.ncbi.nlm.nih.gov', $domains, true ), 'parse_domains: full subdomain preserved when not www.' );
|
||||
pgm_assert_equals( 3, count( $domains ), 'parse_domains: exactly 3 unique domains' );
|
||||
|
||||
// ── Test: parse object-style citations (Perplexity format) ───────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$domains = $detector->parse_domains( [
|
||||
[ 'url' => 'https://peptiderepo.com/tb-500/' ],
|
||||
[ 'url' => 'https://examine.com/tb500' ],
|
||||
[ 'url' => 'https://peptiderepo.com/bpc-157/' ], // duplicate domain
|
||||
] );
|
||||
|
||||
pgm_assert_equals( 2, count( $domains ), 'parse_domains: object-style citations, duplicates removed' );
|
||||
pgm_assert( in_array( 'peptiderepo.com', $domains, true ), 'parse_domains: object-style, peptiderepo.com present' );
|
||||
|
||||
// ── Test: parse empty input ──────────────────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$domains = $detector->parse_domains( [] );
|
||||
pgm_assert_equals( [], $domains, 'parse_domains: empty input returns empty array' );
|
||||
|
||||
// ── Test: is_cited detection ─────────────────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
pgm_assert( $detector->is_cited( ['peptiderepo.com', 'examine.com'] ), 'is_cited: returns true when target present' );
|
||||
pgm_assert( ! $detector->is_cited( ['examine.com', 'pubmed.ncbi.nlm.nih.gov'] ), 'is_cited: returns false when target absent' );
|
||||
pgm_assert( ! $detector->is_cited( [] ), 'is_cited: returns false for empty list' );
|
||||
|
||||
// ── Test: get_our_position ───────────────────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$domains = ['examine.com', 'peptiderepo.com', 'pubmed.ncbi.nlm.nih.gov'];
|
||||
pgm_assert_equals( 2, $detector->get_our_position( $domains ), 'get_our_position: 1-based, position 2' );
|
||||
|
||||
$domains_first = ['peptiderepo.com', 'examine.com'];
|
||||
pgm_assert_equals( 1, $detector->get_our_position( $domains_first ), 'get_our_position: position 1 when first' );
|
||||
|
||||
$domains_absent = ['examine.com', 'pubmed.ncbi.nlm.nih.gov'];
|
||||
pgm_assert_equals( null, $detector->get_our_position( $domains_absent ), 'get_our_position: null when not present' );
|
||||
|
||||
// ── Test: malformed / unexpected input ───────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$domains = $detector->parse_domains( [
|
||||
'not-a-url',
|
||||
42,
|
||||
null,
|
||||
['no_url_key' => 'foo'],
|
||||
['url' => ''],
|
||||
] );
|
||||
pgm_assert_equals( [], $domains, 'parse_domains: gracefully skips malformed items' );
|
||||
|
||||
exit( pgm_test_summary() );
|
||||
85
tests/unit/test-cost-ledger.php
Normal file
85
tests/unit/test-cost-ledger.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for PGM_Cost_Ledger: MTD cost retrieval, budget pre-check, and
|
||||
* the budget-cap abort behaviour.
|
||||
*
|
||||
* @package PeptideGeoMonitor
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../bootstrap.php';
|
||||
|
||||
echo "=== PGM Cost Ledger Tests ===\n";
|
||||
|
||||
// ── Test: can_afford when below cap ─────────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_monthly_budget_usd'] = 5.0;
|
||||
$GLOBALS['pgm_test_state']['wpdb_var'] = '2.50'; // MTD spent
|
||||
|
||||
$ledger = new PGM_Cost_Ledger();
|
||||
pgm_assert( $ledger->can_afford( 1.0 ), 'can_afford: 2.50 spent + 1.00 = 3.50 < 5.00 → true' );
|
||||
|
||||
// ── Test: can_afford when at cap ─────────────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_monthly_budget_usd'] = 5.0;
|
||||
$GLOBALS['pgm_test_state']['wpdb_var'] = '5.00';
|
||||
|
||||
$ledger = new PGM_Cost_Ledger();
|
||||
pgm_assert( ! $ledger->can_afford( 0.01 ), 'can_afford: 5.00 spent + 0.01 > 5.00 → false' );
|
||||
|
||||
// ── Test: can_afford when exactly at cap ─────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_monthly_budget_usd'] = 5.0;
|
||||
$GLOBALS['pgm_test_state']['wpdb_var'] = '4.99';
|
||||
|
||||
$ledger = new PGM_Cost_Ledger();
|
||||
pgm_assert( $ledger->can_afford( 0.01 ), 'can_afford: 4.99 + 0.01 = 5.00 exactly → true (at cap is allowed)' );
|
||||
|
||||
// ── Test: get_remaining_budget_usd ──────────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_monthly_budget_usd'] = 5.0;
|
||||
$GLOBALS['pgm_test_state']['wpdb_var'] = '3.25';
|
||||
|
||||
$ledger = new PGM_Cost_Ledger();
|
||||
pgm_assert_equals( 1.75, round( $ledger->get_remaining_budget_usd(), 2 ), 'get_remaining_budget_usd: 5.00 - 3.25 = 1.75' );
|
||||
|
||||
// ── Test: get_remaining_budget_usd never negative ────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_monthly_budget_usd'] = 5.0;
|
||||
$GLOBALS['pgm_test_state']['wpdb_var'] = '10.00'; // over cap
|
||||
|
||||
$ledger = new PGM_Cost_Ledger();
|
||||
pgm_assert_equals( 0.0, $ledger->get_remaining_budget_usd(), 'get_remaining_budget_usd: clamps to 0.0 when over cap' );
|
||||
|
||||
// ── Test: get_month_to_date_usd with null DB result (no rows yet) ────────
|
||||
|
||||
pgm_test_reset();
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_monthly_budget_usd'] = 5.0;
|
||||
$GLOBALS['pgm_test_state']['wpdb_var'] = null;
|
||||
|
||||
$ledger = new PGM_Cost_Ledger();
|
||||
pgm_assert_equals( 0.0, $ledger->get_month_to_date_usd(), 'get_month_to_date_usd: null DB result → 0.0' );
|
||||
|
||||
// ── Test: update_row_cost returns true ──────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$ledger = new PGM_Cost_Ledger();
|
||||
pgm_assert( $ledger->update_row_cost( 42, 0.00123456 ), 'update_row_cost: returns true on success' );
|
||||
|
||||
// ── Test: budget abort mid-run (runner stops when cap hit) ──────────────
|
||||
|
||||
pgm_test_reset();
|
||||
// Simulate a nearly-exhausted budget.
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_monthly_budget_usd'] = 0.001; // $0.001 cap
|
||||
$GLOBALS['pgm_test_state']['wpdb_var'] = '0.001'; // fully spent
|
||||
|
||||
$ledger2 = new PGM_Cost_Ledger();
|
||||
pgm_assert( ! $ledger2->can_afford( 0.000001 ), 'budget abort: can_afford returns false at cap → runner would stop' );
|
||||
|
||||
exit( pgm_test_summary() );
|
||||
64
tests/unit/test-cron.php
Normal file
64
tests/unit/test-cron.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for PGM_Cron: scheduling, clearing, and registration.
|
||||
*
|
||||
* @package PeptideGeoMonitor
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../bootstrap.php';
|
||||
|
||||
echo "=== PGM Cron Tests ===\n";
|
||||
|
||||
// ── Test: schedule_weekly adds the event ────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
pgm_assert( ! PGM_Cron::is_scheduled(), 'schedule_weekly: not scheduled before call' );
|
||||
|
||||
PGM_Cron::schedule_weekly();
|
||||
pgm_assert( PGM_Cron::is_scheduled(), 'schedule_weekly: event scheduled after call' );
|
||||
|
||||
// ── Test: schedule_weekly is idempotent ──────────────────────────────────
|
||||
|
||||
PGM_Cron::schedule_weekly(); // Second call should no-op.
|
||||
$events = $GLOBALS['pgm_test_state']['cron_events'];
|
||||
pgm_assert_equals( 1, count( $events ), 'schedule_weekly: idempotent — second call does not add duplicate' );
|
||||
|
||||
// ── Test: clear_schedule removes the event ──────────────────────────────
|
||||
|
||||
PGM_Cron::clear_schedule();
|
||||
pgm_assert( ! PGM_Cron::is_scheduled(), 'clear_schedule: event removed after clear' );
|
||||
|
||||
// ── Test: clear_schedule is safe when nothing scheduled ─────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
PGM_Cron::clear_schedule(); // Should not throw.
|
||||
pgm_assert( ! PGM_Cron::is_scheduled(), 'clear_schedule: safe when nothing scheduled' );
|
||||
|
||||
// ── Test: register_hooks registers the cron hook action ─────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$cron = new PGM_Cron();
|
||||
$cron->register_hooks();
|
||||
|
||||
$hooks = array_column( $GLOBALS['pgm_test_state']['actions'], 'hook' );
|
||||
pgm_assert( in_array( PGM_CRON_HOOK, $hooks, true ), 'register_hooks: pgm_weekly_probe action registered' );
|
||||
|
||||
// ── Test: hook constant is correct string ────────────────────────────────
|
||||
|
||||
pgm_assert_equals( 'pgm_weekly_probe', PGM_CRON_HOOK, 'PGM_CRON_HOOK: constant value correct' );
|
||||
|
||||
// ── Test: PGM_Activator calls schedule_weekly ────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
// Seed options so seed_defaults() doesn't fail.
|
||||
PGM_Activator::activate();
|
||||
pgm_assert( PGM_Cron::is_scheduled(), 'PGM_Activator::activate: weekly cron scheduled after activation' );
|
||||
|
||||
// ── Test: PGM_Deactivator calls clear_schedule ───────────────────────────
|
||||
|
||||
PGM_Deactivator::deactivate();
|
||||
pgm_assert( ! PGM_Cron::is_scheduled(), 'PGM_Deactivator::deactivate: cron cleared after deactivation' );
|
||||
|
||||
exit( pgm_test_summary() );
|
||||
180
tests/unit/test-probe-runner.php
Normal file
180
tests/unit/test-probe-runner.php
Normal file
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for PGM_Probe_Runner: budget-cap abort, in-run caching, and run mechanics.
|
||||
*
|
||||
* Uses queued mock HTTP responses (via $GLOBALS['pgm_test_state']['remote_posts'])
|
||||
* and a mock ledger to verify runner behaviour without real API calls.
|
||||
*
|
||||
* @package PeptideGeoMonitor
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../bootstrap.php';
|
||||
|
||||
echo "=== PGM Probe Runner Tests ===\n";
|
||||
|
||||
// ── Define API key constants (required by providers) ──────────────────────
|
||||
|
||||
if ( ! defined( 'PGM_OPENROUTER_API_KEY' ) ) {
|
||||
define( 'PGM_OPENROUTER_API_KEY', 'sk-or-test-key' );
|
||||
}
|
||||
if ( ! defined( 'PGM_CF_ACCOUNT_ID' ) ) {
|
||||
define( 'PGM_CF_ACCOUNT_ID', '' );
|
||||
}
|
||||
if ( ! defined( 'PGM_CF_GATEWAY_ID' ) ) {
|
||||
define( 'PGM_CF_GATEWAY_ID', '' );
|
||||
}
|
||||
|
||||
// ── Mock ledger: tracks afford calls, simulates exhaustion ────────────────
|
||||
|
||||
class PGM_Mock_Ledger extends PGM_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; }
|
||||
}
|
||||
|
||||
// ── Shared mock response body (Perplexity / cited) ────────────────────────
|
||||
|
||||
$cited_body = json_encode([
|
||||
'choices' => [['message' => ['content' => 'BPC-157 info.']]],
|
||||
'citations' => ['https://peptiderepo.com/bpc-157/', 'https://examine.com'],
|
||||
'usage' => ['total_tokens' => 100],
|
||||
]);
|
||||
|
||||
// ── Test: basic run completes — 1×1×1 (peptide × intent × model) ─────────
|
||||
|
||||
pgm_test_reset();
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_peptides'] = [['slug'=>'bpc-157','label'=>'BPC-157']];
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_prompt_intents'] = ['what is {peptide}'];
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_models'] = ['perplexity/sonar'];
|
||||
$GLOBALS['pgm_test_state']['remote_posts'] = [
|
||||
['response' => ['code' => 200], 'body' => $cited_body],
|
||||
];
|
||||
|
||||
$ledger1 = new PGM_Mock_Ledger();
|
||||
$runner1 = new PGM_Probe_Runner( $ledger1 );
|
||||
$counts1 = $runner1->run();
|
||||
|
||||
pgm_assert_equals( 1, $counts1['probed'], 'basic run: 1 probed' );
|
||||
pgm_assert_equals( 0, $counts1['skipped_budget'], 'basic run: 0 budget skips' );
|
||||
pgm_assert_equals( 0, $counts1['skipped_error'], 'basic run: 0 error skips' );
|
||||
pgm_assert_equals( 1, $ledger1->afford_count, 'basic run: 1 can_afford check' );
|
||||
|
||||
// ── Test: 2×1×1 run completes all probes when budget sufficient ───────────
|
||||
|
||||
pgm_test_reset();
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_peptides'] = [
|
||||
['slug'=>'bpc-157','label'=>'BPC-157'],
|
||||
['slug'=>'tb-500','label'=>'TB-500'],
|
||||
];
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_prompt_intents'] = ['what is {peptide}'];
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_models'] = ['perplexity/sonar'];
|
||||
$GLOBALS['pgm_test_state']['remote_posts'] = [
|
||||
['response' => ['code' => 200], 'body' => $cited_body],
|
||||
['response' => ['code' => 200], 'body' => $cited_body],
|
||||
];
|
||||
|
||||
$ledger2 = new PGM_Mock_Ledger();
|
||||
$runner2 = new PGM_Probe_Runner( $ledger2 );
|
||||
$counts2 = $runner2->run();
|
||||
|
||||
pgm_assert_equals( 2, $counts2['probed'], '2×1×1 run: 2 probes completed' );
|
||||
pgm_assert_equals( 2, $ledger2->afford_count, '2×1×1 run: 2 can_afford checks' );
|
||||
|
||||
// ── Test: budget cap abort after N probes ────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_peptides'] = [
|
||||
['slug'=>'bpc-157','label'=>'BPC-157'],
|
||||
['slug'=>'tb-500','label'=>'TB-500'],
|
||||
['slug'=>'mk-677','label'=>'MK-677'],
|
||||
];
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_prompt_intents'] = ['what is {peptide}'];
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_models'] = ['perplexity/sonar'];
|
||||
// Queue 3 responses but cap budget at 1.
|
||||
$GLOBALS['pgm_test_state']['remote_posts'] = [
|
||||
['response' => ['code' => 200], 'body' => $cited_body],
|
||||
['response' => ['code' => 200], 'body' => $cited_body],
|
||||
['response' => ['code' => 200], 'body' => $cited_body],
|
||||
];
|
||||
|
||||
$exhausting_ledger = new PGM_Mock_Ledger();
|
||||
$exhausting_ledger->exhaust_after = 1; // Budget exhausted after 1st can_afford check.
|
||||
|
||||
$runner3 = new PGM_Probe_Runner( $exhausting_ledger );
|
||||
$counts3 = $runner3->run();
|
||||
|
||||
pgm_assert_equals( 1, $counts3['probed'], 'budget abort: 1 probe completed before cap' );
|
||||
pgm_assert( $counts3['skipped_budget'] >= 2, 'budget abort: ≥2 probes skipped (budget)' );
|
||||
pgm_assert_equals( 0, $counts3['skipped_error'], 'budget abort: 0 error skips' );
|
||||
|
||||
// ── Test: HTTP error counts as skipped_error ─────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_peptides'] = [['slug'=>'bpc-157','label'=>'BPC-157']];
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_prompt_intents'] = ['what is {peptide}'];
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_models'] = ['perplexity/sonar'];
|
||||
// All retries fail with 500.
|
||||
$GLOBALS['pgm_test_state']['remote_posts'] = [
|
||||
['response' => ['code' => 500], 'body' => 'Error'],
|
||||
['response' => ['code' => 500], 'body' => 'Error'],
|
||||
['response' => ['code' => 500], 'body' => 'Error'],
|
||||
];
|
||||
|
||||
$ledger4 = new PGM_Mock_Ledger();
|
||||
$runner4 = new PGM_Probe_Runner( $ledger4 );
|
||||
$counts4 = $runner4->run();
|
||||
|
||||
pgm_assert_equals( 0, $counts4['probed'], 'HTTP 500: 0 probes' );
|
||||
pgm_assert( $counts4['skipped_error'] >= 1, 'HTTP 500: ≥1 error skip recorded' );
|
||||
|
||||
// ── Test: WP_Error from wp_remote_post counts as skipped_error ──────────
|
||||
|
||||
pgm_test_reset();
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_peptides'] = [['slug'=>'bpc-157','label'=>'BPC-157']];
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_prompt_intents'] = ['what is {peptide}'];
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_models'] = ['perplexity/sonar'];
|
||||
// Return WP_Error for all retries.
|
||||
$wp_error = new WP_Error( 'http_request_failed', 'cURL error 28: Operation timed out' );
|
||||
$GLOBALS['pgm_test_state']['remote_posts'] = [ $wp_error, $wp_error, $wp_error ];
|
||||
|
||||
$ledger5 = new PGM_Mock_Ledger();
|
||||
$runner5 = new PGM_Probe_Runner( $ledger5 );
|
||||
$counts5 = $runner5->run();
|
||||
|
||||
pgm_assert_equals( 0, $counts5['probed'], 'WP_Error: 0 probes completed' );
|
||||
pgm_assert( $counts5['skipped_error'] >= 1, 'WP_Error: ≥1 error skip recorded' );
|
||||
|
||||
// ── Test: run returns correct keys in counts array ───────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_peptides'] = [['slug'=>'bpc-157','label'=>'BPC-157']];
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_prompt_intents'] = ['what is {peptide}'];
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_models'] = ['perplexity/sonar'];
|
||||
$GLOBALS['pgm_test_state']['remote_posts'] = [
|
||||
['response' => ['code' => 200], 'body' => $cited_body],
|
||||
];
|
||||
|
||||
$ledger6 = new PGM_Mock_Ledger();
|
||||
$runner6 = new PGM_Probe_Runner( $ledger6 );
|
||||
$counts6 = $runner6->run();
|
||||
|
||||
pgm_assert( array_key_exists( 'probed', $counts6 ), 'run: returns probed key' );
|
||||
pgm_assert( array_key_exists( 'skipped_budget', $counts6 ), 'run: returns skipped_budget key' );
|
||||
pgm_assert( array_key_exists( 'skipped_error', $counts6 ), 'run: returns skipped_error key' );
|
||||
|
||||
exit( pgm_test_summary() );
|
||||
135
tests/unit/test-provider-parsing.php
Normal file
135
tests/unit/test-provider-parsing.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for provider response parsing.
|
||||
*
|
||||
* Exercises PGM_Perplexity_Provider::parse_response() and
|
||||
* PGM_OpenRouter_Provider::parse_response() with mock HTTP responses,
|
||||
* verifying domain extraction, citation detection, and cost calculation.
|
||||
*
|
||||
* @package PeptideGeoMonitor
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../bootstrap.php';
|
||||
|
||||
echo "=== PGM Provider Response Parsing Tests ===\n";
|
||||
|
||||
// ── PGM_Perplexity_Provider parsing ─────────────────────────────────────
|
||||
|
||||
$provider = new PGM_Perplexity_Provider( null, new PGM_Citation_Detector() );
|
||||
|
||||
// Test: cited via citations array
|
||||
pgm_test_reset();
|
||||
$data = [
|
||||
'choices' => [[ 'message' => [ 'content' => 'BPC-157 is a peptide. See peptiderepo.com' ] ]],
|
||||
'citations' => [
|
||||
'https://peptiderepo.com/bpc-157/',
|
||||
'https://examine.com/bpc-157',
|
||||
],
|
||||
'usage' => [ 'total_tokens' => 500 ],
|
||||
];
|
||||
$result = $provider->parse_response( $data );
|
||||
pgm_assert( $result->is_cited(), 'Perplexity: cited=true when peptiderepo.com in citations' );
|
||||
pgm_assert_equals( 1, $result->get_our_position(), 'Perplexity: position=1 when first in citations' );
|
||||
pgm_assert( count( $result->get_source_domains() ) === 2, 'Perplexity: 2 source domains extracted' );
|
||||
pgm_assert_equals( 500 * 0.000001, $result->get_cost_usd(), 'Perplexity: cost from token usage' );
|
||||
|
||||
// Test: not cited, no citations
|
||||
pgm_test_reset();
|
||||
$data_empty = [
|
||||
'choices' => [[ 'message' => [ 'content' => 'BPC-157 info here.' ] ]],
|
||||
'citations' => [],
|
||||
'usage' => [ 'total_tokens' => 200 ],
|
||||
];
|
||||
$result_empty = $provider->parse_response( $data_empty );
|
||||
pgm_assert( ! $result_empty->is_cited(), 'Perplexity: cited=false with empty citations' );
|
||||
pgm_assert_equals( null, $result_empty->get_our_position(), 'Perplexity: position=null when not cited' );
|
||||
|
||||
// Test: excerpt truncated to 500 chars
|
||||
pgm_test_reset();
|
||||
$long_content = str_repeat( 'a', 600 );
|
||||
$data_long = [
|
||||
'choices' => [[ 'message' => [ 'content' => $long_content ] ]],
|
||||
'citations' => [],
|
||||
];
|
||||
$result_long = $provider->parse_response( $data_long );
|
||||
pgm_assert_equals( 500, mb_strlen( $result_long->get_raw_excerpt() ), 'Perplexity: excerpt truncated to 500 chars' );
|
||||
|
||||
// Test: object-style citations
|
||||
pgm_test_reset();
|
||||
$data_obj = [
|
||||
'choices' => [[ 'message' => [ 'content' => 'answer' ] ]],
|
||||
'citations' => [
|
||||
[ 'url' => 'https://peptiderepo.com/mk-677/' ],
|
||||
[ 'url' => 'https://examine.com/mk677' ],
|
||||
],
|
||||
];
|
||||
$result_obj = $provider->parse_response( $data_obj );
|
||||
pgm_assert( $result_obj->is_cited(), 'Perplexity: object-style citations — cited=true' );
|
||||
pgm_assert_equals( 1, $result_obj->get_our_position(), 'Perplexity: object-style — position=1' );
|
||||
|
||||
// Test: fallback cost when no usage data
|
||||
pgm_test_reset();
|
||||
$data_no_usage = [
|
||||
'choices' => [[ 'message' => [ 'content' => 'answer' ] ]],
|
||||
'citations' => [],
|
||||
];
|
||||
$result_no_usage = $provider->parse_response( $data_no_usage );
|
||||
pgm_assert_equals( PGM_Perplexity_Provider::ESTIMATED_COST_PER_PROBE, $result_no_usage->get_cost_usd(), 'Perplexity: fallback to ESTIMATED_COST_PER_PROBE when no usage' );
|
||||
|
||||
// ── PGM_OpenRouter_Provider parsing ─────────────────────────────────────
|
||||
|
||||
echo "\n--- OpenRouter provider parsing ---\n";
|
||||
|
||||
$or_provider = new PGM_OpenRouter_Provider( 'openai/gpt-4o-search-preview', null, new PGM_Citation_Detector() );
|
||||
|
||||
// Test: annotations path (structured citations)
|
||||
pgm_test_reset();
|
||||
$data_ann = [
|
||||
'choices' => [[
|
||||
'message' => [
|
||||
'content' => 'BPC-157 reconstitution steps.',
|
||||
'annotations' => [
|
||||
[ 'url_citation' => [ 'url' => 'https://peptiderepo.com/bpc-157-reconstitution/' ] ],
|
||||
[ 'url_citation' => [ 'url' => 'https://examine.com/bpc-157' ] ],
|
||||
],
|
||||
],
|
||||
]],
|
||||
'usage' => [ 'total_tokens' => 300 ],
|
||||
];
|
||||
$result_ann = $or_provider->parse_response( $data_ann );
|
||||
pgm_assert( $result_ann->is_cited(), 'OpenRouter: annotations path — cited=true' );
|
||||
pgm_assert_equals( 1, $result_ann->get_our_position(), 'OpenRouter: annotations path — position=1' );
|
||||
|
||||
// Test: regex URL extraction fallback
|
||||
pgm_test_reset();
|
||||
$data_regex = [
|
||||
'choices' => [[
|
||||
'message' => [
|
||||
'content' => 'See https://peptiderepo.com/tb-500/ and https://examine.com/tb-500 for details.',
|
||||
],
|
||||
]],
|
||||
'usage' => [ 'total_tokens' => 150 ],
|
||||
];
|
||||
$result_regex = $or_provider->parse_response( $data_regex );
|
||||
pgm_assert( $result_regex->is_cited(), 'OpenRouter: regex fallback — cited=true from inline URL' );
|
||||
|
||||
// Test: not cited
|
||||
pgm_test_reset();
|
||||
$data_no_cite = [
|
||||
'choices' => [[ 'message' => [ 'content' => 'Some response without our domain.' ] ]],
|
||||
];
|
||||
$result_no_cite = $or_provider->parse_response( $data_no_cite );
|
||||
pgm_assert( ! $result_no_cite->is_cited(), 'OpenRouter: not cited when peptiderepo.com absent from text' );
|
||||
|
||||
// Test: cost calculation from token usage
|
||||
pgm_test_reset();
|
||||
$data_cost = [
|
||||
'choices' => [[ 'message' => [ 'content' => 'test' ] ]],
|
||||
'usage' => [ 'total_tokens' => 1000 ],
|
||||
];
|
||||
$result_cost = $or_provider->parse_response( $data_cost );
|
||||
pgm_assert_equals( 1000 * 0.000002, $result_cost->get_cost_usd(), 'OpenRouter: cost from token usage (2$/1M)' );
|
||||
|
||||
exit( pgm_test_summary() );
|
||||
80
tests/unit/test-score-calc.php
Normal file
80
tests/unit/test-score-calc.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the visibility score formula in PGM_Ai_Visibility_Collector.
|
||||
*
|
||||
* Score formula (CONTEXT.md):
|
||||
* base_score = cited_probes / total_probes
|
||||
* position_bonus = Σ(1/our_position for cited) / total_probes
|
||||
* visibility_score = round((base_score + position_bonus) / 2, 4)
|
||||
*
|
||||
* @package PeptideGeoMonitor
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../bootstrap.php';
|
||||
|
||||
echo "=== PGM Score Calculation Tests ===\n";
|
||||
|
||||
// Instantiate with a null ledger (no DB calls in these tests).
|
||||
$collector = new PGM_Ai_Visibility_Collector( null );
|
||||
|
||||
// ── Test: zero total → score 0 ──────────────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
pgm_assert_equals( 0.0, $collector->compute_score( 0, 0, 0.0 ), 'score: zero total returns 0.0' );
|
||||
|
||||
// ── Test: all cited, position 1 ─────────────────────────────────────────
|
||||
|
||||
// 10 probes, all cited at position 1
|
||||
// base_score = 10/10 = 1.0
|
||||
// position_bonus = (1+1+1+…)/10 = 10/10 = 1.0
|
||||
// score = (1.0 + 1.0) / 2 = 1.0
|
||||
pgm_test_reset();
|
||||
pgm_assert_equals( 1.0, $collector->compute_score( 10, 10, 10.0 ), 'score: all cited at position 1 → 1.0' );
|
||||
|
||||
// ── Test: none cited ────────────────────────────────────────────────────
|
||||
|
||||
// 10 probes, 0 cited, position_sum=0
|
||||
// base_score = 0, position_bonus = 0 → score = 0
|
||||
pgm_test_reset();
|
||||
pgm_assert_equals( 0.0, $collector->compute_score( 0, 10, 0.0 ), 'score: none cited → 0.0' );
|
||||
|
||||
// ── Test: half cited at position 2 ──────────────────────────────────────
|
||||
|
||||
// 10 probes, 5 cited each at position 2 → position_sum = 5*(1/2) = 2.5
|
||||
// base_score = 5/10 = 0.5
|
||||
// position_bonus = 2.5/10 = 0.25
|
||||
// score = (0.5 + 0.25) / 2 = 0.375
|
||||
pgm_test_reset();
|
||||
$score = $collector->compute_score( 5, 10, 2.5 );
|
||||
pgm_assert_equals( 0.375, $score, 'score: half cited at position 2 → 0.375' );
|
||||
|
||||
// ── Test: single probe, cited at position 3 ─────────────────────────────
|
||||
|
||||
// 1 probe, 1 cited, position_sum = 1/3 ≈ 0.3333
|
||||
// base_score = 1.0
|
||||
// position_bonus = 0.3333
|
||||
// score = (1.0 + 0.3333) / 2 ≈ 0.6667 (rounded to 4 decimals)
|
||||
pgm_test_reset();
|
||||
$score = $collector->compute_score( 1, 1, 1.0 / 3.0 );
|
||||
pgm_assert_equals( 0.6667, $score, 'score: single probe cited at position 3 → 0.6667' );
|
||||
|
||||
// ── Test: score is bounded [0, 1] ───────────────────────────────────────
|
||||
|
||||
// Extreme: position_sum could theoretically exceed total but that's logically
|
||||
// impossible with 1-based positions (position_sum ≤ cited_count ≤ total).
|
||||
// Verify edge case: 1 probe, 1 cited, position 1
|
||||
pgm_test_reset();
|
||||
pgm_assert( $collector->compute_score( 1, 1, 1.0 ) <= 1.0, 'score: never exceeds 1.0' );
|
||||
pgm_assert( $collector->compute_score( 0, 10, 0.0 ) >= 0.0, 'score: never below 0.0' );
|
||||
|
||||
// ── Test: precision — 4 decimal places ──────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$score = $collector->compute_score( 3, 7, 1.5 );
|
||||
// base = 3/7 ≈ 0.4286, bonus = 1.5/7 ≈ 0.2143, avg ≈ 0.3214
|
||||
$expected = round( ( 3.0 / 7.0 + 1.5 / 7.0 ) / 2.0, 4 );
|
||||
pgm_assert_equals( $expected, $score, 'score: 4-decimal precision maintained' );
|
||||
|
||||
exit( pgm_test_summary() );
|
||||
101
tests/unit/test-uninstall.php
Normal file
101
tests/unit/test-uninstall.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for uninstall purge: table drop + options deletion.
|
||||
*
|
||||
* Exercises PGM_Table_Manager::drop_table() and verifies the uninstall
|
||||
* script logic (simulated because it requires WP_UNINSTALL_PLUGIN).
|
||||
*
|
||||
* @package PeptideGeoMonitor
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../bootstrap.php';
|
||||
|
||||
echo "=== PGM Uninstall Purge Tests ===\n";
|
||||
|
||||
// ── Mock $wpdb to track DROP calls ──────────────────────────────────────
|
||||
|
||||
$GLOBALS['pgm_test_state']['drop_calls'] = [];
|
||||
|
||||
// Extend the stub to record DROP statements.
|
||||
global $wpdb;
|
||||
$wpdb = new class extends stdClass_wpdb {
|
||||
public function query( string $sql ): bool {
|
||||
if ( str_contains( strtoupper( $sql ), 'DROP TABLE' ) ) {
|
||||
$GLOBALS['pgm_test_state']['drop_calls'][] = $sql;
|
||||
}
|
||||
if ( str_contains( strtoupper( $sql ), 'DELETE FROM' ) && str_contains( $sql, "pgm\\_" ) ) {
|
||||
$GLOBALS['pgm_test_state']['options_deleted'] = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// ── Test: drop_table issues DROP TABLE IF EXISTS ─────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$GLOBALS['pgm_test_state']['drop_calls'] = [];
|
||||
PGM_Table_Manager::drop_table();
|
||||
|
||||
pgm_assert( ! empty( $GLOBALS['pgm_test_state']['drop_calls'] ), 'drop_table: DROP TABLE query executed' );
|
||||
$drop_sql = $GLOBALS['pgm_test_state']['drop_calls'][0] ?? '';
|
||||
pgm_assert( str_contains( strtoupper( $drop_sql ), 'DROP TABLE IF EXISTS' ), 'drop_table: uses DROP TABLE IF EXISTS' );
|
||||
pgm_assert( str_contains( $drop_sql, 'pgm_ai_visibility' ), 'drop_table: targets pgm_ai_visibility table' );
|
||||
|
||||
// ── Test: create_table sets the schema_version option ────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
PGM_Table_Manager::create_table();
|
||||
$schema_ver = get_option( 'pgm_schema_version' );
|
||||
pgm_assert_equals( PGM_SCHEMA_VERSION, $schema_ver, 'create_table: pgm_schema_version option set after create' );
|
||||
|
||||
// ── Test: get_table_name returns prefixed name ───────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$table = PGM_Table_Manager::get_table_name();
|
||||
pgm_assert_equals( 'wp_pgm_ai_visibility', $table, 'get_table_name: returns wp_ prefixed table name' );
|
||||
|
||||
// ── Test: seed_defaults writes expected option keys ──────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
PGM_Config::seed_defaults();
|
||||
|
||||
pgm_assert( isset( $GLOBALS['pgm_test_state']['options']['pgm_monthly_budget_usd'] ), 'seed_defaults: pgm_monthly_budget_usd set' );
|
||||
pgm_assert( isset( $GLOBALS['pgm_test_state']['options']['pgm_peptides'] ), 'seed_defaults: pgm_peptides set' );
|
||||
pgm_assert( isset( $GLOBALS['pgm_test_state']['options']['pgm_prompt_intents'] ), 'seed_defaults: pgm_prompt_intents set' );
|
||||
pgm_assert( isset( $GLOBALS['pgm_test_state']['options']['pgm_models'] ), 'seed_defaults: pgm_models set' );
|
||||
|
||||
// ── Test: seed_defaults does not overwrite existing values ───────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$GLOBALS['pgm_test_state']['options']['pgm_monthly_budget_usd'] = 99.0;
|
||||
PGM_Config::seed_defaults();
|
||||
pgm_assert_equals( 99.0, $GLOBALS['pgm_test_state']['options']['pgm_monthly_budget_usd'], 'seed_defaults: does not overwrite existing pgm_monthly_budget_usd' );
|
||||
|
||||
// ── Test: default budget is PGM_DEFAULT_MONTHLY_BUDGET_USD ──────────────
|
||||
|
||||
pgm_test_reset();
|
||||
PGM_Config::seed_defaults();
|
||||
pgm_assert_equals( PGM_DEFAULT_MONTHLY_BUDGET_USD, $GLOBALS['pgm_test_state']['options']['pgm_monthly_budget_usd'], 'seed_defaults: default budget = PGM_DEFAULT_MONTHLY_BUDGET_USD (5.0)' );
|
||||
|
||||
// ── Test: default peptide count is 12 ───────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$peptides = PGM_Config::get_peptides();
|
||||
pgm_assert_equals( 12, count( $peptides ), 'get_peptides: default seed has 12 peptides' );
|
||||
|
||||
// ── Test: default prompt intent count is 3 ──────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$intents = PGM_Config::get_prompt_intents();
|
||||
pgm_assert_equals( 3, count( $intents ), 'get_prompt_intents: default seed has 3 intents' );
|
||||
|
||||
// ── Test: default model count is 3 ──────────────────────────────────────
|
||||
|
||||
pgm_test_reset();
|
||||
$models = PGM_Config::get_models();
|
||||
pgm_assert_equals( 3, count( $models ), 'get_models: default seed has 3 models' );
|
||||
pgm_assert( in_array( 'perplexity/sonar', $models, true ), 'get_models: perplexity/sonar in default models' );
|
||||
|
||||
exit( pgm_test_summary() );
|
||||
Reference in New Issue
Block a user