Files
zyt/server/tests/DesktopWorkstationUpdateContractTest.php
T
2026-08-22 10:46:13 +08:00

114 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use app\adminapi\logic\setting\DesktopWorkstationLogic;
function desktopUpdateExpect(bool $condition, string $message): void
{
if (!$condition) {
fwrite(STDERR, "FAIL: {$message}\n");
exit(1);
}
}
desktopUpdateExpect(
DesktopWorkstationLogic::normalizeVersion('0.2') === '0.2.0',
'short versions pad to three segments'
);
desktopUpdateExpect(
DesktopWorkstationLogic::compareVersion('0.1.0', '0.2.0') < 0,
'0.1.0 is older than 0.2.0'
);
desktopUpdateExpect(
DesktopWorkstationLogic::packageKey('win32', 'amd64') === 'windows_x64',
'windows/amd64 maps to windows_x64'
);
desktopUpdateExpect(
DesktopWorkstationLogic::packageKey('darwin', 'aarch64') === 'macos_arm64',
'darwin/arm maps to macos_arm64'
);
$sha = str_repeat('a', 64);
$config = [
'enabled' => 1,
'latest_version' => '0.2.0',
'min_version' => '0.1.5',
'force_update' => 0,
'title' => '医生工作站 0.2.0',
'notes' => '稳定性更新',
'packages' => [
'windows_x64' => [
'url' => 'https://cdn.example.com/DoctorWorkstation-Setup-Windows-x64-0.2.0.exe',
'sha256' => $sha,
'size' => 123,
'filename' => 'DoctorWorkstation-Setup-Windows-x64-0.2.0.exe',
'type' => 'inno_setup',
],
],
];
$optional = DesktopWorkstationLogic::evaluate($config, '0.1.8', 'windows', 'x64');
desktopUpdateExpect($optional['has_update'] === true, 'newer published version is an update');
desktopUpdateExpect($optional['force'] === false, 'force stays off when above min version');
desktopUpdateExpect($optional['can_install'] === true, 'hashed package can be installed');
desktopUpdateExpect(
is_array($optional['package'])
&& $optional['package']['sha256'] === $sha
&& $optional['package']['type'] === 'inno_setup',
'matching Windows installer package is returned with its explicit type'
);
$forcedByMin = DesktopWorkstationLogic::evaluate($config, '0.1.0', 'windows', 'x64');
desktopUpdateExpect($forcedByMin['force'] === true, 'below min version forces upgrade');
$config['force_update'] = 1;
$forcedAll = DesktopWorkstationLogic::evaluate($config, '0.1.9', 'windows', 'x64');
desktopUpdateExpect($forcedAll['force'] === true, 'force_update blocks every older client');
$current = DesktopWorkstationLogic::evaluate($config, '0.2.0', 'windows', 'x64');
desktopUpdateExpect($current['has_update'] === false, 'current latest version is not an update');
$macosMissing = DesktopWorkstationLogic::evaluate($config, '0.1.0', 'macos', 'arm64');
desktopUpdateExpect($macosMissing['has_update'] === true, 'other platforms still see the new version');
desktopUpdateExpect($macosMissing['can_install'] === false, 'missing platform package cannot auto-install');
desktopUpdateExpect($macosMissing['force'] === false, 'force requires an installable package');
$disabled = $config;
$disabled['enabled'] = 0;
$quiet = DesktopWorkstationLogic::evaluate($disabled, '0.1.0', 'windows', 'x64');
desktopUpdateExpect($quiet['has_update'] === false, 'disabled detection never prompts');
$controller = file_get_contents(dirname(__DIR__) . '/app/adminapi/controller/setting/DesktopWorkstationController.php');
desktopUpdateExpect(is_string($controller), 'controller source is readable');
desktopUpdateExpect(
str_contains($controller, "public array \$notNeedLogin = ['check']")
&& str_contains($controller, 'public function check()')
&& str_contains($controller, 'DesktopWorkstationLogic::check('),
'check action is public and delegates to logic'
);
$adminView = file_get_contents(dirname(__DIR__, 2) . '/admin/src/views/setting/desktop_workstation/index.vue');
desktopUpdateExpect(is_string($adminView), 'admin view source is readable');
desktopUpdateExpect(
str_contains($adminView, 'setting.desktop_workstation/setConfig')
&& str_contains($adminView, 'force_update')
&& str_contains($adminView, 'inno_setup'),
'admin page can save force-update and Inno Setup configuration'
);
$migration = file_get_contents(
dirname(__DIR__) . '/sql/1.9.20260821/add_desktop_workstation_update_menu.sql'
);
desktopUpdateExpect(is_string($migration), 'menu migration is readable');
desktopUpdateExpect(
substr_count($migration, "WHERE `perms` = 'setting.desktop_workstation/getConfig'") >= 1
&& str_contains($migration, 'setting.desktop_workstation/setConfig')
&& str_contains($migration, 'setting/desktop_workstation/index'),
'menu registration is idempotent and points at the admin view'
);
echo "Desktop workstation update contract: OK\n";