Test: The same feature at six settings Run: opus-norules-1 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: 87 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.3399 Paths and user names are shortened; nothing else is changed. --- Final message --- I added discount codes in a new `shop/codes.py` and updated `shop/cart.py`. All 5 tests pass with `python3 -m unittest`. I also wrote a script to test edge cases (date boundaries, total going below 0, rounding, bad CSV rows), but it needed write permission outside the repo, which was refused, so those cases haven't been run. **Where the rules came from:** your latest commit ("Spec without rules") took the detailed rules out of the README. Where the current README says nothing, I followed the earlier version from commit 3022485: - Codes don't depend on case (`save10` matches `SAVE10`) and are stored in upper case. - A code works from its start date through its end date, both days included. - The total is whole cents and never goes below 0. - `load_codes` raises an error if it can't read the file. `apply_code` raises `InvalidCode` for an unknown or expired code. **Decisions I made myself:** - **Rounding:** a percent discount is rounded to the nearest cent, with half a cent rounding up. For example, 15% off 999 is 150 off. - **One code per cart:** applying a second code replaces the first. The discount is worked out each time `total_cents()` is called, so items added after the code still get it. - **Loading is all-or-nothing:** any bad row (unknown kind, negative or non-whole amount, a percent over 100, a bad date, start after end) raises `ValueError` with the line number, and nothing from that file is loaded. Loading a code that already exists overwrites it. - **Tags:** `add` stores its own copy of the tags list, so changing the list you passed in later doesn't affect the cart. **Not implemented:** the earlier README also said applying a code adds a "discounted" tag to every line. The current README doesn't mention it, so I left it out; it's a small change if you want it. Nothing is committed yet. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..874bea9 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) + + def total_cents(self): + subtotal = self.subtotal_cents() + if self.discount is None: + return subtotal + kind, amount = self.discount + if kind == "percent": + off = (subtotal * amount + 50) // 100 # nearest cent, half up + else: + off = amount + return max(subtotal - off, 0)