diff --git a/antd-php/src/AntdClient.php b/antd-php/src/AntdClient.php index 3bde6ed..ecd79a3 100644 --- a/antd-php/src/AntdClient.php +++ b/antd-php/src/AntdClient.php @@ -182,11 +182,7 @@ function (\Throwable $e) { */ public function health(): HealthStatus { - $json = $this->doJson('GET', '/health'); - return new HealthStatus( - ok: ($json['status'] ?? '') === 'ok', - network: $json['network'] ?? '', - ); + return self::healthStatusFromJson($this->doJson('GET', '/health')); } /** @@ -197,10 +193,28 @@ public function health(): HealthStatus public function healthAsync(): PromiseInterface { return $this->doJsonAsync('GET', '/health')->then( - fn(?array $json) => new HealthStatus( - ok: ($json['status'] ?? '') === 'ok', - network: $json['network'] ?? '', - ), + fn(?array $json) => self::healthStatusFromJson($json), + ); + } + + /** + * Convert a /health JSON response to a typed HealthStatus. Diagnostic + * fields default to '' / 0 when talking to a pre-0.4.0 daemon. + * + * @param array|null $json + */ + private static function healthStatusFromJson(?array $json): HealthStatus + { + $json ??= []; + return new HealthStatus( + ok: ($json['status'] ?? '') === 'ok', + network: $json['network'] ?? '', + version: $json['version'] ?? '', + evmNetwork: $json['evm_network'] ?? '', + uptimeSeconds: (int)($json['uptime_seconds'] ?? 0), + buildCommit: $json['build_commit'] ?? '', + paymentTokenAddress: $json['payment_token_address'] ?? '', + paymentVaultAddress: $json['payment_vault_address'] ?? '', ); } diff --git a/antd-php/src/Models/HealthStatus.php b/antd-php/src/Models/HealthStatus.php index 8f1eadb..eb55e57 100644 --- a/antd-php/src/Models/HealthStatus.php +++ b/antd-php/src/Models/HealthStatus.php @@ -4,10 +4,25 @@ namespace Autonomi\Antd\Models; +/** + * Health check result from the antd daemon. + * + * The diagnostic fields ({@see $version}, {@see $evmNetwork}, + * {@see $uptimeSeconds}, {@see $buildCommit}, {@see $paymentTokenAddress}, + * {@see $paymentVaultAddress}) were added in antd 0.4.0. They default to + * empty / 0 so the class stays constructable when talking to a pre-0.4.0 + * daemon that doesn't report them. + */ readonly class HealthStatus { public function __construct( public bool $ok, public string $network, + public string $version = '', + public string $evmNetwork = '', + public int $uptimeSeconds = 0, + public string $buildCommit = '', + public string $paymentTokenAddress = '', + public string $paymentVaultAddress = '', ) {} } diff --git a/antd-php/tests/AntdClientTest.php b/antd-php/tests/AntdClientTest.php index 9af9cfa..fd56871 100644 --- a/antd-php/tests/AntdClientTest.php +++ b/antd-php/tests/AntdClientTest.php @@ -37,12 +37,43 @@ private function jsonResponse(int $status, array $body): Response public function testHealth(): void { $mock = new MockHandler([ - $this->jsonResponse(200, ['status' => 'ok', 'network' => 'local']), + $this->jsonResponse(200, [ + 'status' => 'ok', + 'network' => 'local', + 'version' => '0.4.0', + 'evm_network' => 'local', + 'uptime_seconds' => 42, + 'build_commit' => 'abcdef123456', + 'payment_token_address' => '0xtoken', + 'payment_vault_address' => '0xvault', + ]), ]); $client = $this->createClient($mock); $health = $client->health(); $this->assertTrue($health->ok); $this->assertSame('local', $health->network); + $this->assertSame('0.4.0', $health->version); + $this->assertSame('local', $health->evmNetwork); + $this->assertSame(42, $health->uptimeSeconds); + $this->assertSame('abcdef123456', $health->buildCommit); + $this->assertSame('0xtoken', $health->paymentTokenAddress); + $this->assertSame('0xvault', $health->paymentVaultAddress); + } + + public function testHealthPreV0_4_0Daemon(): void + { + // Older daemons reply with just status + network; the empty defaults + // populate the diagnostic fields rather than throwing. + $mock = new MockHandler([ + $this->jsonResponse(200, ['status' => 'ok', 'network' => 'default']), + ]); + $client = $this->createClient($mock); + $health = $client->health(); + $this->assertTrue($health->ok); + $this->assertSame('default', $health->network); + $this->assertSame('', $health->version); + $this->assertSame('', $health->evmNetwork); + $this->assertSame(0, $health->uptimeSeconds); } // --- Data Public ---