|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Pure-PHP smoke for workspace clone ability fallback routing. |
| 4 | + * |
| 5 | + * Run: php tests/smoke-workspace-clone-ability-fallback.php |
| 6 | + */ |
| 7 | + |
| 8 | +declare( strict_types=1 ); |
| 9 | + |
| 10 | +namespace DataMachineCode\Abilities { |
| 11 | + class AbilityRegistry |
| 12 | + { |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +namespace DataMachineCode\Workspace { |
| 17 | + class RemoteWorkspaceBackend |
| 18 | + { |
| 19 | + public static array $clone_input = array(); |
| 20 | + |
| 21 | + public static function should_handle(): bool |
| 22 | + { |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + public function clone_repo( string $url, ?string $name = null ): array |
| 27 | + { |
| 28 | + self::$clone_input = compact('url', 'name'); |
| 29 | + return array( |
| 30 | + 'success' => true, |
| 31 | + 'backend' => 'github_api', |
| 32 | + 'name' => (string) $name, |
| 33 | + 'path' => 'github://Extra-Chill/data-machine-code', |
| 34 | + 'message' => 'Registered remote workspace.', |
| 35 | + ); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + class Workspace |
| 40 | + { |
| 41 | + public static array $clone_input = array(); |
| 42 | + |
| 43 | + public function clone_repo( string $url, ?string $name = null, array $options = array() ): \WP_Error |
| 44 | + { |
| 45 | + self::$clone_input = compact('url', 'name', 'options'); |
| 46 | + return new \WP_Error( |
| 47 | + 'datamachine_workspace_git_unavailable', |
| 48 | + 'Clone workspace repository cannot run with the current workspace backend.' |
| 49 | + ); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +namespace DataMachineCode\Support { |
| 55 | + class RuntimeCapabilities |
| 56 | + { |
| 57 | + } |
| 58 | + |
| 59 | + class GitRunner |
| 60 | + { |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +namespace { |
| 65 | + if ( ! defined('ABSPATH') ) { |
| 66 | + define('ABSPATH', __DIR__); |
| 67 | + } |
| 68 | + |
| 69 | + class WP_Error |
| 70 | + { |
| 71 | + public function __construct( private string $code, private string $message, private array $data = array() ) |
| 72 | + { |
| 73 | + } |
| 74 | + |
| 75 | + public function get_error_code(): string |
| 76 | + { |
| 77 | + return $this->code; |
| 78 | + } |
| 79 | + |
| 80 | + public function get_error_message(): string |
| 81 | + { |
| 82 | + return $this->message; |
| 83 | + } |
| 84 | + |
| 85 | + public function get_error_data(): array |
| 86 | + { |
| 87 | + return $this->data; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + function is_wp_error( $value ): bool |
| 92 | + { |
| 93 | + return $value instanceof WP_Error; |
| 94 | + } |
| 95 | + |
| 96 | + require __DIR__ . '/../inc/Abilities/WorkspaceAbilities.php'; |
| 97 | + |
| 98 | + $failures = array(); |
| 99 | + $assert = function ( string $label, bool $condition ) use ( &$failures ): void { |
| 100 | + if ( $condition ) { |
| 101 | + echo " ok {$label}\n"; |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + $failures[] = $label; |
| 106 | + echo " fail {$label}\n"; |
| 107 | + }; |
| 108 | + |
| 109 | + echo "Workspace clone ability fallback - smoke\n"; |
| 110 | + |
| 111 | + $result = \DataMachineCode\Abilities\WorkspaceAbilities::cloneRepo( |
| 112 | + array( |
| 113 | + 'url' => 'https://github.com/Extra-Chill/data-machine-code.git', |
| 114 | + 'name' => 'data-machine-code', |
| 115 | + 'allow_duplicate_remote' => true, |
| 116 | + ) |
| 117 | + ); |
| 118 | + |
| 119 | + $assert('local clone path was attempted first', 'https://github.com/Extra-Chill/data-machine-code.git' === ( \DataMachineCode\Workspace\Workspace::$clone_input['url'] ?? '' )); |
| 120 | + $assert('local clone receives duplicate remote option', true === ( \DataMachineCode\Workspace\Workspace::$clone_input['options']['allow_duplicate_remote'] ?? false )); |
| 121 | + $assert('remote fallback receives clone URL', 'https://github.com/Extra-Chill/data-machine-code.git' === ( \DataMachineCode\Workspace\RemoteWorkspaceBackend::$clone_input['url'] ?? '' )); |
| 122 | + $assert('remote fallback receives clone name', 'data-machine-code' === ( \DataMachineCode\Workspace\RemoteWorkspaceBackend::$clone_input['name'] ?? '' )); |
| 123 | + $assert('fallback returns remote backend result', is_array($result) && 'github_api' === ( $result['backend'] ?? '' )); |
| 124 | + $assert('fallback keeps clone guidance', is_array($result) && 'workspace_worktree_add' === ( $result['next_required_tool'] ?? '' )); |
| 125 | + |
| 126 | + if ( $failures ) { |
| 127 | + echo "\nFailures:\n"; |
| 128 | + foreach ( $failures as $failure ) { |
| 129 | + echo " - {$failure}\n"; |
| 130 | + } |
| 131 | + exit(1); |
| 132 | + } |
| 133 | + |
| 134 | + echo "\nOK\n"; |
| 135 | +} |
0 commit comments