Files
pr-vision/tests/unit/test-score-calc.php
peptiderepo ca1a41f6f9 feat: peptide-geo-monitor v0.1.0 — AI-visibility dashboard
Add initial plugin implementation per the CTO engineering handoff
(geomonitor/threads/2026-06-ai-visibility-v1/01-cto-eng-handoff.md).

## What
- PGM_Probe_Provider interface + PGM_Probe_Result value object
- PGM_Perplexity_Provider (sonar via OpenRouter/CF AI Gateway)
- PGM_OpenRouter_Provider (GPT-search + Gemini-search, generic parameterised)
- PGM_Gateway_Client: shared HTTP + exponential-backoff retry (mirrors PRAB)
- PGM_Citation_Detector: domain extraction, is_cited(), get_our_position()
- PGM_Cost_Ledger: MTD cost from DB, can_afford() pre-check, hard monthly cap
- PGM_Probe_Runner: peptides×intents×models run, budget abort, in-run cache
- PGM_Table_Manager: pgm_ai_visibility dbDelta, schema-version, drop_table()
- PGM_Config: typed getters, seed_defaults() (12 peptides, 3 intents, 3 models)
- PGM_Cron: weekly pgm_weekly_probe schedule/clear
- PGM_Data_Collector + PGM_Dashboard_Panel interfaces (seam for future panels)
- PGM_Collector_Registry: singleton registry
- PGM_Ai_Visibility_Collector: trendline, standings, MTD cost (score formula)
- PGM_Ai_Visibility_Panel: proxy note, Chart.js trendline, standings table
- PGM_Admin_Page: GEO Monitor menu, Run now POST handler (nonce+capability)
- uninstall.php: DROP TABLE + DELETE pgm_* options + wp_clear_scheduled_hook
- Unit tests (6 files): citation detection, score calc, cost ledger + budget
  abort, provider response parsing (mock HTTP), cron, uninstall purge
- ARCHITECTURE.md, CONVENTIONS.md, CONTEXT.md (score formula), README.md,
  CHANGELOG.md — same-commit per DoD
- CI workflow: PHP lint (8.1/8.2/8.3) + PHPCS + 300-line check

## Security
- Secrets from wp-config constants only; none hardcoded
- manage_options + nonce on every admin action
- Input sanitized; output escaped

Agent-Session: geomonitor v0.1.0
2026-06-13 08:18:56 +00:00

81 lines
3.6 KiB
PHP

<?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() );