Files
preland/tests/check-cons-config-patch.php
2026-10-24 04:35:44 +03:00

34 lines
3.4 KiB
PHP

<?php
/** preland#17: точечный, идемпотентный patch только chb/cons и его defaults/weights. */
declare(strict_types=1);
$script=dirname(__DIR__).'/bin/ensure-cons-config.php';
if(!is_file($script)){fwrite(STDERR,"FAIL: patch cons ещё не реализован.\n");exit(1);}
$fixture=['host'=>'keep.example','requisites'=>['Не менять'],'utm'=>['theme'=>['default'=>'dark','weights'=>['light'=>7]],'chb'=>['default'=>'b','weights'=>['a'=>1,'b'=>0]],'focus'=>['default'=>'face']]];
$path=tempnam(sys_get_temp_dir(),'cons-config-');
try{
file_put_contents($path,json_encode($fixture,JSON_THROW_ON_ERROR));
exec(escapeshellarg(PHP_BINARY).' '.escapeshellarg($script).' '.escapeshellarg($path),$output,$code);
$actual=json_decode(file_get_contents($path),true,512,JSON_THROW_ON_ERROR);
if($code!==10||isset($actual['utm']['chb'])||($actual['utm']['cons']??null)!==['weights'=>['1'=>100,'2'=>0],'default'=>'2'])throw new RuntimeException('Patch должен заменить только согласованный ключ chb на cons.');
$comparison=$actual;unset($comparison['utm']['cons']);$old=$fixture;unset($old['utm']['chb']);
if($comparison!==$old)throw new RuntimeException('Patch изменил соседние поля runtime config.');
$before=file_get_contents($path);exec(escapeshellarg(PHP_BINARY).' '.escapeshellarg($script).' '.escapeshellarg($path),$output,$code);
if($code!==0||file_get_contents($path)!==$before)throw new RuntimeException('Повторный patch должен оставить байты без изменений.');
$custom=$actual;$custom['utm']['cons']=['weights'=>['1'=>20,'3'=>80],'default'=>'3'];
file_put_contents($path,json_encode($custom,JSON_THROW_ON_ERROR));$before=file_get_contents($path);
exec(escapeshellarg(PHP_BINARY).' '.escapeshellarg($script).' '.escapeshellarg($path),$output,$code);
if($code!==0||file_get_contents($path)!==$before)throw new RuntimeException('Следующий deploy не должен сбросить настроенный владельцем cons.');
foreach([[],true,1.5,'2147483648','0']as$invalid){
$broken=$custom;$broken['utm']['cons']['default']=$invalid;
file_put_contents($path,json_encode($broken,JSON_THROW_ON_ERROR));$before=file_get_contents($path);
exec(escapeshellarg(PHP_BINARY).' '.escapeshellarg($script).' '.escapeshellarg($path).' 2>/dev/null',$output,$code);
if($code!==2||file_get_contents($path)!==$before)throw new RuntimeException('Некорректный существующий cons должен остановить deploy без перезаписи config.');
}
$objectFixture='{"host":"keep","extra":{},"map":{"0":"a","1":"b"},"list":[],"utm":{"chb":{"default":"a"},"other":{"weights":{}}}}';
file_put_contents($path,$objectFixture);exec(escapeshellarg(PHP_BINARY).' '.escapeshellarg($script).' '.escapeshellarg($path),$output,$code);
$beforeObject=json_decode($objectFixture,false,512,JSON_THROW_ON_ERROR);$afterObject=json_decode(file_get_contents($path),false,512,JSON_THROW_ON_ERROR);
unset($beforeObject->utm->chb,$afterObject->utm->cons);
if($code!==10||json_encode($beforeObject)!==json_encode($afterObject))throw new RuntimeException('Patch должен сохранять типы соседних пустых и numeric-key JSON объектов.');
echo "PASS: cons config migration preserves other fields and JSON types, idempotent\n";
}finally{@unlink($path);}