Connect with the Product Hunt community

Start a thread, join a discussion, stay in the loop.

Start new thread
trending
General

p/general

by

•
How do you test a time-based rule? I broke mine on purpose seven ways

When a user is flagged by my safety layer, the app goes quiet for a while. I kept the whole rule in one pure function: it takes the user s recent flags and the current time, and returns whether they re paused, until when, and why. No database, no clock reads inside, and the lengths live in named constants (pause length, repeat window, minimum gap between two flags that count as a repeat).

The rule itself is small, but time rules go wrong at the edges: exactly at a boundary, two flags a minute apart, a timestamp with microseconds and a +00:00 offset. So I wrote 61 tests, then did something I now do for every rule like this: I broke the code on purpose, seven different ways, and checked that a test failed each time. I removed the minimum gap, counted flags I shouldn t count, measured a repeat from the wrong flag, made the pause read fail closed, and so on. All seven were caught. A test that has never failed proves nothing.

Two things the design review found before users did:

  1. I first started the soft period (gentler messaging after a pause) at the flag time. That meant it ended the moment the longest pause ended, which is the worst time to switch back to normal. I moved the start to the pause end. That quietly broke something else: the database query only looked back as far as the old rule needed, so a repeat flag would have fallen outside it while the soft period was still running. The look-back had to grow to cover pause plus soft period plus repeat window. A test now fails if I shrink it.

  2. Contact-form flags must not count as repeats. The table holds only a user ID, a level, and a time, and I didn t want a text column, so I added a single yes/no column instead.

1

First
Previous
•••
555657
•••
Next