test(db): fix singleton-reset isolation and remove duplicate type in capture-critical-db-state - #11327
Merged
diegosouzapw merged 1 commit intoAug 24, 2026
Conversation
… 3 DB-state tests
The three capture-critical-db-state tests were skipped because they only
passed in isolation and failed under the full suite. The setup()/cleanup()
helpers created a fresh temp DATA_DIR per test and reset the singleton with
a CJS require("../../src/lib/db/core.ts") inside cleanup(). Under tsx that
require resolves to a different module instance than the test's
await import(...) of the same specifier, so cleanup() reset a singleton
that was never the one the test used. The real singleton leaked across
tests, DATA_DIR was rebound after the module had already cached it, and the
reset assertion ("persisted row should survive a singleton reset") saw an
empty DB.
Fix the isolation instead of hiding it:
- Create the temp dir and set DATA_DIR once, before the single top-level
import of core.ts, so DATA_DIR/SQLITE_FILE resolve to the temp dir (they
are module-level consts cached at first import).
- Import getDbInstance/resetDbInstance/ensureDbInitialized/closeDbInstance
once via the same ESM specifier the tests use, so before/after operate on
the real singleton rather than a stale require() copy.
- before() closes any singleton left by a prior file in the shard and
ensureDbInitialized()s a fresh DB; after() lets reset errors surface.
- The reset test closes the handle before reset so the fd is released, then
re-inits and asserts a new handle identity while the on-disk row survives.
All three previously skipped tests now run and pass alongside the rest,
under the suite command:
DISABLE_SQLITE_AUTO_BACKUP=true node --import tsx/esm \
--import ./open-sse/utils/setupPolyfill.ts \
--import ./tests/_setup/isolateDataDir.ts \
--test --test-force-exit tests/unit/capture-critical-db-state.test.ts
Refs diegosouzapw#10906
diegosouzapw
merged commit Aug 24, 2026
24ac714
into
diegosouzapw:release/v3.8.50
14 of 16 checks passed
muhamadgalihsaputra
pushed a commit
to niyatna/NiyatnaRoute
that referenced
this pull request
Sep 27, 2026
… 3 DB-state tests (diegosouzapw#11327) Validated on a 17-PR combined board: capture-critical-db-state 7/7 (all three previously-skipped tests now run) within the board's 287/287, typecheck:core clean. Fixes the racy DATA_DIR-after-dynamic-import isolation and removes a duplicate type declaration. Thank you @pacocartones!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Base:
release/v3.8.50. Single file:tests/unit/capture-critical-db-state.test.ts.What
release/v3.8.50already un-skips the three DB-state tests, but its version of the file has a duplicatetype CoreModuledeclaration (lines 3 and 7 — a duplicate-identifier typecheck error) and initializes the singleton via a dynamicawait import()insidebefore()after settingDATA_DIR.This makes the isolation correct instead of incidental:
DATA_DIRat module scope, before the single static import ofcore.ts—DATA_DIR/SQLITE_FILEare module-level consts cached at first import, so setting the env var after a dynamic import is racy.getDbInstance/resetDbInstance/ensureDbInitialized/closeDbInstanceonce through the same ESM specifier the tests use, sobefore()/after()operate on the real singleton rather than a stale copy.before()closes any singleton left by a prior file in the shard andensureDbInitialized()s a fresh DB;after()lets reset errors surface.resetDbInstance()so the fd is released, then re-inits and asserts a new handle identity while the on-disk row survives.type CoreModuledeclaration.Tests
node --testwith tsx (the repo's runner fortests/unit/*.test.ts), the affected file only:The three previously-skipped tests —
resetDbInstance clears the singleton so next call creates a new DB,getDbInstance sets WAL journal mode,getDbInstance stores schema_version in db_meta— now run and pass.Note
No open PR touches this file. This is a small correctness + hygiene change on a test file that currently has a duplicate-identifier error in the release branch.