|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Smoke test for workspace cleanup run scheduling context. |
| 4 | + * |
| 5 | + * php tests/smoke-workspace-cleanup-run-context.php |
| 6 | + * |
| 7 | + * @package DataMachineCode\Tests |
| 8 | + */ |
| 9 | + |
| 10 | +declare( strict_types=1 ); |
| 11 | + |
| 12 | +namespace { |
| 13 | + if ( ! defined('ABSPATH') ) { |
| 14 | + define('ABSPATH', __DIR__); |
| 15 | + } |
| 16 | + |
| 17 | + function sanitize_key( $key ): string { |
| 18 | + return strtolower(preg_replace('/[^a-z0-9_\-]/', '', (string) $key)); |
| 19 | + } |
| 20 | + |
| 21 | + class WP_Error { |
| 22 | + public function __construct( |
| 23 | + private string $code, |
| 24 | + private string $message, |
| 25 | + private array $data = array() |
| 26 | + ) {} |
| 27 | + |
| 28 | + public function get_error_code(): string { |
| 29 | + return $this->code; |
| 30 | + } |
| 31 | + |
| 32 | + public function get_error_message(): string { |
| 33 | + return $this->message; |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +namespace DataMachine\Core\FilesRepository { |
| 39 | + class DirectoryManager { |
| 40 | + public function get_effective_user_id( int $user_id ): int { |
| 41 | + return $user_id > 0 ? $user_id : 1; |
| 42 | + } |
| 43 | + |
| 44 | + public function resolve_agent_slug( array $args ): string { |
| 45 | + return 1 === (int) ( $args['user_id'] ?? 0 ) ? 'intelligence-chubes4' : ''; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +namespace DataMachine\Engine\Tasks { |
| 51 | + class TaskScheduler { |
| 52 | + public static array $last_context = array(); |
| 53 | + public static array|false $next_result = array( 'job_ids' => array( 987 ), 'batch_job_id' => 0 ); |
| 54 | + |
| 55 | + public static function scheduleBatch( string $task_type, array $items, array $context = array() ): array|false { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found |
| 56 | + self::$last_context = $context; |
| 57 | + return self::$next_result; |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +namespace { |
| 63 | + include_once dirname(__DIR__) . '/inc/Abilities/WorkspaceAbilities.php'; |
| 64 | + |
| 65 | + function datamachine_code_cleanup_run_context_assert( bool $condition, string $message ): void { |
| 66 | + if ( ! $condition ) { |
| 67 | + fwrite(STDERR, "Assertion failed: {$message}\n"); |
| 68 | + exit(1); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + $result = \DataMachineCode\Abilities\WorkspaceAbilities::workspaceCleanupRun( |
| 73 | + array( |
| 74 | + 'mode' => 'artifacts', |
| 75 | + 'source' => 'workspace_cleanup_cli', |
| 76 | + ) |
| 77 | + ); |
| 78 | + |
| 79 | + datamachine_code_cleanup_run_context_assert(is_array($result), 'cleanup run returns success array'); |
| 80 | + datamachine_code_cleanup_run_context_assert('cleanup-run-987' === ( $result['run_id'] ?? '' ), 'cleanup run uses direct job id'); |
| 81 | + datamachine_code_cleanup_run_context_assert('intelligence-chubes4' === ( \DataMachine\Engine\Tasks\TaskScheduler::$last_context['agent_slug'] ?? '' ), 'cleanup run supplies resolved agent slug'); |
| 82 | + |
| 83 | + \DataMachine\Engine\Tasks\TaskScheduler::$next_result = array( 'job_ids' => array(), 'batch_job_id' => 0 ); |
| 84 | + $result = \DataMachineCode\Abilities\WorkspaceAbilities::workspaceCleanupRun( |
| 85 | + array( |
| 86 | + 'mode' => 'artifacts', |
| 87 | + 'agent_slug' => 'explicit-agent', |
| 88 | + ) |
| 89 | + ); |
| 90 | + |
| 91 | + datamachine_code_cleanup_run_context_assert($result instanceof \WP_Error, 'cleanup run without scheduled job returns an error'); |
| 92 | + datamachine_code_cleanup_run_context_assert('workspace_cleanup_schedule_empty' === $result->get_error_code(), 'cleanup run reports empty schedule explicitly'); |
| 93 | + datamachine_code_cleanup_run_context_assert('explicit-agent' === ( \DataMachine\Engine\Tasks\TaskScheduler::$last_context['agent_slug'] ?? '' ), 'explicit agent slug is sanitized and forwarded'); |
| 94 | + |
| 95 | + echo "Workspace cleanup run context smoke passed.\n"; |
| 96 | +} |
0 commit comments