-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
285 lines (236 loc) · 11.2 KB
/
Copy pathtest_cli.py
File metadata and controls
285 lines (236 loc) · 11.2 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import json
import os
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
EXPECTED_INVALID_CODES = {
"close-before-agree.json": "CLOSE_BEFORE_AGREE",
"duplicate-message-id.json": "DUPLICATE_MESSAGE_ID",
"invalid-transition.json": "INVALID_TRANSITION",
"missing-parent.json": "PARENT_REQUIRED",
"redefine-message-type.json": "CORE_SEMANTIC_REDEFINITION",
}
def run_cli(*args):
env = os.environ.copy()
env["PYTHONDONTWRITEBYTECODE"] = "1"
return subprocess.run(
[sys.executable, "-m", "passpod.cli", *args],
cwd=ROOT,
env=env,
text=True,
capture_output=True,
check=False,
)
def write_json(directory, name, value):
path = Path(directory) / name
path.write_text(json.dumps(value), encoding="utf-8")
return path
def assert_no_traceback(test_case, result):
test_case.assertNotIn("Traceback", result.stdout)
test_case.assertNotIn("Traceback", result.stderr)
class ValidateCommandTests(unittest.TestCase):
def test_validate_human_success_for_supported_artifacts(self):
cases = [
("message", ROOT / "examples" / "valid" / "minimal-propose.json"),
("handshake", ROOT / "examples" / "valid" / "complete-handshake.json"),
("profile", ROOT / "examples" / "valid" / "minimal-profile.json"),
]
for artifact_type, path in cases:
with self.subTest(artifact_type=artifact_type):
result = run_cli("validate", str(path))
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual(f"VALID {artifact_type}\n", result.stdout)
self.assertEqual("", result.stderr)
def test_validate_json_success(self):
result = run_cli(
"validate",
str(ROOT / "examples" / "valid" / "complete-handshake.json"),
"--json",
)
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual(
{
"valid": True,
"artifact_type": "handshake",
"errors": [],
},
json.loads(result.stdout),
)
self.assertEqual("", result.stderr)
def test_validate_semantic_failures_preserve_validator_codes(self):
for path in sorted((ROOT / "examples" / "invalid").glob("*.json")):
with self.subTest(path=path.name):
result = run_cli("validate", str(path))
self.assertEqual(1, result.returncode)
self.assertEqual("", result.stdout)
self.assertIn("INVALID ", result.stderr)
self.assertIn(EXPECTED_INVALID_CODES[path.name], result.stderr)
assert_no_traceback(self, result)
def test_validate_json_semantic_failure_preserves_validator_errors(self):
result = run_cli(
"validate",
str(ROOT / "examples" / "invalid" / "missing-parent.json"),
"--json",
)
payload = json.loads(result.stderr)
self.assertEqual(1, result.returncode)
self.assertEqual("", result.stdout)
self.assertFalse(payload["valid"])
self.assertEqual("handshake", payload["artifact_type"])
self.assertIn("PARENT_REQUIRED", [error["code"] for error in payload["errors"]])
assert_no_traceback(self, result)
def test_validate_rejects_empty_handshake(self):
with tempfile.TemporaryDirectory() as directory:
path = write_json(
directory,
"empty-handshake.json",
{"handshakeIdentity": "hs-empty-001", "messages": []},
)
result = run_cli("validate", str(path), "--json")
payload = json.loads(result.stderr)
self.assertEqual(1, result.returncode)
self.assertEqual("", result.stdout)
self.assertFalse(payload["valid"])
self.assertEqual("handshake", payload["artifact_type"])
self.assertIn("SCHEMA_INVALID", [error["code"] for error in payload["errors"]])
assert_no_traceback(self, result)
def test_validate_rejects_missing_messages(self):
with tempfile.TemporaryDirectory() as directory:
path = write_json(
directory,
"missing-messages.json",
{"handshakeIdentity": "hs-missing-001"},
)
result = run_cli("validate", str(path), "--json")
payload = json.loads(result.stderr)
self.assertEqual(2, result.returncode)
self.assertEqual("", result.stdout)
self.assertFalse(payload["valid"])
self.assertIsNone(payload["artifact_type"])
self.assertEqual("ARTIFACT_TYPE_UNKNOWN", payload["errors"][0]["code"])
assert_no_traceback(self, result)
class InputFailureTests(unittest.TestCase):
def test_detection_and_input_failures_are_exit_code_2(self):
with tempfile.TemporaryDirectory() as directory:
malformed = Path(directory) / "malformed.json"
malformed.write_text("{", encoding="utf-8")
non_object = Path(directory) / "non-object.json"
non_object.write_text("[]", encoding="utf-8")
unknown = write_json(directory, "unknown.json", {"hello": "world"})
ambiguous = write_json(
directory,
"ambiguous.json",
{
"messageIdentity": "msg-ambiguous-001",
"messageType": "PROPOSE",
"profileIdentity": "profile-ambiguous-001",
"profileVersion": "0.1",
"lifecycle": "draft",
},
)
cases = [
("FILE_NOT_FOUND", Path(directory) / "missing.json"),
("JSON_INVALID", malformed),
("ROOT_NOT_OBJECT", non_object),
("ARTIFACT_TYPE_UNKNOWN", unknown),
("ARTIFACT_TYPE_AMBIGUOUS", ambiguous),
]
for code, path in cases:
with self.subTest(code=code):
result = run_cli("validate", str(path))
self.assertEqual(2, result.returncode)
self.assertEqual("", result.stdout)
self.assertIn(code, result.stderr)
assert_no_traceback(self, result)
def test_json_input_failure_uses_cli_error_code(self):
with tempfile.TemporaryDirectory() as directory:
path = write_json(directory, "unknown.json", {"hello": "world"})
result = run_cli("validate", str(path), "--json")
payload = json.loads(result.stderr)
self.assertEqual(2, result.returncode)
self.assertEqual("", result.stdout)
self.assertEqual("ARTIFACT_TYPE_UNKNOWN", payload["errors"][0]["code"])
self.assertIsNone(payload["artifact_type"])
assert_no_traceback(self, result)
class InspectCommandTests(unittest.TestCase):
def test_inspect_message_human_summary(self):
result = run_cli("inspect", str(ROOT / "examples" / "valid" / "minimal-propose.json"))
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual("", result.stderr)
self.assertIn("artifact_type: message\n", result.stdout)
self.assertIn("message_identity: msg-propose-001\n", result.stdout)
self.assertIn("handshake_identity: hs-minimal-propose-001\n", result.stdout)
self.assertIn("message_type: PROPOSE\n", result.stdout)
self.assertIn("parent_reference: null\n", result.stdout)
self.assertIn("sender_present: true\n", result.stdout)
self.assertIn("recipient_present: false\n", result.stdout)
def test_inspect_handshake_json_summary(self):
result = run_cli(
"inspect",
str(ROOT / "examples" / "valid" / "complete-handshake.json"),
"--json",
)
payload = json.loads(result.stdout)
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual("", result.stderr)
self.assertEqual("handshake", payload["artifact_type"])
self.assertEqual("closed", payload["state"])
self.assertTrue(payload["closed"])
self.assertEqual(4, payload["message_count"])
self.assertEqual(
[
{"message_identity": "msg-propose-001", "message_type": "PROPOSE"},
{"message_identity": "msg-challenge-001", "message_type": "CHALLENGE"},
{"message_identity": "msg-agree-001", "message_type": "AGREE"},
{"message_identity": "msg-close-001", "message_type": "CLOSE"},
],
payload["messages"],
)
def test_inspect_profile_human_summary(self):
result = run_cli("inspect", str(ROOT / "examples" / "valid" / "minimal-profile.json"))
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual("", result.stderr)
self.assertIn("artifact_type: profile\n", result.stdout)
self.assertIn("profile_identity: profile-minimal-001\n", result.stdout)
self.assertIn("profile_version: 0.1\n", result.stdout)
self.assertIn("lifecycle: draft\n", result.stdout)
self.assertNotIn("Minimal Profile Model Example", result.stdout)
def test_inspect_rejects_invalid_artifact(self):
result = run_cli("inspect", str(ROOT / "examples" / "invalid" / "invalid-transition.json"))
self.assertEqual(1, result.returncode)
self.assertEqual("", result.stdout)
self.assertIn("INVALID handshake", result.stderr)
self.assertIn("INVALID_TRANSITION", result.stderr)
assert_no_traceback(self, result)
class InspectPrivacyTests(unittest.TestCase):
def test_inspect_does_not_expose_sensitive_payloads(self):
with tempfile.TemporaryDirectory() as directory:
path = write_json(
directory,
"private-message.json",
{
"messageIdentity": "msg-private-001",
"handshakeIdentity": "hs-private-001",
"messageType": "PROPOSE",
"sender": {"id": "participant-alpha", "secret": "sender-secret"},
"recipient": {"id": "participant-beta", "secret": "recipient-secret"},
"evidenceReferences": [{"secretEvidence": "evidence-secret"}],
"extensions": {"privateExtension": "extension-secret"},
},
)
human = run_cli("inspect", str(path))
machine = run_cli("inspect", str(path), "--json")
combined = human.stdout + human.stderr + machine.stdout + machine.stderr
self.assertEqual(0, human.returncode, human.stderr)
self.assertEqual(0, machine.returncode, machine.stderr)
self.assertIn("sender_present: true", human.stdout)
self.assertIn("recipient_present: true", human.stdout)
self.assertNotIn("sender-secret", combined)
self.assertNotIn("recipient-secret", combined)
self.assertNotIn("evidence-secret", combined)
self.assertNotIn("extension-secret", combined)
if __name__ == "__main__":
unittest.main()