gabriel / muse public
test_envelope.py python
90 lines 2.8 KB
Raw
1 """Tests for the standard JSON envelope utility (muse.core.envelope)."""
2
3 from __future__ import annotations
4
5 import time
6 from typing import get_type_hints
7
8 import pytest
9
10 from muse.core.envelope import EnvelopeJson
11 from muse.core.timing import start_timer
12
13
14 class TestMakeEnvelope:
15 """make_envelope() must return all six standard fields."""
16
17 def _call(self, **kwargs: str) -> "EnvelopeJson":
18 from muse.core.envelope import make_envelope
19 elapsed = start_timer()
20 return make_envelope(elapsed, **kwargs)
21
22 def test_has_muse_version(self) -> None:
23 assert "muse_version" in self._call()
24
25 def test_has_schema(self) -> None:
26 assert "schema" in self._call()
27
28 def test_has_exit_code(self) -> None:
29 assert "exit_code" in self._call()
30
31 def test_has_duration_ms(self) -> None:
32 assert "duration_ms" in self._call()
33
34 def test_has_timestamp(self) -> None:
35 assert "timestamp" in self._call()
36
37 def test_has_warnings(self) -> None:
38 assert "warnings" in self._call()
39
40 def test_muse_version_is_str(self) -> None:
41 v = self._call()["muse_version"]
42 assert isinstance(v, str) and v
43
44 def test_schema_defaults_to_1(self) -> None:
45 assert self._call()["schema"] == 1
46
47 def test_schema_custom(self) -> None:
48 assert self._call(schema=2)["schema"] == 2
49
50 def test_exit_code_defaults_to_0(self) -> None:
51 assert self._call()["exit_code"] == 0
52
53 def test_exit_code_custom(self) -> None:
54 assert self._call(exit_code=1)["exit_code"] == 1
55
56 def test_duration_ms_is_float(self) -> None:
57 assert isinstance(self._call()["duration_ms"], float)
58
59 def test_duration_ms_nonnegative(self) -> None:
60 assert self._call()["duration_ms"] >= 0.0
61
62 def test_warnings_defaults_to_empty_list(self) -> None:
63 assert self._call()["warnings"] == []
64
65 def test_warnings_custom(self) -> None:
66 w = self._call(warnings=["watch out"])["warnings"]
67 assert w == ["watch out"]
68
69 def test_timestamp_is_iso8601_z(self) -> None:
70 ts = self._call()["timestamp"]
71 assert isinstance(ts, str)
72 assert ts.endswith("Z")
73 assert "T" in ts
74
75 def test_timestamp_is_utc(self) -> None:
76 import datetime
77 ts = self._call()["timestamp"]
78 # Must parse without error
79 datetime.datetime.fromisoformat(ts.replace("Z", "+00:00"))
80
81 def test_duration_ms_increases_with_time(self) -> None:
82 from muse.core.envelope import make_envelope
83 elapsed = start_timer()
84 time.sleep(0.01)
85 ms = make_envelope(elapsed)["duration_ms"]
86 assert ms >= 10.0
87
88 def test_no_schema_version_key(self) -> None:
89 """The old name must not appear in the envelope."""
90 assert "schema_version" not in self._call()
File History 1 commit