1+ from __future__ import annotations
2+
3+ import json
4+ import subprocess
5+ import sys
6+ from pathlib import Path
7+
8+
9+
10+ def test_devices_export_writes_json (tmp_path : Path ) -> None :
11+ """hiri-bridge devices export --out writes device registry snapshot."""
12+ out = tmp_path / "snapshot.json"
13+ result = subprocess .run (
14+ [sys .executable , "-m" , "hiri_bridge.cli" , "devices" , "export" , "--out" , str (out )],
15+ capture_output = True ,
16+ text = True ,
17+ )
18+ assert result .returncode == 0 , f"stdout={ result .stdout } stderr={ result .stderr } "
19+ assert out .exists (), f"Expected { out } to exist. stderr={ result .stderr } "
20+
21+ data = json .loads (out .read_text (encoding = "utf-8" ))
22+ assert isinstance (data , list ), f"Expected list, got { type (data )} "
23+ assert len (data ) > 0 , "Registry should contain seeded devices"
24+
25+ # Check structure of first device
26+ d = data [0 ]
27+ for field in ("id" , "name" , "domain" , "manufacturer" , "model" , "area" , "online" , "state" , "adapter" ):
28+ assert field in d , f"Device missing field: { field } "
29+
30+ # Verify no tokens/secrets leaked
31+ text = out .read_text (encoding = "utf-8" ).lower ()
32+ assert "token" not in text or "code" not in text , "Snapshot should not contain tokens/codes"
33+
34+ # Verify output mentions device count
35+ assert "devices=" in result .stdout and str (len (data )) in result .stdout
36+
37+
38+ def test_devices_export_creates_parent_dirs (tmp_path : Path ) -> None :
39+ """export --out creates parent directories if they don't exist."""
40+ out = tmp_path / "nested" / "deep" / "snapshot.json"
41+ assert not out .parent .exists ()
42+ result = subprocess .run (
43+ [sys .executable , "-m" , "hiri_bridge.cli" , "devices" , "export" , "--out" , str (out )],
44+ capture_output = True ,
45+ text = True ,
46+ )
47+ assert result .returncode == 0 , f"stdout={ result .stdout } stderr={ result .stderr } "
48+ assert out .exists ()
49+
50+
51+ def test_devices_export_requires_out_option () -> None :
52+ """export command requires --out argument."""
53+ result = subprocess .run (
54+ [sys .executable , "-m" , "hiri_bridge.cli" , "devices" , "export" ],
55+ capture_output = True ,
56+ text = True ,
57+ )
58+ assert result .returncode != 0 , "export without --out should fail"
0 commit comments