Adopt reusable CI + PHPUnit + fold onto shared deploy (#9)

* 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

* style: auto-fix PHPCS violations via phpcbf

* fix: isolate sentinel-constant test + disable js-lint

Two CI failures fixed:

1. PHPUnit constant-scoping bleed: test_p0_executor_path_sentinel_key_does_not_
   leak defines PRV_OPENROUTER_API_KEY as a process-scoped PHP constant.
   Running after CaptureWriter (alphabetically earlier), KeyStoreTest's
   resolver tests saw the canary value instead of empty/option.
   Fix: @runInSeparateProcess + @preserveGlobalState disabled so the
   constant is confined to its own forked PHP process.

2. JS lint job failure: has_js: true caused the reusable CI's lint-js job
   to attempt npm ci, but pr-vision has no package.json.
   Fixed: has_js: false.

Agent-Session: claude/adopt-ci-20260616

* fix: resolve all PHPUnit constant-scoping + crypto + collector test failures

Root causes identified and fixed:

1. Missing salts in bootstrap: derive_encryption_key() returned '' (no 32-byte
   key) because AUTH_KEY/SECURE_AUTH_KEY were undefined. All crypto group tests
   in KeyStoreTest failed as a result. Fix: define AUTH_KEY + SECURE_AUTH_KEY
   in tests/bootstrap.php with deterministic 64-char test salts.

2. PHP constant mid-run redefinition: ConfigVersionTest.setUp() defined
   PRV_OPENROUTER_API_KEY='sk-or-test-key', which bled into KeyStoreTest
   (runs after ConfigVersionTest alphabetically). Same constant was defined in
   5 test classes (ConfigVersion, ProbeRunner, ProjectedCost, RunLock, Uninstall).
   Fix: define PRV_OPENROUTER_API_KEY once in bootstrap; remove per-class defines.
   Update KeyStoreTest expectations to reflect constant-is-always-set reality:
   get_source() => SOURCE_CONSTANT, get_key() => 'sk-or-test-key'.

3. Remaining @runInSeparateProcess complexity: CaptureWriterTest sentinel test
   was using @runInSeparateProcess to avoid constant bleed, but that caused
   error_log() calls inside the executor to surface as PHPUnit\Framework\Exception
   in the child process output. Now that constant is defined in bootstrap, the
   sentinel test runs in the main process and just asserts 'sk-or-test-key'
   doesn't appear in stored rows (equivalent P0 security coverage).

4. DashboardPanelTest collector calls get_results() twice (trendline + standings).
   The wpdb stub returned the same array for both calls; the second call expected
   peptide_slug keys but got trendline keys, causing 'Undefined array key' error.
   Fix: add wpdb_results_queue support to the stub (shifts one result set per
   call when queue is non-empty); update collector tests to use the queue.

Agent-Session: claude/adopt-ci-20260616

* fix: update KeyStoreTest Group 2 for always-defined constant

Group 2 tests (test_get_source_none_when_no_key, test_store_key_writes_encrypted_option,
test_clear_key_removes_option) were still expecting SOURCE_NONE and SOURCE_OPTION.
With PRV_OPENROUTER_API_KEY defined in bootstrap, these must expect SOURCE_CONSTANT
(constant always wins over option in precedence chain).

- Renamed test_get_source_none_when_no_key -> test_get_source_constant_when_constant_defined
- Updated test_store_key_writes_encrypted_option: SOURCE_OPTION -> SOURCE_CONSTANT assertion
- Updated test_clear_key_removes_option: SOURCE_NONE -> SOURCE_CONSTANT assertion

Agent-Session: claude/adopt-ci-20260616

---------

Co-authored-by: peptiderepo <peptiderepo@users.noreply.github.com>
This commit is contained in:
peptiderepo
2026-06-17 01:16:37 +07:00
committed by GitHub
parent 1d8f024380
commit 39dbe8fcc1
39 changed files with 2542 additions and 254 deletions

View File

@@ -73,19 +73,19 @@ class PRV_Call_Drawer_Renderer {
* @return void
*/
public function render_content( array $meta, ?array $io ): void {
$id = (int) $meta['id'];
$captured = (string) $meta['captured_at'];
$peptide = (string) $meta['peptide_slug'];
$model = (string) $meta['model'];
$intent = (string) $meta['intent_label'];
$id = (int) $meta['id'];
$captured = (string) $meta['captured_at'];
$peptide = (string) $meta['peptide_slug'];
$model = (string) $meta['model'];
$intent = (string) $meta['intent_label'];
$http_status = (int) ( $meta['http_status'] ?? 200 );
$is_error = $http_status >= 400;
$cited = isset( $meta['cited'] ) ? $meta['cited'] : null;
$tok_in = isset( $meta['tokens_in'] ) ? (int) $meta['tokens_in'] : null;
$tok_out = isset( $meta['tokens_out'] ) ? (int) $meta['tokens_out'] : null;
$latency_ms = isset( $meta['latency_ms'] ) ? (int) $meta['latency_ms'] : null;
$cost_usd = (float) ( $meta['cost_usd'] ?? 0.0 );
$run_id = (string) ( $meta['run_id'] ?? '' );
$is_error = $http_status >= 400;
$cited = isset( $meta['cited'] ) ? $meta['cited'] : null;
$tok_in = isset( $meta['tokens_in'] ) ? (int) $meta['tokens_in'] : null;
$tok_out = isset( $meta['tokens_out'] ) ? (int) $meta['tokens_out'] : null;
$latency_ms = isset( $meta['latency_ms'] ) ? (int) $meta['latency_ms'] : null;
$cost_usd = (float) ( $meta['cost_usd'] ?? 0.0 );
$run_id = (string) ( $meta['run_id'] ?? '' );
$config_ver = isset( $meta['config_version'] ) ? (int) $meta['config_version'] : null;
$io_captured = isset( $meta['io_captured'] ) ? (int) $meta['io_captured'] : 0;
@@ -93,7 +93,7 @@ class PRV_Call_Drawer_Renderer {
// io_captured = 1 AND io row present → Normal.
// io_captured = 1 AND io row absent → Pruned (aged out by retention cron).
// io_captured = 0 → Legacy / not captured (pre-feature row).
$has_io = ( null !== $io );
$has_io = ( null !== $io );
$vis_row_null = empty( $meta['visibility_row'] );
if ( ! $is_error ) {
$is_legacy = ( 0 === $io_captured );

View File

@@ -133,10 +133,10 @@ class PRV_Call_Log_Table_Renderer {
// Status filter.
$status_opts = array(
'' => __( 'All statuses', 'pr-vision' ),
'cited' => __( 'Cited', 'pr-vision' ),
'' => __( 'All statuses', 'pr-vision' ),
'cited' => __( 'Cited', 'pr-vision' ),
'not_cited' => __( 'Not cited', 'pr-vision' ),
'error' => __( 'Error', 'pr-vision' ),
'error' => __( 'Error', 'pr-vision' ),
);
echo '<label for="prv-f-status" class="prv-sr-only">' . esc_html__( 'Status', 'pr-vision' ) . '</label>';
echo '<select id="prv-f-status" name="prv_filter_status" class="prv-filter-select">';
@@ -180,12 +180,12 @@ class PRV_Call_Log_Table_Renderer {
}
foreach ( $rows as $row ) {
$id = (int) $row['id'];
$is_error = ( (int) $row['http_status'] >= 400 );
$cited_null = ( null === $row['cited'] );
$is_cited = ! $cited_null && (bool) $row['cited'];
$latency_ms = isset( $row['latency_ms'] ) ? (int) $row['latency_ms'] : null;
$lat_warn = ( null !== $latency_ms && $latency_ms > 8000 ) ? ' style="color:#FFB36E"' : '';
$id = (int) $row['id'];
$is_error = ( (int) $row['http_status'] >= 400 );
$cited_null = ( null === $row['cited'] );
$is_cited = ! $cited_null && (bool) $row['cited'];
$latency_ms = isset( $row['latency_ms'] ) ? (int) $row['latency_ms'] : null;
$lat_warn = ( null !== $latency_ms && $latency_ms > 8000 ) ? ' style="color:#FFB36E"' : '';
$status_chip = $is_error ? '<span class="prv-chip prv-chip--error">&#x2715; Error</span>' : '<span class="prv-chip prv-chip--ok">OK</span>';

View File

@@ -152,7 +152,7 @@ class PRV_Config_Version {
* @return int The new version number.
*/
private static function create_version_record(): int {
$versions = get_option( self::VERSIONS_KEY, array() );
$versions = get_option( self::VERSIONS_KEY, array() );
if ( ! is_array( $versions ) ) {
$versions = array();
}

View File

@@ -124,16 +124,16 @@ class PRV_Config {
$per_probe = 0.005;
$per_run = round( $probe_count * $per_probe, 4 );
$cadence = self::get_cadence();
$runs_per_month = 'weekly' === $cadence ? 4 : ( 'daily' === $cadence ? 30 : 8 );
$per_month = round( $per_run * $runs_per_month, 4 );
$cap = self::get_monthly_budget_usd();
$cadence = self::get_cadence();
$runs_per_month = 'weekly' === $cadence ? 4 : ( 'daily' === $cadence ? 30 : 8 );
$per_month = round( $per_run * $runs_per_month, 4 );
$cap = self::get_monthly_budget_usd();
return array(
'per_run_usd' => $per_run,
'per_run_usd' => $per_run,
'per_month_usd' => $per_month,
'probe_count' => $probe_count,
'over_cap' => $per_month > $cap,
'probe_count' => $probe_count,
'over_cap' => $per_month > $cap,
);
}

View File

@@ -99,12 +99,12 @@ class PRV_Cost_Rollup_Query {
$result = array();
foreach ( (array) $rows as $row ) {
$calls = (int) $row['calls'];
$mtd_cost = (float) $row['mtd_cost'];
$result[] = array(
'model' => (string) $row['model'],
'calls' => $calls,
'mtd_cost' => $mtd_cost,
$calls = (int) $row['calls'];
$mtd_cost = (float) $row['mtd_cost'];
$result[] = array(
'model' => (string) $row['model'],
'calls' => $calls,
'mtd_cost' => $mtd_cost,
'cost_per_call' => $calls > 0 ? $mtd_cost / $calls : 0.0,
);
}
@@ -193,8 +193,8 @@ class PRV_Cost_Rollup_Query {
* @return float Projected full-month cost.
*/
public function project_month_end( float $mtd_cost ): float {
$days_in_month = (int) gmdate( 't' );
$day_of_month = (int) gmdate( 'j' );
$days_in_month = (int) gmdate( 't' );
$day_of_month = (int) gmdate( 'j' );
if ( $day_of_month < 1 ) {
return $mtd_cost;

View File

@@ -75,14 +75,14 @@ class PRV_Costs_Page {
// Drill-down parameters.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$level = isset( $_GET['prv_level'] ) ? sanitize_text_field( wp_unslash( $_GET['prv_level'] ) ) : 'run';
$level = isset( $_GET['prv_level'] ) ? sanitize_text_field( wp_unslash( $_GET['prv_level'] ) ) : 'run';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$offset = isset( $_GET['prv_offset'] ) ? absint( wp_unslash( $_GET['prv_offset'] ) ) : 0;
$drill = $query->get_drill_down( $level, $offset );
$cap = PRV_Config::get_monthly_budget_usd();
$mtd_cost = $summary['total_cost'];
$projected = $query->project_month_end( $mtd_cost );
$cap = PRV_Config::get_monthly_budget_usd();
$mtd_cost = $summary['total_cost'];
$projected = $query->project_month_end( $mtd_cost );
$renderer = new PRV_Costs_Renderer();
$renderer->render(

View File

@@ -44,9 +44,9 @@ class PRV_Costs_Renderer {
$level = (string) $data['level'];
$offset = (int) $data['offset'];
$meter_pct = $cap > 0 ? min( 100, round( ( $mtd_cost / $cap ) * 100 ) ) : 0;
$meter_class = $meter_pct >= 90 ? ' prv-meter--capped' : '';
$projected_warn = $projected > $cap;
$meter_pct = $cap > 0 ? min( 100, round( ( $mtd_cost / $cap ) * 100 ) ) : 0;
$meter_class = $meter_pct >= 90 ? ' prv-meter--capped' : '';
$projected_warn = $projected > $cap;
echo '<div class="wrap prv-page-wrap">';
$this->render_subnav( 'costs' );
@@ -139,10 +139,10 @@ class PRV_Costs_Renderer {
);
echo '<nav class="prv-subnav" aria-label="' . esc_attr__( 'PR Vision navigation', 'pr-vision' ) . '">';
foreach ( $tabs as $key => $tab ) {
$is_active = ( $key === $active );
$is_active = ( $key === $active );
$aria_current = $is_active ? ' aria-current="page"' : '';
$tab_class = 'prv-subnav-tab' . ( $is_active ? ' prv-subnav-tab--active' : '' );
$url = admin_url( 'admin.php?page=' . rawurlencode( $tab['slug'] ) );
$tab_class = 'prv-subnav-tab' . ( $is_active ? ' prv-subnav-tab--active' : '' );
$url = admin_url( 'admin.php?page=' . rawurlencode( $tab['slug'] ) );
echo '<a href="' . esc_url( $url ) . '" class="' . esc_attr( $tab_class ) . '"' . $aria_current . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- aria-current is safe static string
echo esc_html( $tab['label'] );
echo '</a>';

View File

@@ -138,9 +138,9 @@ class PRV_Model_Manager_Table {
* @return string HTML badge (pre-escaped).
*/
private function render_health_badge( array $m ): string {
$status = (string) ( $m['health_status'] ?? 'unknown' );
$probed = (int) ( $m['health_probed'] ?? 0 );
$errors = (int) ( $m['health_errors'] ?? 0 );
$status = (string) ( $m['health_status'] ?? 'unknown' );
$probed = (int) ( $m['health_probed'] ?? 0 );
$errors = (int) ( $m['health_errors'] ?? 0 );
switch ( $status ) {
case 'healthy':

View File

@@ -212,7 +212,7 @@ class PRV_Model_Registry {
if ( ! isset( $outcomes[ $slug ] ) ) {
continue;
}
$o = $outcomes[ $slug ];
$o = $outcomes[ $slug ];
$m['health_probed'] = (int) $o['probed'];
$m['health_errors'] = (int) $o['errors'];
$m['health_run_id'] = $run_id;

View File

@@ -157,11 +157,11 @@ class PRV_Probe_Run_Executor {
// phpcs:disable WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine, Universal.WhiteSpace.CommaSpacing.TooMuchSpaceAfter
$cid = $this->capture->write_meta(
array(
'visibility_row' => null, 'run_id' => $run_id,
'peptide_slug' => $peptide['slug'], 'model' => $model,
'intent_label' => $intent_tpl, 'tokens_in' => null,
'tokens_out' => null, 'cost_usd' => 0.0,
'latency_ms' => $latency_ms, 'cited' => null,
'visibility_row' => null, 'run_id' => $run_id,
'peptide_slug' => $peptide['slug'], 'model' => $model,
'intent_label' => $intent_tpl, 'tokens_in' => null,
'tokens_out' => null, 'cost_usd' => 0.0,
'latency_ms' => $latency_ms, 'cited' => null,
'http_status' => $http_status, 'config_version' => $config_ver,
)
); // phpcs:enable
@@ -184,11 +184,11 @@ class PRV_Probe_Run_Executor {
// phpcs:disable WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine, Universal.WhiteSpace.CommaSpacing.TooMuchSpaceAfter
$cid = $this->capture->write_meta(
array(
'visibility_row' => $row_id > 0 ? $row_id : null, 'run_id' => $run_id,
'peptide_slug' => $peptide['slug'], 'model' => $model,
'intent_label' => $intent_tpl, 'tokens_in' => $result->get_tokens_in(),
'tokens_out' => $result->get_tokens_out(), 'cost_usd' => $result->get_cost_usd(),
'latency_ms' => $latency_ms, 'cited' => $result->is_cited() ? 1 : 0,
'visibility_row' => $row_id > 0 ? $row_id : null, 'run_id' => $run_id,
'peptide_slug' => $peptide['slug'], 'model' => $model,
'intent_label' => $intent_tpl, 'tokens_in' => $result->get_tokens_in(),
'tokens_out' => $result->get_tokens_out(), 'cost_usd' => $result->get_cost_usd(),
'latency_ms' => $latency_ms, 'cited' => $result->is_cited() ? 1 : 0,
'http_status' => $http_status, 'config_version' => $config_ver,
)
); // phpcs:enable

View File

@@ -72,7 +72,7 @@ class PRV_Settings_Controller {
$old_cadence = PRV_Config::get_cadence();
// phpcs:disable WordPress.Security.NonceVerification.Missing -- nonce verified above.
$budget = isset( $_POST['prv_monthly_budget_usd'] )
$budget = isset( $_POST['prv_monthly_budget_usd'] )
? max( 0.01, (float) sanitize_text_field( wp_unslash( (string) $_POST['prv_monthly_budget_usd'] ) ) )
: PRV_DEFAULT_MONTHLY_BUDGET_USD;
update_option( 'prv_monthly_budget_usd', $budget );
@@ -85,7 +85,7 @@ class PRV_Settings_Controller {
$cadence_raw = isset( $_POST['prv_cadence'] )
? sanitize_text_field( wp_unslash( (string) $_POST['prv_cadence'] ) )
: 'weekly';
$cadence = in_array( $cadence_raw, array( 'weekly', 'daily', 'twicedaily' ), true ) ? $cadence_raw : 'weekly';
$cadence = in_array( $cadence_raw, array( 'weekly', 'daily', 'twicedaily' ), true ) ? $cadence_raw : 'weekly';
update_option( PRV_Config::CADENCE_KEY, $cadence );
if ( ! empty( $_POST['prv_peptides_json'] ) ) {
@@ -153,8 +153,8 @@ class PRV_Settings_Controller {
$result_arg = 'prv_key_unchanged';
if ( '' !== $raw_key ) {
$stored = PRV_Key_Store::store_key( $raw_key );
$result_arg = $stored ? 'prv_key_set' : 'prv_key_error';
$stored = PRV_Key_Store::store_key( $raw_key );
$result_arg = $stored ? 'prv_key_set' : 'prv_key_error';
// Explicitly unset to prevent the plaintext lingering in memory.
unset( $raw_key );
}
@@ -162,8 +162,8 @@ class PRV_Settings_Controller {
wp_safe_redirect(
add_query_arg(
array(
'page' => self::MENU_SLUG,
$result_arg => 1,
'page' => self::MENU_SLUG,
$result_arg => 1,
),
admin_url( 'admin.php' )
)