Skip to content

test(db): fix singleton-reset isolation and remove duplicate type in capture-critical-db-state - #11327

Merged
diegosouzapw merged 1 commit into
diegosouzapw:release/v3.8.50from
pacocartones:fix/test-isolation-singleton-reset
Aug 24, 2026
Merged

diegosouzapw merged 1 commit into
diegosouzapw:release/v3.8.50from
pacocartones:fix/test-isolation-singleton-reset

Conversation

@pacocartones

Copy link
Copy Markdown
Contributor

Base: release/v3.8.50. Single file: tests/unit/capture-critical-db-state.test.ts.

What

release/v3.8.50 already un-skips the three DB-state tests, but its version of the file has a duplicate type CoreModule declaration (lines 3 and 7 — a duplicate-identifier typecheck error) and initializes the singleton via a dynamic await import() inside before() after setting DATA_DIR.

This makes the isolation correct instead of incidental:

  • Create the temp dir and set DATA_DIR at module scope, before the single static import of core.ts — DATA_DIR/SQLITE_FILE are module-level consts cached at first import, so setting the env var after a dynamic import is racy.
  • Import getDbInstance/resetDbInstance/ensureDbInitialized/closeDbInstance once through the same ESM specifier the tests use, so before()/after() operate on the real singleton rather than a stale 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 resetDbInstance() so the fd is released, then re-inits and asserts a new handle identity while the on-disk row survives.
  • Removes the duplicate type CoreModule declaration.

Tests

node --test with tsx (the repo's runner for tests/unit/*.test.ts), the affected file only:

✔ 7 tests · pass 7 · fail 0 · skipped 0  (563 ms)

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.

… 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
diegosouzapw merged commit 24ac714 into diegosouzapw:release/v3.8.50 Aug 24, 2026
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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants