Skip to content
Source
Sign inSign up
TanStack/db

test(db): expand includes oracle state coverage #1719

MergedKyleAMathews(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/1719/headmerged by tannerlinsley

Expand the includes recompute oracle across depths 1–4, generated full-row batches, reorders, child relationship changes, and flat array/concat materialization. This is test-only: it records two newly isolated runtime defect classes as assertion-specific expected failures but does not change query behavior.

Reviewer guidance

Coverage gap

The reusable trace runner from #1718 had only a short depth-1 full-row trace. It did not guarantee that generated histories reached the selected include depth, exercise full-row multi-change batches, preserve reorder coverage while isolating relationship changes, or compare flat materialize() and concat(toArray(...)) outputs with independent recomputation.

Without those cases, the oracle could miss routing failures that depend on shared correlation keys, delete/reinsert lifetimes, or several child changes arriving in one batch.

Approach

  • Generalize the full-row batch driver to depths 1–4.
  • Generate bounded multi-change histories and prepend a connected path whose reserved IDs cannot be overwritten by generated rows. This makes every selected depth observable at every checkpoint.
  • Keep root reorder changes in the green corpus while excluding the known delete/reinsert failure class.
  • Exercise child reparenting and rekeying one row at a time. Multi-row relationship changes remain represented by a reduced expected-failure trace.
  • Add independent flat projections for array materialization and concat(toArray(...)).
  • Reduce each discovered mismatch to a deterministic trace and accept only the oracle's equality assertion as the expected failure.

The expanded suite now preserves three unfixed state-equivalence defect classes:

  1. A nested scalar reference redirect retains the old materialized row (landed with #1718).
  2. Deleting and reinserting one of two parents that shared a correlation key can leave the reinserted parent subscribed to its old route.
  3. Reparenting two children in one batch can leave aggregate membership stale. The array and concat traces are two projections of this same defect, not separate bugs.

Key invariants

  • The incremental result equals a fresh recomputation after startup and every batch.
  • Every generated depth has a live, connected result path.
  • Row ordering follows position and then id, including generated reorders.
  • A child belongs only to parents whose current group matches its current parentGroup.
  • Array and concat materializations derive from the same current child membership and order.
  • Known failures pass only for an oracle assertion mismatch; setup, runtime, and cleanup errors still fail the test.

Non-goals

  • No production fix for any recorded defect.
  • No readiness, progressive-sync timing, publication, adapter, or ownership driver.
  • No claim that array and concat expose distinct underlying bugs.
  • No unbounded fuzzing or replay framework.

Trade-offs

The generated runs are deterministic and bounded to keep CI fast and reproducible. The green generators avoid state transitions already proven broken; those transitions live as small expected-failure traces until the routing refactor makes them green. Single-row child relationship changes keep reparent/rekey coverage active without masking the separate intra-batch hand-off defect.

Verification

From packages/db:

pnpm exec vitest run tests/query/includes-oracle.property.test.ts
pnpm test

The focused oracle suite passes 18/18. TypeScript, ESLint, Prettier, the full DB suite, and git diff --check also pass.

Files changed

  • packages/db/tests/query/includes-oracle.property.test.ts: expands the full-row generators and depth coverage, adds flat materialization projections, and records the shared-route and intra-batch hand-off failures.

Release impact

  • Tests only; no changeset or release is needed.

Related work: RFC #1658, #1718.

Summary by CodeRabbit

  • Tests
    • Expanded coverage for nested data updates across multiple relationship levels.
    • Added scenarios for inserts, deletes, updates, reparenting, and key changes.
    • Added deterministic tests for shared routing and child handoffs within a batch.
    • Added validation for flat and array-based result materialization.
    1. Review Change Stack

      📝 Walkthrough

      Walkthrough

      The includes property-test oracle now supports flat concat materialization and depth-aware full-row batches. New generated and deterministic scenarios cover nested updates, reparenting, rekeying, child handoffs, and routing regressions.

      Changes

      Includes oracle coverage

      Layer / File(s)Summary
      Depth-aware full-row batches
      packages/db/tests/query/includes-oracle.property.test.ts
      Full-row batch generation and execution now support every include depth. Root and child models remain synchronized across generated scenarios.
      Flat materialization oracle
      packages/db/tests/query/includes-oracle.property.test.ts
      The oracle tests array materialization and scalar concat materialization with generated traces and recomputation projections.
      Routing regression coverage
      packages/db/tests/query/includes-oracle.property.test.ts
      Property and deterministic tests cover child handoffs, reparenting, rekeying, and stale routing after parent reinsertion.

      Estimated code review effort: 4 (Complex) | ~45 minutes

      Possibly related PRs

      • TanStack/db#1669: Expands regression and property coverage for child reordering and nested includes behavior.
      • TanStack/db#1716: Introduces the recomputation oracle extended here with flat concat materialization and multi-depth batches.
      • TanStack/db#1718: Adds related full-row batch testing infrastructure and scenarios.

      Suggested reviewers: kevin-dp

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly identifies the main change: expanded database includes oracle state coverage.
      Description check✅ PassedThe description explains the changes, motivation, testing, and release impact, with sufficient detail despite using custom headings instead of the full template.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      ✨ Finishing Touches 💡 1
      🛠️ Fix failing CI checks 💡
      • Create stacked PR
      • Commit on current branch
      📝 Generate docstrings
      • Create stacked PR
      • Commit on current branch
      🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Commit unit tests in branch `codex/includes-state-traces`

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. More templates

      @tanstack/angular-db

      npm i https://pkg.pr.new/@tanstack/angular-db@1719

      @tanstack/browser-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/browser-db-sqlite-persistence@1719

      @tanstack/capacitor-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/capacitor-db-sqlite-persistence@1719

      @tanstack/cloudflare-durable-objects-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/cloudflare-durable-objects-db-sqlite-persistence@1719

      @tanstack/db

      npm i https://pkg.pr.new/@tanstack/db@1719

      @tanstack/db-ivm

      npm i https://pkg.pr.new/@tanstack/db-ivm@1719

      @tanstack/db-sqlite-persistence-core

      npm i https://pkg.pr.new/@tanstack/db-sqlite-persistence-core@1719

      @tanstack/electric-db-collection

      npm i https://pkg.pr.new/@tanstack/electric-db-collection@1719

      @tanstack/electron-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/electron-db-sqlite-persistence@1719

      @tanstack/expo-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/expo-db-sqlite-persistence@1719

      @tanstack/node-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/node-db-sqlite-persistence@1719

      @tanstack/offline-transactions

      npm i https://pkg.pr.new/@tanstack/offline-transactions@1719

      @tanstack/powersync-db-collection

      npm i https://pkg.pr.new/@tanstack/powersync-db-collection@1719

      @tanstack/query-db-collection

      npm i https://pkg.pr.new/@tanstack/query-db-collection@1719

      @tanstack/react-db

      npm i https://pkg.pr.new/@tanstack/react-db@1719

      @tanstack/react-native-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/react-native-db-sqlite-persistence@1719

      @tanstack/rxdb-db-collection

      npm i https://pkg.pr.new/@tanstack/rxdb-db-collection@1719

      @tanstack/solid-db

      npm i https://pkg.pr.new/@tanstack/solid-db@1719

      @tanstack/svelte-db

      npm i https://pkg.pr.new/@tanstack/svelte-db@1719

      @tanstack/tauri-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/tauri-db-sqlite-persistence@1719

      @tanstack/trailbase-db-collection

      npm i https://pkg.pr.new/@tanstack/trailbase-db-collection@1719

      @tanstack/vue-db

      npm i https://pkg.pr.new/@tanstack/vue-db@1719

      commit: 0e6b69f

    3. 🧹 Nitpick comments (1)
      packages/db/tests/query/includes-oracle.property.test.ts (1)

      1199-1247: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

      Tighten the flat materialization result and observed types.

      FlatMaterializationResult is a union of both shapes. A recompute that returns the wrong shape for the selected materialization still typechecks. flatMaterializationProjection also declares unknown as the observed type, so assertEqual loses its type link to observe.

      Discriminate the result type by materialization and use the precise observed type.

      ♻️ Proposed typing refactor
      -type FlatMaterializationResult = Array<
      -  | {
      -      id: number
      -      group: number
      -      children: Array<{ id: number; value: number }>
      -    }
      -  | { id: number; group: number; content: string }
      ->
      +type FlatMaterializationRow<TMaterialization extends FlatMaterialization> =
      +  TMaterialization extends `array`
      +    ? { id: number; group: number; children: Array<{ id: number; value: number }> }
      +    : { id: number; group: number; content: string }
      +
      +type FlatMaterializationResult<
      +  TMaterialization extends FlatMaterialization = FlatMaterialization,
      +> = Array<FlatMaterializationRow<TMaterialization>>
      -function flatMaterializationProjection(
      -  materialization: FlatMaterialization,
      -): TraceProjection<
      -  FlatMaterializationContext,
      -  unknown,
      -  FlatMaterializationResult
      -> {
      +function flatMaterializationProjection(
      +  materialization: FlatMaterialization,
      +): TraceProjection<
      +  FlatMaterializationContext,
      +  FlatMaterializationResult,
      +  FlatMaterializationResult
      +> {

      As per coding guidelines: "Always provide the most precise return type annotation; avoid unknown or any return types unless truly necessary".

      🤖 Prompt for AI Agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      In `@packages/db/tests/query/includes-oracle.property.test.ts` around lines 1199 -
      1247, Make FlatMaterializationResult generic over the materialization mode so
      `recomputeFlatMaterialization` can only return the corresponding array or
      concatenated-content shape, and update its callers accordingly. In
      `flatMaterializationProjection`, replace the `unknown` observed type with the
      precise type produced by `stripVirtualProperties(incremental.toArray)`,
      preserving the type relationship between `observe` and `assertEqual`.

      Source: Coding guidelines

      🤖 Prompt for all review comments with AI agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      Nitpick comments:
      In `@packages/db/tests/query/includes-oracle.property.test.ts`:
      - Around line 1199-1247: Make FlatMaterializationResult generic over the
      materialization mode so `recomputeFlatMaterialization` can only return the
      corresponding array or concatenated-content shape, and update its callers
      accordingly. In `flatMaterializationProjection`, replace the `unknown` observed
      type with the precise type produced by
      `stripVirtualProperties(incremental.toArray)`, preserving the type relationship
      between `observe` and `assertEqual`.

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: d0c71f19-ae74-4527-b94e-3c6a012d486f

      📥 Commits

      Reviewing files that changed from the base of the PR and between e4940a4756d8118e9a35a506c05ce6622b2e964e and 0e6b69fa76df5b319573563d4621e899ad9cac07.

      📒 Files selected for processing (1)
      • packages/db/tests/query/includes-oracle.property.test.ts
    4. Size Change: 0 B

      Total Size: 128 kB

      ℹ️ View Unchanged
      FilenameSize
      packages/db/dist/esm/collection/change-events.js1.44 kB
      packages/db/dist/esm/collection/changes.js1.43 kB
      packages/db/dist/esm/collection/cleanup-queue.js810 B
      packages/db/dist/esm/collection/events.js434 B
      packages/db/dist/esm/collection/index.js3.83 kB
      packages/db/dist/esm/collection/indexes.js1.99 kB
      packages/db/dist/esm/collection/lifecycle.js1.7 kB
      packages/db/dist/esm/collection/mutations.js2.47 kB
      packages/db/dist/esm/collection/state.js5.51 kB
      packages/db/dist/esm/collection/subscription.js3.82 kB
      packages/db/dist/esm/collection/sync.js2.94 kB
      packages/db/dist/esm/collection/transaction-metadata.js144 B
      packages/db/dist/esm/deferred.js207 B
      packages/db/dist/esm/errors.js5.13 kB
      packages/db/dist/esm/event-emitter.js748 B
      packages/db/dist/esm/index.js3.21 kB
      packages/db/dist/esm/indexes/auto-index.js829 B
      packages/db/dist/esm/indexes/base-index.js784 B
      packages/db/dist/esm/indexes/basic-index.js2.17 kB
      packages/db/dist/esm/indexes/btree-index.js2.29 kB
      packages/db/dist/esm/indexes/index-registry.js820 B
      packages/db/dist/esm/indexes/reverse-index.js557 B
      packages/db/dist/esm/live-query-adapter.js318 B
      packages/db/dist/esm/live-query-observer.js2.29 kB
      packages/db/dist/esm/local-only.js916 B
      packages/db/dist/esm/local-storage.js2.12 kB
      packages/db/dist/esm/optimistic-action.js359 B
      packages/db/dist/esm/paced-mutations.js496 B
      packages/db/dist/esm/proxy.js3.75 kB
      packages/db/dist/esm/query/builder/functions.js1.47 kB
      packages/db/dist/esm/query/builder/index.js5.84 kB
      packages/db/dist/esm/query/builder/ref-proxy.js1.24 kB
      packages/db/dist/esm/query/compiler/evaluators.js1.89 kB
      packages/db/dist/esm/query/compiler/expressions.js430 B
      packages/db/dist/esm/query/compiler/group-by.js3.56 kB
      packages/db/dist/esm/query/compiler/index.js6.67 kB
      packages/db/dist/esm/query/compiler/joins.js2.5 kB
      packages/db/dist/esm/query/compiler/lazy-targets.js923 B
      packages/db/dist/esm/query/compiler/order-by.js1.74 kB
      packages/db/dist/esm/query/compiler/select.js1.53 kB
      packages/db/dist/esm/query/effect.js4.77 kB
      packages/db/dist/esm/query/expression-helpers.js1.43 kB
      packages/db/dist/esm/query/ir.js1.25 kB
      packages/db/dist/esm/query/live-query-collection.js360 B
      packages/db/dist/esm/query/live/collection-config-builder.js9.24 kB
      packages/db/dist/esm/query/live/collection-registry.js264 B
      packages/db/dist/esm/query/live/collection-subscriber.js1.93 kB
      packages/db/dist/esm/query/live/internal.js145 B
      packages/db/dist/esm/query/live/utils.js1.81 kB
      packages/db/dist/esm/query/optimizer.js2.92 kB
      packages/db/dist/esm/query/predicate-utils.js2.97 kB
      packages/db/dist/esm/query/query-once.js359 B
      packages/db/dist/esm/query/subset-dedupe.js960 B
      packages/db/dist/esm/scheduler.js1.3 kB
      packages/db/dist/esm/SortedMap.js1.3 kB
      packages/db/dist/esm/strategies/debounceStrategy.js247 B
      packages/db/dist/esm/strategies/queueStrategy.js428 B
      packages/db/dist/esm/strategies/throttleStrategy.js246 B
      packages/db/dist/esm/transactions.js3.04 kB
      packages/db/dist/esm/utils.js927 B
      packages/db/dist/esm/utils/array-utils.js273 B
      packages/db/dist/esm/utils/browser-polyfills.js304 B
      packages/db/dist/esm/utils/btree.js5.61 kB
      packages/db/dist/esm/utils/comparison.js1.15 kB
      packages/db/dist/esm/utils/cursor.js457 B
      packages/db/dist/esm/utils/index-optimization.js2.39 kB
      packages/db/dist/esm/utils/type-guards.js157 B
      packages/db/dist/esm/utils/uuid.js449 B
      packages/db/dist/esm/virtual-props.js360 B
      compressed-size-action::db-package-size
    5. Size Change: 0 B

      Total Size: 3.81 kB

      ℹ️ View Unchanged
      FilenameSize
      packages/react-db/dist/esm/index.js249 B
      packages/react-db/dist/esm/useLiveInfiniteQuery.js1.32 kB
      packages/react-db/dist/esm/useLiveQuery.js920 B
      packages/react-db/dist/esm/useLiveQueryEffect.js355 B
      packages/react-db/dist/esm/useLiveSuspenseQuery.js567 B
      packages/react-db/dist/esm/usePacedMutations.js401 B
      compressed-size-action::react-db-package-size
    6. tannerlinsley merged this9005885

    7. tannerlinsley closed this

Sign in to join this conversation.