Test: The same feature at six settings Run: opus-low-rules-3 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: 19 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.0984 Paths and user names are shortened; nothing else is changed. --- Final message --- I've added discount codes: a new `shop/codes.py` and the changes to `shop/cart.py`. All 5 tests pass with `python3 -m unittest`. **`shop/codes.py`** - `open_db(path=":memory:")` opens an sqlite database and creates the codes table. - `load_codes(db, csv_path)` reads the CSV and stores each code upper-case. A file it can't read raises the normal Python file error, which counts as the error the README asks for. A row whose kind isn't `percent` or `fixed` raises a `ValueError`. - `InvalidCode` is raised for a code that doesn't exist or isn't valid on the given date. The start and end dates both count as valid days, so a code that hasn't started yet is also rejected. **`shop/cart.py`** - `add` now takes `tags`, and each line in `items` is a 4-tuple with its own tags list. - `apply_code` stores the discount and adds the "discounted" tag to every line. Applying a second code replaces the first. - `total_cents` takes the discount off the subtotal and never goes below 0. Decisions for you: - **Percent rounding:** a percentage discount that doesn't come out to whole cents is rounded down, so the customer pays the extra fraction of a cent. The README doesn't say which way to round; change this if you want the customer to get the benefit instead. - **Lines added after a code:** only lines in the cart when the code is applied get the "discounted" tag. The discount itself still applies to the whole total, including lines added later. The tests only cover a percent code, a fixed code and an unknown code. Expired codes, the never-below-0 rule, tags and unreadable files aren't tested yet. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..7606dce 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(subtotal - off, 0)