<?php

use PHPUnit\Framework\TestCase;
use PortalPeople\monitor\services\MonitorService;
use yii\base\Application;
use yii\base\Module;

class MonitorServiceModuleMonitoringTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        $config = [
            'id' => 'test-app',
            'basePath' => dirname(__DIR__),
            'runtimePath' => dirname(__DIR__) . '/runtime',
        ];

        \Yii::$app = new Application($config);
    }

    public function testModuleMonitoringDiscoversDirectoryChecks(): void
    {
        $module = new class extends Module {
            public function getMonitoringConfig(): array
            {
                return [
                    'name' => 'Demo module',
                    'checks' => [
                        [
                            'id' => 'runtime-dir',
                            'label' => 'Runtime dir',
                            'type' => 'directory',
                            'path' => '@runtime',
                        ],
                    ],
                ];
            }
        };

        \Yii::$app->setModule('demo', $module);

        $service = new MonitorService();
        $result = $service->moduleMonitoring();

        $this->assertArrayHasKey('demo', $result);
        $this->assertSame('ok', $result['demo']['status']);
        $this->assertSame('runtime-dir', $result['demo']['checks'][0]['id']);
    }
}
