Test: The same feature at six settings Run: opus-low-rules-1 Tool: Claude Code 2.1.302, model Opus 5.5 (--effort low) 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: 26 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.1032 Paths and user names are shortened; nothing else is changed. --- Final message --- I've added discount codes: a new `shop/codes.py` and changes to `shop/cart.py`. All 5 tests pass with `python3 -m unittest`. **`shop/codes.py`** - `open_db()` opens an sqlite connection and creates the codes table. - `load_codes()` reads the CSV and stores each code upper-case, overwriting a code that's already there. A file it can't read raises an error, as the README asks. It also rejects any `kind` other than `percent` or `fixed`. - A lookup checks the code ignoring case and only matches if today falls between the start and end dates, both included. Otherwise it raises `InvalidCode`. **`shop/cart.py`** - `add()` now takes `tags=` and keeps its own copy of the list, so changing your list afterwards doesn't affect the cart. - `apply_code()` checks the code and tags every line "discounted". - `total_cents()` takes the discount off the subtotal and never goes below 0. Decisions worth checking: - **Rounding:** for percent codes I round the discount down to whole cents, so any rounding goes in the shop's favour. Say if you'd prefer normal rounding. - **Codes not started yet:** the README only mentions unknown or expired codes, but I also reject codes whose start date is still in the future, since they're outside their valid dates. - **Lines added later:** if you add a line after applying a code, the discount still covers it, but that line isn't tagged "discounted". - **One code per cart:** applying a second code replaces the first. The existing tests only cover a percent code, a fixed code and an unknown code. Nothing yet tests expired codes, the both-days-included dates, the 0 floor, tags or an unreadable file. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..1d94c2e 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,32 @@ +from shop.codes import find_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): + self.code = 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.code is None: + return subtotal + kind, amount = self.code + if kind == "percent": + discount = subtotal * amount // 100 + else: + discount = amount + return max(0, subtotal - discount)