import argparse
import asyncio
import os

from clarabot_nano.common.config.constants import CommonConfigConstants
from clarabot_nano.common.spawn.constants import ShortNames
from clarabot_nano.development.spawn.common import write_spawn_debug_info
from clarabot_nano.server.fstx.process import FilesystemTransactorServerProcess
from clarabot_nano.utility.path.resolve import resolve_path


class _TestingFilesystemTransactorServerProcess(FilesystemTransactorServerProcess):
    def _worker_start_delay(self) -> float:
        assert 10.0 < FilesystemTransactorServerProcess._worker_start_delay(self) < 90.0
        return 0.5  # for testing


async def startup():
    with _TestingFilesystemTransactorServerProcess.create(ShortNames.FSTX_SERVER) as proc:
        config_dir = resolve_path(proc.get_config()["local"]["config_dir"], strict=True)
        write_spawn_debug_info(config_dir, os.getpid(), ShortNames.FSTX_SERVER)
        await proc.run()


def main():
    CommonConfigConstants.DEVELOPMENT = True
    parser = argparse.ArgumentParser("Clarabot Nano - Test Server process (filesystem transactor)")
    args = parser.parse_args()
    asyncio.run(startup())


if __name__ == "__main__":
    main()
