"""The one-time move of an older instance's panels file into devices.""" import json import subprocess from pathlib import Path from fluksio.flow.devices import DeviceStore from fluksio.flow.store import FlowStore def test_panels_move_into_devices_once(tmp_path: Path) -> None: flows = FlowStore(tmp_path / "flows") panels = tmp_path / "panels.json" panels.write_text( json.dumps( { "panels": [ {"id": "hall", "title": "Hall", "dashboards": ["home"], "nonce": 2}, {"id": "knob", "touch": True, "io": [], "nonce": 0}, ], "version": 7, } ) ) store = DeviceStore(flows, tmp_path / "devices.json") store.migrate(panels) assert not panels.exists() assert (tmp_path / "panels.json.migrated").exists() hall = store.find("hall") assert hall is not None and hall.dashboards == ["home"] and hall.version == 1 assert store.find("knob").touch is True # type: ignore[union-attr] # The nonce stays outside git, so a credential minted at it still stands. assert store.nonce("hall") == 2 assert "nonce" not in (flows.root / "_devices" / "hall.json").read_text() log = subprocess.run( ["git", "-C", str(flows.root), "log", "--format=%s"], capture_output=True, text=True, ).stdout assert log.splitlines()[0] == "Migrate panels to devices" # Nothing in `_devices` is taken for a flow. assert flows.list_flows() == [] # A second start finds nothing to do, and a panels file restored from a # backup does not lower a nonce an unpair has raised since. store.bump("hall") panels.write_text((tmp_path / "panels.json.migrated").read_text()) store.migrate(panels) assert store.nonce("hall") == 3 assert ( log == subprocess.run( ["git", "-C", str(flows.root), "log", "--format=%s"], capture_output=True, text=True, ).stdout )