Files
pr-vision/tests/unit/test-citation-detector.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

82 lines
3.7 KiB
PHP

<?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( 'ncbi.nlm.nih.gov', $domains, true ), 'parse_domains: subdomain-only domain extracted' );
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() );