Test: The same feature at six settings Run: opus-rules-2 Tool: Claude Code 2.1.302, model Opus 5.5 (Default effort) 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: 114 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.3838 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`, including the 3 in `tests/test_codes.py`. I haven't committed anything. My extra edge-case checks didn't run: running that script was declined, so I deleted it. That leaves the date boundaries, rounding, the floor at 0, and the bad-CSV handling below untested beyond the three existing tests. **`shop/codes.py`** - `open_db(path=":memory:")` opens sqlite and creates a `codes` table, with `code` as the primary key. - `load_codes(db, csv_path)`: - Codes are stored upper-case. - A file it can't open or read raises the normal error. - A badly formed row raises `ValueError` with the file and line number. That covers a missing column, an unknown kind, an amount that isn't a whole number or is negative, a bad date, or a start date after the end date. - The whole file is checked before anything is saved, so a bad file loads nothing. - `find_code(db, code, today)` is a helper `Cart.apply_code` uses. It ignores case and surrounding spaces, and raises `InvalidCode` for an unknown code or a date outside start–end (both days count). That includes a code whose start date hasn't come yet. **`shop/cart.py`** - `add(..., tags=None)` saves each line as `(sku, price, qty, tags)`. It keeps its own copy of `tags`, so the list you pass in isn't changed later. - `apply_code` looks the code up before changing anything, so an invalid code leaves the cart as it was. It then tags every line "discounted", without adding the tag twice. - `total_cents()` returns the subtotal less the discount, never below 0. **Choices the README doesn't settle:** - **Percent rounding:** the discount is calculated in whole numbers and rounded to the nearest cent, with half a cent rounding up. For example, 50% off 3¢ takes 2¢ off. - **Loading the same code again:** a later row replaces the earlier one, so reloading a CSV updates the codes rather than failing. - **Lines added after a code:** the discount covers the full subtotal at the time `total_cents()` is called, but those new lines don't get the "discounted" tag. The README only says lines are tagged when the code is applied. Tell me if new lines should be tagged too. - **A second code** replaces the first. - **Percent over 100** isn't rejected; the total just stops at 0. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..06d3135 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,33 @@ +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": + # Integer arithmetic, discount rounded half up to the nearest cent. + off = (subtotal * amount + 50) // 100 + else: + off = amount + return max(subtotal - off, 0)