Test: The same feature at six settings Run: opus-low-rules-2 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: 21 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.0980 Paths and user names are shortened; nothing else is changed. --- Final message --- I've added discount codes: a new module `shop/codes.py` and the changes to `shop/cart.py`. All 5 tests pass with `python3 -m unittest`. **`shop/codes.py`** - `InvalidCode` is the error for unknown or expired codes. - `open_db(path=":memory:")` returns an sqlite3 connection with the `codes` table created. - `load_codes(db, csv_path)` reads the CSV and stores each code upper-cased. A file it can't read raises the normal file error. It also rejects any kind other than `percent` or `fixed`. - A helper, `find_code`, looks a code up case-insensitively and checks the date range, counting both the start and end days. **`shop/cart.py`** - `add()` now takes `tags`, and each line in `items` is `(sku, price, qty, tags)`. The cart keeps its own copy of the tags list, so changing the list you passed in later doesn't affect the cart. - `apply_code()` raises `InvalidCode` for unknown or expired codes and tags every line `"discounted"` (without adding it twice). - `total_cents()` returns the subtotal minus the discount and never goes below 0. Decisions the README didn't settle: - **Rounding:** a percent discount is rounded down to whole cents, which favours the shop by at most a cent. - **Several codes:** applying a second code replaces the first rather than adding to it. - **Loading the same code twice:** the later row overwrites the earlier one, whether it's a repeat in one file or a later file. - **Lines added after a code:** they count toward the discount, but they don't get the `"discounted"` tag. The existing tests don't cover expired codes, the start and end days themselves, a total floored at 0, or tagging. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..5345a74 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.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 + if kind == "percent": + off = subtotal * amount // 100 + else: + off = amount + return max(0, subtotal - off)