Test: The same feature at six settings Run: haiku-rules-3 Tool: Claude Code 2.1.302, model Haiku 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: 28 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.0063 Paths and user names are shortened; nothing else is changed. --- Final message --- Discount codes are implemented in a new `shop/codes.py` and in `shop/cart.py`. `python3 -m unittest` runs 5 tests and all pass, including the 3 code tests already in `tests/test_codes.py`. I haven't committed anything. **What changed** - **`shop/codes.py`** (new): `InvalidCode`, `open_db(path=":memory:")` which creates the `codes` table, and `load_codes(db, csv_path)`. Codes are stored upper-case. An unknown `kind` in the CSV raises `ValueError`. An unreadable file raises the `OSError` from `open`. - **`shop/cart.py`**: - `add(..., tags=None)` stores each line as `(sku, price, qty, tags)`. - `apply_code(db, code, today)` looks up the code case-insensitively and checks the date range inclusively in SQL. Unknown or expired codes raise `InvalidCode`. On success it stores the discount and adds the `"discounted"` tag to every line. - `total_cents()` returns the subtotal minus the discount, floored at 0. Percent discounts use integer division, so any fractional cent is kept in the total. **Choices I made that the README didn't settle** - `load_codes` replaces an existing code with the same name (`INSERT OR REPLACE`). - A cart holds one code at a time. Applying a second code replaces the first. - Lines added after a code is applied don't get the `"discounted"` tag. The README only says that applying a code tags the lines in the cart at that point. **Not verified:** I tried to run an ad hoc script for the edge cases the existing tests don't cover (start and end dates both inclusive, a fixed discount larger than the subtotal, a file that can't be read). The command was blocked for approval, so those behaviours are checked only by reading the code. Adding them as tests in `tests/test_codes.py` would confirm them. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..d37f04e 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,35 @@ +from shop.codes import InvalidCode + + 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 apply_code(self, db, code, today): + row = db.execute( + "SELECT kind, amount FROM codes WHERE code = ? AND starts <= ? AND ends >= ?", + (code.strip().upper(), today, today), + ).fetchone() + if row is None: + raise InvalidCode(code) + self.discount = row + for _, _, _, tags in self.items: + if "discounted" not in tags: + tags.append("discounted") 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 total_cents(self): + subtotal = self.subtotal_cents() + if self.discount is None: + return subtotal + kind, amount = self.discount + off = subtotal * amount // 100 if kind == "percent" else amount + return max(0, subtotal - off)