Test: The same feature at six settings Run: haiku-norules-2 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: 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.0054 Paths and user names are shortened; nothing else is changed. --- Final message --- Discount codes are implemented in `shop/codes.py` and `shop/cart.py`. `python3 -m unittest` runs 5 tests and all pass. I did not commit. The latest commit (`f6964b9`, "Spec without rules") removed the behavior rules from the README, but `tests/test_codes.py` still exercises them. I implemented the README interface and used the rules from the previous README version (`3022485`) for behavior the current README no longer states: - **Codes are case-insensitive** and stored upper-case. - **Validity** is inclusive of both the start and end dates. A code outside that window raises `InvalidCode`, the same as an unknown code. - **Percent** discounts round half up to whole cents. For example, 10% of 1005 is 101 cents off. The README doesn't say how to round, so this is my choice. - **Fixed** discounts subtract the amount in cents. - **Totals** never go below 0. - **Tags:** applying a code adds the tag `"discounted"` to every line. If the removed rules shouldn't apply, tell me which ones and I'll change them. Other details: - `open_db` creates the `codes` table. `load_codes` upper-cases codes, stores amounts as integers, and lets a missing or unreadable file raise `OSError`. - `Cart.items` is now 4-tuples `(sku, unit_price_cents, qty, tags)`. `Cart.add` takes an optional `tags` argument. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..2df4aea 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,38 @@ +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 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): + row = db.execute( + "SELECT kind, amount FROM codes WHERE code = ? AND starts <= ? AND ? <= ends", + (code.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 total_cents(self): + subtotal = self.subtotal_cents() + if self.discount is None: + return subtotal + kind, amount = self.discount + if kind == "percent": + off = (subtotal * amount + 50) // 100 + else: + off = amount + return max(0, subtotal - off)