Four copy-pasted order-status handlers have drifted apart. Give the message format one home, keep each status's own wording as data, and add a tracking link in one place.
A delivery app texts the customer when an order is placed, accepted, picked up and delivered. The code in the editor is four handlers, each building its own text. Someone copied the first, edited it by hand, and the third now words things differently. Marketing wants a tracking link on every text. Give the knowledge of how an order-status SMS is worded and addressed one home, keep each status's own wording as data, and make the link one edit.
An order is a dict (a plain object in JavaScript and TypeScript) with id, name, phone and total (whole rupees). For order 1001, Asha, phone 90000-00001, total 590, the texts are:
| Status | Text |
|---|---|
placed |
Hi Asha, your order #1001 is placed. Total 590 rupees. |
accepted |
Hi Asha, your order #1001 is accepted by the restaurant. |
picked_up |
Hi Asha, your order #1001 is on its way. |
delivered |
Hi Asha, your order #1001 was delivered. Enjoy! |
The only change a user sees: the old picked_up text said order 1001 is on its way, which was the drift. It now reads like the others. With a tracking link, the text gets Track: <link> added at the end (and nothing is added without one).
Build this API (names are Python's; JavaScript and TypeScript use camelCase where it differs):
STATUS_TEXT: a mapping from status to its own wording, written once. It uses the placeholder {total} where the total goes (only placed does).status_sms(order, status, tracking="", texts=None): the full text. The greeting Hi <name>, your order #<id> and the tracking suffix are built only here. texts is an optional wording table (default: STATUS_TEXT). An unknown status raises ValueError (JavaScript and TypeScript: an Error whose message contains unknown status).Notifier(sender, texts=None) with notify(order, status, tracking=""): it calls sender.send(order["phone"], <the text>) once. A sender is anything with send(phone, text), so tests pass their own and nothing real is sent. JavaScript and TypeScript: order.phone.The tests check every text, the tracking suffix, a sender and a wording table the tests write (a new status is data, not a new handler), that a custom wording table doesn't leak into the default, and that the greeting is written in one place only.
Write your code, then run 9 example tests and 3 hidden tests. Each run is limited to 8 seconds: an infinite loop gets stopped and the runtime restarts, your code stays.