Test: The same feature at six settings Run: opus-low-norules-2 Tool: Claude Code 2.1.302, model Opus 5.5 (--effort low) 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: 24 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.0925 Paths and user names are shortened; nothing else is changed. --- Final message --- Discount codes now work as the README describes, and all 5 tests pass with `python3 -m unittest`. - **`shop/codes.py`** (new): `InvalidCode`, `open_db()` and `load_codes()`. When codes are loaded, each row is checked: the kind must be `percent` or `fixed`, the amount can't be negative, and a percent can't exceed 100. Reloading a code replaces the old entry. - **`shop/cart.py`**: `add()` takes an optional `tags` list, and each item is now a 4-part tuple. `apply_code()` raises `InvalidCode` for a code that doesn't exist or isn't valid on `today`. `total_cents()` applies the discount. The README didn't cover some details, so I chose these: - Codes are case-insensitive (the test applies `save10` to `SAVE10`). - The `starts` and `ends` dates both count as valid days. - Percent discounts round down to whole cents, so the customer pays the extra fraction of a cent. - A fixed discount can't take the total below 0. - A cart holds one code; applying another replaces it. - Tags are stored but don't affect any discount yet. I haven't committed anything. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..25883ee 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 = amount + return max(0, subtotal - min(discount, subtotal))