Test: The same feature at six settings Run: haiku-norules-1 Tool: Claude Code 2.1.302, model Haiku 5.5 (Default effort) Condition: Only the interface written 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: 25 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.0058 Paths and user names are shortened; nothing else is changed. --- Final message --- Discount codes are implemented as the README describes, and `python3 -m unittest` passes all 5 tests (3 existing cart/code tests plus the 2 existing code tests I hadn't yet exercised; all green). **Changes** - **`shop/codes.py`** (new): `InvalidCode`, `open_db(path=":memory:")` which creates the `codes` table, `load_codes(db, csv_path)` which reads the CSV, and a helper `find_code(db, code, today)` that looks up a code valid on `today`. - **`shop/cart.py`**: `add` takes `tags=None` and stores `(sku, price, qty, tags)` tuples. New `apply_code(db, code, today)` and `total_cents()`. **Choices I made where the spec was silent** - Codes are case-insensitive (stored and looked up uppercased, whitespace trimmed). The existing test applies `"save10"` to `SAVE10`, so this was required. - A code is valid when `starts <= today <= ends`, inclusive. Out-of-range dates raise `InvalidCode`, same as unknown codes. - Percent discounts are computed as `subtotal * amount // 100`, so fractional cents round down in the customer's favour. - Fixed discounts are capped at the subtotal, so the total never goes negative. - `tags` is stored but does not affect discounts. The README doesn't say what tags are for, so I didn't guess. - The discount is computed at `total_cents()` time, not when the code is applied, so adding items after applying a code still gets the discount. - `load_codes` validates `kind`, the percent range (0–100), non-negative amounts, and ISO dates, and raises `ValueError` on bad rows. Reloading the same file replaces existing rows (`INSERT OR REPLACE`). Nothing is committed. Let me know if you want a commit, and whether `tags` should restrict which items a code applies to. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..40b0ae3 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,29 @@ +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) + + 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 = min(amount, subtotal) + return subtotal - discount