logical-test 是做什麼的?
Goal: prove the system does the right thing, not just a thing. Logic flaws pass every input-validation check and still lose money or break promises.
Follow references/core-rules.md, references/automation-workflow.md, references/severity-model.md. Write the report in the format of references/testcase-template.md. Prefix IDs with LOG-. For every detected language, also apply references/languages/<language>.md (index: references/languages/README.md), especially its Logic and negative traps section.
Procedure
- Extract the rules. From requirements, docs, tickets, code comments, and tests, list every business rule as a testable statement: "A coupon can be used once per customer", "Order total ≥ 0", "Refund only when status = paid". If no written requirement exists, infer the rule from the code and mark it
(inferred). - Find where each rule is enforced. Server, client, DB constraint, or nowhere. A rule enforced only on the client is a finding.
- Model the state machine for each core entity (order, payment, subscription, account, booking). List the states and the allowed transitions. Then check that every disallowed transition is rejected on the server.
- Write invariants: properties that must always hold. Test them with examples and, where the project supports it, property-based tests (Hypothesis, fast-check, jqwik, rapid, PropEr, RSpec + rantly, RapidCheck for C++).
- Probe concurrency on every rule that reads state, decides, then writes.
- Classify by severity. Money, data, or privilege impact is at least High.
Rule categories to check
| Category | Questions | Typical flaw |
|---|---|---|
| Calculations | Who computes price, tax, discount, shipping? Which rounding mode? In which currency unit? | Client-supplied price trusted; float rounding; discount applied after tax instead of before; percentage over 100% |
| Limits | Min/max quantity, per-user limits, quotas, credit limits, free-tier caps | Limit checked on create but not on update; limit per request instead of per user |
| One-time actions | Coupons, referrals, trials, sign-up bonuses, votes, OTPs | Reusable through replay, a parallel request, account recreation, or case variants (WELCOME10 vs welcome10) |
| State transitions | Can you cancel after shipping? Refund twice? Pay a cancelled order? Re-open a closed ticket? | Missing current-state guard; transitions allowed from any state |
| Time rules | Expiry windows, "within 30 minutes", cut-offs, schedules, trial ends | Client clock trusted; time zone or DST shifts; boundary off-by-one (< vs <=); window not enforced at all |
| Ownership and roles | Who may do X to whose Y? Delegation, teams, shared resources | Role checked but not ownership; demoted user keeps an active session with old rights |
| Ordering and sequencing | Must step A happen before B (verify email → purchase, KYC → withdraw)? | Steps skippable by calling the later endpoint directly |
| Aggregates and consistency | Do totals, counters, balances, and inventory match their line items? | Counter updated in one service but not another; cached totals stale after a refund |
| Idempotency | Does repeating the same business operation change the result? | Duplicate charge, double stock decrement, two welcome emails |
| Reversal | Is every operation correctly undone (refund, cancel, delete, rollback)? | Refund marks the order without returning stock or reversing loyalty points |
Checklist by layer
Backend / API (BE)
- Every business rule from step 1 is enforced on the server in the service layer, not only in the controller or UI.
- Prices, discounts, and totals are recomputed on the server from catalogue data. Client-sent amounts are ignored or only compared.
- State-transition guards exist:
if order.status != PAID: rejectbefore refund, ship, or cancel. - Read-decide-write sequences use a transaction with row locks, atomic updates (
UPDATE … SET stock = stock - 1 WHERE stock > 0), or optimistic locking. - One-time resources are consumed atomically, with a unique constraint on
(coupon_id, user_id). - Time rules use server time in UTC. Boundaries are tested at exactly the limit, 1 s before, and 1 s after.
- Side effects (emails, webhooks, ledger entries) fire once, after commit.
Frontend web (FE)
- The displayed total equals the server-calculated total. Show the server value; don't recompute it.
- Hidden or disabled UI is not the only enforcement of a rule (e.g. the "Cancel" button is hidden after 30 min, but the API still accepts the cancel).
- Optimistic UI updates roll back correctly when the server rejects the action.
- Multi-step wizards can't be completed out of order via URL changes or the back button.
Mobile (MOB)
- Offline-queued actions are re-validated by the server when they sync. A queued "redeem coupon" from yesterday must still obey today's rules.
- The device clock is never trusted for expiry or time windows.
- In-app purchase and entitlement state is verified on the server with receipts, not just from a local flag.
Admin panel (ADM)
- Admin overrides (manual refund, price change, status change) follow the same state machine, or are explicitly audited as overrides.
- Bulk actions apply per-row rules. They don't skip validation because the caller is an admin.
- Role hierarchy rules hold: support can't grant themselves admin, and admins can't remove the last super-admin.
- Financial reports reconcile: sum of orders − refunds = recorded revenue.
Database (DB)
- Invariants are backed by constraints where possible:
CHECK (total >= 0),CHECK (stock >= 0),UNIQUE (coupon_id, user_id), FKs, enum/check onstatus. - Money is stored as integer minor units or
DECIMAL(p,s)with a defined rounding policy. - Derived columns (totals, counters) have a single owner, and a reconciliation query exists.
- Soft-deleted rows can't still satisfy or block business rules unintentionally.
Concurrency probes (local or staging only)
Send N identical requests in parallel, e.g. a short script or xargs -P 10 curl … against your own local server. Then check:
- coupon used once
- stock never below 0
- balance never double-debited
- exactly one order created per idempotency key
Scope, prerequisites, and missing tools
Don't use it for malformed input (negative-test), dependency outages (fallback-test), or missing features versus the spec (product-test). This skill checks whether the implemented rules are correct.
Prerequisites: read access to the repository. Requirements docs help but aren't required; inferred rules are marked (inferred). Optional: a property-based testing library in the project's language.
If something is missing: if there are no requirements, the app can't run, or the parallel-request probe is blocked, record NOT RUN: <reason>, analyse the code paths statically, and still write the report.
Example finding row
| ID | Layer | Type | Finding | Location / Evidence | Failure scenario | Fix | Status |
|---|---|---|---|---|---|---|---|
| LOG-BE-002 | BE | LOGIC | Coupon reusable; total can go negative | orders.service.ts:57 subtracts coupon.amount with no redemption record and no max(0, …) | Same coupon applied to 5 orders; a 9.99 order with a 10.00 coupon totals −0.01 | UNIQUE(coupon_id,user_id) redemption row in the same transaction; clamp total at 0 | Open |