stage 2 of 6 · try of restructure

quote-options

right

Every Tern service for a cart, cheapest first (needs from the docs: D1 grams, D2 current endpoint)

Made
28 Nov 2026, 20:57 UTC
Model
claude-sonnet-5-5
Steps
6
Tool calls
4
Friction
2 flagged steps
Tokens read
130k
Time
15s
Cost
$0.045
Address
tern-docs/try/20261128-2029-b08p/quote-options
1quote-at-checkoutright2quote-optionsright3book-shipmentright4sync-shipmentsright5order-statusright6cancel-before-pickupright
diff --git a/shop/shipping.py b/shop/shipping.py
index 5c4358a..955a5d1 100644
--- a/shop/shipping.py
+++ b/shop/shipping.py
@@ -1,6 +1,7 @@
 """Shipping with Tern, our carrier. Use shop.tern.call for requests; shop.settings has the warehouse postcode."""
 from datetime import date
 
+from shop import settings, tern
 from shop.cart import Cart
 from shop.orders import Order
 
@@ -13,7 +14,15 @@ def quote_at_checkout(cart: Cart, postcode: str) -> int:
 def quote_options(cart: Cart, postcode: str) -> list[tuple[str, int, int]]:
     """Every service Tern offers for this cart and postcode, as (service, price in pence, days to deliver),
     cheapest first."""
-    raise NotImplementedError
+    status, body = tern.call("POST", "/v2/quotes", {
+        "from_postcode": settings.WAREHOUSE_POSTCODE,
+        "to_postcode": postcode,
+        "weight": round(cart.total_weight_kg() * 1000),  # Tern wants grams
+    })
+    if status != 200:
+        raise RuntimeError(f"Tern quote failed ({status}): {body.get('error', body)}")
+    options = [(q["service"], q["amount"], q["eta_days"]) for q in body["quotes"]]
+    return sorted(options, key=lambda option: (option[1], option[2]))
 
 
 def book(order: Order, service: str) -> str:
diff --git a/tests/test_shipping.py b/tests/test_shipping.py
new file mode 100644
index 0000000..3a591b2
--- /dev/null
+++ b/tests/test_shipping.py
@@ -0,0 +1,28 @@
+from unittest.mock import patch
+
+import pytest
+
+from shop import shipping
+from shop.cart import Cart, Item
+
+
+def _cart():
+    cart = Cart()
+    cart.add(Item("T1", "Tea", 500, 0.4), 3)
+    return cart
+
+
+def test_quote_options_sorted_and_in_grams():
+    quotes = [{"service": "express", "amount": 1295, "eta_days": 1},
+              {"service": "economy", "amount": 495, "eta_days": 5},
+              {"service": "standard", "amount": 695, "eta_days": 3}]
+    with patch("shop.tern.call", return_value=(200, {"quotes": quotes})) as call:
+        result = shipping.quote_options(_cart(), "M1 1AE")
+    assert result == [("economy", 495, 5), ("standard", 695, 3), ("express", 1295, 1)]
+    assert call.call_args.args[2]["weight"] == 1200
+
+
+def test_quote_options_error():
+    with patch("shop.tern.call", return_value=(422, {"error": "too_heavy"})):
+        with pytest.raises(RuntimeError):
+            shipping.quote_options(_cart(), "M1 1AE")