|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +if ( ! defined('ABSPATH') ) { |
| 6 | + define('ABSPATH', __DIR__ . '/fixtures/'); |
| 7 | +} |
| 8 | + |
| 9 | +require_once dirname(__DIR__) . '/vendor/autoload.php'; |
| 10 | +require_once dirname(__DIR__) . '/inc/Workspace/WorkspaceCleanupPlan.php'; |
| 11 | +require_once dirname(__DIR__) . '/inc/Workspace/WorkspaceWorktreeCleanupEngine.php'; |
| 12 | + |
| 13 | +use DataMachineCode\Workspace\WorkspaceCleanupPlan; |
| 14 | +use DataMachineCode\Workspace\WorkspaceWorktreeCleanupEngine; |
| 15 | + |
| 16 | +function cleanup_retention_plan_assert( bool $condition, string $message ): void { |
| 17 | + if ( ! $condition ) { |
| 18 | + throw new RuntimeException($message); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +final class CleanupRetentionPlanWorktreeRemovalWorkspace { |
| 23 | + use WorkspaceCleanupPlan; |
| 24 | + |
| 25 | + public const CLEANUP_PLAN_DEFAULT_LIMIT = 100; |
| 26 | + public const CLEANUP_PLAN_DEFAULT_BUDGET = '30s'; |
| 27 | + |
| 28 | + private string $workspace_path = '/tmp/dmc-retention-plan-contract'; |
| 29 | + public array $last_worktree_cleanup_opts = array(); |
| 30 | + |
| 31 | + public function worktree_cleanup_artifacts( array $opts = array() ): array { |
| 32 | + return array( |
| 33 | + 'success' => true, |
| 34 | + 'dry_run' => true, |
| 35 | + 'candidates' => array(), |
| 36 | + 'skipped' => array(), |
| 37 | + 'summary' => array(), |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + public function worktree_cleanup_merged( array $opts = array() ): array { |
| 42 | + $this->last_worktree_cleanup_opts = $opts; |
| 43 | + |
| 44 | + return array( |
| 45 | + 'success' => true, |
| 46 | + 'dry_run' => true, |
| 47 | + 'candidates' => array( |
| 48 | + array( |
| 49 | + 'handle' => 'repo@local-merged', |
| 50 | + 'repo' => 'repo', |
| 51 | + 'branch' => 'local-merged', |
| 52 | + 'path' => '/tmp/dmc-retention-plan-contract/repo@local-merged', |
| 53 | + 'signal' => 'local-merged', |
| 54 | + 'reason_code' => 'local-merged', |
| 55 | + 'size_bytes' => 100, |
| 56 | + ), |
| 57 | + array( |
| 58 | + 'handle' => 'repo@remote-clean', |
| 59 | + 'repo' => 'repo', |
| 60 | + 'branch' => 'remote-clean', |
| 61 | + 'path' => '/tmp/dmc-retention-plan-contract/repo@remote-clean', |
| 62 | + 'signal' => 'remote-tracking-clean', |
| 63 | + 'reason_code' => 'remote-tracking-clean', |
| 64 | + 'size_bytes' => 200, |
| 65 | + ), |
| 66 | + array( |
| 67 | + 'handle' => 'repo@upstream-gone', |
| 68 | + 'repo' => 'repo', |
| 69 | + 'branch' => 'upstream-gone', |
| 70 | + 'path' => '/tmp/dmc-retention-plan-contract/repo@upstream-gone', |
| 71 | + 'signal' => 'upstream-gone', |
| 72 | + 'reason_code' => 'upstream-gone', |
| 73 | + 'size_bytes' => 300, |
| 74 | + ), |
| 75 | + ), |
| 76 | + 'skipped' => array(), |
| 77 | + 'summary' => array( |
| 78 | + 'fresh_safe_removable_count' => 3, |
| 79 | + ), |
| 80 | + 'pagination' => array( |
| 81 | + 'total' => 5, |
| 82 | + 'offset' => 0, |
| 83 | + 'limit' => 3, |
| 84 | + 'scanned' => 3, |
| 85 | + 'partial' => true, |
| 86 | + 'complete' => false, |
| 87 | + 'next_offset' => 3, |
| 88 | + 'next_command' => 'studio wp datamachine-code workspace worktree cleanup --dry-run --limit=3 --offset=3 --until-budget=45s --format=json', |
| 89 | + 'budget_stopped' => false, |
| 90 | + ), |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + private function stable_cleanup_hash( array $data, string $prefix ): string { |
| 95 | + return $prefix . '-' . substr(hash('sha256', wp_json_encode($data)), 0, 12); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +if ( ! function_exists('wp_json_encode') ) { |
| 100 | + function wp_json_encode( $data, $options = 0, $depth = 512 ) { |
| 101 | + return json_encode($data, $options, $depth); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +$workspace = new CleanupRetentionPlanWorktreeRemovalWorkspace(); |
| 106 | +$plan = $workspace->workspace_cleanup_plan( |
| 107 | + array( |
| 108 | + 'include_artifacts' => false, |
| 109 | + 'limit' => 3, |
| 110 | + 'offset' => 0, |
| 111 | + 'until_budget' => '45s', |
| 112 | + ) |
| 113 | +); |
| 114 | + |
| 115 | +cleanup_retention_plan_assert(is_array($plan), 'retention cleanup plan should return an array'); |
| 116 | +cleanup_retention_plan_assert(empty($workspace->last_worktree_cleanup_opts['inventory_only']), 'retention plan should run the probed worktree cleanup preview, not inventory-only cleanup'); |
| 117 | +cleanup_retention_plan_assert(3 === (int) ( $workspace->last_worktree_cleanup_opts['limit'] ?? 0 ), 'retention plan should pass the bounded page limit to worktree cleanup'); |
| 118 | +cleanup_retention_plan_assert('45s' === ( $workspace->last_worktree_cleanup_opts['until_budget'] ?? null ), 'retention plan should pass the budget to worktree cleanup'); |
| 119 | + |
| 120 | +$rows = (array) ( $plan['rows']['worktree_removal'] ?? array() ); |
| 121 | +cleanup_retention_plan_assert(3 === count($rows), 'retention plan should preserve fresh safe worktree cleanup candidates as removal rows'); |
| 122 | + |
| 123 | +$signals = array_column($rows, 'signal', 'handle'); |
| 124 | +cleanup_retention_plan_assert('local-merged' === ( $signals['repo@local-merged'] ?? null ), 'local-merged candidates should be applyable worktree_removal rows'); |
| 125 | +cleanup_retention_plan_assert('remote-tracking-clean' === ( $signals['repo@remote-clean'] ?? null ), 'remote-tracking-clean candidates should be applyable worktree_removal rows'); |
| 126 | +cleanup_retention_plan_assert('upstream-gone' === ( $signals['repo@upstream-gone'] ?? null ), 'upstream-gone candidates should be applyable worktree_removal rows'); |
| 127 | +cleanup_retention_plan_assert(3 === (int) ( $plan['summary']['rows_by_action']['remove_worktree'] ?? 0 ), 'remove_worktree action rows should match reviewed safe rows'); |
| 128 | +cleanup_retention_plan_assert('studio wp datamachine-code workspace cleanup apply <run-id>' === ( $plan['summary']['apply_command'] ?? '' ), 'retention plan should expose the DB-backed apply command'); |
| 129 | + |
| 130 | +$engine = new class { |
| 131 | + use WorkspaceWorktreeCleanupEngine; |
| 132 | +}; |
| 133 | +$review_command = ( new ReflectionMethod($engine, 'build_worktree_cleanup_review_plan_command') )->invoke($engine, 100, 25, '180s'); |
| 134 | +cleanup_retention_plan_assert( |
| 135 | + 'studio wp datamachine-code workspace cleanup plan --mode=retention --limit=100 --offset=25 --until-budget=180s --format=json' === $review_command, |
| 136 | + 'bounded worktree cleanup should point to the DB-backed reviewed apply path for the same page' |
| 137 | +); |
| 138 | + |
| 139 | +fwrite(STDOUT, "cleanup-retention-plan-worktree-removals ok\n"); |
0 commit comments