<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abhay Singh</title>
    <description>The latest articles on DEV Community by Abhay Singh (@asmodeus14).</description>
    <link>https://dev.to/asmodeus14</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4256870%2F837dc7d0-5f90-4630-b4e6-2665a070e543.jpg</url>
      <title>DEV Community: Abhay Singh</title>
      <link>https://dev.to/asmodeus14</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asmodeus14"/>
    <language>en</language>
    <item>
      <title>A trash cleanup that deleted records a day early, and how I tested it in 2 minutes</title>
      <dc:creator>Abhay Singh</dc:creator>
      <pubDate>Sat, 21 Nov 2026 08:06:36 +0000</pubDate>
      <link>https://dev.to/asmodeus14/a-trash-cleanup-that-deleted-records-a-day-early-and-how-i-tested-it-in-2-minutes-4ljn</link>
      <guid>https://dev.to/asmodeus14/a-trash-cleanup-that-deleted-records-a-day-early-and-how-i-tested-it-in-2-minutes-4ljn</guid>
      <description>&lt;p&gt;Twenty, the open-source CRM, keeps deleted records in the trash for 14 days before deleting them for good. At least, that's what it says. A record deleted at 23:50 was actually gone after 13 days and 20 minutes. With retention set to 1 day, a record deleted just before midnight UTC disappeared 11 minutes later.&lt;/p&gt;

&lt;p&gt;The code looked reasonable:&lt;/p&gt;

&lt;p&gt;private calculateCutoffDate(trashRetentionDays: number): Date {&lt;br&gt;
  const cutoffDate = new Date();&lt;/p&gt;

&lt;p&gt;cutoffDate.setUTCHours(0, 0, 0, 0);&lt;br&gt;
  cutoffDate.setDate(cutoffDate.getDate() - trashRetentionDays + 1);&lt;/p&gt;

&lt;p&gt;return cutoffDate;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;A nightly job at 00:10 deletes everything with deletedAt &amp;lt; cutoff. The + 1 moves the cutoff forward a whole day, so anything deleted during day 0 is removed at 00:10 on day 14, whatever time it was deleted. There's a quieter second problem too: setUTCHours is mixed with the local-time setDate/getDate, so the result also depends on the server's time zone.&lt;/p&gt;

&lt;p&gt;The fix was small: drop the + 1, and stay in UTC. Twenty merged it.&lt;/p&gt;

&lt;p&gt;Why nobody caught it&lt;/p&gt;

&lt;p&gt;This bug only shows up if you wait two weeks, delete records at the right time of day, and check after a real cron run. Unit tests usually mock new Date(), so they check the arithmetic, not what happens when Postgres's now(), the cron schedule and the job all run together.&lt;/p&gt;

&lt;p&gt;So most of us test time-based features by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;editing timestamps in the database by hand,&lt;/li&gt;
&lt;li&gt;adding a "make the trial 2 minutes long" flag, or&lt;/li&gt;
&lt;li&gt;not testing them at all.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What I built&lt;/p&gt;

&lt;p&gt;I got tired of doing the first one, so I built ChronoStack. It moves the clock forward for a whole Docker Compose stack at once (Postgres now(), Redis TTLs and your services) with no code changes. It runs a separate copy of your stack, so your real data isn't touched.&lt;/p&gt;

&lt;p&gt;chronostack up --wait                  # a copy of your compose stack on a virtual clock&lt;br&gt;
chronostack advance 15d                # 15 days later everywhere; your nightly jobs ran on the way&lt;br&gt;
chronostack run scenarios/trial.yml    # a scripted, repeatable test with a report&lt;/p&gt;

&lt;p&gt;For the Twenty bug, a scenario moves a record to the trash at 23:50, steps forward a day at a time, and checks the record is still there just before the 14 days are up. Then it sets retention to 1 day and does the same at 23:59. On Twenty's published image it fails. With the fix, it passes. A full run takes under 2 minutes instead of two weeks.&lt;/p&gt;

&lt;p&gt;The hard part wasn't the fake clock&lt;/p&gt;

&lt;p&gt;Faking the time is an old trick (libfaketime has done it for years). What took most of the work was everything that breaks once you do it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Timers don't fire when the wall clock jumps. setTimeout and most in-process schedulers run on the monotonic clock, which ChronoStack deliberately leaves alone. So advance stops at each job's scheduled time and runs it.&lt;/li&gt;
&lt;li&gt;Some waits hang forever. pthread_cond_timedwait and friends take a wall-clock deadline. Once virtual time is ahead, those deadlines are wrong, so the shim translates them back to real time. That's checked every night in C, Python, Node, the JVM, Ruby, PHP and .NET.&lt;/li&gt;
&lt;li&gt;pg_cron skips jobs after a jump of more than 3 hours, so the clock stops on each job's minute.&lt;/li&gt;
&lt;li&gt;BullMQ runs a job twice if you jump while it's running, because its lock expires. So advance waits for the queues to drain first.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Users, too&lt;/p&gt;

&lt;p&gt;Real bugs often need real behaviour: people signing up over a week, coming back days later, some of them paying. So a scenario can bring simulated users along, and they act on the virtual clock:&lt;/p&gt;

&lt;p&gt;cohorts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;{ name: trialists, users: 20, arrive: { over: 7d }, journey: trial }
journeys:
trial:

&lt;ul&gt;
&lt;li&gt;http: { method: POST, url: "${api}/signup?email=u${user}-${run_id}@example.com" }
save: { token: /token }&lt;/li&gt;
&lt;li&gt;think: { between: [1h, 20h] }&lt;/li&gt;
&lt;li&gt;http: { url: "${api}/me", headers: { x-session: "${token}" } }
expect: { status: 200 }&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the demo, 20 trialists over 30 virtual days, with the partner's webhook endpoint down for 4 hours on day 15, take about 3 minutes. Every random choice comes from a seed, so the same seed gives the same timeline of who did what when. chronostack report diff then tells you what changed between two runs, for example before and after a fix.&lt;/p&gt;

&lt;p&gt;Other bugs it found&lt;/p&gt;

&lt;p&gt;I ran it against a few open-source projects' published Docker images. Besides Twenty, it found date bugs in Saleor, Chatwoot, Formbricks, tududi and Worklenz; Worklenz has merged the fix too. The full list, with issues and fix PRs, is in the README (&lt;a href="https://github.com/Asmodeus14/ChronoStack#found-in-real-projects" rel="noopener noreferrer"&gt;https://github.com/Asmodeus14/ChronoStack#found-in-real-projects&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Try it&lt;/p&gt;

&lt;p&gt;It's an alpha (0.1.0-alpha.8), open source under MIT/Apache-2.0. It works on Linux, Windows and Intel Macs; Apple-silicon Macs are untested so far (if you have one, issue #5 (&lt;a href="https://github.com/Asmodeus14/ChronoStack/issues/5" rel="noopener noreferrer"&gt;https://github.com/Asmodeus14/ChronoStack/issues/5&lt;/a&gt;) is a 5-minute test).&lt;/p&gt;

&lt;p&gt;brew install asmodeus14/tap/chronostack         # macOS, Linux&lt;br&gt;
scoop bucket add chronostack &lt;a href="https://github.com/Asmodeus14/scoop-bucket" rel="noopener noreferrer"&gt;https://github.com/Asmodeus14/scoop-bucket&lt;/a&gt;   # Windows&lt;br&gt;
scoop install chronostack/chronostack&lt;br&gt;
cargo install &lt;a href="mailto:chronostack@0.1.0-alpha.8"&gt;chronostack@0.1.0-alpha.8&lt;/a&gt;          # anywhere with Rust&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Repo: &lt;a href="https://github.com/Asmodeus14/ChronoStack" rel="noopener noreferrer"&gt;https://github.com/Asmodeus14/ChronoStack&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Site, with an interactive demo: &lt;a href="https://chronostack-dev.vercel.app" rel="noopener noreferrer"&gt;https://chronostack-dev.vercel.app&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you try it on your own stack, I'd love to hear what breaks, especially the output of chronostack doctor.&lt;/p&gt;

&lt;p&gt;And yes i used AI specifically Claude Code but only for testing the codes , pipelines and finding bugs in chronostack and fixing them .&lt;/p&gt;

</description>
      <category>docker</category>
      <category>testing</category>
      <category>postgres</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
