from clarabot_nano.core.process import CoreProcess
from clarabot_nano.utility.path.resolve import resolve_path


class DebugTestCoreProcess(CoreProcess):
    """
    Dump the new configuration into a file so the  testing framework can ensure consistency between processes.
    """

    def __init__(self, config, headless):
        CoreProcess.__init__(self, config, headless)

    def _apply_and_broadcast_config(self):
        self._write_dbg_config()
        CoreProcess._apply_and_broadcast_config(self)

    def _write_dbg_config(self):
        config_dir = resolve_path(self._config.local["config_dir"], strict=True)
        dbg_config = config_dir / "dbg-config.mpk"
        with dbg_config.open("wb") as f:
            f.write(self._config.serialize())
