<?php

$candidates = [WD . '/openapi.yaml'];
for ($depth = 1; $depth <= 6; $depth++) {
    $candidates[] = dirname(WD, $depth) . DIRECTORY_SEPARATOR . 'openapi.yaml';
}
$cwd = getcwd() ?: '';
if ($cwd !== '') {
    $candidates[] = $cwd . DIRECTORY_SEPARATOR . 'openapi.yaml';
    $candidates[] = dirname($cwd) . DIRECTORY_SEPARATOR . 'openapi.yaml';
}

$openApiPath = null;
foreach (array_unique($candidates) as $candidate) {
    if (is_file($candidate)) {
        $openApiPath = $candidate;
        break;
    }
}

if ($openApiPath === null) {
    test()->markTestSkipped('openapi.yaml is not available in this runtime environment.');
}

it('documents the superuser system status snapshot endpoint in openapi', function () use ($openApiPath): void {
    $content = file_get_contents($openApiPath);

    expect($content)->toContain('/superuser/system/status:');
    expect($content)->toContain('operationId: getSuperuserSystemStatus');
    expect($content)->toContain('SuperuserSystemStatusResponse');
    expect($content)->toContain('Bypass cached external module probes');
});

it('defines the reusable system status schemas and enums', function () use ($openApiPath): void {
    $content = file_get_contents($openApiPath);

    expect($content)->toContain('SuperuserSystemStatusPayload:');
    expect($content)->toContain('SuperuserSystemStatusEnum:');
    expect($content)->toContain('enum: [ok, degraded, down]');
    expect($content)->toContain('SuperuserModuleStatusEnum:');
    expect($content)->toContain('enum: [disabled, not_configured, configured, ok, degraded, down]');
    expect($content)->toContain('SuperuserModuleUsage:');
    expect($content)->toContain('status_reason_key:');
    expect($content)->toContain('status_reason_params:');
    expect($content)->toContain('usage_percent:');
    expect($content)->toContain('SuperuserSessionStatus:');
});
