'http', 'version' => '1.0.0', 'description' => 'Utilidades HTTP', 'components' => [ 'curlRequest' => ['files' => ['src/Libs/CurlRequest.php']], 'aria2' => ['files' => ['src/Libs/Aria2.php']], 'compreFace' => [ 'files' => ['src/Libs/CompreFace.php'], 'require' => ['http:curlRequest' => '>=1.0'], ], ], 'default' => $compreFaceInDefault ? ['curlRequest', 'aria2', 'compreFace'] : ['aria2'], ], JSON_PRETTY_PRINT)); write_file($dir . '/packages/http/src/Libs/CurlRequest.php', " $paths */ function g7_materialize(string $repo, string $ref, string $root, array $paths): void { foreach ($paths as $path) { $contents = shell_exec( 'git -C ' . escapeshellarg($repo) . ' show ' . escapeshellarg($ref . ':' . $path) . ' 2>/dev/null' ); write_file($root . '/' . $path, (string) $contents); } } /** @return array */ function g7_legacy_lock(string $proj, string $repo): array { $oldCommit = trim((string) shell_exec('git -C ' . escapeshellarg($repo) . ' rev-parse v0.1.0')); $routerHash = 'sha256:' . hash('sha256', (string) shell_exec( 'git -C ' . escapeshellarg($repo) . ' show v0.1.0:src/Libs/Router.php' )); $files = [ 'autoload.php' => Drift::hash($proj . '/autoload.php'), 'config.php' => Drift::hash($proj . '/config.php'), 'src/Libs/Router.php' => $routerHash, 'src/Libs/Legacy.php' => Drift::hash($proj . '/src/Libs/Legacy.php'), ]; write_file($proj . '/.duckbrain/lock.json', json_encode([ 'core' => ['version' => '0.1.0', 'commit' => $oldCommit, 'files' => $files], 'components' => [], ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"); return $files; } Harness::it('install descarga el core en un directorio vacío', function () { $proj = tmpdir('proj'); $core = g7_core('0.1.0'); $tool = g7_toolbox(); $cmd = new InstallCommand(new Console()); $code = $cmd->run([$proj, '--toolbox=' . $tool, '--core-dir=' . $core, '--yes']); eq(0, $code); ok(is_file($proj . '/autoload.php')); ok(is_file($proj . '/src/Libs/Router.php')); ok(!is_file($proj . '/readme.org'), 'no copia la documentación del core'); eq('0.1.0', Lock::load($proj)->core()['version'], 'versión resuelta por tag v0.1.0'); rrmdir($proj); rrmdir($core); rrmdir($tool); }); Harness::it('add copia componente, registra lock y manifest', function () { $proj = tmpdir('proj'); $tool = g7_toolbox(); $cmd = new AddCommand(new Console()); $code = $cmd->run(['http:aria2', '--root=' . $proj, '--toolbox=' . $tool, '--yes']); eq(0, $code); ok(is_file($proj . '/src/Libs/Aria2.php'), 'archivo copiado'); $lock = Lock::load($proj); ok($lock->has('http:aria2'), 'lock registra el componente'); rrmdir($proj); rrmdir($tool); }); Harness::it('add de paquete expande default; require trae dependencia', function () { $proj = tmpdir('proj'); $tool = g7_toolbox(false); // default sólo aria2 $cmd = new AddCommand(new Console()); $cmd->run(['http:compreFace', '--root=' . $proj, '--toolbox=' . $tool, '--yes']); ok(is_file($proj . '/src/Libs/CompreFace.php')); ok(is_file($proj . '/src/Libs/CurlRequest.php'), 'dependencia require instalada'); $proj2 = tmpdir('proj2'); $cmd->run(['http', '--root=' . $proj2, '--toolbox=' . $tool, '--yes']); ok(is_file($proj2 . '/src/Libs/Aria2.php')); ok(!is_file($proj2 . '/src/Libs/CompreFace.php'), 'default sólo trae lo declarado'); rrmdir($proj); rrmdir($proj2); rrmdir($tool); }); Harness::it('remove sólo quita archivos del componente', function () { $proj = tmpdir('proj'); $core = g7_core(); $tool = g7_toolbox(); (new InstallCommand(new Console()))->run([$proj, '--toolbox=' . $tool, '--core-dir=' . $core, '--yes']); (new AddCommand(new Console()))->run(['http:aria2', '--root=' . $proj, '--toolbox=' . $tool, '--yes']); (new RemoveCommand(new Console()))->run(['http:aria2', '--root=' . $proj, '--toolbox=' . $tool, '--yes']); ok(!is_file($proj . '/src/Libs/Aria2.php'), 'archivo del componente eliminado'); ok(is_file($proj . '/src/Libs/Router.php'), 'archivo del core intacto'); rrmdir($proj); rrmdir($core); rrmdir($tool); }); Harness::it('update core sólo toca rutas del core', function () { $proj = tmpdir('proj'); $tool = g7_toolbox(); $core = g7_core('1.0.0', 'router-v1'); (new InstallCommand(new Console()))->run([$proj, '--toolbox=' . $tool, '--core-dir=' . $core, '--yes']); (new AddCommand(new Console()))->run(['http:aria2', '--root=' . $proj, '--toolbox=' . $tool, '--yes']); $core2 = g7_core('2.0.0', 'router-v2'); (new UpdateCommand(new Console()))->run(['core', '--root=' . $proj, '--toolbox=' . $tool, '--core-dir=' . $core2, '--yes']); ok(str_contains((string) file_get_contents($proj . '/src/Libs/Router.php'), 'router-v2'), 'core actualizado'); ok(is_file($proj . '/src/Libs/Aria2.php'), 'componente intacto'); eq('2.0.0', Lock::load($proj)->core()['version']); rrmdir($proj); rrmdir($tool); rrmdir($core); rrmdir($core2); }); Harness::it('list muestra instalados con versión y modo', function () { $proj = tmpdir('proj'); $tool = g7_toolbox(); (new AddCommand(new Console()))->run(['http:aria2', '--root=' . $proj, '--toolbox=' . $tool, '--yes']); $console = g7_console($stream); (new ListCommand($console))->run(['--root=' . $proj, '--toolbox=' . $tool]); rewind($stream); $output = stream_get_contents($stream); ok(str_contains($output, 'http:aria2'), 'id en la salida'); ok(str_contains($output, 'copy'), 'modo en la salida'); rrmdir($proj); rrmdir($tool); }); Harness::it('--dry-run no escribe archivos ni lock', function () { $proj = tmpdir('proj'); $tool = g7_toolbox(); (new AddCommand(new Console()))->run(['http:aria2', '--root=' . $proj, '--toolbox=' . $tool, '--yes', '--dry-run']); ok(!is_file($proj . '/src/Libs/Aria2.php'), 'no copia en dry-run'); ok(!is_file(Lock::path($proj)), 'no crea lock en dry-run'); rrmdir($proj); rrmdir($tool); }); Harness::it('update core --dry-run no escribe archivos', function () { $proj = tmpdir('proj'); $tool = g7_toolbox(); $core = g7_core('1.0.0', 'router-v1'); $core2 = g7_core('2.0.0', 'router-v2'); (new InstallCommand(new Console()))->run([$proj, '--toolbox=' . $tool, '--core-dir=' . $core, '--yes']); $routerBefore = (string) file_get_contents($proj . '/src/Libs/Router.php'); (new UpdateCommand(new Console()))->run(['core', '--root=' . $proj, '--toolbox=' . $tool, '--core-dir=' . $core2, '--yes', '--dry-run']); ok(str_contains($routerBefore, 'router-v1'), 'estado inicial correcto'); ok(str_contains((string) file_get_contents($proj . '/src/Libs/Router.php'), 'router-v1'), 'dry-run no escribe el fichero'); eq('1.0.0', Lock::load($proj)->core()['version'], 'dry-run no actualiza la version en el lock'); rrmdir($proj); rrmdir($tool); rrmdir($core); rrmdir($core2); }); Harness::it('update core --dry-run aborta por drift sin escribir', function () { $proj = tmpdir('proj'); $tool = g7_toolbox(); $core = g7_core('1.0.0', 'router-v1'); $core2 = g7_core('2.0.0', 'router-v2'); (new InstallCommand(new Console()))->run([$proj, '--toolbox=' . $tool, '--core-dir=' . $core, '--yes']); write_file($proj . '/config.php', "run(['core', '--root=' . $proj, '--toolbox=' . $tool, '--core-dir=' . $core2, '--yes', '--dry-run']); ok(!is_file($proj . '/config.php.bak'), 'dry-run no crea backups'); ok(str_contains((string) file_get_contents($proj . '/config.php'), 'customizado'), 'dry-run conserva la edicion local'); ok(str_contains((string) file_get_contents($proj . '/src/Libs/Router.php'), 'router-v1'), 'dry-run no toca ficheros sin drift'); rrmdir($proj); rrmdir($tool); rrmdir($core); rrmdir($core2); }); Harness::it('core sin tags: versión desconocida y commit pineado', function () { $proj = tmpdir('proj'); $tool = g7_toolbox(); $core = g7_core(''); // repo git sin tags (new InstallCommand(new Console()))->run([$proj, '--toolbox=' . $tool, '--core-dir=' . $core, '--yes']); $lock = Lock::load($proj); eq(null, $lock->core()['version'], 'sin tags la versión es desconocida'); ok($lock->core()['commit'] !== null, 'commit pineado sin depender de tags'); rrmdir($proj); rrmdir($tool); rrmdir($core); }); Harness::it('--force resuelve drift pero no salta avisos de compatibilidad', function () { $proj = tmpdir('proj'); $tool = g7_toolbox(); // add once and edit locally (new AddCommand(new Console()))->run(['http:aria2', '--root=' . $proj, '--toolbox=' . $tool, '--yes']); write_file($proj . '/src/Libs/Aria2.php', " warning against core 0.0.0 (unknown) is OK; // set a tested value to force a warning $json = json_decode((string) file_get_contents($tool . '/packages/http/duckbrain.json'), true); $json['core'] = ['requires' => '>=1.0']; write_file($tool . '/packages/http/duckbrain.json', json_encode($json)); $stream = fopen('php://memory', 'r+'); $console = new Console($stream, $stream); $code = (new AddCommand($console))->run(['http:aria2', '--root=' . $proj, '--toolbox=' . $tool, '--yes', '--force']); eq(0, $code, 'force resuelve el drift'); rewind($stream); $output = stream_get_contents($stream); ok(str_contains($output, 'WARN'), 'sigue avisando de compatibilidad'); ok(str_contains((string) file_get_contents($proj . '/src/Libs/Aria2.php'), 'aria2'), 'sobreescribe con --force'); rrmdir($proj); rrmdir($tool); }); Harness::it('el catálogo describe cada componente con su texto propio o heredado', function () { $toolbox = g7_toolbox(); $manifest = json_decode((string) file_get_contents($toolbox . '/packages/http/duckbrain.json'), true); $manifest['components']['curlRequest']['description'] = 'Cliente HTTP sobre cURL'; write_file($toolbox . '/packages/http/duckbrain.json', json_encode($manifest, JSON_PRETTY_PRINT)); $proj = tmpdir('proj'); $out = fopen('php://memory', 'rw'); $code = (new Toolbox\Commands\CatalogCommand(new Console($out)))->run( ['--root=' . $proj, '--toolbox=' . $toolbox] ); rewind($out); $text = (string) stream_get_contents($out); eq(0, $code, 'catalog termina bien'); ok((bool) preg_match('/curlRequest.*Cliente HTTP sobre cURL/', $text), 'fila propia con su texto'); ok((bool) preg_match('/aria2.*Utilidades HTTP/', $text), 'fila sin texto hereda el del paquete'); ok((bool) preg_match('/compreFace.*Utilidades HTTP/', $text), 'vecino heredando igual'); ok(!preg_match('/aria2.*cURL/', $text), 'los textos no se mezclan entre filas'); rrmdir($toolbox); rrmdir($proj); }); Harness::it('un comando del proyecto sin describe() no carga (interfaz estricta)', function () { $proj = tmpdir('proj'); mkdir($proj . '/src/Commands', 0777, true); write_file($proj . '/autoload.php', "&1', $out, $code); ok($code !== 0 || str_contains(implode('', $out), 'Fatal'), 'la carga de un comando sin describe produce error fatal'); rrmdir($proj); }); Harness::it('canonicalAt lee un ref viejo y falla cerrado con refs o rutas ausentes', function () { $repo = g7_core_history(); $old = trim((string) shell_exec('git -C ' . escapeshellarg($repo) . ' rev-parse v0.1.0')); $provider = CoreProvider::fromDirectory($repo); $hashes = $provider->canonicalAt($old, null, ['src/Libs/Router.php', 'src/Libs/Legacy.php', 'src/Empty/.gitkeep']); ok(is_array($hashes), 'resuelve el ref viejo'); $canonicalRouter = 'sha256:' . hash('sha256', (string) shell_exec( 'git -C ' . escapeshellarg($repo) . ' show v0.1.0:src/Libs/Router.php' )); eq($canonicalRouter, $hashes['src/Libs/Router.php'], 'hash canónico histórico'); eq('sha256:' . hash('sha256', ''), $hashes['src/Empty/.gitkeep'], 'archivo vacío resuelto'); eq(null, $provider->canonicalAt('deadbeef', '9.9.9', ['src/Libs/Router.php']), 'ref inexistente'); eq(null, $provider->canonicalAt($old, null, ['src/Libs/Nope.php']), 'ruta inexistente'); rrmdir($repo); }); Harness::it('update core repara un lock heredado y protege keep-local de forma durable', function () { $repo = g7_core_history(); $tool = g7_toolbox(); $proj = tmpdir('proj'); $paths = ['autoload.php', 'config.php', 'src/Libs/Router.php', 'src/Libs/Legacy.php']; g7_materialize($repo, 'v0.1.0', $proj, $paths); $config = "run([ 'core', '--root=' . $proj, '--toolbox=' . $tool, '--core-dir=' . $repo, '--yes', '--keep-local', ]); rewind($stream); $out = (string) stream_get_contents($stream); eq(0, $code, 'el update termina sin abortos'); eq($config, (string) file_get_contents($proj . '/config.php'), 'config local intacto'); eq($legacy, (string) file_get_contents($proj . '/src/Libs/Legacy.php'), 'obsoleto local intacto'); ok(str_contains((string) file_get_contents($proj . '/src/Libs/Router.php'), 'router-v2'), 'Router actualizado'); ok(str_contains($out, 'skipped config.php'), 'reporta el archivo saltado'); ok(str_contains($out, 'resumen:'), 'reporta un resumen por archivo'); ok(str_contains($out, 'obsoleto conservado localmente: src/Libs/Legacy.php'), 'avisa del obsoleto conservado'); $lock = Lock::load($proj); eq(2, $lock->coreSchema(), 'el lock migra a esquema 2'); eq('0.1.1', $lock->core()['version']); ok(!isset($lock->core()['files']['src/Libs/Legacy.php']), 'el obsoleto sale del ledger'); $canonicalConfig = 'sha256:' . hash('sha256', (string) shell_exec( 'git -C ' . escapeshellarg($repo) . ' show v0.1.1:config.php' )); eq($canonicalConfig, $lock->core()['files']['config.php'], 'el ledger guarda el canónico destino, no el hash local'); g7_core_tag($repo, '0.1.2', 'router-v3'); (new UpdateCommand(g7_console($quiet)))->run([ 'core', '--root=' . $proj, '--toolbox=' . $tool, '--core-dir=' . $repo, '--yes', '--keep-local', ]); eq($config, (string) file_get_contents($proj . '/config.php'), 'keep-local durable: config sigue intacto'); ok(str_contains((string) file_get_contents($proj . '/src/Libs/Router.php'), 'router-v3'), 'Router sigue actualizándose'); eq('0.1.2', Lock::load($proj)->core()['version']); g7_core_tag($repo, '0.1.3', 'router-v4'); $rc = (new UpdateCommand(g7_console($quiet2)))->run([ 'core', '--root=' . $proj, '--toolbox=' . $tool, '--core-dir=' . $repo, '--yes', ]); eq(1, $rc, 'sin --keep-local el drift aborta'); eq($config, (string) file_get_contents($proj . '/config.php'), 'el aborto no pisa config'); eq('0.1.2', Lock::load($proj)->core()['version'], 'el lock no avanza con abortos'); rrmdir($proj); rrmdir($tool); rrmdir($repo); }); Harness::it('update core falla cerrado si no puede reparar el lock heredado', function () { $repo = g7_core_history(); $fresh = g7_core('9.9.9', 'router-fresh'); $tool = g7_toolbox(); $proj = tmpdir('proj'); $paths = ['autoload.php', 'config.php', 'src/Libs/Router.php', 'src/Libs/Legacy.php']; g7_materialize($repo, 'v0.1.0', $proj, $paths); write_file($proj . '/config.php', "run([ 'core', '--root=' . $proj, '--toolbox=' . $tool, '--core-dir=' . $fresh, '--yes', '--keep-local', ]); rewind($stream); $out = (string) stream_get_contents($stream); eq(1, $code, 'falla cerrado'); eq($lockBefore, (string) file_get_contents(Lock::path($proj)), 'el lock no se reescribe'); eq($routerBefore, (string) file_get_contents($proj . '/src/Libs/Router.php'), 'no se escribe el core'); ok(str_contains($out, 'No se pudo reparar el lock del core'), 'explica el bloqueo'); rrmdir($proj); rrmdir($tool); rrmdir($repo); rrmdir($fresh); }); Harness::it('update core --dry-run planifica el drift sin escribir', function () { $repo = g7_core_history(); $tool = g7_toolbox(); $proj = tmpdir('proj'); $paths = ['autoload.php', 'config.php', 'src/Libs/Router.php', 'src/Libs/Legacy.php']; g7_materialize($repo, 'v0.1.0', $proj, $paths); write_file($proj . '/config.php', "run([ 'core', '--root=' . $proj, '--toolbox=' . $tool, '--core-dir=' . $repo, '--yes', '--keep-local', '--dry-run', ]); rewind($stream); $out = (string) stream_get_contents($stream); eq(0, $code, 'dry-run termina bien'); ok(str_contains($out, 'planned-skipped config.php'), 'planifica el archivo protegido'); ok(str_contains($out, 'planned-overwritten src/Libs/Router.php'), 'planifica el archivo a actualizar'); eq($lockBefore, (string) file_get_contents(Lock::path($proj)), 'dry-run no toca el lock'); eq($routerBefore, (string) file_get_contents($proj . '/src/Libs/Router.php'), 'dry-run no escribe archivos'); rrmdir($proj); rrmdir($tool); rrmdir($repo); });