|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DataMachine\Core\Steps { |
| 6 | + trait HandlerRegistrationTrait { |
| 7 | + public static function registerHandler( mixed ...$args ): void {} |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +namespace DataMachine\Core\Steps\Publish\Handlers { |
| 12 | + class PublishHandler { |
| 13 | + public function __construct( string $slug ) {} |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +namespace { |
| 18 | + if ( ! defined('ABSPATH') ) { |
| 19 | + define('ABSPATH', __DIR__ . '/fixtures/'); |
| 20 | + } |
| 21 | + |
| 22 | + if ( ! function_exists('sanitize_key') ) { |
| 23 | + function sanitize_key( string $key ): string { |
| 24 | + return strtolower(preg_replace('/[^a-zA-Z0-9_\-]/', '', $key) ?? ''); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + if ( ! function_exists('sanitize_title') ) { |
| 29 | + function sanitize_title( string $title ): string { |
| 30 | + $title = strtolower(trim($title)); |
| 31 | + $title = preg_replace('/[^a-z0-9]+/', '-', $title) ?? ''; |
| 32 | + return trim($title, '-'); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + require_once dirname(__DIR__) . '/inc/RunArtifacts/RunArtifactRepositoryInterface.php'; |
| 37 | + require_once dirname(__DIR__) . '/inc/Support/RunArtifactBundleFileWriter.php'; |
| 38 | + require_once dirname(__DIR__) . '/inc/Handlers/GitHub/GitHubPullRequestPublish.php'; |
| 39 | + |
| 40 | + use DataMachineCode\Handlers\GitHub\GitHubPullRequestPublish; |
| 41 | + use DataMachineCode\RunArtifacts\RunArtifactRepositoryInterface; |
| 42 | + |
| 43 | + final class FakeRunArtifactRepository implements RunArtifactRepositoryInterface { |
| 44 | + public int $artifact_job_id = 0; |
| 45 | + public int $policy_job_id = 0; |
| 46 | + |
| 47 | + public function artifacts_for_job( int $job_id ): array { |
| 48 | + $this->artifact_job_id = $job_id; |
| 49 | + |
| 50 | + return array( |
| 51 | + 'daily_memory_artifacts' => array( |
| 52 | + array( |
| 53 | + 'type' => 'daily_memory', |
| 54 | + 'agent_slug' => 'boundary agent', |
| 55 | + 'bundle_relative_path' => 'memory.md', |
| 56 | + 'content' => '# Boundary evidence', |
| 57 | + ), |
| 58 | + ), |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + public function egress_policy_for_job( int $job_id ): array { |
| 63 | + $this->policy_job_id = $job_id; |
| 64 | + |
| 65 | + return array( |
| 66 | + 'daily_memory' => array( |
| 67 | + 'egress' => array( 'bundle-file' ), |
| 68 | + ), |
| 69 | + ); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + function boundary_assert_same( mixed $expected, mixed $actual, string $message ): void { |
| 74 | + if ( $expected !== $actual ) { |
| 75 | + throw new RuntimeException(sprintf("%s\nExpected: %s\nActual: %s", $message, var_export($expected, true), var_export($actual, true))); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + $repository = new FakeRunArtifactRepository(); |
| 80 | + $handler = new GitHubPullRequestPublish($repository); |
| 81 | + $method = new ReflectionMethod($handler, 'bundleFileArtifactsForPublish'); |
| 82 | + |
| 83 | + $files = $method->invoke($handler, array( 'job_id' => 42 ), array( 'bundle_root' => 'bundles/{agent_slug}' )); |
| 84 | + |
| 85 | + boundary_assert_same(42, $repository->artifact_job_id, 'Handler should read artifacts through the injected repository.'); |
| 86 | + boundary_assert_same(42, $repository->policy_job_id, 'Handler should read egress policy through the injected repository.'); |
| 87 | + boundary_assert_same('bundles/boundary-agent/memory.md', $files[0]['file_path'] ?? null, 'Injected repository artifacts should still become bundle-file writes.'); |
| 88 | + boundary_assert_same('# Boundary evidence', $files[0]['content'] ?? null, 'Artifact content should be preserved.'); |
| 89 | + |
| 90 | + echo "run artifact repository boundary test passed.\n"; |
| 91 | +} |
0 commit comments