Test: The same feature at six settings Run: opus-low-norules-1 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: 21 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.1081 Paths and user names are shortened; nothing else is changed. --- Final message --- I added `shop/codes.py` and updated `shop/cart.py`. All 5 tests pass when run with `python3 -m unittest`. **Where the behaviour rules came from:** the current README only gives the interface. The most recent commit ("Spec without rules") deleted the actual rules. I went with the ones from the commit before it, `3022485`: - Codes are case-insensitive and stored upper-case. - A code is valid from its start date to its end date, with both days included. - A `percent` code takes that percentage off the subtotal and a `fixed` code takes that many cents off. The total is whole cents and never goes below 0. - `load_codes` raises an error if it can't read the file. - `apply_code` raises `InvalidCode` for a code that is unknown or outside its dates. - `add` takes optional `tags`, and applying a code tags every line `"discounted"`. If those rules were removed on purpose, tell me which ones to drop. **Choices I made where neither version of the README says anything:** - **Rounding:** percent discounts round down to whole cents, so the shop keeps any fraction of a cent. 10% off 1999 cents is 199 off. - **Bad kinds:** `load_codes` rejects any kind other than `percent` or `fixed`. - **Reloading:** if a code appears again in a later load, the new values replace the old ones. - **One code per cart:** applying a second code replaces the first. - **Lines added after a code:** they count toward the discounted total but aren't tagged `"discounted"`. I haven't committed anything. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..5d4763e 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) + for _, _, _, tags in self.items: + if "discounted" not in tags: + tags.append("discounted") + + def total_cents(self): + subtotal = self.subtotal_cents() + if self.code is None: + return subtotal + kind, amount = self.code + discount = subtotal * amount // 100 if kind == "percent" else amount + return max(0, subtotal - discount)