Test: The same feature at six settings Run: sonnet-rules-2 Tool: Claude Code 2.1.302, model Sonnet 5.5 (Default effort) Condition: Rules written in README 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: 15 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.0502 Paths and user names are shortened; nothing else is changed. --- Final message --- I added `shop/codes.py` and changed `shop/cart.py` as the README describes. `python3 -m unittest` passes all 5 tests, but I only ran the existing tests and added none. Nothing is committed. - **`shop/codes.py`:** - It has `InvalidCode`, `open_db` and `load_codes`, plus a small `lookup` helper that `Cart` uses. - Codes are stored upper-case, and the start and end dates are both included. - A CSV file that can't be read raises an error. - A bad `kind` or a bad date in the CSV also raises an error, and nothing from the file is loaded in that case. - Loading a code that already exists replaces it. - **`shop/cart.py`:** - `add` now takes `tags`, and items are `(sku, price, qty, tags)` tuples. - `apply_code` raises `InvalidCode` for an unknown or expired code. Otherwise it tags every line "discounted". - `total_cents` takes the discount off the subtotal and never goes below 0. The README doesn't say how to round a percent discount, so I round the discount amount down to a whole cent. That gives the customer slightly less off, for example 10% of 1005 cents takes off 100 rather than 101. If you'd rather round to nearest, it's a one-line change in `total_cents`. Two things the spec leaves open: a code applied before more lines are added won't tag those later lines, and applying a second code replaces the first. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..9f48300 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,29 @@ +from shop.codes import lookup + + 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 or []))) 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 = lookup(db, code, today) + for _, _, _, tags in self.items: + if "discounted" not in tags: + tags.append("discounted") + + 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(0, subtotal - off)