Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_resolve_run.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_resolve_run.py · 8.4 KB · 210 lines
1"""Resolving what the machine refused to decide.
2
3The sweeper stops a submitted-but-unresolved run at `needs_review` with
4its hold intact, because both automatic answers are wrong: releasing
5gives away money the provider may already have taken, charging bills for
6work that may never have arrived.
7
8Stopping there is only a design if somebody can continue. A queue nobody
9can act on is money parked forever, so this is the other half -- and it
10is the only hand that moves a run out of needs_review.
11"""
12
13import pathlib
14
15from conftest import stage_collection
16
17import object_execution
18import object_records
19import object_template_runs
20import python_object_runtime
21
22REPO_ROOT = pathlib.Path(__file__).resolve().parents[1]
23RUNNER_OBJECTS = REPO_ROOT / "packages" / "app-runner" / "objects"
24RUNTIME = python_object_runtime.PythonObjectRuntime()
25
26
27def setup_env(tmp_path, monkeypatch, *, status="needs_review", price="600"):
28 data_dir = tmp_path / "data"
29 for pkg, name in (("app-runner", "template_runs"),
30 ("app-templates", "templates"),
31 ("app-billing", "wallets"),
32 ("app-billing", "wallet_entries")):
33 stage_collection(data_dir, pkg, name)
34 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir))
35 object_records.create_collection_record(
36 "templates", {"id": "tpl", "name": "Picture", "handler": "ai_image",
37 "body": "x", "owner_id": "dan"}, base_dir=data_dir)
38 object_records.create_collection_record(
39 "wallets", {"id": "w1", "owner_id": "dan", "is_active": "true"},
40 base_dir=data_dir)
41 object_records.create_collection_record(
42 "wallet_entries",
43 {"wallet_id": "w1", "amount_minor": "5000", "kind": "topup",
44 "owner_id": "dan"}, base_dir=data_dir)
45 object_records.create_collection_record(
46 "wallet_entries",
47 {"wallet_id": "w1", "amount_minor": f"-{price}", "kind": "hold",
48 "generated_from": "template_run/r1/hold", "owner_id": "dan"},
49 base_dir=data_dir)
50 object_records.create_collection_record(
51 "template_runs",
52 {"id": "r1", "template_id": "tpl", "template_name": "Picture",
53 "handler": "ai_image", "status": status, "price_cents": price,
54 "wallet_id": "w1", "owner_id": "dan",
55 "provider_job_id": "job-abc", "provider_cost_cents": "40",
56 "error": "unresolved after 3600s"},
57 base_dir=data_dir)
58 return data_dir
59
60
61def resolve(payload, *, user_id="dan", is_admin=False):
62 return object_execution.execute_object(
63 RUNTIME,
64 object_execution.ObjectExecutionRequest(
65 "action_resolve_run", method="POST",
66 payload={**payload,
67 "_identity": {"user_id": user_id, "is_admin": is_admin}}),
68 roots=[RUNNER_OBJECTS]).result
69
70
71def kinds(data_dir):
72 return [e["kind"] for e in object_records.read_collection_records(
73 "wallet_entries", base_dir=data_dir)
74 if "template_run/r1/" in str(e.get("generated_from"))]
75
76
77def test_charging_settles_the_run_as_delivered(tmp_path, monkeypatch):
78 data_dir = setup_env(tmp_path, monkeypatch)
79 result = resolve({"run_id": "r1", "decision": "charge",
80 "note": "file landed in the bucket"})
81 assert result["ok"] and result["settlement"] == "settled"
82
83 assert kinds(data_dir) == ["hold", "release", "debit"]
84 run = object_records.get_collection_record("template_runs", "r1",
85 base_dir=data_dir)
86 assert run["status"] == "succeeded"
87 assert "file landed in the bucket" in run["error"]
88 assert "Resolved by dan as charge" in run["error"]
89
90
91def test_refunding_releases_the_hold_and_charges_nothing(tmp_path, monkeypatch):
92 data_dir = setup_env(tmp_path, monkeypatch)
93 result = resolve({"run_id": "r1", "decision": "refund"})
94 assert result["ok"]
95
96 assert kinds(data_dir) == ["hold", "release"]
97 run = object_records.get_collection_record("template_runs", "r1",
98 base_dir=data_dir)
99 assert run["status"] == "failed"
100
101
102def test_what_it_cost_us_stays_visible_after_a_refund(tmp_path, monkeypatch):
103 """Eating a provider cost is a number somebody can find, never a
104 silent write-off."""
105 data_dir = setup_env(tmp_path, monkeypatch)
106 resolve({"run_id": "r1", "decision": "refund"})
107 run = object_records.get_collection_record("template_runs", "r1",
108 base_dir=data_dir)
109 assert run["provider_cost_cents"] == "40"
110
111
112def test_a_second_decision_writes_money_once(tmp_path, monkeypatch):
113 """Idempotent by the same release marker everything else uses."""
114 data_dir = setup_env(tmp_path, monkeypatch)
115 resolve({"run_id": "r1", "decision": "charge"})
116 before = kinds(data_dir)
117 again = resolve({"run_id": "r1", "decision": "charge"})
118 assert again["status"] == 409 # no longer needs_review
119 assert kinds(data_dir) == before
120
121
122def test_only_a_needs_review_run_can_be_resolved(tmp_path, monkeypatch):
123 """A terminal run is already settled; a running one may still be
124 answered, and polling beats a guess."""
125 for status in ("running", "succeeded", "failed", "abandoned", "queued"):
126 data_dir = setup_env(tmp_path, monkeypatch, status=status)
127 result = resolve({"run_id": "r1", "decision": "charge"})
128 assert result["status"] == 409, status
129 assert kinds(data_dir) == ["hold"], status
130
131
132def test_a_bad_decision_is_refused_explaining_both(tmp_path, monkeypatch):
133 setup_env(tmp_path, monkeypatch)
134 result = resolve({"run_id": "r1", "decision": "maybe"})
135 assert result["status"] == 400
136 assert "charge" in result["error"] and "refund" in result["error"]
137
138
139def test_a_stranger_cannot_decide_somebody_elses_money(tmp_path, monkeypatch):
140 data_dir = setup_env(tmp_path, monkeypatch)
141 result = resolve({"run_id": "r1", "decision": "charge"}, user_id="mallory")
142 assert result["status"] == 403
143 assert kinds(data_dir) == ["hold"]
144
145
146def test_an_admin_can_resolve_any_run(tmp_path, monkeypatch):
147 data_dir = setup_env(tmp_path, monkeypatch)
148 result = resolve({"run_id": "r1", "decision": "refund"},
149 user_id="ops", is_admin=True)
150 assert result["ok"]
151 assert kinds(data_dir) == ["hold", "release"]
152
153
154def test_anonymous_is_refused(tmp_path, monkeypatch):
155 setup_env(tmp_path, monkeypatch)
156 assert resolve({"run_id": "r1", "decision": "charge"},
157 user_id="")["status"] == 401
158
159
160def test_an_unknown_run_is_a_404(tmp_path, monkeypatch):
161 setup_env(tmp_path, monkeypatch)
162 assert resolve({"run_id": "nope", "decision": "charge"})["status"] == 404
163
164
165def test_resolving_never_contacts_a_provider(tmp_path, monkeypatch):
166 """The standing refusal survives: a resolution verb settles MONEY
167 against a fate somebody established by other means. It is not a
168 retry, and must never become one."""
169 source = (RUNNER_OBJECTS / "action" / "resolve_run.py").read_text()
170 assert "never re-runs anything" in source
171 for forbidden in ("HANDLERS", "send_http", "urlopen", "object_ai"):
172 assert forbidden not in source, forbidden
173
174
175# --- the queue has to be visible ------------------------------------------------
176
177def count_review():
178 return object_execution.execute_object(
179 RUNTIME,
180 object_execution.ObjectExecutionRequest(
181 "system_review_attention", method="COUNT", payload={}),
182 roots=[RUNNER_OBJECTS]).result
183
184
185def test_a_parked_run_shows_up_with_the_money_it_is_holding(
186 tmp_path, monkeypatch):
187 """A queue nobody can see is the same as no queue -- and this is the
188 one where doing nothing costs somebody money continuously, so the
189 detail leads with the amount held rather than the age."""
190 setup_env(tmp_path, monkeypatch)
191 result = count_review()
192 assert result["count"] == 1
193 assert "$6.00 held" in result["detail"]
194
195
196def test_resolving_clears_it_from_the_queue(tmp_path, monkeypatch):
197 setup_env(tmp_path, monkeypatch)
198 resolve({"run_id": "r1", "decision": "refund"})
199 assert count_review()["count"] == 0
200
201
202def test_only_needs_review_counts(tmp_path, monkeypatch):
203 for status in ("running", "succeeded", "failed", "abandoned", "queued"):
204 setup_env(tmp_path, monkeypatch, status=status)
205 assert count_review()["count"] == 0, status
206
207
208def test_it_degrades_to_zero_without_the_collection(tmp_path, monkeypatch):
209 monkeypatch.setenv("DBBASIC_DATA_DIR", str(tmp_path / "empty"))
210 assert count_review() == {"count": 0}