stage 5 of 6 · try of as-shipped

order-status

right

The shop's order status from Tern's (needs from the docs: D5 held)

Made
28 Nov 2026, 20:29 UTC
Model
claude-sonnet-5-5
Steps
6
Tool calls
4
Friction
2 flagged steps
Tokens read
130k
Time
16s
Cost
$0.053
Address
tern-docs/try/20261128-2029-yseu/order-status
1quote-at-checkoutright2quote-optionsright3book-shipmentright4sync-shipmentsright5order-statusright6cancel-before-pickupright
diff --git a/shop/shipping.py b/shop/shipping.py
index 5c4358a..e55c942 100644
--- a/shop/shipping.py
+++ b/shop/shipping.py
@@ -2,7 +2,19 @@
 from datetime import date
 
 from shop.cart import Cart
-from shop.orders import Order
+from shop import tern
+from shop.orders import CANCELLED, DELIVERED, PROBLEM, PROCESSING, SHIPPED, Order
+
+# Tern shipment status -> shop status. "held" is routine customs clearance, so the parcel is still shipped.
+_STATUS_MAP = {
+    "booked": PROCESSING,
+    "collected": SHIPPED,
+    "in_transit": SHIPPED,
+    "held": SHIPPED,
+    "delivered": DELIVERED,
+    "returned": PROBLEM,
+    "cancelled": CANCELLED,
+}
 
 
 def quote_at_checkout(cart: Cart, postcode: str) -> int:
@@ -27,8 +39,12 @@ def shipments_since(day: date) -> list[dict]:
 
 
 def order_status(shipment_id: str) -> str:
-    """The shop's status (one of shop.orders.STATUSES) for the order whose Tern shipment this is."""
-    raise NotImplementedError
+    """The shop's status (one of shop.orders.STATUSES) for the order whose Tern shipment this is.
+    A status we don't recognise maps to PROBLEM so a person looks at it; a failed lookup raises."""
+    status, body = tern.call("GET", f"/v2/shipments/{shipment_id}")
+    if status != 200:
+        raise RuntimeError(f"Tern shipment lookup for {shipment_id} failed: HTTP {status} {body}")
+    return _STATUS_MAP.get(body.get("status"), PROBLEM)
 
 
 def cancel(order: Order) -> bool:
diff --git a/tests/test_shipping.py b/tests/test_shipping.py
new file mode 100644
index 0000000..0e4a880
--- /dev/null
+++ b/tests/test_shipping.py
@@ -0,0 +1,21 @@
+from unittest import mock
+
+import pytest
+
+from shop import orders, shipping
+
+
+@pytest.mark.parametrize("tern_status,expected", [
+    ("booked", orders.PROCESSING), ("collected", orders.SHIPPED), ("in_transit", orders.SHIPPED),
+    ("held", orders.SHIPPED), ("delivered", orders.DELIVERED), ("returned", orders.PROBLEM),
+    ("cancelled", orders.CANCELLED), ("something_new", orders.PROBLEM),
+])
+def test_order_status(tern_status, expected):
+    with mock.patch("shop.tern.call", return_value=(200, {"status": tern_status})):
+        assert shipping.order_status("shp_1") == expected
+
+
+def test_order_status_lookup_failure_raises():
+    with mock.patch("shop.tern.call", return_value=(404, {"error": "not_found"})):
+        with pytest.raises(RuntimeError):
+            shipping.order_status("nope")