from bubblecam import config


def test_load_falls_back_to_defaults_when_no_file(sim_env):
    cfg = config.load()
    assert cfg["camera"]["mode"] == "sbs_mjpeg"
    assert cfg["lumen"]["brightness_pct"] == 30
    assert cfg["sensors"]["interval_seconds"] == 10


def test_load_merges_partial_user_config(sim_env):
    config_path = sim_env["tmp_path"] / "bubblecam.toml"
    config_path.write_text(
        """
[lumen]
brightness_pct = 50

[camera]
mode = "dual_node_h264"
"""
    )
    import os

    os.environ["BUBBLECAM_CONFIG"] = str(config_path)

    cfg = config.load()
    assert cfg["lumen"]["brightness_pct"] == 50
    # Untouched fields in a partially-overridden section keep their defaults.
    assert cfg["lumen"]["dusk_definition"] == "civil"
    assert cfg["camera"]["mode"] == "dual_node_h264"
    # Untouched sections are completely unaffected.
    assert cfg["sensors"]["interval_seconds"] == 10


def test_real_shipped_template_parses_and_merges_cleanly(sim_env):
    import os
    from pathlib import Path

    # Point directly at the actual shipped template in the repo, so a typo
    # or bad edit to boot/firmware/bubblecam.toml is caught by the suite.
    repo_root = Path(__file__).resolve().parent.parent
    shipped_template = repo_root / "boot" / "firmware" / "bubblecam.toml"
    os.environ["BUBBLECAM_CONFIG"] = str(shipped_template)

    cfg = config.load()
    assert cfg["camera"]["mode"] == "sbs_mjpeg"
    assert cfg["sensors"]["i2c_address"] == 0x76
