9074121898
Pause/resume sync without losing the instance_id.
disable sync.json -> sync.json.disabled
enable sync.json.disabled -> sync.json
Re-enabling preserves the original ULID + url so the same restic
repo continues. No new instance_id minted, no orphaned snapshot
history. Tradeoff vs the previous 'disable = delete' semantics:
the on-disk artifact survives, so a truly fresh start now needs
'disable && rm sync.json.disabled' before 'setup'.
Implementation:
config.disable(pack) os.rename(sync.json -> sync.json.disabled).
False if no sync.json.
config.enable(pack) os.rename(sync.json.disabled -> sync.json).
Refuses if sync.json already exists
(caller must disable first).
config.delete(pack) now sweeps BOTH forms (escape hatch / tests).
setup_flow gains a precheck: if sync.json.disabled is present, point
the user at 'enable' instead of silently minting a fresh ULID over
their existing instance.
Opt-in gate (cfgmod.exists) is unchanged — only literal sync.json
counts. The .disabled sibling is invisible to pull/push, so the
silent-no-op behavior for paused instances Just Works.
cli adds 'enable' subcommand alongside 'disable'. _run_disable prints
'already disabled' when called twice; _run_enable refuses to clobber
an active config (exits 2 with the FileExistsError message).
7 new tests for disable/enable behavior + edge cases (idempotency,
nothing-to-X, refuse-clobber). 80 tests total.
176 lines
5.7 KiB
Python
176 lines
5.7 KiB
Python
"""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_delete_also_clears_disabled_sibling(tmp_path: Path):
|
|
"""Hard delete sweeps both active + disabled forms — used by tests
|
|
and as an escape hatch when the user wants a truly clean slate."""
|
|
cfg = cfgmod.mint("https://x")
|
|
cfgmod.write(tmp_path, cfg)
|
|
cfgmod.disable(tmp_path) # now only .disabled exists
|
|
cfgmod.write(tmp_path, cfgmod.mint("https://y")) # also active
|
|
assert cfgmod.config_path(tmp_path).exists()
|
|
assert cfgmod.disabled_path(tmp_path).exists()
|
|
assert cfgmod.delete(tmp_path) is True
|
|
assert not cfgmod.config_path(tmp_path).exists()
|
|
assert not cfgmod.disabled_path(tmp_path).exists()
|
|
|
|
|
|
# ---- disable / enable round-trip ----
|
|
|
|
|
|
def test_disable_renames_to_disabled(tmp_path: Path):
|
|
cfg = cfgmod.mint("https://x")
|
|
cfgmod.write(tmp_path, cfg)
|
|
assert cfgmod.disable(tmp_path) is True
|
|
assert not cfgmod.config_path(tmp_path).exists()
|
|
assert cfgmod.disabled_path(tmp_path).exists()
|
|
# opt-in gate now reads "off"
|
|
assert cfgmod.exists(tmp_path) is False
|
|
|
|
|
|
def test_disable_returns_false_when_nothing_to_disable(tmp_path: Path):
|
|
assert cfgmod.disable(tmp_path) is False
|
|
|
|
|
|
def test_enable_restores(tmp_path: Path):
|
|
cfg = cfgmod.mint("https://x")
|
|
cfgmod.write(tmp_path, cfg)
|
|
cfgmod.disable(tmp_path)
|
|
assert cfgmod.enable(tmp_path) is True
|
|
got = cfgmod.read(tmp_path)
|
|
assert got is not None
|
|
assert got.instance_id == cfg.instance_id # same id preserved!
|
|
assert got.url == cfg.url
|
|
assert not cfgmod.disabled_path(tmp_path).exists()
|
|
|
|
|
|
def test_enable_returns_false_when_nothing_disabled(tmp_path: Path):
|
|
assert cfgmod.enable(tmp_path) is False
|
|
|
|
|
|
def test_enable_refuses_to_clobber_active(tmp_path: Path):
|
|
"""If both sync.json AND sync.json.disabled exist (manual mess),
|
|
enable refuses rather than silently overwriting the active one."""
|
|
cfgmod.write(tmp_path, cfgmod.mint("https://x"))
|
|
cfgmod.disable(tmp_path)
|
|
cfgmod.write(tmp_path, cfgmod.mint("https://y")) # fresh active
|
|
with pytest.raises(FileExistsError):
|
|
cfgmod.enable(tmp_path)
|
|
|
|
|
|
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"
|