Test: The same feature at six settings Run: sonnet-rules-1 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: 13 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.0519 Paths and user names are shortened; nothing else is changed. --- Final message --- I added `shop/codes.py` and updated `shop/cart.py`, and `python3 -m unittest` passes all 5 tests. Those tests don't cover expired codes, date boundaries, tags or percent rounding, so I haven't exercised those paths. - **`shop/codes.py`**: - It defines `InvalidCode`, `open_db` and `load_codes`. - Codes are stored upper-case and looked up case-insensitively. A code is valid from its start date through its end date, both included. - A CSV file that can't be read raises the underlying error. - **`shop/cart.py`**: - `add` takes `tags`, and `items` now holds `(sku, price, qty, tags)` tuples. - `apply_code` raises `InvalidCode` for an unknown or expired code. Otherwise it tags every line "discounted". - `total_cents` returns the subtotal minus the discount, never below 0. - **Not in the README:** - **Percent rounding:** a percent discount is rounded to the nearest cent, with halves rounding up. The README only says "whole cents", so say if you'd rather round the other way. - **Applying a second code:** it replaces the first one. - **Lines added after a code:** they don't get the "discounted" tag. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..24aafaa 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,29 @@ +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 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 = find_code(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 + 50) // 100 if kind == "percent" else amount + return max(0, subtotal - off)