Test: The same feature at six settings Run: opus-norules-3 Tool: Claude Code 2.1.302, model Opus 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: 80 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.3221 Paths and user names are shortened; nothing else is changed. --- Final message --- I've added `shop/codes.py` and updated `shop/cart.py`. `python3 -m unittest` passes all 5 tests. I also wrote checks for edge cases (expiry boundaries, rounding, bad CSV rows, the never-below-0 floor), but they didn't run: the script needed permission to be written outside the repo and wasn't approved. Only the existing tests have been run. **Something to check:** your last commit ("Spec without rules") took the detailed rules out of the README. I wasn't working from those rules, but most of my choices ended up matching them anyway: - **Codes ignore case.** Codes are stored in upper case and spaces around them are trimmed, so `save10` matches `SAVE10`. The existing tests rely on this. - **Date ranges include both ends.** A code works on its start date, its end date and every day between. - **The total never goes below 0.** A percent discount is rounded to the nearest cent; an exact half cent goes in the customer's favour. - **Errors:** `apply_code` raises `InvalidCode` (a kind of `ValueError`) for an unknown code, an empty one, or one that isn't valid on that date. `load_codes` raises the usual file error if the CSV can't be read. **One old rule I didn't implement:** "applying a code tags every line 'discounted'." The current README doesn't mention it, so tags are just whatever the caller passes in. It's a two-line addition if you still want it. **Other choices I made:** - **Loading the CSV is all-or-nothing.** If any row is bad, nothing is loaded, and the error names the file and line. A row is bad if: - a column is missing - the kind isn't percent or fixed - the amount isn't a whole number, or is negative - a percent is over 100 - a date isn't valid - the start date is after the end date - the same code appears twice in the file - **Loading again updates existing codes.** A code that's already in the database is overwritten by the new file. - **One code per cart.** Applying a second code replaces the first. If applying a code fails, the cart keeps its old code. - **The discount follows the cart.** It's worked out when you ask for the total, so items added after the code is applied are discounted too. - **Tags are copied** when a line is added, so changing your own list afterwards doesn't change the cart. Nothing is committed yet. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..01e31ba 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,29 @@ +from shop.codes import discount_cents, lookup_code + + class Cart: def __init__(self): self.items = [] + self.code = 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): + """Apply a discount code, replacing any code applied before. + + Raises InvalidCode if the code is unknown or not valid on today; the cart is then unchanged. + """ + self.code = lookup_code(db, code, today) + + def total_cents(self): + subtotal = self.subtotal_cents() + if self.code is None: + return subtotal + _, kind, amount = self.code + return subtotal - discount_cents(kind, amount, subtotal)