forked from ChelseaKR/habitable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_demo.py
More file actions
196 lines (161 loc) · 6.9 KB
/
Copy pathtest_cli_demo.py
File metadata and controls
196 lines (161 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright 2026 Chelsea Kelly-Reif
"""End-to-end CLI flow and the offline demo."""
from __future__ import annotations
import json
from collections.abc import Callable
from pathlib import Path
import pytest
from habitable.cli import main
from habitable.demo import run_demo
def test_demo_runs_offline() -> None:
assert run_demo() == 0
def _init_capture_export(
tmp_path: Path,
make_jpeg: Callable[..., Path],
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> Path:
monkeypatch.setenv("HABITABLE_PASSPHRASE", "pw")
vault = tmp_path / "vault"
assert main(["init", str(vault), "--case", "bldg-12", "--unit", "4B"]) == 0
assert main(["issue", "--vault", str(vault), "--category", "mold", "--title", "Mold"]) == 0
out = capsys.readouterr().out
issue_id = next(token for token in out.split() if token.startswith("issue-"))
photo = make_jpeg(with_location=True)
capture_args = ["capture", str(photo), "--vault", str(vault), "--issue", issue_id, "--dev-tsa"]
assert main(capture_args) == 0
assert (
main(
[
"timeline",
"--vault",
str(vault),
"--issue",
issue_id,
"--kind",
"observed",
"--text",
"cold",
]
)
== 0
)
assert main(["status", "--vault", str(vault)]) == 0
packet = tmp_path / "packet"
assert main(["export", "--vault", str(vault), "--out", str(packet)]) == 0
return packet
def test_cli_full_flow_verifies(
tmp_path: Path,
make_jpeg: Callable[..., Path],
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
packet = _init_capture_export(tmp_path, make_jpeg, monkeypatch, capsys)
assert main(["verify", str(packet)]) == 0
assert "packet intact" in capsys.readouterr().out
def test_cli_status_shows_record_strength(
tmp_path: Path,
make_jpeg: Callable[..., Path],
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""EXP-03: ``habitable status`` surfaces a per-issue record-strength line
with its honesty caveat — never a bare level with no framing."""
monkeypatch.setenv("HABITABLE_PASSPHRASE", "pw")
vault = tmp_path / "vault"
assert main(["init", str(vault), "--case", "bldg-12", "--unit", "4B"]) == 0
assert main(["issue", "--vault", str(vault), "--category", "mold", "--title", "Mold"]) == 0
out = capsys.readouterr().out
issue_id = next(token for token in out.split() if token.startswith("issue-"))
photo = make_jpeg(with_location=True)
assert (
main(["capture", str(photo), "--vault", str(vault), "--issue", issue_id, "--dev-tsa"]) == 0
)
capsys.readouterr()
assert main(["status", "--vault", str(vault)]) == 0
status_out = capsys.readouterr().out
assert "record strength: developing" in status_out
assert "not admissibility" in status_out
def test_cli_verify_detects_tamper(
tmp_path: Path,
make_jpeg: Callable[..., Path],
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
packet = _init_capture_export(tmp_path, make_jpeg, monkeypatch, capsys)
bundle = json.loads((packet / "bundle.json").read_text())
bundle["unit"] = "TAMPERED"
(packet / "bundle.json").write_text(json.dumps(bundle))
assert main(["verify", str(packet)]) == 1
def test_cli_verify_json_is_structured(
tmp_path: Path,
make_jpeg: Callable[..., Path],
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
packet = _init_capture_export(tmp_path, make_jpeg, monkeypatch, capsys)
capsys.readouterr() # drop prior output
assert main(["verify", str(packet), "--json"]) == 0
report = json.loads(capsys.readouterr().out)
assert report["ok"] is True
assert report["signature_ok"] and report["custody_ok"]
assert report["item_count"] >= 1 and report["verified_items"] == report["item_count"]
item = report["items"][0]
for key in ("capture_id", "content_hash", "ok", "timestamp_verified", "notes"):
assert key in item
def test_no_command_prints_help() -> None:
assert main([]) == 2
def test_cli_id_prints_fingerprint(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setenv("HABITABLE_PASSPHRASE", "pw")
vault = tmp_path / "vault"
assert main(["init", str(vault), "--case", "c"]) == 0
capsys.readouterr()
assert main(["id", "--vault", str(vault)]) == 0
out = capsys.readouterr().out
assert "fingerprint:" in out and "public-id:" in out
def test_cli_sync_requires_transport(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
from habitable.vault import Vault
monkeypatch.setenv("HABITABLE_PASSPHRASE", "pw")
vault = tmp_path / "vault"
assert main(["init", str(vault), "--case", "c"]) == 0
peer = Vault.open(vault, "pw").identity.public().encode()
capsys.readouterr()
# No --relay or --dir: the command reports a clear error and exits non-zero.
assert main(["sync", "--vault", str(vault), "--peer", peer, "--channel", "r"]) == 1
assert "transport" in capsys.readouterr().err
def test_cli_export_discloses_awaiting_state(
tmp_path: Path,
make_jpeg: Callable[..., Path],
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Exporting with queued-offline items states the degraded state and next step.
FIX-09: a packet with awaiting items reports NOT intact under `habitable verify`
(correct, degraded behavior) — the export must say so up front, with the
`habitable resolve` next step, instead of the recipient discovering it.
"""
monkeypatch.setenv("HABITABLE_PASSPHRASE", "pw")
vault = tmp_path / "vault"
assert main(["init", str(vault), "--case", "bldg-12", "--unit", "4B"]) == 0
assert main(["issue", "--vault", str(vault), "--category", "mold", "--title", "Mold"]) == 0
out = capsys.readouterr().out
issue_id = next(token for token in out.split() if token.startswith("issue-"))
photo = make_jpeg()
capture_args = ["capture", str(photo), "--vault", str(vault), "--issue", issue_id]
assert main([*capture_args, "--no-timestamp"]) == 0
capsys.readouterr()
assert main(["export", "--vault", str(vault), "--out", str(tmp_path / "p1")]) == 0
out = capsys.readouterr().out
assert "awaiting a trusted timestamp" in out
assert "habitable resolve" in out
# Once every item is timestamped, the hint disappears — nothing cries wolf.
assert main(["resolve", "--vault", str(vault), "--dev-tsa"]) == 0
capsys.readouterr()
assert main(["export", "--vault", str(vault), "--out", str(tmp_path / "p2")]) == 0
out = capsys.readouterr().out
assert "awaiting a trusted timestamp" not in out