Test: The same feature at six settings Run: codex-norules-2 Tool: Codex CLI 0.145.0, model gpt-5.6-sol (Built-in default reasoning) 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: 45 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost: Codex on a ChatGPT plan reports tokens, not a price Paths and user names are shortened; nothing else is changed. --- Final message --- Implemented discount codes in: - [shop/codes.py](shop/codes.py) - [shop/cart.py](shop/cart.py) Includes case-insensitive codes, inclusive validity dates, percent/fixed discounts, zero-floor totals, line tags, and SQLite CSV loading. `python3 -m unittest` passes all 5 tests. Additional edge checks and `git diff --check` also pass. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..0de20bd 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,30 @@ +from .codes import check_dates, discounted_total, 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)) + if tags is None: + tags = [] + self.items.append((sku, unit_price_cents, qty, tags)) 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): + kind, amount, starts, ends = find_code(db, code) + check_dates(starts, ends, today) + self.code = (kind, amount) + for _, _, _, tags in self.items: + tags.append("discounted") + + def total_cents(self): + subtotal = self.subtotal_cents() + if self.code is None: + return subtotal + return discounted_total(subtotal, *self.code)