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_shipping.py · 32.4 KB · 745 lines
| 1 | """Shipments: the document that says what physically moved. |
| 2 | |
| 3 | The properties worth holding here are the ones a shop discovers the hard |
| 4 | way. You cannot ship more than was ordered, and being told so is only |
| 5 | useful if the refusal carries the numbers. Two shipments against one order |
| 6 | derive partial then shipped, each exactly once, without anybody typing a |
| 7 | status. A replayed event moves no stock twice, because events ARE replayed |
| 8 | (object_change_dispatch promises at-least-once). A packing slip carries no |
| 9 | prices, ever, which is what makes every parcel gift-safe with no flag to |
| 10 | forget. And the zero-touch shop keeps working: payment still ships the |
| 11 | order and still moves the stock -- it just travels through the shipment |
| 12 | noun now. |
| 13 | """ |
| 14 | |
| 15 | import pathlib |
| 16 | import shutil |
| 17 | |
| 18 | from conftest import stage_collection |
| 19 | |
| 20 | import object_execution |
| 21 | import object_ids |
| 22 | import object_records |
| 23 | import python_object_runtime |
| 24 | |
| 25 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 26 | PACKAGES = REPO_ROOT / "packages" |
| 27 | SHIPPING_OBJECTS = PACKAGES / "app-shipping" / "objects" |
| 28 | SHOP_OBJECTS = PACKAGES / "app-shop" / "objects" |
| 29 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 30 | |
| 31 | TOKEN = "sess-ship-1" |
| 32 | |
| 33 | |
| 34 | def setup_env(tmp_path, monkeypatch, *, settings=()): |
| 35 | """A data dir with every collection this slice touches, and ONE object |
| 36 | root holding both packages' objects. |
| 37 | |
| 38 | The merged root is not a convenience: it is what an installed server |
| 39 | actually looks like (every package installs into the same objects |
| 40 | directory), and it is the only way an object that calls a sibling by id |
| 41 | -- system_shop_fulfillment reaching action_create_shipment -- resolves |
| 42 | the way it will in production. |
| 43 | """ |
| 44 | data_dir = tmp_path / "data" |
| 45 | for pkg, name in (("app-shipping", "shipments"), |
| 46 | ("app-shipping", "shipment_lines"), |
| 47 | ("app-shop", "carts"), ("app-shop", "cart_items"), |
| 48 | ("app-catalog", "products"), ("app-catalog", "locations"), |
| 49 | ("app-catalog", "stock_moves"), |
| 50 | ("app-orders", "orders"), ("app-orders", "order_lines"), |
| 51 | ("app-invoices", "invoices"), ("app-invoices", "invoice_lines"), |
| 52 | ("app-payments", "payments")): |
| 53 | stage_collection(data_dir, pkg, name) |
| 54 | rows = "".join(f"s{i}\t{k}\t{v}\t\n" for i, (k, v) in enumerate(settings)) |
| 55 | stage_collection(data_dir, "app-settings", "app_settings", rows=rows) |
| 56 | |
| 57 | objects_root = tmp_path / "objects" |
| 58 | for source in (SHIPPING_OBJECTS, SHOP_OBJECTS): |
| 59 | shutil.copytree(source, objects_root, dirs_exist_ok=True) |
| 60 | |
| 61 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 62 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(objects_root)) |
| 63 | return data_dir, objects_root |
| 64 | |
| 65 | |
| 66 | def run(objects_root, object_id, method, payload): |
| 67 | return object_execution.execute_object( |
| 68 | RUNTIME, |
| 69 | object_execution.ObjectExecutionRequest( |
| 70 | object_id, method=method, payload=payload), |
| 71 | roots=[objects_root]).result |
| 72 | |
| 73 | |
| 74 | # --- fixtures shaped like a real shop ----------------------------------------- |
| 75 | |
| 76 | def product(data_dir, product_id, name, cents=1200): |
| 77 | return object_records.create_collection_record( |
| 78 | "products", |
| 79 | {"id": product_id, "name": name, "sku": product_id.upper(), |
| 80 | "product_type": "physical", "price_cents": str(cents), |
| 81 | "currency": "USD", "is_active": "true", "owner_id": "shop"}, |
| 82 | base_dir=data_dir) |
| 83 | |
| 84 | |
| 85 | def location(data_dir, location_id, name, kind="warehouse"): |
| 86 | return object_records.create_collection_record( |
| 87 | "locations", |
| 88 | {"id": location_id, "name": name, "location_type": kind, "owner_id": "shop"}, |
| 89 | base_dir=data_dir) |
| 90 | |
| 91 | |
| 92 | def order(data_dir, order_id="ord-1", *, status="confirmed", number="SO-0001", |
| 93 | owner="shop", **fields): |
| 94 | record = {"id": order_id, "doc_type": "sale", "number": number, |
| 95 | "customer_name": "Ada Lovelace", |
| 96 | "customer_email": "ada@example.test", "currency": "USD", |
| 97 | "status": "draft", "order_date": "2026-07-01", "owner_id": owner} |
| 98 | record.update({k: str(v) for k, v in fields.items()}) |
| 99 | created = object_records.create_collection_record("orders", record, |
| 100 | base_dir=data_dir) |
| 101 | if status != "draft": |
| 102 | # Through the ladder, the way a human would: draft is where every |
| 103 | # order starts. |
| 104 | object_records.update_collection_record( |
| 105 | "orders", order_id, {"status": "confirmed"}, |
| 106 | base_dir=data_dir, actor="test") |
| 107 | if status != "confirmed": |
| 108 | object_records.update_collection_record( |
| 109 | "orders", order_id, {"status": status}, |
| 110 | base_dir=data_dir, actor="test") |
| 111 | created = object_records.get_collection_record("orders", order_id, |
| 112 | base_dir=data_dir) |
| 113 | return created |
| 114 | |
| 115 | |
| 116 | def order_line(data_dir, line_id, order_id="ord-1", *, product_id="p1", |
| 117 | description="Enamel Mug", quantity="3", cents=1200): |
| 118 | return object_records.create_collection_record( |
| 119 | "order_lines", |
| 120 | {"id": line_id, "order_id": order_id, "product_id": product_id, |
| 121 | "description": description, "quantity": str(quantity), |
| 122 | "unit_price_cents": str(cents), |
| 123 | "line_total_cents": str(int(float(quantity) * cents)), |
| 124 | "owner_id": "shop"}, |
| 125 | base_dir=data_dir) |
| 126 | |
| 127 | |
| 128 | def stock_in(data_dir, product_id, quantity, *, to="loc-shelf"): |
| 129 | return object_records.create_collection_record( |
| 130 | "stock_moves", |
| 131 | {"id": object_ids.new_uuid4(), "product_id": product_id, |
| 132 | "to_location_id": to, "quantity": str(quantity), "reason": "purchase", |
| 133 | "occurred_at": "2026-06-01", "owner_id": "shop"}, |
| 134 | base_dir=data_dir) |
| 135 | |
| 136 | |
| 137 | def shipments(data_dir): |
| 138 | return object_records.read_collection_records("shipments", base_dir=data_dir) |
| 139 | |
| 140 | |
| 141 | def shipment_lines(data_dir): |
| 142 | return object_records.read_collection_records("shipment_lines", |
| 143 | base_dir=data_dir) |
| 144 | |
| 145 | |
| 146 | def sales(data_dir): |
| 147 | return [move for move in object_records.read_collection_records( |
| 148 | "stock_moves", base_dir=data_dir) if move["reason"] == "sale"] |
| 149 | |
| 150 | |
| 151 | def order_status(data_dir, order_id="ord-1"): |
| 152 | return object_records.get_collection_record("orders", order_id, |
| 153 | base_dir=data_dir)["status"] |
| 154 | |
| 155 | |
| 156 | def create_shipment(objects_root, **payload): |
| 157 | return run(objects_root, "action_create_shipment", "POST", payload) |
| 158 | |
| 159 | |
| 160 | def hook(objects_root, record, action="create"): |
| 161 | return run(objects_root, "hook_shipment_lines", "BEFORE_WRITE", |
| 162 | {"action": action, "collection": "shipment_lines", "record": record}) |
| 163 | |
| 164 | |
| 165 | def fulfil_shipment(objects_root, shipment_id, *, today=None): |
| 166 | """The dispatcher's own payload shape: EVENT verb, record_id, raw action. |
| 167 | |
| 168 | `today` is the same optional stamp every other pass in this house takes |
| 169 | -- the handler dates the handover with it, so a test can put parcels on |
| 170 | a named day instead of on whatever day it is being run. |
| 171 | """ |
| 172 | payload = {"event": "shipments.record.updated", "collection": "shipments", |
| 173 | "record_id": shipment_id, "action": "update"} |
| 174 | if today: |
| 175 | payload["today"] = today |
| 176 | return run(objects_root, "system_order_fulfillment", "EVENT", payload) |
| 177 | |
| 178 | |
| 179 | def ship(data_dir, objects_root, shipment_id, *, today=None): |
| 180 | """Take a shipment out of the door the way a packer would, then let the |
| 181 | handler react to it.""" |
| 182 | for status in ("packed", "shipped"): |
| 183 | object_records.update_collection_record( |
| 184 | "shipments", shipment_id, {"status": status}, |
| 185 | base_dir=data_dir, actor="test") |
| 186 | return fulfil_shipment(objects_root, shipment_id, today=today) |
| 187 | |
| 188 | |
| 189 | def stocked_shop(tmp_path, monkeypatch, *, quantity="3", auto=None, |
| 190 | status="confirmed"): |
| 191 | settings = [("shop.stock_location", "loc-shelf"), |
| 192 | ("shop.customer_location", "loc-customer")] |
| 193 | if auto is not None: |
| 194 | settings.append(("shop.auto_fulfill", auto)) |
| 195 | data_dir, objects_root = setup_env(tmp_path, monkeypatch, settings=settings) |
| 196 | location(data_dir, "loc-shelf", "Shelf") |
| 197 | location(data_dir, "loc-customer", "Customers", kind="customer") |
| 198 | product(data_dir, "p1", "Enamel Mug") |
| 199 | stock_in(data_dir, "p1", 20) |
| 200 | order(data_dir, status=status) |
| 201 | order_line(data_dir, "line-1", quantity=quantity) |
| 202 | return data_dir, objects_root |
| 203 | |
| 204 | |
| 205 | # --- the gate ------------------------------------------------------------------ |
| 206 | |
| 207 | def test_over_shipping_is_refused_with_all_three_numbers(tmp_path, monkeypatch): |
| 208 | """"No" is not an answer a packer can act on. Which of the two shipments |
| 209 | is wrong is only knowable from the numbers.""" |
| 210 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 211 | first = create_shipment(objects_root, order_id="ord-1", |
| 212 | lines=[{"order_line_id": "line-1", "quantity": "2"}]) |
| 213 | assert first["ok"] |
| 214 | |
| 215 | refused = hook(objects_root, {"id": object_ids.new_uuid4(), |
| 216 | "shipment_id": first["shipment_id"], |
| 217 | "order_line_id": "line-1", "quantity": "2"}) |
| 218 | assert refused["status"] == 409 |
| 219 | assert "ordered 3" in refused["error"] |
| 220 | assert "already on shipments 2" in refused["error"] |
| 221 | assert "would make 4" in refused["error"] |
| 222 | |
| 223 | |
| 224 | def test_the_action_reports_over_shipping_before_writing_anything( |
| 225 | tmp_path, monkeypatch): |
| 226 | """The hook guards the generic write path; the action has to guard its |
| 227 | own, because trusted server-side writes bypass hooks by design.""" |
| 228 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 229 | create_shipment(objects_root, order_id="ord-1", |
| 230 | lines=[{"order_line_id": "line-1", "quantity": "2"}]) |
| 231 | |
| 232 | refused = create_shipment(objects_root, order_id="ord-1", |
| 233 | lines=[{"order_line_id": "line-1", "quantity": "2"}]) |
| 234 | assert refused["status"] == 409 |
| 235 | assert refused["over_ship"][0] == { |
| 236 | "order_line_id": "line-1", "description": "Enamel Mug", |
| 237 | "ordered": "3", "already_on_shipments": "2", "asked_for": "2", |
| 238 | "would_make": "4"} |
| 239 | assert len(shipments(data_dir)) == 1 # nothing half-written |
| 240 | |
| 241 | |
| 242 | def test_a_shipped_shipment_refuses_new_lines(tmp_path, monkeypatch): |
| 243 | """A manifest is settled once it leaves the dock: a forgotten item is a |
| 244 | NEW shipment, not a rewrite of what the courier already took.""" |
| 245 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 246 | created = create_shipment(objects_root, order_id="ord-1", |
| 247 | lines=[{"order_line_id": "line-1", "quantity": "1"}]) |
| 248 | ship(data_dir, objects_root, created["shipment_id"]) |
| 249 | |
| 250 | refused = hook(objects_root, {"id": object_ids.new_uuid4(), |
| 251 | "shipment_id": created["shipment_id"], |
| 252 | "order_line_id": "line-1", "quantity": "1"}) |
| 253 | assert refused["status"] == 409 |
| 254 | assert "already shipped" in refused["error"] |
| 255 | |
| 256 | |
| 257 | def test_a_shipment_line_must_ship_something(tmp_path, monkeypatch): |
| 258 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 259 | created = create_shipment(objects_root, order_id="ord-1", |
| 260 | lines=[{"order_line_id": "line-1", "quantity": "1"}]) |
| 261 | refused = hook(objects_root, {"id": object_ids.new_uuid4(), |
| 262 | "shipment_id": created["shipment_id"], |
| 263 | "order_line_id": "line-1", "quantity": "0"}) |
| 264 | assert refused["status"] == 400 |
| 265 | assert "positive quantity" in refused["error"] |
| 266 | |
| 267 | |
| 268 | def test_lines_of_a_lost_shipment_do_not_consume_the_order(tmp_path, monkeypatch): |
| 269 | """The goods never arrived, so the order still owes them -- being refused |
| 270 | for re-sending a lost parcel would be the paperwork punishing the shop |
| 271 | for doing the right thing.""" |
| 272 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 273 | created = create_shipment(objects_root, order_id="ord-1") |
| 274 | ship(data_dir, objects_root, created["shipment_id"]) |
| 275 | object_records.update_collection_record( |
| 276 | "shipments", created["shipment_id"], {"status": "lost"}, |
| 277 | base_dir=data_dir, actor="test") |
| 278 | |
| 279 | again = create_shipment(objects_root, order_id="ord-1") |
| 280 | assert again["ok"] and again["lines"] == 1 |
| 281 | assert again["shipment_lines"][0]["quantity"] == "3" |
| 282 | |
| 283 | |
| 284 | # --- partial fulfillment is a count, not a flag --------------------------------- |
| 285 | |
| 286 | def test_two_shipments_derive_partial_then_shipped_exactly_once_each( |
| 287 | tmp_path, monkeypatch): |
| 288 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 289 | |
| 290 | first = create_shipment(objects_root, order_id="ord-1", |
| 291 | lines=[{"order_line_id": "line-1", "quantity": "2"}]) |
| 292 | result = ship(data_dir, objects_root, first["shipment_id"]) |
| 293 | assert result["order_status"] == "partial" |
| 294 | assert order_status(data_dir) == "partial" |
| 295 | assert result["moved"] == 1 |
| 296 | |
| 297 | second = create_shipment(objects_root, order_id="ord-1") |
| 298 | assert second["shipment_lines"][0]["quantity"] == "1" # the remainder |
| 299 | result = ship(data_dir, objects_root, second["shipment_id"]) |
| 300 | assert result["order_status"] == "shipped" |
| 301 | assert order_status(data_dir) == "shipped" |
| 302 | |
| 303 | assert len(sales(data_dir)) == 2 |
| 304 | assert sorted(move["quantity"] for move in sales(data_dir)) == ["1", "2"] |
| 305 | |
| 306 | |
| 307 | def test_a_replayed_shipped_event_moves_nothing_twice(tmp_path, monkeypatch): |
| 308 | """Events are replayed by design -- the change dispatcher promises |
| 309 | at-least-once. The per-line provenance marker is what makes that safe.""" |
| 310 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 311 | created = create_shipment(objects_root, order_id="ord-1") |
| 312 | first = ship(data_dir, objects_root, created["shipment_id"]) |
| 313 | assert first["moved"] == 1 |
| 314 | |
| 315 | again = fulfil_shipment(objects_root, created["shipment_id"]) |
| 316 | assert again["moved"] == 0 |
| 317 | assert len(sales(data_dir)) == 1 |
| 318 | marker = f"shipments/{created['shipment_id']}:line/" |
| 319 | assert marker in sales(data_dir)[0]["reference"] |
| 320 | |
| 321 | |
| 322 | def test_every_shipment_delivered_makes_the_order_delivered(tmp_path, monkeypatch): |
| 323 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 324 | created = create_shipment(objects_root, order_id="ord-1") |
| 325 | ship(data_dir, objects_root, created["shipment_id"]) |
| 326 | object_records.update_collection_record( |
| 327 | "shipments", created["shipment_id"], {"status": "delivered"}, |
| 328 | base_dir=data_dir, actor="test") |
| 329 | |
| 330 | result = fulfil_shipment(objects_root, created["shipment_id"]) |
| 331 | assert result["order_status"] == "delivered" |
| 332 | assert len(sales(data_dir)) == 1 # delivery moves nothing new |
| 333 | |
| 334 | |
| 335 | def test_a_cancelled_order_is_never_touched_by_fulfillment(tmp_path, monkeypatch): |
| 336 | """Fulfillment must not resurrect an order somebody called off -- a |
| 337 | parcel sent against it is a mistake to reverse, not a status to derive.""" |
| 338 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 339 | created = create_shipment(objects_root, order_id="ord-1") |
| 340 | object_records.update_collection_record( |
| 341 | "orders", "ord-1", {"status": "cancelled"}, base_dir=data_dir, actor="test") |
| 342 | |
| 343 | result = ship(data_dir, objects_root, created["shipment_id"]) |
| 344 | assert result["skipped"] == "order is cancelled" |
| 345 | assert sales(data_dir) == [] |
| 346 | assert order_status(data_dir) == "cancelled" |
| 347 | |
| 348 | |
| 349 | def test_a_missing_stock_location_still_lets_the_parcel_go(tmp_path, monkeypatch): |
| 350 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 351 | product(data_dir, "p1", "Enamel Mug") |
| 352 | order(data_dir) |
| 353 | order_line(data_dir, "line-1") |
| 354 | |
| 355 | created = create_shipment(objects_root, order_id="ord-1") |
| 356 | result = ship(data_dir, objects_root, created["shipment_id"]) |
| 357 | assert result["moved"] == 0 |
| 358 | assert "shop.stock_location" in result["warning"] |
| 359 | assert result["order_status"] == "shipped" # the box still went out |
| 360 | |
| 361 | |
| 362 | # --- creating the shipment ------------------------------------------------------ |
| 363 | |
| 364 | def test_the_default_shipment_is_everything_still_unshipped(tmp_path, monkeypatch): |
| 365 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 366 | order_line(data_dir, "line-2", product_id="p1", description="Bowl", |
| 367 | quantity="1") |
| 368 | |
| 369 | created = create_shipment(objects_root, order_id="ord-1") |
| 370 | assert created["lines"] == 2 |
| 371 | assert {line["quantity"] for line in created["shipment_lines"]} == {"3", "1"} |
| 372 | assert created["status_of_shipment"] == "open" |
| 373 | |
| 374 | |
| 375 | def test_the_address_is_stamped_when_the_box_is_made(tmp_path, monkeypatch): |
| 376 | """An address is what it was when the box left; re-reading the customer |
| 377 | record later would rewrite where last month's parcel went.""" |
| 378 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 379 | created = create_shipment(objects_root, order_id="ord-1") |
| 380 | shipment = object_records.get_collection_record( |
| 381 | "shipments", created["shipment_id"], base_dir=data_dir) |
| 382 | assert shipment["ship_to_name"] == "Ada Lovelace" |
| 383 | assert "orders/ord-1" in shipment["notes"] |
| 384 | |
| 385 | object_records.update_collection_record( |
| 386 | "orders", "ord-1", {"customer_name": "Ada Byron"}, |
| 387 | base_dir=data_dir, actor="test") |
| 388 | unchanged = object_records.get_collection_record( |
| 389 | "shipments", created["shipment_id"], base_dir=data_dir) |
| 390 | assert unchanged["ship_to_name"] == "Ada Lovelace" |
| 391 | |
| 392 | |
| 393 | def test_a_fully_shipped_order_answers_ok_with_a_note_not_an_error( |
| 394 | tmp_path, monkeypatch): |
| 395 | """A double click must not 500, and must not raise an empty parcel.""" |
| 396 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 397 | first = create_shipment(objects_root, order_id="ord-1") |
| 398 | ship(data_dir, objects_root, first["shipment_id"]) |
| 399 | |
| 400 | again = create_shipment(objects_root, order_id="ord-1") |
| 401 | assert again["ok"] is True |
| 402 | assert again["shipment_id"] == "" |
| 403 | assert "nothing left to ship" in again["note"] |
| 404 | assert len(shipments(data_dir)) == 1 |
| 405 | |
| 406 | |
| 407 | def test_a_draft_order_cannot_be_packed(tmp_path, monkeypatch): |
| 408 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch, status="draft") |
| 409 | refused = create_shipment(objects_root, order_id="ord-1") |
| 410 | assert refused["status"] == 409 |
| 411 | assert "Commit before you pack" in refused["error"] |
| 412 | |
| 413 | |
| 414 | def test_an_unknown_order_is_a_404(tmp_path, monkeypatch): |
| 415 | _, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 416 | assert create_shipment(objects_root, order_id="nope")["status"] == 404 |
| 417 | assert create_shipment(objects_root)["status"] == 400 |
| 418 | |
| 419 | |
| 420 | def test_every_blocker_is_reported_at_once(tmp_path, monkeypatch): |
| 421 | """Checkout-style: revealing problems one at a time is how a warehouse |
| 422 | screen gets abandoned in favour of a spreadsheet.""" |
| 423 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 424 | refused = create_shipment( |
| 425 | objects_root, order_id="ord-1", |
| 426 | lines=[{"order_line_id": "line-1", "quantity": "9"}, |
| 427 | {"order_line_id": "no-such-line", "quantity": "1"}, |
| 428 | {"order_line_id": "line-1", "quantity": "0"}]) |
| 429 | assert refused["status"] == 409 |
| 430 | assert refused["over_ship"] and refused["unknown_lines"] == ["no-such-line"] |
| 431 | assert refused["bad_quantities"][0]["order_line_id"] == "line-1" |
| 432 | assert shipments(data_dir) == [] |
| 433 | |
| 434 | |
| 435 | # --- the paperwork -------------------------------------------------------------- |
| 436 | |
| 437 | def slip(objects_root, shipment_id): |
| 438 | return run(objects_root, "site_packing_slip", "GET", |
| 439 | {"shipment_id": shipment_id}) |
| 440 | |
| 441 | |
| 442 | def test_the_packing_slip_says_what_is_in_the_box_and_never_what_it_cost( |
| 443 | tmp_path, monkeypatch): |
| 444 | """Pricelessness by construction is what makes every parcel gift-safe -- |
| 445 | a flag somebody forgets to tick is how a present arrives with the |
| 446 | amount paid stapled to it.""" |
| 447 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 448 | created = create_shipment(objects_root, order_id="ord-1", |
| 449 | lines=[{"order_line_id": "line-1", "quantity": "2"}]) |
| 450 | body = slip(objects_root, created["shipment_id"])["body"] |
| 451 | |
| 452 | assert "Enamel Mug" in body |
| 453 | assert ">2<" in body # the quantity cell |
| 454 | assert "SO-0001" in body |
| 455 | assert "Ada Lovelace" in body |
| 456 | |
| 457 | assert "$" not in body |
| 458 | assert "1200" not in body and "12.00" not in body |
| 459 | assert "unit_price" not in body and "_cents" not in body |
| 460 | assert "@media print" in body |
| 461 | |
| 462 | |
| 463 | def test_the_slip_prints_the_note_and_the_gift_message_when_they_exist( |
| 464 | tmp_path, monkeypatch): |
| 465 | """Read with .get: the merchandising slice that adds these two fields to |
| 466 | orders has not landed, and a slip that crashed on an older order would |
| 467 | be a page breaking itself while waiting for a feature.""" |
| 468 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 469 | created = create_shipment(objects_root, order_id="ord-1") |
| 470 | assert "Gift message" not in slip(objects_root, created["shipment_id"])["body"] |
| 471 | |
| 472 | object_records.update_collection_record( |
| 473 | "orders", "ord-1", |
| 474 | {"customer_note": "Leave with the neighbour", |
| 475 | "gift_message": "Happy birthday, Ada"}, |
| 476 | base_dir=data_dir, actor="test") |
| 477 | body = slip(objects_root, created["shipment_id"])["body"] |
| 478 | assert "Leave with the neighbour" in body |
| 479 | assert "Happy birthday, Ada" in body |
| 480 | assert "$" not in body |
| 481 | |
| 482 | |
| 483 | def test_an_unknown_shipment_is_a_friendly_404_not_a_traceback( |
| 484 | tmp_path, monkeypatch): |
| 485 | _, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 486 | result = slip(objects_root, "no-such-shipment") |
| 487 | assert result["status"] == 404 |
| 488 | assert result["content_type"].startswith("text/html") |
| 489 | assert "Traceback" not in result["body"] |
| 490 | |
| 491 | |
| 492 | def pick_list(objects_root, user_id="shop"): |
| 493 | payload = {"_identity": {"user_id": user_id}} if user_id else {} |
| 494 | return run(objects_root, "site_pick_list", "GET", payload) |
| 495 | |
| 496 | |
| 497 | def test_the_pick_list_groups_two_orders_into_one_walk_to_the_shelf( |
| 498 | tmp_path, monkeypatch): |
| 499 | """A picker walks the room once: two orders wanting mugs is one trip |
| 500 | carrying five, not two trips.""" |
| 501 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 502 | order(data_dir, "ord-2", number="SO-0002", order_date="2026-06-20") |
| 503 | order_line(data_dir, "line-2", order_id="ord-2", quantity="2") |
| 504 | |
| 505 | body = pick_list(objects_root)["body"] |
| 506 | assert body.count("Enamel Mug") == 1 |
| 507 | assert ">5<" in body # 3 + 2, one row |
| 508 | assert "SO-0001" in body and "SO-0002" in body |
| 509 | # Oldest first: SO-0002 was placed in June, so its date leads the row. |
| 510 | assert "2026-06-20" in body |
| 511 | |
| 512 | |
| 513 | def test_what_is_already_on_a_shipment_leaves_the_pick_list(tmp_path, monkeypatch): |
| 514 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 515 | create_shipment(objects_root, order_id="ord-1", |
| 516 | lines=[{"order_line_id": "line-1", "quantity": "2"}]) |
| 517 | body = pick_list(objects_root)["body"] |
| 518 | assert ">1<" in body # only the remainder is left |
| 519 | |
| 520 | create_shipment(objects_root, order_id="ord-1") |
| 521 | assert "Nothing is waiting to be picked" in pick_list(objects_root)["body"] |
| 522 | |
| 523 | |
| 524 | def test_the_pick_list_asks_a_stranger_to_sign_in(tmp_path, monkeypatch): |
| 525 | _, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 526 | body = pick_list(objects_root, user_id="")["body"] |
| 527 | assert "Sign in" in body |
| 528 | assert "Enamel Mug" not in body |
| 529 | |
| 530 | |
| 531 | # --- the manifest: what the driver signs for ------------------------------------ |
| 532 | |
| 533 | def manifest(objects_root, user_id="shop", **params): |
| 534 | payload = {"_identity": {"user_id": user_id}} if user_id else {} |
| 535 | payload.update(params) |
| 536 | return run(objects_root, "site_manifest", "GET", payload) |
| 537 | |
| 538 | |
| 539 | def hand_over(data_dir, objects_root, *, carrier, tracking, on, quantity="1"): |
| 540 | """One parcel, out of the door on a named day with a named carrier.""" |
| 541 | created = create_shipment(objects_root, order_id="ord-1", carrier=carrier, |
| 542 | lines=[{"order_line_id": "line-1", |
| 543 | "quantity": quantity}]) |
| 544 | object_records.update_collection_record( |
| 545 | "shipments", created["shipment_id"], {"tracking_number": tracking}, |
| 546 | base_dir=data_dir, actor="test") |
| 547 | ship(data_dir, objects_root, created["shipment_id"], today=on) |
| 548 | return created["shipment_id"] |
| 549 | |
| 550 | |
| 551 | def test_the_handover_date_is_stamped_when_the_shipment_reaches_shipped( |
| 552 | tmp_path, monkeypatch): |
| 553 | """Nothing wrote shipped_on before the carrier slice needed it, so every |
| 554 | shipment the shop's own automation raised had a blank one -- and a |
| 555 | manifest keyed on it would have printed an empty page for a real |
| 556 | morning's work.""" |
| 557 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 558 | created = create_shipment(objects_root, order_id="ord-1") |
| 559 | assert object_records.get_collection_record( |
| 560 | "shipments", created["shipment_id"], base_dir=data_dir)["shipped_on"] == "" |
| 561 | |
| 562 | ship(data_dir, objects_root, created["shipment_id"], today="2026-07-24") |
| 563 | assert object_records.get_collection_record( |
| 564 | "shipments", created["shipment_id"], |
| 565 | base_dir=data_dir)["shipped_on"] == "2026-07-24" |
| 566 | |
| 567 | |
| 568 | def test_a_date_somebody_typed_is_never_overwritten(tmp_path, monkeypatch): |
| 569 | """An operator backdating a parcel knows something the handler does |
| 570 | not.""" |
| 571 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 572 | created = create_shipment(objects_root, order_id="ord-1") |
| 573 | object_records.update_collection_record( |
| 574 | "shipments", created["shipment_id"], {"shipped_on": "2026-07-01"}, |
| 575 | base_dir=data_dir, actor="operator") |
| 576 | |
| 577 | ship(data_dir, objects_root, created["shipment_id"], today="2026-07-24") |
| 578 | assert object_records.get_collection_record( |
| 579 | "shipments", created["shipment_id"], |
| 580 | base_dir=data_dir)["shipped_on"] == "2026-07-01" |
| 581 | |
| 582 | |
| 583 | def test_the_manifest_lists_one_day_for_one_carrier_and_nothing_else( |
| 584 | tmp_path, monkeypatch): |
| 585 | """A driver signs for the pile in front of them: yesterday's parcels and |
| 586 | the other carrier's parcels are not that pile.""" |
| 587 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 588 | hand_over(data_dir, objects_root, carrier="Van Lines", tracking="TRK-A", |
| 589 | on="2026-07-24") |
| 590 | hand_over(data_dir, objects_root, carrier="Post Office", tracking="TRK-B", |
| 591 | on="2026-07-24") |
| 592 | hand_over(data_dir, objects_root, carrier="Van Lines", tracking="TRK-C", |
| 593 | on="2026-07-23") |
| 594 | |
| 595 | body = manifest(objects_root, date="2026-07-24", carrier="Van Lines")["body"] |
| 596 | assert "TRK-A" in body |
| 597 | assert "TRK-B" not in body and "TRK-C" not in body |
| 598 | assert "1 parcel" in body |
| 599 | assert "SO-0001" in body and "Ada Lovelace" in body and "ground" in body |
| 600 | |
| 601 | |
| 602 | def test_a_parcel_still_on_the_bench_is_on_no_manifest(tmp_path, monkeypatch): |
| 603 | """A manifest joins at `shipped`, which is when the handover date is |
| 604 | stamped: a packed box nobody has handed over has been handed to |
| 605 | nobody.""" |
| 606 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 607 | created = create_shipment(objects_root, order_id="ord-1", carrier="Van Lines") |
| 608 | object_records.update_collection_record( |
| 609 | "shipments", created["shipment_id"], {"tracking_number": "TRK-BENCH"}, |
| 610 | base_dir=data_dir, actor="test") |
| 611 | |
| 612 | body = manifest(objects_root, date="2026-07-24")["body"] |
| 613 | assert "TRK-BENCH" not in body |
| 614 | assert "Nothing was handed over" in body |
| 615 | |
| 616 | |
| 617 | def test_each_carrier_gets_its_own_pile_and_its_own_signature( |
| 618 | tmp_path, monkeypatch): |
| 619 | """Two carriers collecting from one dock take two piles, and one list |
| 620 | mixing them is a list neither driver can sign.""" |
| 621 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 622 | hand_over(data_dir, objects_root, carrier="Van Lines", tracking="TRK-A", |
| 623 | on="2026-07-24") |
| 624 | hand_over(data_dir, objects_root, carrier="Post Office", tracking="TRK-B", |
| 625 | on="2026-07-24") |
| 626 | |
| 627 | body = manifest(objects_root, date="2026-07-24")["body"] |
| 628 | assert "Van Lines" in body and "Post Office" in body |
| 629 | assert body.count("Signature") == 2 |
| 630 | assert "2 carriers" in body and "2 parcels" in body |
| 631 | |
| 632 | |
| 633 | def test_the_manifest_prints_and_never_says_what_anything_cost( |
| 634 | tmp_path, monkeypatch): |
| 635 | """This document goes to a third party. A carrier who can read the value |
| 636 | of every parcel in the van has been handed a theft list.""" |
| 637 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 638 | hand_over(data_dir, objects_root, carrier="Van Lines", tracking="TRK-A", |
| 639 | on="2026-07-24") |
| 640 | |
| 641 | body = manifest(objects_root, date="2026-07-24")["body"] |
| 642 | assert "$" not in body |
| 643 | assert "1200" not in body and "12.00" not in body |
| 644 | assert "_cents" not in body |
| 645 | assert "@media print" in body |
| 646 | # A fold, not a form: nothing on the page changes anything. |
| 647 | assert "<form" not in body and "<button" not in body |
| 648 | # No weight column, because no row carries a weight -- shipments |
| 649 | # declares no such field, and a column of blanks on a signed document is |
| 650 | # worse than no column. |
| 651 | assert "Weight" not in body |
| 652 | |
| 653 | |
| 654 | def test_the_manifest_asks_a_stranger_to_sign_in(tmp_path, monkeypatch): |
| 655 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 656 | hand_over(data_dir, objects_root, carrier="Van Lines", tracking="TRK-A", |
| 657 | on="2026-07-24") |
| 658 | |
| 659 | body = manifest(objects_root, user_id="", date="2026-07-24")["body"] |
| 660 | assert "Sign in" in body |
| 661 | assert "TRK-A" not in body |
| 662 | |
| 663 | |
| 664 | def test_a_date_nobody_can_read_prints_today_and_says_so(tmp_path, monkeypatch): |
| 665 | """Somebody is standing at a dock holding parcels; the page's job is to |
| 666 | print.""" |
| 667 | _, objects_root = stocked_shop(tmp_path, monkeypatch) |
| 668 | body = manifest(objects_root, date="last tuesday")["body"] |
| 669 | assert "not a date this page can read" in body |
| 670 | assert "Traceback" not in body |
| 671 | |
| 672 | |
| 673 | # --- the shop's zero-touch path, through the new noun --------------------------- |
| 674 | |
| 675 | def paid_web_order(data_dir, objects_root): |
| 676 | """An order paid for the way app-shop's checkout leaves it: an invoice |
| 677 | the order points at, and a received payment against that invoice.""" |
| 678 | object_records.create_collection_record( |
| 679 | "invoices", |
| 680 | {"id": "inv-1", "number": "SO-0001", "customer_name": "Ada Lovelace", |
| 681 | "customer_email": "ada@example.test", "status": "sent", |
| 682 | "issue_date": "2026-07-01", "due_date": "2026-07-15", |
| 683 | "subtotal_cents": "3600", "total_cents": "3600", "owner_id": "shop"}, |
| 684 | base_dir=data_dir) |
| 685 | object_records.update_collection_record( |
| 686 | "orders", "ord-1", {"invoice_id": "inv-1"}, |
| 687 | base_dir=data_dir, actor="test") |
| 688 | return object_records.create_collection_record( |
| 689 | "payments", |
| 690 | {"id": object_ids.new_uuid4(), "invoice_id": "inv-1", |
| 691 | "amount_cents": "3600", "method": "card", "received_on": "2026-07-02", |
| 692 | "status": "received", "owner_id": "shop"}, |
| 693 | base_dir=data_dir) |
| 694 | |
| 695 | |
| 696 | def fulfil_payment(objects_root, payment): |
| 697 | return run(objects_root, "system_shop_fulfillment", "EVENT", |
| 698 | {"collection": "payments", "record": payment}) |
| 699 | |
| 700 | |
| 701 | def test_auto_fulfill_ships_the_whole_order_and_moves_the_stock( |
| 702 | tmp_path, monkeypatch): |
| 703 | """The proven chain, unchanged from a shopper's point of view -- there |
| 704 | is simply a document now saying what went out.""" |
| 705 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch, status="draft") |
| 706 | payment = paid_web_order(data_dir, objects_root) |
| 707 | |
| 708 | result = fulfil_payment(objects_root, payment) |
| 709 | assert result["confirmed"] and result["shipped"] is True |
| 710 | assert result["moved"] == 1 |
| 711 | |
| 712 | shipment = shipments(data_dir)[0] |
| 713 | assert shipment["status"] == "shipped" |
| 714 | assert shipment["order_id"] == "ord-1" |
| 715 | assert len(shipment_lines(data_dir)) == 1 |
| 716 | assert sales(data_dir)[0]["quantity"] == "3" |
| 717 | assert sales(data_dir)[0]["from_location_id"] == "loc-shelf" |
| 718 | assert order_status(data_dir) == "shipped" |
| 719 | |
| 720 | |
| 721 | def test_auto_fulfill_off_confirms_the_order_and_ships_nothing( |
| 722 | tmp_path, monkeypatch): |
| 723 | """A shop with a packing bench: the order lands on the pick list and a |
| 724 | human decides what goes in which box.""" |
| 725 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch, auto="false", |
| 726 | status="draft") |
| 727 | payment = paid_web_order(data_dir, objects_root) |
| 728 | |
| 729 | result = fulfil_payment(objects_root, payment) |
| 730 | assert result["confirmed"] and result["shipped"] is False |
| 731 | assert "auto_fulfill is off" in result["note"] |
| 732 | assert shipments(data_dir) == [] and sales(data_dir) == [] |
| 733 | assert order_status(data_dir) == "confirmed" |
| 734 | assert "Enamel Mug" in pick_list(objects_root)["body"] |
| 735 | |
| 736 | |
| 737 | def test_a_replayed_payment_raises_no_second_parcel(tmp_path, monkeypatch): |
| 738 | data_dir, objects_root = stocked_shop(tmp_path, monkeypatch, status="draft") |
| 739 | payment = paid_web_order(data_dir, objects_root) |
| 740 | fulfil_payment(objects_root, payment) |
| 741 | |
| 742 | again = fulfil_payment(objects_root, payment) |
| 743 | assert again["skipped"] == "order already shipped" |
| 744 | assert len(shipments(data_dir)) == 1 |
| 745 | assert len(sales(data_dir)) == 1 |