Files
compras/tests/Unit/SgfCodeDeploymentV2Test.php
T
nala c9bd6db90b
tests / ci (push) Canceled after 0s
commit compras2 3011
2026-11-30 23:04:25 +02:00

219 lines
12 KiB
PHP

<?php
use Tempo\Purchasing\Delivery\CodeDeployment;
require_once __DIR__.'/../../integrations/sgf/operational/CodeDeployment.php';
function deploymentV2Write(string $path, string $body): void
{
if (! is_dir(dirname($path))) {
mkdir(dirname($path), 0700, true);
}
file_put_contents($path, $body);
}
function deploymentV2Fixture(): array
{
$root = dirname(__DIR__, 2).'/storage/framework/testing/deployment-v2-'.bin2hex(random_bytes(6));
$host = $root.'/host';
deploymentV2Write($host.'/app/Existing.php', 'original');
deploymentV2Write($host.'/packages/purchasing/current.json', '{"version":"0.1.1"}');
deploymentV2Write($host.'/storage/framework/down', '{}');
deploymentV2Write($host.'/bootstrap/cache/config.php', '<?php return [];');
return [$root, $host, new CodeDeployment($host)];
}
function deploymentV2Bundle(string $root, array $bodies, array $before, string $format = 'v2', ?string $from = null, array $added = [], array $schema = []): array
{
$dir = $root.'/bundle-'.bin2hex(random_bytes(4));
$files = [];
$overlays = [];
foreach ($bodies as $file => $body) {
deploymentV2Write($dir.'/'.$file, $body);
$files[$file] = hash('sha256', $body);
$overlays[$file] = ['before_sha256' => $before[$file], 'after_sha256' => $files[$file]];
}
$manifest = ['format' => 'tempo.operational-review.'.$format, 'support_files' => $files, 'host_overlays' => $overlays];
if ($format === 'v2') {
$manifest['composition_transition'] = ['from_manifest_sha256' => $from, 'added_files' => $added, 'schema_files' => $schema];
}
deploymentV2Write($dir.'/manifest.json', json_encode($manifest, JSON_THROW_ON_ERROR));
return [$dir, hash_file('sha256', $dir.'/manifest.json')];
}
function deploymentV2Apply(CodeDeployment $deploy, string $dir, string $hash): array
{
$plan = $deploy->plan($dir, $hash);
return $deploy->apply($dir, $hash, $plan['target_sha256'], 'Synthetic file-only rehearsal', true);
}
test('v1 still applies replays and restores its exact code cache and module pointer', function () {
[$root, $host, $deploy] = deploymentV2Fixture();
[$dir, $hash] = deploymentV2Bundle($root, ['app/Existing.php' => 'v1'], ['app/Existing.php' => hash('sha256', 'original')], 'v1');
$result = deploymentV2Apply($deploy, $dir, $hash);
expect(deploymentV2Apply($deploy, $dir, $hash)['replayed'])->toBeTrue();
$deploy->rollback($result['deployment_id'], 'Synthetic recovery', true);
expect(file_get_contents($host.'/app/Existing.php'))->toBe('original')
->and(file_get_contents($host.'/bootstrap/cache/config.php'))->toBe('<?php return [];')
->and(file_get_contents($host.'/packages/purchasing/current.json'))->toBe('{"version":"0.1.1"}');
});
test('v2 expands v1 updates new files and recovers their original absence in order', function () {
[$root, $host, $deploy] = deploymentV2Fixture();
[$v1, $h1] = deploymentV2Bundle($root, ['app/Existing.php' => 'v1'], ['app/Existing.php' => hash('sha256', 'original')], 'v1');
$first = deploymentV2Apply($deploy, $v1, $h1);
[$v2, $h2] = deploymentV2Bundle($root, ['app/Existing.php' => 'v2', 'config/merchandise.php' => '<?php return false;'],
['app/Existing.php' => hash('sha256', 'v1'), 'config/merchandise.php' => null], 'v2', $h1, ['config/merchandise.php' => null]);
$secondPlan = $deploy->plan($v2, $h2);
$second = deploymentV2Apply($deploy, $v2, $h2);
expect($deploy->apply($v2, $h2, $secondPlan['target_sha256'], 'Replay same operation', true)['replayed'])->toBeTrue()
->and(file_get_contents($host.'/config/merchandise.php'))->toBe('<?php return false;');
[$v3, $h3] = deploymentV2Bundle($root, ['app/Existing.php' => 'v3', 'config/merchandise.php' => '<?php return null;'],
['app/Existing.php' => hash('sha256', 'v2'), 'config/merchandise.php' => hash('sha256', '<?php return false;')], 'v2', $h2);
$third = deploymentV2Apply($deploy, $v3, $h3);
$deploy->rollback($third['deployment_id'], 'Recover third', true);
expect(file_get_contents($host.'/config/merchandise.php'))->toBe('<?php return false;');
$deploy->rollback($second['deployment_id'], 'Recover second', true);
expect(file_exists($host.'/config/merchandise.php'))->toBeFalse()
->and(file_get_contents($host.'/app/Existing.php'))->toBe('v1');
$deploy->rollback($first['deployment_id'], 'Recover first', true);
expect(file_get_contents($host.'/app/Existing.php'))->toBe('original');
});
test('v2 rejects an undeclared expansion wrong predecessor or existing supposedly new file', function (string $case) {
[$root, $host, $deploy] = deploymentV2Fixture();
$added = ['config/merchandise.php' => null];
if ($case === 'undeclared') {
$added = [];
}
if ($case === 'existing') {
deploymentV2Write($host.'/config/merchandise.php', 'preserve');
}
[$dir, $hash] = deploymentV2Bundle($root, ['config/merchandise.php' => 'new'], ['config/merchandise.php' => null], 'v2', $case === 'predecessor' ? str_repeat('a', 64) : null, $added);
expect(fn () => $deploy->plan($dir, $hash))->toThrow(RuntimeException::class);
expect(file_get_contents($host.'/app/Existing.php'))->toBe('original');
})->with(['undeclared', 'predecessor', 'existing']);
test('v2 partial failure recovers newly created files without touching unknown edits', function (bool $unknownEdit) {
[$root, $host, $deploy] = deploymentV2Fixture();
[$dir, $hash] = deploymentV2Bundle($root, ['app/New.php' => 'candidate', 'app/Existing.php' => 'updated'],
['app/New.php' => null, 'app/Existing.php' => hash('sha256', 'original')], 'v2', null,
['app/New.php' => null, 'app/Existing.php' => hash('sha256', 'original')]);
$plan = $deploy->plan($dir, $hash);
try {
$deploy->apply($dir, $hash, $plan['target_sha256'], 'Synthetic interruption', true, function ($point) {
if ($point === 'file:app/New.php') {
throw new RuntimeException('Synthetic interruption');
}
});
test()->fail('Expected interruption.');
} catch (RuntimeException $error) {
expect($error->getMessage())->toBe('Synthetic interruption');
}
if ($unknownEdit) {
deploymentV2Write($host.'/app/New.php', 'user edit');
expect(fn () => $deploy->rollback($plan['deployment_id'], 'Recover', true))->toThrow(RuntimeException::class)
->and(file_get_contents($host.'/app/New.php'))->toBe('user edit');
} else {
$deploy->rollback($plan['deployment_id'], 'Recover', true);
expect(file_exists($host.'/app/New.php'))->toBeFalse();
}
expect(file_get_contents($host.'/app/Existing.php'))->toBe('original');
})->with([false, true]);
test('v2 requires an exact schema receipt and pins it to the reviewed target', function () {
[$root, $host, $deploy] = deploymentV2Fixture();
$file = 'database/sql/synthetic.sql';
$body = '-- synthetic, never executed';
$schema = [$file => ['before' => null, 'after' => hash('sha256', $body)]];
[$dir, $hash] = deploymentV2Bundle($root, [$file => $body], [$file => null], 'v2', null, [$file => null], $schema);
expect(fn () => $deploy->plan($dir, $hash))->toThrow(RuntimeException::class);
$receiptPath = $host.'/storage/app/private/module-deployments/schema-transition-'.$hash.'.json';
$receipt = ['from_manifest_sha256' => null, 'to_manifest_sha256' => $hash, 'host' => str_replace('\\', '/', realpath($host)), 'schema_verified' => true, 'changes' => $schema];
deploymentV2Write($receiptPath, json_encode($receipt, JSON_THROW_ON_ERROR));
$plan = $deploy->plan($dir, $hash);
$receipt['review_reference'] = 'changed after planning';
deploymentV2Write($receiptPath, json_encode($receipt, JSON_THROW_ON_ERROR));
expect(fn () => $deploy->apply($dir, $hash, $plan['target_sha256'], 'Synthetic', true))->toThrow(RuntimeException::class);
expect(file_exists($host.'/'.$file))->toBeFalse();
});
test('v2 rejects forbidden paths extra content and equivalent Windows paths', function (string $case) {
[$root, $host, $deploy] = deploymentV2Fixture();
[$dir] = deploymentV2Bundle($root, ['app/New.php' => 'candidate'], ['app/New.php' => null], 'v2', null, ['app/New.php' => null]);
$manifest = json_decode(file_get_contents($dir.'/manifest.json'), true, 32, JSON_THROW_ON_ERROR);
if ($case === 'extra') {
deploymentV2Write($dir.'/unexpected.php', 'unlisted');
} elseif ($case === 'equivalent') {
$manifest['support_files']['app/new.php'] = hash('sha256', 'candidate');
} elseif ($case === 'hash') {
deploymentV2Write($dir.'/app/New.php', 'tampered');
} elseif ($case === 'format') {
$manifest['format'] = 'tempo.operational-review.v999';
} else {
$manifest['host_overlays'] = [$case => $manifest['host_overlays']['app/New.php']];
}
deploymentV2Write($dir.'/manifest.json', json_encode($manifest, JSON_THROW_ON_ERROR));
expect(fn () => $deploy->plan($dir, hash_file('sha256', $dir.'/manifest.json')))->toThrow(RuntimeException::class);
expect(file_exists($host.'/app/New.php'))->toBeFalse();
})->with(['extra', 'equivalent', 'hash', 'format', '../outside.php', '.env', 'public/vendor/secret.php', 'packages/purchasing/current.json', 'routes/console.php']);
test('v2 update interruption restores the prior bundle at each durable stage', function (string $point) {
[$root, $host, $deploy] = deploymentV2Fixture();
[$v1, $h1] = deploymentV2Bundle($root, ['app/Existing.php' => 'v1'], ['app/Existing.php' => hash('sha256', 'original')], 'v1');
$first = deploymentV2Apply($deploy, $v1, $h1);
[$v2, $h2] = deploymentV2Bundle($root, ['app/Existing.php' => 'v2', 'public/sgf-new.mjs' => 'candidate'],
['app/Existing.php' => hash('sha256', 'v1'), 'public/sgf-new.mjs' => null], 'v2', $h1, ['public/sgf-new.mjs' => null]);
$plan = $deploy->plan($v2, $h2);
try {
$deploy->apply($v2, $h2, $plan['target_sha256'], 'Interrupt update', true, function ($at) use ($point) {
if ($at === $point) {
throw new RuntimeException('Synthetic interruption');
}
});
test()->fail('Expected interruption.');
} catch (RuntimeException $error) {
expect($error->getMessage())->toBe('Synthetic interruption');
}
$deploy->rollback($plan['deployment_id'], 'Recover update', true);
expect(file_get_contents($host.'/app/Existing.php'))->toBe('v1')
->and(file_exists($host.'/public/sgf-new.mjs'))->toBeFalse()
->and(hash_file('sha256', $host.'/packages/internal-requisitions/manifest.json'))->toBe($h1)
->and(json_decode(file_get_contents($host.'/storage/app/private/module-deployments/current.json'), true)['deployment_id'])->toBe($first['deployment_id']);
})->with(['prepared', 'caches-cleared', 'file:app/Existing.php', 'file:public/sgf-new.mjs', 'previous-bundle-preserved', 'bundle-switched']);
test('deployment uses the verified staged bytes instead of a mutable source directory', function () {
[$root, $host, $deploy] = deploymentV2Fixture();
[$dir, $hash] = deploymentV2Bundle($root, ['app/New.php' => 'candidate'], ['app/New.php' => null], 'v2', null, ['app/New.php' => null]);
$plan = $deploy->plan($dir, $hash);
$result = $deploy->apply($dir, $hash, $plan['target_sha256'], 'Stage isolation', true, function ($point) use ($dir) {
if ($point === 'prepared') {
deploymentV2Write($dir.'/app/New.php', 'source changed after staging');
}
});
expect(file_get_contents($host.'/app/New.php'))->toBe('candidate');
$deploy->rollback($result['deployment_id'], 'Recover', true);
expect(file_exists($host.'/app/New.php'))->toBeFalse();
});
test('v2 refuses links in either payload or destination', function (string $side) {
[$root, $host, $deploy] = deploymentV2Fixture();
deploymentV2Write($root.'/outside/New.php', 'preserve outside host');
$file = $side === 'host' ? 'app/Linked/New.php' : 'app/New.php';
[$dir, $hash] = deploymentV2Bundle($root, [$file => 'candidate'], [$file => null], 'v2', null, [$file => null]);
if ($side === 'host') {
$linked = @symlink($root.'/outside', $host.'/app/Linked');
} else {
unlink($dir.'/app/New.php');
$linked = @symlink($root.'/outside/New.php', $dir.'/app/New.php');
}
if (! $linked) {
test()->markTestSkipped('Creating symbolic links is unavailable; no privileges were changed.');
}
expect(fn () => $deploy->plan($dir, $hash))->toThrow(RuntimeException::class);
expect(file_get_contents($root.'/outside/New.php'))->toBe('preserve outside host');
})->with(['host', 'bundle']);