askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_pickup_service.py · 24.3 KB · 567 lines
| 1 | """Counter service: an instruction on a line, a queue with a clock in it, |
| 2 | and the message that stops somebody standing there guessing. |
| 3 | |
| 4 | Three slices meet in this file and the properties worth holding are the |
| 5 | seams between them. |
| 6 | |
| 7 | **The delta is money, so it is money everywhere.** A modifier that shows |
| 8 | up in the basket total and not on the invoice is the shop quietly eating |
| 9 | sixty cents a cup, and nothing in the system would ever say so -- every |
| 10 | document would be internally consistent and one of them would be wrong. |
| 11 | So the modifier test walks the whole chain in one go, basket to invoice, |
| 12 | rather than asserting four separate arithmetics that could each be right |
| 13 | about a different number. |
| 14 | |
| 15 | **The note is NOT money, so it is money nowhere.** It reaches the cook, |
| 16 | who needs it, and touches no total, which is the other half of the same |
| 17 | property: the day somebody "helpfully" prices a note is the day an |
| 18 | invoice stops matching its own lines. |
| 19 | |
| 20 | **A ticket is not an invoice.** No prices, ever, by construction and not |
| 21 | by a flag -- the same rule the packing slip holds, tested the same way. |
| 22 | |
| 23 | **A shipping order is untouched by every pickup rule.** That is the |
| 24 | regression that matters: a warehouse that started printing kitchen |
| 25 | tickets for parcels would be this slice leaking into the one that was |
| 26 | already working. |
| 27 | |
| 28 | **The ready message queues exactly once.** The dispatcher promises |
| 29 | at-least-once delivery, so the handler WILL see a ready order again; a |
| 30 | customer told twice is noise, and a customer told never is the whole |
| 31 | failure this message exists to prevent. |
| 32 | """ |
| 33 | |
| 34 | import pathlib |
| 35 | import shutil |
| 36 | from datetime import datetime, timedelta |
| 37 | |
| 38 | from conftest import stage_collection |
| 39 | |
| 40 | import object_execution |
| 41 | import object_ids |
| 42 | import object_records |
| 43 | import python_object_runtime |
| 44 | |
| 45 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 46 | PACKAGES = REPO_ROOT / "packages" |
| 47 | SHOP_OBJECTS = PACKAGES / "app-shop" / "objects" |
| 48 | ORDERS_OBJECTS = PACKAGES / "app-orders" / "objects" |
| 49 | KITCHEN_OBJECTS = PACKAGES / "app-kitchen" / "objects" |
| 50 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 51 | |
| 52 | TOKEN = "sess-pickup-1" |
| 53 | |
| 54 | |
| 55 | def setup_env(tmp_path, monkeypatch, *, settings=()): |
| 56 | """A data dir with every collection this slice touches, and ONE object |
| 57 | root holding three packages' objects -- what an installed server |
| 58 | actually looks like, and the only way site_shop reaching action_cart |
| 59 | or action_checkout reaching object_cart resolves as it will in |
| 60 | production. |
| 61 | """ |
| 62 | data_dir = tmp_path / "data" |
| 63 | for pkg, name in (("app-shop", "carts"), ("app-shop", "cart_items"), |
| 64 | ("app-catalog", "products"), ("app-catalog", "locations"), |
| 65 | ("app-catalog", "stock_moves"), |
| 66 | ("app-orders", "orders"), ("app-orders", "order_lines"), |
| 67 | ("app-invoices", "invoices"), |
| 68 | ("app-invoices", "invoice_lines"), |
| 69 | ("app-email", "email_outbox")): |
| 70 | stage_collection(data_dir, pkg, name) |
| 71 | rows = "".join(f"s{i}\t{k}\t{v}\t\n" for i, (k, v) in enumerate(settings)) |
| 72 | stage_collection(data_dir, "app-settings", "app_settings", rows=rows) |
| 73 | |
| 74 | objects_root = tmp_path / "objects" |
| 75 | for source in (SHOP_OBJECTS, ORDERS_OBJECTS, KITCHEN_OBJECTS): |
| 76 | shutil.copytree(source, objects_root, dirs_exist_ok=True) |
| 77 | |
| 78 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 79 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(objects_root)) |
| 80 | return data_dir, objects_root |
| 81 | |
| 82 | |
| 83 | def run(objects_root, object_id, method, payload): |
| 84 | return object_execution.execute_object( |
| 85 | RUNTIME, |
| 86 | object_execution.ObjectExecutionRequest( |
| 87 | object_id, method=method, payload=payload), |
| 88 | roots=[objects_root]).result |
| 89 | |
| 90 | |
| 91 | # --- fixtures shaped like a small café --------------------------------------- |
| 92 | |
| 93 | def product(data_dir, product_id, name, cents=1200): |
| 94 | return object_records.create_collection_record( |
| 95 | "products", |
| 96 | {"id": product_id, "name": name, "sku": product_id.upper(), |
| 97 | "product_type": "physical", "price_cents": str(cents), |
| 98 | "currency": "USD", "is_active": "true", "owner_id": "shop"}, |
| 99 | base_dir=data_dir) |
| 100 | |
| 101 | |
| 102 | def stock_in(data_dir, product_id, quantity, *, to="loc-shelf"): |
| 103 | object_records.create_collection_record( |
| 104 | "locations", |
| 105 | {"id": to, "name": "Counter", "location_type": "warehouse", |
| 106 | "owner_id": "shop"}, base_dir=data_dir) |
| 107 | return object_records.create_collection_record( |
| 108 | "stock_moves", |
| 109 | {"id": object_ids.new_uuid4(), "product_id": product_id, |
| 110 | "to_location_id": to, "quantity": str(quantity), "reason": "purchase", |
| 111 | "occurred_at": "2026-07-01", "owner_id": "shop"}, |
| 112 | base_dir=data_dir) |
| 113 | |
| 114 | |
| 115 | def cart(objects_root, action="get", **payload): |
| 116 | return run(objects_root, "action_cart", "POST", |
| 117 | {"session_token": TOKEN, "action": action, **payload}) |
| 118 | |
| 119 | |
| 120 | def checkout(objects_root, **payload): |
| 121 | return run(objects_root, "action_checkout", "POST", |
| 122 | {"session_token": TOKEN, **payload}) |
| 123 | |
| 124 | |
| 125 | def when(minutes_from_now): |
| 126 | """A promised time as the shop's own naive wall clock -- the |
| 127 | convention app-pickup's slot generator writes and every reader of |
| 128 | promised_at in this repo shares. |
| 129 | |
| 130 | Half a minute of cushion, away from now in whichever direction the |
| 131 | caller asked. The page reports WHOLE minutes elapsed, so a promise |
| 132 | written exactly 35 minutes out and read a few milliseconds later is |
| 133 | honestly 34; the cushion buys the assertion a stable number without |
| 134 | the page having to round up and claim a minute that is not there. |
| 135 | """ |
| 136 | cushion = timedelta(seconds=30 if minutes_from_now >= 0 else -30) |
| 137 | return (datetime.now() + timedelta(minutes=minutes_from_now) |
| 138 | + cushion).isoformat(timespec="seconds") |
| 139 | |
| 140 | |
| 141 | def pickup_order(data_dir, order_id, *, status="preparing", number="PU-1", |
| 142 | promised=None, method="pickup", owner="", email="", |
| 143 | name="Ada Lovelace", **fields): |
| 144 | record = {"id": order_id, "doc_type": "sale", "number": number, |
| 145 | "customer_name": name, "customer_email": email, |
| 146 | "currency": "USD", "status": status, "order_date": "2026-07-26", |
| 147 | "fulfillment_method": method, "owner_id": owner} |
| 148 | if promised is not None: |
| 149 | record["promised_at"] = promised |
| 150 | record.update({k: str(v) for k, v in fields.items()}) |
| 151 | return object_records.create_collection_record( |
| 152 | "orders", record, base_dir=data_dir, preserve_read_only=True) |
| 153 | |
| 154 | |
| 155 | def order_line(data_dir, line_id, *, order_id, description="Flat White", |
| 156 | quantity="1", cents=400, note="", modifier=0): |
| 157 | return object_records.create_collection_record( |
| 158 | "order_lines", |
| 159 | {"id": line_id, "order_id": order_id, "description": description, |
| 160 | "quantity": quantity, "unit_price_cents": str(cents), |
| 161 | "line_note": note, "modifier_cents": str(modifier), |
| 162 | "line_total_cents": str(int(float(quantity)) * (cents + modifier)), |
| 163 | "owner_id": "shop"}, |
| 164 | base_dir=data_dir) |
| 165 | |
| 166 | |
| 167 | def kitchen(objects_root, user_id="dan"): |
| 168 | payload = {"_identity": {"user_id": user_id}} if user_id else {} |
| 169 | return run(objects_root, "site_kitchen", "GET", payload)["body"] |
| 170 | |
| 171 | |
| 172 | def ticket(objects_root, order_id): |
| 173 | return run(objects_root, "site_kitchen_ticket", "GET", |
| 174 | {"order_id": order_id}) |
| 175 | |
| 176 | |
| 177 | def fire(objects_root, order_id, action="update"): |
| 178 | """One change event, shaped exactly as object_change_dispatch sends |
| 179 | them (raw verb in `action`, participle in `event`).""" |
| 180 | return run(objects_root, "system_order_email", "EVENT", |
| 181 | {"event": f"orders.record.{action}d", "collection": "orders", |
| 182 | "record_id": order_id, "action": action}) |
| 183 | |
| 184 | |
| 185 | def rows(data_dir, collection): |
| 186 | return object_records.read_collection_records(collection, |
| 187 | base_dir=data_dir) |
| 188 | |
| 189 | |
| 190 | def outbox(data_dir): |
| 191 | return rows(data_dir, "email_outbox") |
| 192 | |
| 193 | |
| 194 | # --- 1. the delta is money, and money is the same number everywhere ---------- |
| 195 | |
| 196 | def test_a_modifier_reaches_the_basket_the_order_and_the_invoice( |
| 197 | tmp_path, monkeypatch): |
| 198 | """One test on purpose, walking the whole chain. |
| 199 | |
| 200 | Four separate assertions about four totals can all pass while the |
| 201 | totals disagree with each other -- which is precisely the failure |
| 202 | mode: every document internally consistent, one of them quietly |
| 203 | short. So the chain is walked once, and the last assertion is that |
| 204 | the invoice's own lines add up to the invoice. |
| 205 | """ |
| 206 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 207 | product(data_dir, "latte", "Latte", 400) |
| 208 | stock_in(data_dir, "latte", 20) |
| 209 | |
| 210 | # Two lattes with oat milk: 2 x (400 + 60), because two oat lattes are |
| 211 | # two lots of oat milk. |
| 212 | basket = cart(objects_root, "add", product_id="latte", quantity="2", |
| 213 | line_note="oat milk", modifier_cents="60") |
| 214 | assert basket["subtotal_cents"] == 920 |
| 215 | assert basket["lines"][0]["modifier_cents"] == 60 |
| 216 | assert basket["lines"][0]["line_note"] == "oat milk" |
| 217 | |
| 218 | preview = checkout(objects_root, preview="true") |
| 219 | assert preview["total_cents"] == 920 |
| 220 | |
| 221 | result = checkout(objects_root, customer_email="ada@example.test", |
| 222 | customer_name="Ada Lovelace") |
| 223 | assert result["ok"] is True |
| 224 | assert result["total_cents"] == 920 |
| 225 | |
| 226 | order = object_records.get_collection_record("orders", result["order_id"], |
| 227 | base_dir=data_dir) |
| 228 | assert order["total_cents"] == "920" |
| 229 | |
| 230 | line = rows(data_dir, "order_lines")[0] |
| 231 | assert line["modifier_cents"] == "60" |
| 232 | assert line["unit_price_cents"] == "400" # the catalogue price, kept |
| 233 | assert line["line_total_cents"] == "920" # with the delta inside it |
| 234 | |
| 235 | invoice = rows(data_dir, "invoices")[0] |
| 236 | invoice_line = rows(data_dir, "invoice_lines")[0] |
| 237 | # The invoice carries the EFFECTIVE unit price, so the bill is still an |
| 238 | # ordinary quantity x price document that multiplies out -- and so a |
| 239 | # restatement by invoice_totals could never drop the delta. |
| 240 | assert invoice_line["unit_price_cents"] == "460" |
| 241 | assert invoice_line["line_total_cents"] == "920" |
| 242 | assert invoice["total_cents"] == "920" |
| 243 | assert (sum(int(row["line_total_cents"]) for row in rows(data_dir, "invoice_lines")) |
| 244 | == int(invoice["total_cents"])) |
| 245 | |
| 246 | |
| 247 | def test_order_totals_restating_a_line_keeps_the_modifier(tmp_path, monkeypatch): |
| 248 | """The fold has to be able to reproduce the number it is restating. |
| 249 | |
| 250 | order_totals recomputes line_total_cents from the line's own columns. |
| 251 | If it multiplied the base price alone it would silently replace a |
| 252 | correct total with a smaller one every time anybody touched the line, |
| 253 | and the shop would be short the delta with nothing to point at. |
| 254 | """ |
| 255 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 256 | pickup_order(data_dir, "ord-mod", number="PU-MOD") |
| 257 | order_line(data_dir, "line-mod", order_id="ord-mod", quantity="2", |
| 258 | cents=400, modifier=60) |
| 259 | |
| 260 | run(objects_root, "system_order_totals", "EVENT", |
| 261 | {"event": "order_lines.record.updated", "collection": "order_lines", |
| 262 | "record_id": "line-mod", "action": "update"}) |
| 263 | |
| 264 | line = object_records.get_collection_record("order_lines", "line-mod", |
| 265 | base_dir=data_dir) |
| 266 | assert line["line_total_cents"] == "920" |
| 267 | order = object_records.get_collection_record("orders", "ord-mod", |
| 268 | base_dir=data_dir) |
| 269 | assert order["subtotal_cents"] == "920" |
| 270 | |
| 271 | |
| 272 | def test_two_notes_on_one_product_are_two_lines(tmp_path, monkeypatch): |
| 273 | """One burger with no onions and one with are two things to make. |
| 274 | |
| 275 | Merging them on product_id would hand the cook a line of two carrying |
| 276 | one instruction, and one of those two customers gets the wrong lunch. |
| 277 | """ |
| 278 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 279 | product(data_dir, "burger", "Burger", 900) |
| 280 | stock_in(data_dir, "burger", 20) |
| 281 | |
| 282 | cart(objects_root, "add", product_id="burger", quantity="1", |
| 283 | line_note="no onions") |
| 284 | basket = cart(objects_root, "add", product_id="burger", quantity="1") |
| 285 | assert len(basket["lines"]) == 2 |
| 286 | assert {line["line_note"] for line in basket["lines"]} == {"no onions", ""} |
| 287 | |
| 288 | # And the ordinary case is untouched: same product, same instruction, |
| 289 | # same delta still merges into one line of two. |
| 290 | basket = cart(objects_root, "add", product_id="burger", quantity="1", |
| 291 | line_note="no onions") |
| 292 | assert len(basket["lines"]) == 2 |
| 293 | noted = [line for line in basket["lines"] if line["line_note"]][0] |
| 294 | assert noted["quantity"] == "2" |
| 295 | |
| 296 | |
| 297 | def test_a_shopper_cannot_price_their_own_lunch(tmp_path, monkeypatch): |
| 298 | """action_cart is a PUBLIC object, so anything the request can put |
| 299 | into money is something a stranger can put into money.""" |
| 300 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 301 | product(data_dir, "latte", "Latte", 400) |
| 302 | stock_in(data_dir, "latte", 20) |
| 303 | |
| 304 | result = cart(objects_root, "add", product_id="latte", |
| 305 | modifier_cents="-350") |
| 306 | assert result["status"] == 400 |
| 307 | assert "never take money off" in result["error"] |
| 308 | assert rows(data_dir, "cart_items") == [] |
| 309 | |
| 310 | |
| 311 | def test_the_shop_page_takes_a_note_and_shows_it_back(tmp_path, monkeypatch): |
| 312 | """The box is on the ADD form, because a note belongs to a line. |
| 313 | /shop's own `customer_note` is a different field addressed to a |
| 314 | different person -- the packer, not the cook -- and one box at the |
| 315 | bottom of a checkout cannot say which burger it meant. |
| 316 | """ |
| 317 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 318 | product(data_dir, "burger", "Burger", 900) |
| 319 | stock_in(data_dir, "burger", 20) |
| 320 | |
| 321 | body = run(objects_root, "site_shop", "GET", |
| 322 | {"_cookies": {"cart": TOKEN}})["body"] |
| 323 | assert 'name="line_note"' in body |
| 324 | # And no way to type a price: action_cart is public, so a delta input |
| 325 | # on a public page is a shopper pricing their own lunch. |
| 326 | assert 'name="modifier_cents"' not in body |
| 327 | |
| 328 | cart(objects_root, "add", product_id="burger", quantity="1", |
| 329 | line_note="no onions", modifier_cents="150") |
| 330 | body = run(objects_root, "site_shop", "GET", |
| 331 | {"_cookies": {"cart": TOKEN}})["body"] |
| 332 | assert "no onions" in body |
| 333 | assert "+1.50 each" in body |
| 334 | # The Update button says WHICH line, now that two can share a product. |
| 335 | assert 'name="cart_item_id"' in body |
| 336 | |
| 337 | |
| 338 | # --- 2. the note is not money, and reaches the person who needs it ---------- |
| 339 | |
| 340 | def test_a_line_note_reaches_the_kitchen_ticket_and_never_the_invoice_total( |
| 341 | tmp_path, monkeypatch): |
| 342 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 343 | product(data_dir, "burger", "Burger", 900) |
| 344 | stock_in(data_dir, "burger", 20) |
| 345 | |
| 346 | cart(objects_root, "add", product_id="burger", quantity="2", |
| 347 | line_note="no onions, cut in half") |
| 348 | result = checkout(objects_root, customer_email="ada@example.test") |
| 349 | order_id = result["order_id"] |
| 350 | |
| 351 | # An instruction with no delta must move NO total: 2 x 900, exactly |
| 352 | # what the same basket cost before anybody typed anything. |
| 353 | assert result["total_cents"] == 1800 |
| 354 | invoice = rows(data_dir, "invoices")[0] |
| 355 | assert invoice["total_cents"] == "1800" |
| 356 | invoice_line = rows(data_dir, "invoice_lines")[0] |
| 357 | assert invoice_line["line_total_cents"] == "1800" |
| 358 | assert "onions" not in invoice_line["description"] |
| 359 | |
| 360 | # And it does reach the cook, under the line it is true of. |
| 361 | body = ticket(objects_root, order_id)["body"] |
| 362 | assert "no onions, cut in half" in body |
| 363 | assert "Burger" in body |
| 364 | |
| 365 | |
| 366 | def test_the_kitchen_ticket_shows_no_prices(tmp_path, monkeypatch): |
| 367 | """By construction, not behind a flag -- the packing slip's rule. A |
| 368 | cook cannot act on what it cost, and the money conversation already |
| 369 | happened with somebody who may not be the person cooking.""" |
| 370 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 371 | pickup_order(data_dir, "ord-t", number="PU-TICKET", promised=when(20)) |
| 372 | order_line(data_dir, "line-t", order_id="ord-t", quantity="1", |
| 373 | cents=1234, modifier=567, note="extra hot") |
| 374 | |
| 375 | body = ticket(objects_root, "ord-t")["body"] |
| 376 | assert "extra hot" in body |
| 377 | assert "Flat White" in body |
| 378 | for money in ("1234", "12.34", "567", "5.67", "1801", "18.01"): |
| 379 | assert money not in body, f"a price ({money}) reached a kitchen ticket" |
| 380 | |
| 381 | |
| 382 | def test_an_unknown_order_is_a_friendly_404_not_a_traceback(tmp_path, monkeypatch): |
| 383 | _data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 384 | page = ticket(objects_root, "no-such-order") |
| 385 | assert page["status"] == 404 |
| 386 | assert "Not found" in page["body"] |
| 387 | |
| 388 | |
| 389 | # --- 3. the queue, and the clock in it --------------------------------------- |
| 390 | |
| 391 | def test_the_queue_sorts_by_promised_time_and_shouts_about_a_late_one( |
| 392 | tmp_path, monkeypatch): |
| 393 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 394 | pickup_order(data_dir, "ord-late", number="PU-LATE", promised=when(-7), |
| 395 | name="Grace Hopper") |
| 396 | pickup_order(data_dir, "ord-soon", number="PU-SOON", promised=when(35), |
| 397 | name="Ada Lovelace", owner="dan") |
| 398 | order_line(data_dir, "line-late", order_id="ord-late", |
| 399 | description="Bacon Roll", note="brown sauce") |
| 400 | order_line(data_dir, "line-soon", order_id="ord-soon") |
| 401 | |
| 402 | body = kitchen(objects_root) |
| 403 | assert "LATE by 7 min" in body |
| 404 | assert "in 35 min" in body |
| 405 | # Soonest promise first, and the late one is the soonest of all. |
| 406 | assert body.index("PU-LATE") < body.index("PU-SOON") |
| 407 | # The lines and their notes are on the queue, not only on the ticket: |
| 408 | # a cook glancing at the screen is reading the instruction there. |
| 409 | assert "brown sauce" in body |
| 410 | |
| 411 | |
| 412 | def test_a_shipping_order_never_appears_on_the_kitchen_queue( |
| 413 | tmp_path, monkeypatch): |
| 414 | """The regression that matters. A warehouse that started printing |
| 415 | kitchen tickets for parcels would be this slice leaking into the one |
| 416 | that was already working.""" |
| 417 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 418 | pickup_order(data_dir, "ord-ship", number="PU-SHIPPED", status="confirmed", |
| 419 | method="shipping", promised=when(5)) |
| 420 | pickup_order(data_dir, "ord-blank", number="PU-NOMETHOD", |
| 421 | status="confirmed", method="", promised=when(5)) |
| 422 | order_line(data_dir, "line-ship", order_id="ord-ship") |
| 423 | order_line(data_dir, "line-blank", order_id="ord-blank") |
| 424 | |
| 425 | body = kitchen(objects_root) |
| 426 | assert "PU-SHIPPED" not in body |
| 427 | assert "PU-NOMETHOD" not in body |
| 428 | assert "Nothing is being prepared" in body |
| 429 | |
| 430 | |
| 431 | def test_a_ready_order_has_left_the_queue(tmp_path, monkeypatch): |
| 432 | """`ready` is made and on the shelf, and the customer has been told. |
| 433 | A queue that still showed it would never empty.""" |
| 434 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 435 | pickup_order(data_dir, "ord-ready", number="PU-DONE", status="ready", |
| 436 | promised=when(-2)) |
| 437 | order_line(data_dir, "line-ready", order_id="ord-ready") |
| 438 | assert "PU-DONE" not in kitchen(objects_root) |
| 439 | |
| 440 | |
| 441 | def test_an_order_with_no_promised_time_sorts_last_and_claims_nothing( |
| 442 | tmp_path, monkeypatch): |
| 443 | """'We do not know when this was promised' is not a claim to urgency, |
| 444 | and must never invent an emergency at the top of the queue.""" |
| 445 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 446 | pickup_order(data_dir, "ord-none", number="PU-UNSET") |
| 447 | pickup_order(data_dir, "ord-soon", number="PU-SOON", promised=when(30)) |
| 448 | order_line(data_dir, "line-none", order_id="ord-none") |
| 449 | order_line(data_dir, "line-soon", order_id="ord-soon") |
| 450 | |
| 451 | body = kitchen(objects_root) |
| 452 | assert body.index("PU-SOON") < body.index("PU-UNSET") |
| 453 | assert "no promised time" in body |
| 454 | assert "LATE" not in body |
| 455 | |
| 456 | |
| 457 | def test_the_queue_asks_a_stranger_to_sign_in(tmp_path, monkeypatch): |
| 458 | """A sign-in prompt, not a 403 -- the same shape site_pick_list uses, |
| 459 | because the person reading this is staff who have not logged in yet.""" |
| 460 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 461 | pickup_order(data_dir, "ord-1", number="PU-SECRET", promised=when(10)) |
| 462 | body = kitchen(objects_root, user_id="") |
| 463 | assert "Sign in" in body |
| 464 | assert "PU-SECRET" not in body |
| 465 | |
| 466 | |
| 467 | # --- 4. "your order is ready" ------------------------------------------------- |
| 468 | |
| 469 | def test_the_ready_message_queues_exactly_once_and_a_replay_queues_nothing( |
| 470 | tmp_path, monkeypatch): |
| 471 | """The dispatcher promises at-least-once delivery, so this handler |
| 472 | WILL see the same ready order again.""" |
| 473 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 474 | pickup_order(data_dir, "ord-r", number="PU-READY", status="ready", |
| 475 | email="ada@example.test", promised=when(-1), |
| 476 | ready_at="2026-07-26T18:15:00") |
| 477 | |
| 478 | first = fire(objects_root, "ord-r") |
| 479 | # `confirmed` rides along on purpose: the confirmation fires anywhere |
| 480 | # at or past confirmed, and `ready` is past it. A counter shop that |
| 481 | # takes an order and has it made in one burst of writes would |
| 482 | # otherwise never send a confirmation at all. |
| 483 | assert first["queued"] == ["confirmed", "ready"] |
| 484 | |
| 485 | ready_mail = [row for row in outbox(data_dir) |
| 486 | if row["source_object_id"].endswith(":ready")] |
| 487 | assert len(ready_mail) == 1 |
| 488 | assert "ready to collect" in ready_mail[0]["subject"] |
| 489 | assert "PU-READY" in ready_mail[0]["text_body"] |
| 490 | assert "ready to collect" in ready_mail[0]["text_body"] |
| 491 | |
| 492 | second = fire(objects_root, "ord-r") |
| 493 | assert second["queued"] == [] |
| 494 | assert "ready" in second["skipped_already_sent"] |
| 495 | assert len([row for row in outbox(data_dir) |
| 496 | if row["source_object_id"].endswith(":ready")]) == 1 |
| 497 | |
| 498 | |
| 499 | def test_a_shipping_order_is_never_told_to_come_and_collect( |
| 500 | tmp_path, monkeypatch): |
| 501 | """'Ready for collection' said to somebody whose parcel is on a van is |
| 502 | a customer driving to a shop for nothing.""" |
| 503 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 504 | pickup_order(data_dir, "ord-s", number="SO-SHIP", status="shipped", |
| 505 | method="shipping", email="ada@example.test") |
| 506 | |
| 507 | fire(objects_root, "ord-s") |
| 508 | assert [row for row in outbox(data_dir) |
| 509 | if row["source_object_id"].endswith(":ready")] == [] |
| 510 | |
| 511 | |
| 512 | def test_a_pickup_order_with_no_email_skips_with_a_reason(tmp_path, monkeypatch): |
| 513 | """Say so plainly. An operator reading 'no customer_email' knows |
| 514 | exactly what to add, whereas silence looks like a working mailer.""" |
| 515 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 516 | pickup_order(data_dir, "ord-n", number="PU-NOMAIL", status="ready", |
| 517 | email="") |
| 518 | |
| 519 | result = fire(objects_root, "ord-n") |
| 520 | assert "customer_email" in result["skipped"] |
| 521 | assert outbox(data_dir) == [] |
| 522 | |
| 523 | |
| 524 | def test_with_no_sms_provider_the_customer_is_still_told_by_email( |
| 525 | tmp_path, monkeypatch): |
| 526 | """The absence of a connector degrades exactly the way the carrier |
| 527 | work degrades: the shop loses nothing, the message still goes, and |
| 528 | nothing pretends to have sent a text.""" |
| 529 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 530 | pickup_order(data_dir, "ord-r", number="PU-READY", status="ready", |
| 531 | email="ada@example.test") |
| 532 | |
| 533 | result = fire(objects_root, "ord-r") |
| 534 | assert "ready" in result["queued"] |
| 535 | assert "no SMS provider configured" in result["sms"] |
| 536 | assert len(outbox(data_dir)) == 2 # the confirmation, and this |
| 537 | |
| 538 | |
| 539 | def test_a_named_sms_provider_with_no_adapter_says_so_and_still_emails( |
| 540 | tmp_path, monkeypatch): |
| 541 | """An operator who pasted a provider name in and heard nothing would |
| 542 | reasonably conclude the system was texting people. It is not, and it |
| 543 | says which channel actually carried the message.""" |
| 544 | data_dir, objects_root = setup_env( |
| 545 | tmp_path, monkeypatch, |
| 546 | settings=(("notify.sms_provider", "twilio"),)) |
| 547 | pickup_order(data_dir, "ord-r", number="PU-READY", status="ready", |
| 548 | email="ada@example.test") |
| 549 | |
| 550 | result = fire(objects_root, "ord-r") |
| 551 | assert "ready" in result["queued"] |
| 552 | assert "twilio" in result["sms"] |
| 553 | assert "no SMS connector is installed" in result["sms"] |
| 554 | assert len(outbox(data_dir)) == 2 |
| 555 | |
| 556 | |
| 557 | def test_a_manual_sms_shop_is_told_the_counter_does_it_by_hand( |
| 558 | tmp_path, monkeypatch): |
| 559 | data_dir, objects_root = setup_env( |
| 560 | tmp_path, monkeypatch, |
| 561 | settings=(("notify.sms_provider", "manual"),)) |
| 562 | pickup_order(data_dir, "ord-r", number="PU-READY", status="ready", |
| 563 | email="ada@example.test") |
| 564 | |
| 565 | result = fire(objects_root, "ord-r") |
| 566 | assert "ready" in result["queued"] |
| 567 | assert "by hand" in result["sms"] |