"""sync.json read/write/mint + label resolution.""" from __future__ import annotations import os import re from pathlib import Path import pytest from cloud_sync import config as cfgmod # ---- mint / new_instance_id ---- def test_new_instance_id_shape(): iid = cfgmod.new_instance_id() assert len(iid) == 26 # base32 alphabet (uppercase + 2-7), no padding assert re.fullmatch(r"[A-Z2-7]+", iid), iid def test_new_instance_id_uniqueness(): ids = {cfgmod.new_instance_id() for _ in range(200)} assert len(ids) == 200 # collisions effectively impossible def test_mint_returns_filled_config(): cfg = cfgmod.mint(url="https://x.test") assert cfg.url == "https://x.test" assert len(cfg.instance_id) == 26 assert cfg.host_fingerprint # non-empty hex assert cfg.created_at.tzinfo is not None # ---- read / write / exists / delete ---- def test_exists_false_when_missing(tmp_path: Path): assert cfgmod.exists(tmp_path) is False assert cfgmod.read(tmp_path) is None def test_write_then_read_roundtrip(tmp_path: Path): cfg = cfgmod.mint(url="https://x.test") cfgmod.write(tmp_path, cfg) assert cfgmod.exists(tmp_path) is True got = cfgmod.read(tmp_path) assert got is not None assert got.url == cfg.url assert got.instance_id == cfg.instance_id assert got.host_fingerprint == cfg.host_fingerprint def test_write_sets_mode_644(tmp_path: Path): """sync.json is NOT a secret — it's the opt-in marker + (instance_id, url) pair. token is the secret (mode 600).""" cfg = cfgmod.mint(url="https://x.test") cfgmod.write(tmp_path, cfg) mode = cfgmod.config_path(tmp_path).stat().st_mode & 0o777 assert mode == 0o644 def test_delete_returns_true_when_existed(tmp_path: Path): cfgmod.write(tmp_path, cfgmod.mint("https://x")) assert cfgmod.delete(tmp_path) is True assert cfgmod.exists(tmp_path) is False def test_delete_returns_false_when_missing(tmp_path: Path): assert cfgmod.delete(tmp_path) is False def test_corrupt_json_returns_none(tmp_path: Path): cfg_path = cfgmod.config_path(tmp_path) cfg_path.parent.mkdir(parents=True) cfg_path.write_text("{not json") assert cfgmod.read(tmp_path) is None def test_wrong_schema_returns_none(tmp_path: Path): cfg_path = cfgmod.config_path(tmp_path) cfg_path.parent.mkdir(parents=True) cfg_path.write_text('{"schema": 999, "url": "x", "instance_id": "y", "created_at": "2026-01-01T00:00:00Z"}') assert cfgmod.read(tmp_path) is None # ---- resolve_label ---- def test_label_flag_wins_over_env(monkeypatch: pytest.MonkeyPatch): monkeypatch.setenv("INST_NAME", "from-env") assert cfgmod.resolve_label("from-flag", "ABCDEF") == "from-flag" def test_label_falls_back_to_inst_name(monkeypatch: pytest.MonkeyPatch): monkeypatch.setenv("INST_NAME", "Frazaserver 1.21.4") monkeypatch.delenv("INST_ID", raising=False) assert cfgmod.resolve_label(None, "ABCDEF") == "Frazaserver 1.21.4" def test_label_falls_back_to_inst_id(monkeypatch: pytest.MonkeyPatch): monkeypatch.delenv("INST_NAME", raising=False) monkeypatch.setenv("INST_ID", "26.1.2") assert cfgmod.resolve_label(None, "ABCDEF") == "26.1.2" def test_label_last_resort_uses_instance_id_prefix(monkeypatch: pytest.MonkeyPatch): monkeypatch.delenv("INST_NAME", raising=False) monkeypatch.delenv("INST_ID", raising=False) assert cfgmod.resolve_label(None, "ABCDEFGHIJKLMN") == "ABCDEFGH" def test_label_blank_flag_does_not_override(monkeypatch: pytest.MonkeyPatch): """Empty string from --instance-label="" should not win over env.""" monkeypatch.setenv("INST_NAME", "env-name") assert cfgmod.resolve_label("", "ABCDEF") == "env-name"