Test: The same feature at six settings Run: sonnet-norules-2 Tool: Claude Code 2.1.302, model Sonnet 5.5 (Default effort) Condition: Only the interface written Prompt: Implement discount codes as described in README.md: a new module shop/codes.py and the changes to shop/cart.py. Run the tests with python3 -m unittest. Time: 14 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.0479 Paths and user names are shortened; nothing else is changed. --- Final message --- Discount codes are in and all 5 tests pass with `python3 -m unittest`. - **`shop/codes.py`:** It defines `InvalidCode`, `open_db` and `load_codes` as the README describes, plus a `find_code` helper that `Cart` uses to look up a code. - **`shop/cart.py`:** - `add` takes `tags` and stores each item as `(sku, price, qty, tags)`, with `tags` as a list. - `apply_code(db, code, today)` raises `InvalidCode` if the code is unknown or outside its date range. - `total_cents()` applies the code to the subtotal. The README doesn't say how some cases should behave, so I chose: - **Case:** Codes are case-insensitive, which the `save10` test needs. - **Date range:** The `starts` and `ends` dates are both inclusive. - **Fixed amounts:** A fixed discount can't take the total below zero. - **Percent rounding:** A percent discount is rounded down to whole cents. - **Reloading:** Loading a CSV again overwrites any code that already exists. - **Tags:** Tags are stored but don't affect discounts yet. I didn't commit anything. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..8f2c3ca 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,26 @@ +from shop.codes import find_code + + class Cart: def __init__(self): self.items = [] + self.discount = None - def add(self, sku, unit_price_cents, qty=1): + def add(self, sku, unit_price_cents, qty=1, tags=None): if qty < 1: raise ValueError("qty must be at least 1") - self.items.append((sku, unit_price_cents, qty)) + self.items.append((sku, unit_price_cents, qty, list(tags) if tags else [])) def subtotal_cents(self): - return sum(price * qty for _, price, qty in self.items) + return sum(price * qty for _, price, qty, _ in self.items) + + def apply_code(self, db, code, today): + self.discount = find_code(db, code, today) + + def total_cents(self): + subtotal = self.subtotal_cents() + if self.discount is None: + return subtotal + kind, amount = self.discount + off = subtotal * amount // 100 if kind == "percent" else amount + return max(subtotal - off, 0)