Skip to content

improvement(testing): consolidate @sim/db mocks into one table-aware chain mock - #5856

Merged
waleedlatif1 merged 5 commits into
stagingfrom
test/db-mock-consolidation
Jul 22, 2026
Merged

improvement(testing): consolidate @sim/db mocks into one table-aware chain mock#5856
waleedlatif1 merged 5 commits into
stagingfrom
test/db-mock-consolidation

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • Back databaseMock (global setup mock) and dbChainMock with the same db instance — a module bound to either export now hits identical chain fns, eliminating the rival-implementation split between the two @sim/testing db mocks
  • Add queueTableRows(table, rows): first-class FIFO per-table select routing keyed by schema-mock table identity; every downstream terminal (limit/orderBy/groupBy/for/joins/direct await) resolves the queued rows
  • Delete createMockDb — a duplicate chain implementation with no external consumers
  • Migrate the five suites that hand-rolled table routing + databaseMock delegation (billing plan/usage/usage-log, admin dashboard-organizations, workspaces/utils) onto the new API — net −295 lines of bespoke test plumbing
  • Add a contract test for the mock itself, and a test script to @sim/testing so its tests actually run under turbo (they previously never ran in CI)
  • Groundwork for the shared-worker (isolate: false) campaign: with one shared db-mock instance, first-importer binding freezes can no longer split @sim/db behavior between suites

Type of Change

  • Improvement

Testing

Full apps/sim suite green locally (13640/13642; the two failures are a pre-existing local ripgrep dependency and an unrelated load-induced regex-timing flake, both passing standalone). New @sim/testing contract test covers instance sharing, FIFO routing, terminal resolution, override precedence, reset, and transactions. tsc + biome clean on both packages.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

…chain mock
- back databaseMock and dbChainMock with the SAME db instance so a module
bound to either export hits identical chain fns — rival-mock divergence
between the two @sim/testing db mocks is structurally impossible now
- add queueTableRows(table, rows): FIFO per-table select routing keyed by
schema-mock table identity, consumed at where() materialization and
resolved by every downstream terminal (limit/orderBy/groupBy/for/joins)
- delete createMockDb (duplicate chain implementation, no external users)
- migrate the five suites that hand-rolled table routing + databaseMock
delegation (billing plan/usage/usage-log, admin dashboard-organizations,
workspaces/utils) onto queueTableRows; net -295 lines
- add a contract test for the mock itself and a test script to
@sim/testing so its tests actually run under turbo
@waleedlatif1
waleedlatif1 requested a review from a team as a code ownerJuly 22, 2026 20:31
@vercel

vercelBot commented Jul 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
ProjectDeploymentActionsUpdated (UTC)
docsSkippedSkippedJul 22, 2026 9:09pm

Request Review

@cursor

cursorBot commented Jul 22, 2026

Copy link
Copy Markdown

PR Summary

Low Risk
Changes are limited to test utilities and test files; no production runtime behavior is modified.

Overview
Unifies databaseMock and dbChainMock behind a single shared db instance and adds queueTableRows(table, rows) so select chains resolve FIFO result sets by schema table identity (joins, direct from awaits, terminals, and selectDistinctOn included; mutations do not drain select queues). createMockDb is removed.

Five apps/sim suites (admin dashboard-orgs, billing plan/usage/usage-log, workspaces utils) drop hand-rolled queue routing and databaseMock ↔ chain delegation (~295 lines) in favor of the new API. @sim/testing gains a database.mock contract test and a test script so those tests run in CI/turbo.

Reviewed by Cursor Bugbot for commit 46e056c. Configure here.

Comment threadpackages/testing/src/mocks/database.mock.ts Outdated
@greptile-apps

greptile-appsBot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR consolidates the database test mocks around one table-aware shared instance. The main changes are:

  • Adds FIFO result queues keyed by schema table identity.
  • Shares one database instance across both mock exports.
  • Migrates five test suites away from local query-routing helpers.
  • Adds contract tests and enables the testing package in the test pipeline.

Confidence Score: 5/5

This looks safe to merge.

  • The updated mock keeps routing and result consumption local to each query chain.
  • The latest fixes cover direct awaits, joined tables, interleaved builders, and terminal overrides.
  • No blocking issue remains in the changed code.

Important Files Changed

FilenameOverview
packages/testing/src/mocks/database.mock.tsAdds the shared table-aware query mock with lazy, chain-local result resolution.
packages/testing/src/mocks/database.mock.test.tsCovers instance sharing, table routing, joins, direct awaits, overrides, resets, and transactions.
packages/testing/src/mocks/index.tsExports the table queue helper and removes the unused database mock factory.
packages/testing/package.jsonAdds the package test command.
apps/sim/lib/admin/dashboard-organizations.test.tsReplaces local query routing with shared per-table queues.
apps/sim/lib/billing/core/plan.test.tsMigrates subscription and organization query results to shared table queues.
apps/sim/lib/billing/core/usage-log.test.tsUses shared insert and transaction spies without database delegation.
apps/sim/lib/billing/core/usage.test.tsRemoves redundant database mock delegation.
apps/sim/lib/workspaces/utils.test.tsUses the shared database spies directly.

Reviews (5): Last reviewed commit: "fix(testing): lazy queue consumption at ..." | Re-trigger Greptile

Comment threadpackages/testing/src/mocks/database.mock.ts Outdated
Comment threadpackages/testing/src/mocks/database.mock.ts Outdated
…from, mutation isolation
- track the chain's tables as a list (from + joins) so rows queued for a
join-only table route correctly; from-table queue checked first
- make the from/join builder a lazy thenable so awaiting a select with no
where clause resolves queued rows (dequeue at await, never double-consumed)
- update/delete/set clear the routing context so a mutation's where() can
never consume rows queued for a select
- document the left-to-right chain-construction assumption; contract tests
for all three behaviors
@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@greptile

@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@cursor review

Comment threadpackages/testing/src/mocks/database.mock.ts Outdated

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit d34ca5f. Configure here.

@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@greptile

@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@cursor review

Comment threadpackages/testing/src/mocks/database.mock.ts Outdated

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 858ae53. Configure here.

- shared dbChainMockFns entries become pure spy/override ports: their default
implementation returns a sentinel that chain-local builders replace, while
any mock* override on the spy wins verbatim
- each select().from() captures its own immutable table list; where(),
joins, terminals, and direct awaits all resolve through that closure, so
partially-built chains for different tables interleave without cross-talk
- no module-level routing state remains
@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@greptile

@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@cursor review

Comment threadpackages/testing/src/mocks/database.mock.ts Outdated
Comment threadpackages/testing/src/mocks/database.mock.ts
…e on reset
- each chain holds one lazy rows supplier: the queued set is dequeued only
when a default thenable actually resolves, so a chain answered by a
per-test terminal override leaves its queued rows for the next chain
- resetDbChainMock also mockReset()s the stable db entry-point wrappers so
direct overrides on databaseMock.db.* cannot outlive a suite
@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@greptile

@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@cursor review

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 46e056c. Configure here.

Sign up for freeto 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

@waleedlatif1