forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_goal.py
More file actions
209 lines (181 loc) · 7.25 KB
/
Copy pathtest_goal.py
File metadata and controls
209 lines (181 loc) · 7.25 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
"""Tests for ``omi goal`` commands."""
from __future__ import annotations
import json
import sys
import pytest
from omi_cli.main import app, main
def test_goal_list(authed_profile, respx_mock, cli_runner) -> None:
respx_mock.get("/v1/dev/user/goals").respond(
json=[
{
"id": "g1",
"title": "ship cli",
"goal_type": "scale",
"target_value": 10,
"current_value": 3,
"min_value": 0,
"max_value": 10,
"is_active": True,
}
]
)
result = cli_runner.invoke(app, ["--json", "goal", "list"])
assert result.exit_code == 0
payload = json.loads(result.stdout)
assert payload[0]["id"] == "g1"
def test_goal_create_posts(authed_profile, respx_mock, cli_runner) -> None:
route = respx_mock.post("/v1/dev/user/goals").respond(
json={
"id": "g1",
"title": "drink water",
"goal_type": "numeric",
"target_value": 2.0,
"current_value": 0,
"min_value": 0,
"max_value": 10,
"unit": "liters",
"is_active": True,
}
)
result = cli_runner.invoke(
app,
[
"--json",
"goal",
"create",
"drink water",
"--target",
"2",
"--type",
"numeric",
"--unit",
"liters",
],
)
assert result.exit_code == 0
body = json.loads(route.calls.last.request.content)
assert body["target_value"] == 2.0
assert body["goal_type"] == "numeric"
assert body["unit"] == "liters"
def test_goal_create_qualitative_omits_metrics(authed_profile, respx_mock, cli_runner) -> None:
route = respx_mock.post("/v1/dev/user/goals").respond(
json={
"id": "g2",
"title": "Build stronger relationships",
"goal_type": None,
"target_value": None,
"current_value": None,
"min_value": None,
"max_value": None,
"unit": None,
"is_active": True,
}
)
result = cli_runner.invoke(app, ["--json", "goal", "create", "Build stronger relationships"])
assert result.exit_code == 0, result.stdout
body = json.loads(route.calls.last.request.content)
assert body == {"title": "Build stronger relationships"}
def test_goal_create_metric_defaults_preserved(authed_profile, respx_mock, cli_runner) -> None:
route = respx_mock.post("/v1/dev/user/goals").respond(
json={
"id": "g3",
"title": "drink water",
"goal_type": "scale",
"target_value": 2.0,
"current_value": 0,
"min_value": 0,
"max_value": 10,
"is_active": True,
}
)
result = cli_runner.invoke(app, ["--json", "goal", "create", "drink water", "--target", "2"])
assert result.exit_code == 0, result.stdout
body = json.loads(route.calls.last.request.content)
# Historical defaults still apply when any metric option is given.
assert body["goal_type"] == "scale"
assert body["target_value"] == 2.0
assert body["current_value"] == 0
assert body["min_value"] == 0
assert body["max_value"] == 10
def test_goal_create_unit_without_metrics_rejected(authed_profile, cli_runner) -> None:
result = cli_runner.invoke(app, ["--json", "goal", "create", "meditate", "--unit", "sessions"])
assert result.exit_code == 1 # EXIT_USAGE
assert "--unit requires a metric goal" in result.stderr
def test_goal_create_partial_metrics_rejected(authed_profile, cli_runner) -> None:
result = cli_runner.invoke(app, ["--json", "goal", "create", "drink water", "--current", "5"])
assert result.exit_code == 1 # EXIT_USAGE
assert "--target is required" in result.stderr
def test_goal_create_unit_attached_to_metric_goal(authed_profile, respx_mock, cli_runner) -> None:
route = respx_mock.post("/v1/dev/user/goals").respond(
json={
"id": "g4",
"title": "drink water",
"goal_type": "scale",
"target_value": 2.0,
"current_value": 0,
"min_value": 0,
"max_value": 10,
"unit": "liters",
"is_active": True,
}
)
result = cli_runner.invoke(app, ["--json", "goal", "create", "drink water", "--target", "2", "--unit", "liters"])
assert result.exit_code == 0, result.stdout
body = json.loads(route.calls.last.request.content)
assert body["unit"] == "liters"
def test_goal_progress_uses_query_param(authed_profile, respx_mock, cli_runner) -> None:
route = respx_mock.patch("/v1/dev/user/goals/g1/progress").respond(
json={
"id": "g1",
"title": "x",
"goal_type": "scale",
"target_value": 10,
"current_value": 7,
"min_value": 0,
"max_value": 10,
"is_active": True,
}
)
result = cli_runner.invoke(app, ["--json", "goal", "progress", "g1", "7"])
assert result.exit_code == 0
request = route.calls.last.request
# httpx serializes Python floats with the trailing ``.0`` — accept either form.
assert request.url.params["current_value"] in ("7", "7.0")
def test_goal_history(authed_profile, respx_mock, cli_runner) -> None:
respx_mock.get("/v1/dev/user/goals/g1/history").respond(json=[{"recorded_at": "2026-04-25T00:00:00Z", "value": 3}])
result = cli_runner.invoke(app, ["--json", "goal", "history", "g1", "--days", "7"])
assert result.exit_code == 0
payload = json.loads(result.stdout)
assert payload[0]["value"] == 3
def test_goal_delete(authed_profile, respx_mock, cli_runner) -> None:
respx_mock.delete("/v1/dev/user/goals/g1").respond(json={"success": True})
result = cli_runner.invoke(app, ["goal", "delete", "g1", "-y"])
assert result.exit_code == 0
@pytest.mark.parametrize(
("options", "expected"),
[
(["--unit", "liters"], {"unit": "liters"}),
(["--unit", ""], {"unit": ""}),
(["--clear-unit"], {"unit": None}),
(["--title", "drink water"], {"title": "drink water"}),
(["--clear-unit", "--current", "2"], {"unit": None, "current_value": 2.0}),
],
)
def test_goal_update_unit_patch(authed_profile, respx_mock, cli_runner, options, expected) -> None:
route = respx_mock.patch("/v1/dev/user/goals/g1").respond(json={"id": "g1", **expected})
result = cli_runner.invoke(app, ["--json", "goal", "update", "g1", *options])
assert result.exit_code == 0, result.output
assert json.loads(route.calls.last.request.content) == expected
assert json.loads(result.stdout) == {"id": "g1", **expected}
@pytest.mark.parametrize("unit", ["liters", ""])
def test_goal_update_rejects_set_and_clear_unit(authed_profile, respx_mock, monkeypatch, capsys, unit) -> None:
monkeypatch.setattr(sys, "argv", ["omi", "--json", "goal", "update", "g1", "--unit", unit, "--clear-unit"])
with pytest.raises(SystemExit) as exc:
main()
assert exc.value.code == 1
output = capsys.readouterr()
assert output.out == ""
error = json.loads(output.err)
assert "--unit" in error["detail"]
assert "--clear-unit" in error["detail"]
assert not respx_mock.calls