Skip to content

fix(test): run every test against <db>_test, never the configured database - #190

Merged
fog-workflows[bot] merged 1 commit into
masterfrom
fix/test-db-isolation
Nov 10, 2026
Merged

fog-workflows[bot] merged 1 commit into
masterfrom
fix/test-db-isolation

Conversation

@mastacontrola

Copy link
Copy Markdown
Member

The bug

npm test lifted against whatever database config/local.js names. In a dev or production checkout, that is the real database. Its after() hook then deleted AuditLog rows by name prefix (mocha, mc, mpa, mph), so a real host such as mcafee-01 could lose its audit rows. Every test record (hosts, users, the agent root Setting) also landed in the real database. test:ui and test:ui:prod did the same through their server child.

The fix

Every test run now uses a separate database, as is standard practice for test suites.

  • tools/lib/test-db.js resolves the test database as the configured name plus _test, always appended (fog_test becomes fog_test_test). It handles:

    • a url;
    • a url with a top-level database (sails-mongo prefers the top-level key);
    • the installer's host/port/database shape.

    It adds authSource=<configured db> when the URL carries credentials. It drops only a name that ends in _test and differs from the configured name. No error or log line prints a URL.

  • Three guards, before any write:

    • config/bootstrap.js refuses a lift on any database other than custom.testDatabase, before its index writes. It is inert when that key is unset, as it always is in production.
    • test/lifecycle.test.js checks the database the models really use, straight after the lift.
    • tools/smoke/ui.js checks the child server's database before the browser starts.
  • test:install now drops fog_install_smoke_test, never the configured database.

  • Cleanup: the prefix-based audit cleanup is gone, because nothing in a _test database is real. datastoreUrl in tools/lib/database.js is now the one installer-shape URL builder (it no longer emits user:@ or :undefined). ADR 0003 is updated.

Test

  • npm test: 432 passing. test:ui, test:ui:prod and test:install: green.
  • A scratch database's collection counts were identical before and after a full run of all four.
  • New resolver tests cover every config shape, the _test suffix rule, authSource, and datastoreUrl.
  • Each guard and test was made to fail with its fix removed. For example, removing the lift override stops the suite with "Refusing to lift: the models use the database …" before any test runs.

🤖 Generated with Claude Code

…abase

npm test used whatever database config/local.js names. In a dev or
production checkout that is the real database. Its after() then deleted
AuditLog rows by name prefix, which could remove real audit rows. Every
test record (hosts, users, the agent root Setting) also landed there.
test:ui did the same through its server child.

- tools/lib/test-db.js resolves the test database from the config: the
  configured name plus _test, always appended. It handles a url, a url
  with a top-level database, and the installer's host/port/database
  shape, and adds authSource when the url has credentials. It drops
  only a name that ends in _test and differs from the configured name.
- test/lifecycle.test.js and tools/smoke/ui.js (test:ui and
  test:ui:prod) lift on that database. config/bootstrap.js refuses a
  lift on any other database before its index writes, and the suite
  and ui.js check the database the models really use before any test
  or browser action.
- test:install drops fog_install_smoke_test (was fog_install_smoke),
  and never the configured database.
- The prefix-based audit cleanup is gone: nothing in a _test database
  is real. tools/lib/database.js datastoreUrl is the one installer-shape
  URL builder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fog-workflows
fog-workflows Bot enabled auto-merge November 10, 2026 18:10
@fog-workflows
fog-workflows Bot added this pull request to the merge queue Nov 10, 2026
Merged via the queue into master with commit 8eddc8a Nov 10, 2026
6 checks passed
@fog-workflows
fog-workflows Bot deleted the fix/test-db-isolation branch November 10, 2026 18:27
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.

1 participant