Two build roots, one constant: the test that reads the other package's source
pub-trivia.app is two deployed things: a Next.js app, and a standalone WebSocket server that owns question timers. They are separate TypeScript projects with separate build roots, deployed to different platforms, and the WS server cannot import from the Next.js package. They also have to agree on this: export const QUESTION_REVEAL_SECONDS = 3 export const CLOSE_GRACE_SECONDS = 1 The app renders a…
pub-trivia.app consists of a Next.js app and a separate WebSocket server handling question timers. These server are independent TypeScript projects with distinct build roots and cannot directly import from each other. However, they share specific constants - QUESTION_REVEAL_SECONDS and CLOSE_GRACE_SECONDS - which dictate the countdown timing and question closure rules.
The WebSocket server originally mirrored the constants from the Next.js package, maintaining them manually and through comments. However, over time, these duplicates diverged, leading to discrepancies like lost codes and missing columns in the database schema. This manual duplication provided a false sense of security while consuming maintenance resources without any guarantee of consistency.
To address this, the team simplified the approach. They retained only the constants required by the WebSocket server, deleting the rest and adding an executable assertion in a test file that ensures the constants match between the two projects. This test, written in JavaScript, reads the constant file, extracts the desired values using a regular expression, and verifies their equality.
The key takeaway from this approach is the use of an executable assertion to enforce consistency between the two codebases. While it involves reading from another package's source in a test, it provides a clear, loud failure if the values diverge. This approach is more reliable than a comment asking for care, as it cannot silently pass and ensures the critical timing constants stay aligned, preventing issues like answers being accepted after zero or questions closing prematurely.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.