|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +if ( ! defined('ABSPATH') ) { |
| 6 | + define('ABSPATH', __DIR__ . '/fixtures/'); |
| 7 | +} |
| 8 | + |
| 9 | +if ( ! class_exists('WP_Error') ) { |
| 10 | + final class WP_Error { |
| 11 | + public function __construct( private string $code = '', private string $message = '', private array $data = array() ) {} |
| 12 | + public function get_error_code(): string { return $this->code; } |
| 13 | + public function get_error_message(): string { return $this->message; } |
| 14 | + public function get_error_data(): array { return $this->data; } |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +function is_wp_error( mixed $value ): bool { |
| 19 | + return $value instanceof WP_Error; |
| 20 | +} |
| 21 | + |
| 22 | +require_once dirname(__DIR__) . '/inc/Workspace/WorktreeStalenessProbe.php'; |
| 23 | + |
| 24 | +use DataMachineCode\Workspace\WorktreeStalenessProbe; |
| 25 | + |
| 26 | +function staleness_timeout_assert_same( mixed $expected, mixed $actual, string $message ): void { |
| 27 | + if ( $expected !== $actual ) { |
| 28 | + throw new RuntimeException($message . ' Expected ' . var_export($expected, true) . ', got ' . var_export($actual, true) . '.'); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +$calls = array(); |
| 33 | +$result = WorktreeStalenessProbe::fetch( |
| 34 | + '/repo', |
| 35 | + static function ( string $path, string $args, int $timeout ) use ( &$calls ): WP_Error { |
| 36 | + $calls[] = array( $path, $args, $timeout ); |
| 37 | + return new WP_Error('git_command_timeout', 'Process command timed out after 5 second(s).', array( 'timeout' => 5 )); |
| 38 | + } |
| 39 | +); |
| 40 | + |
| 41 | +staleness_timeout_assert_same(array( array( '/repo', 'fetch --quiet origin', 5 ) ), $calls, 'Freshness fetch must use the bounded GitRunner timeout contract.'); |
| 42 | +staleness_timeout_assert_same(false, $result['ok'], 'Timed-out freshness fetch must fail verification.'); |
| 43 | +staleness_timeout_assert_same(true, $result['timed_out'] ?? null, 'Timed-out freshness fetch must remain distinct from stale evidence.'); |
| 44 | +staleness_timeout_assert_same(5, $result['timeout_seconds'] ?? null, 'Timed-out freshness fetch must surface its budget.'); |
| 45 | +staleness_timeout_assert_same(true, str_contains((string) ( $result['error'] ?? '' ), 'allow_unverified_freshness=true'), 'Timed-out freshness fetch must provide an actionable opt-in diagnostic.'); |
| 46 | + |
| 47 | +fwrite(STDOUT, "worktree-staleness-fetch-timeout: ok\n"); |
0 commit comments