@spooky-sync/core 0.0.1-canary.24 → 0.0.1-canary.240
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +58 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +476 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov-report/src/boot/auth-flip.saga.ts.html +271 -0
- package/coverage/lcov-report/src/boot/boot.saga.ts.html +469 -0
- package/coverage/lcov-report/src/boot/bucket-switch.saga.ts.html +430 -0
- package/coverage/lcov-report/src/boot/index.html +161 -0
- package/coverage/lcov-report/src/boot/preload.saga.ts.html +178 -0
- package/coverage/lcov-report/src/client/bucket-handle.ts.html +766 -0
- package/coverage/lcov-report/src/client/index.html +161 -0
- package/coverage/lcov-report/src/client/router.ts.html +367 -0
- package/coverage/lcov-report/src/client/runtime.ts.html +940 -0
- package/coverage/lcov-report/src/client/sp00ky-client.ts.html +1531 -0
- package/coverage/lcov-report/src/events/index.html +116 -0
- package/coverage/lcov-report/src/events/index.ts.html +877 -0
- package/coverage/lcov-report/src/index.html +146 -0
- package/coverage/lcov-report/src/index.ts.html +202 -0
- package/coverage/lcov-report/src/kernel/constants.ts.html +247 -0
- package/coverage/lcov-report/src/kernel/effects.ts.html +562 -0
- package/coverage/lcov-report/src/kernel/events.ts.html +250 -0
- package/coverage/lcov-report/src/kernel/index.html +176 -0
- package/coverage/lcov-report/src/kernel/interpreter.ts.html +514 -0
- package/coverage/lcov-report/src/kernel/saga.ts.html +304 -0
- package/coverage/lcov-report/src/modules/app-release/index.html +116 -0
- package/coverage/lcov-report/src/modules/app-release/index.ts.html +712 -0
- package/coverage/lcov-report/src/modules/auth/events/index.html +116 -0
- package/coverage/lcov-report/src/modules/auth/events/index.ts.html +142 -0
- package/coverage/lcov-report/src/modules/auth/index.html +116 -0
- package/coverage/lcov-report/src/modules/auth/index.ts.html +1225 -0
- package/coverage/lcov-report/src/modules/crdt/crdt-field.ts.html +967 -0
- package/coverage/lcov-report/src/modules/crdt/index.html +146 -0
- package/coverage/lcov-report/src/modules/crdt/index.ts.html +1474 -0
- package/coverage/lcov-report/src/modules/crdt/loro-loader.ts.html +160 -0
- package/coverage/lcov-report/src/modules/devtools/flags.ts.html +1132 -0
- package/coverage/lcov-report/src/modules/devtools/index.html +161 -0
- package/coverage/lcov-report/src/modules/devtools/index.ts.html +2413 -0
- package/coverage/lcov-report/src/modules/devtools/storage-info.ts.html +589 -0
- package/coverage/lcov-report/src/modules/devtools/versions.ts.html +415 -0
- package/coverage/lcov-report/src/modules/feature-flag/index.html +116 -0
- package/coverage/lcov-report/src/modules/feature-flag/index.ts.html +1033 -0
- package/coverage/lcov-report/src/modules/index.html +116 -0
- package/coverage/lcov-report/src/modules/ref-tables.ts.html +349 -0
- package/coverage/lcov-report/src/mutation/index.html +191 -0
- package/coverage/lcov-report/src/mutation/jobs.ts.html +208 -0
- package/coverage/lcov-report/src/mutation/mutation-id.ts.html +190 -0
- package/coverage/lcov-report/src/mutation/push.saga.ts.html +730 -0
- package/coverage/lcov-report/src/mutation/rows.ts.html +1216 -0
- package/coverage/lcov-report/src/mutation/tray.saga.ts.html +229 -0
- package/coverage/lcov-report/src/mutation/write.saga.ts.html +583 -0
- package/coverage/lcov-report/src/otel/index.html +116 -0
- package/coverage/lcov-report/src/otel/index.ts.html +466 -0
- package/coverage/lcov-report/src/query/env.ts.html +253 -0
- package/coverage/lcov-report/src/query/fetch.saga.ts.html +412 -0
- package/coverage/lcov-report/src/query/hash.ts.html +139 -0
- package/coverage/lcov-report/src/query/index.html +281 -0
- package/coverage/lcov-report/src/query/lifecycle.saga.ts.html +388 -0
- package/coverage/lcov-report/src/query/materialize.saga.ts.html +310 -0
- package/coverage/lcov-report/src/query/materialize.ts.html +217 -0
- package/coverage/lcov-report/src/query/membership.saga.ts.html +709 -0
- package/coverage/lcov-report/src/query/membership.ts.html +559 -0
- package/coverage/lcov-report/src/query/register.saga.ts.html +655 -0
- package/coverage/lcov-report/src/query/render-set.ts.html +265 -0
- package/coverage/lcov-report/src/query/sql.ts.html +322 -0
- package/coverage/lcov-report/src/query/window-query.ts.html +670 -0
- package/coverage/lcov-report/src/services/blobs/blob-cache.ts.html +1894 -0
- package/coverage/lcov-report/src/services/blobs/blob-manifest.ts.html +766 -0
- package/coverage/lcov-report/src/services/blobs/blob-store.ts.html +1162 -0
- package/coverage/lcov-report/src/services/blobs/index.html +161 -0
- package/coverage/lcov-report/src/services/blobs/index.ts.html +295 -0
- package/coverage/lcov-report/src/services/database/cache-engine.ts.html +664 -0
- package/coverage/lcov-report/src/services/database/connection-supervisor.ts.html +1330 -0
- package/coverage/lcov-report/src/services/database/database.ts.html +658 -0
- package/coverage/lcov-report/src/services/database/engine-factory.ts.html +184 -0
- package/coverage/lcov-report/src/services/database/errors.ts.html +187 -0
- package/coverage/lcov-report/src/services/database/events/index.html +116 -0
- package/coverage/lcov-report/src/services/database/events/index.ts.html +184 -0
- package/coverage/lcov-report/src/services/database/index.html +386 -0
- package/coverage/lcov-report/src/services/database/index.ts.html +121 -0
- package/coverage/lcov-report/src/services/database/local-migrator.ts.html +703 -0
- package/coverage/lcov-report/src/services/database/local.ts.html +1633 -0
- package/coverage/lcov-report/src/services/database/plan-render.ts.html +409 -0
- package/coverage/lcov-report/src/services/database/relation-resolver.ts.html +682 -0
- package/coverage/lcov-report/src/services/database/remote.ts.html +1036 -0
- package/coverage/lcov-report/src/services/database/sqlite-cache-engine.ts.html +4132 -0
- package/coverage/lcov-report/src/services/database/sqlite-lock-verify.ts.html +220 -0
- package/coverage/lcov-report/src/services/database/sqlite-open.ts.html +577 -0
- package/coverage/lcov-report/src/services/database/sqlite-plan-sql.ts.html +499 -0
- package/coverage/lcov-report/src/services/database/sqlite-select.ts.html +478 -0
- package/coverage/lcov-report/src/services/database/sqlite-transport.ts.html +757 -0
- package/coverage/lcov-report/src/services/database/surql-translate.ts.html +1333 -0
- package/coverage/lcov-report/src/services/database/surreal-cache-engine.ts.html +568 -0
- package/coverage/lcov-report/src/services/logger/index.html +116 -0
- package/coverage/lcov-report/src/services/logger/index.ts.html +154 -0
- package/coverage/lcov-report/src/services/persistence/index.html +146 -0
- package/coverage/lcov-report/src/services/persistence/localstorage.ts.html +163 -0
- package/coverage/lcov-report/src/services/persistence/resilient.ts.html +208 -0
- package/coverage/lcov-report/src/services/persistence/surrealdb.ts.html +271 -0
- package/coverage/lcov-report/src/services/stream-processor/index.html +146 -0
- package/coverage/lcov-report/src/services/stream-processor/index.ts.html +3313 -0
- package/coverage/lcov-report/src/services/stream-processor/permissions.ts.html +244 -0
- package/coverage/lcov-report/src/services/stream-processor/wasm-types.ts.html +346 -0
- package/coverage/lcov-report/src/services/tabs/broker-client.ts.html +934 -0
- package/coverage/lcov-report/src/services/tabs/coordinator.ts.html +1984 -0
- package/coverage/lcov-report/src/services/tabs/index.html +191 -0
- package/coverage/lcov-report/src/services/tabs/leader-locks.ts.html +310 -0
- package/coverage/lcov-report/src/services/tabs/protocol.ts.html +871 -0
- package/coverage/lcov-report/src/services/tabs/support.ts.html +193 -0
- package/coverage/lcov-report/src/services/tabs/tabs-broker-worker.ts.html +2005 -0
- package/coverage/lcov-report/src/sp00ky.ts.html +100 -0
- package/coverage/lcov-report/src/state/client-state.ts.html +637 -0
- package/coverage/lcov-report/src/state/index.html +161 -0
- package/coverage/lcov-report/src/state/lifecycle.ts.html +343 -0
- package/coverage/lcov-report/src/state/reducers.ts.html +1471 -0
- package/coverage/lcov-report/src/state/selectors.ts.html +637 -0
- package/coverage/lcov-report/src/sync/connection.saga.ts.html +343 -0
- package/coverage/lcov-report/src/sync/index.html +176 -0
- package/coverage/lcov-report/src/sync/live.saga.ts.html +232 -0
- package/coverage/lcov-report/src/sync/policy.ts.html +1606 -0
- package/coverage/lcov-report/src/sync/poll.saga.ts.html +193 -0
- package/coverage/lcov-report/src/sync/tabs.saga.ts.html +361 -0
- package/coverage/lcov-report/src/types.ts.html +2419 -0
- package/coverage/lcov-report/src/utils/blurhash.ts.html +355 -0
- package/coverage/lcov-report/src/utils/error-classification.ts.html +244 -0
- package/coverage/lcov-report/src/utils/index.html +191 -0
- package/coverage/lcov-report/src/utils/index.ts.html +820 -0
- package/coverage/lcov-report/src/utils/parser.ts.html +313 -0
- package/coverage/lcov-report/src/utils/semver.ts.html +175 -0
- package/coverage/lcov-report/src/utils/surql.ts.html +667 -0
- package/coverage/lcov.info +19079 -0
- package/dist/index.d.ts +2946 -140
- package/dist/index.js +13424 -2356
- package/dist/otel/index.d.ts +2 -2
- package/dist/otel/index.js +6 -6
- package/dist/sqlite-open.js +322 -0
- package/dist/sqlite-worker.d.ts +1 -0
- package/dist/sqlite-worker.js +439 -0
- package/dist/tabs-broker-worker.d.ts +8 -0
- package/dist/tabs-broker-worker.js +472 -0
- package/dist/types.d.ts +769 -11
- package/package.json +11 -7
- package/scripts/check-broker-bundle.mjs +33 -0
- package/skills/{spooky-core → sp00ky-core}/SKILL.md +12 -12
- package/skills/{spooky-core → sp00ky-core}/references/auth.md +1 -1
- package/skills/{spooky-core → sp00ky-core}/references/config.md +2 -2
- package/src/bucket-blurhash.test.ts +148 -0
- package/src/build-globals.d.ts +12 -0
- package/src/events/events.test.ts +2 -1
- package/src/events/index.ts +3 -0
- package/src/index.ts +36 -2
- package/src/modules/app-release/index.test.ts +125 -0
- package/src/modules/app-release/index.ts +201 -0
- package/src/modules/auth/auth.local-first.test.ts +141 -0
- package/src/modules/auth/events/index.ts +2 -1
- package/src/modules/auth/index.ts +138 -25
- package/src/modules/cache/cache.relay.test.ts +95 -0
- package/src/modules/cache/index.ts +163 -43
- package/src/modules/cache/types.ts +2 -2
- package/src/modules/crdt/crdt-field.ts +294 -0
- package/src/modules/crdt/crdt-hydration.test.ts +210 -0
- package/src/modules/crdt/crdt-reconnect.test.ts +195 -0
- package/src/modules/crdt/index.ts +463 -0
- package/src/modules/crdt/loro-loader.ts +25 -0
- package/src/modules/data/data.activity.test.ts +109 -0
- package/src/modules/data/data.authority.test.ts +122 -0
- package/src/modules/data/data.heartbeat-wake.test.ts +115 -0
- package/src/modules/data/data.hydration.test.ts +142 -0
- package/src/modules/data/data.membership.test.ts +623 -0
- package/src/modules/data/data.notify-table.test.ts +41 -0
- package/src/modules/data/data.pending-ids.test.ts +199 -0
- package/src/modules/data/data.rebind.test.ts +170 -0
- package/src/modules/data/data.rematerialize.test.ts +114 -0
- package/src/modules/data/data.run.test.ts +113 -0
- package/src/modules/data/data.settled-writes.test.ts +235 -0
- package/src/modules/data/data.status.test.ts +249 -0
- package/src/modules/data/id-set-plan.test.ts +122 -0
- package/src/modules/data/index.ts +2031 -150
- package/src/modules/data/mutation-id.test.ts +25 -0
- package/src/modules/data/mutation-id.ts +35 -0
- package/src/modules/data/window-query.test.ts +52 -0
- package/src/modules/data/window-query.ts +194 -0
- package/src/modules/devtools/flags.ts +349 -0
- package/src/modules/devtools/index.ts +451 -46
- package/src/modules/devtools/notify-throttle.test.ts +154 -0
- package/src/modules/devtools/state-shape.test.ts +146 -0
- package/src/modules/devtools/storage-info.test.ts +79 -0
- package/src/modules/devtools/storage-info.ts +168 -0
- package/src/modules/devtools/versions.test.ts +74 -0
- package/src/modules/devtools/versions.ts +110 -0
- package/src/modules/feature-flag/index.test.ts +251 -0
- package/src/modules/feature-flag/index.ts +308 -0
- package/src/modules/ref-tables.test.ts +91 -0
- package/src/modules/ref-tables.ts +88 -0
- package/src/modules/sync/engine.ts +170 -86
- package/src/modules/sync/events/index.ts +9 -2
- package/src/modules/sync/queue/queue-down.test.ts +228 -0
- package/src/modules/sync/queue/queue-down.ts +108 -13
- package/src/modules/sync/queue/queue-up.forwarded.test.ts +164 -0
- package/src/modules/sync/queue/queue-up.ts +241 -57
- package/src/modules/sync/scheduler.pause.test.ts +109 -0
- package/src/modules/sync/scheduler.retry.test.ts +237 -0
- package/src/modules/sync/scheduler.ts +215 -13
- package/src/modules/sync/sync.cleanup.test.ts +116 -0
- package/src/modules/sync/sync.health.test.ts +167 -0
- package/src/modules/sync/sync.heartbeat.test.ts +80 -0
- package/src/modules/sync/sync.list-ref-meta.test.ts +193 -0
- package/src/modules/sync/sync.live-removal.test.ts +289 -0
- package/src/modules/sync/sync.reconnect.test.ts +145 -0
- package/src/modules/sync/sync.rematerialize-after-fetch.test.ts +93 -0
- package/src/modules/sync/sync.settled-poll.test.ts +58 -0
- package/src/modules/sync/sync.subquery.test.ts +82 -0
- package/src/modules/sync/sync.tabs.test.ts +249 -0
- package/src/modules/sync/sync.ts +2350 -133
- package/src/modules/sync/utils.test.ts +322 -2
- package/src/modules/sync/utils.ts +286 -16
- package/src/otel/index.ts +13 -10
- package/src/services/blobs/blob-cache.test.ts +359 -0
- package/src/services/blobs/blob-cache.ts +603 -0
- package/src/services/blobs/blob-manifest.ts +227 -0
- package/src/services/blobs/blob-store.test.ts +77 -0
- package/src/services/blobs/blob-store.ts +359 -0
- package/src/services/blobs/blob.fixture.ts +90 -0
- package/src/services/blobs/index.ts +70 -0
- package/src/services/database/cache-engine.ts +193 -0
- package/src/services/database/connection-supervisor.test.ts +289 -0
- package/src/services/database/connection-supervisor.ts +415 -0
- package/src/services/database/database.query-timeout.test.ts +140 -0
- package/src/services/database/database.ts +130 -62
- package/src/services/database/engine-factory.ts +33 -0
- package/src/services/database/errors.ts +34 -0
- package/src/services/database/events/index.ts +2 -1
- package/src/services/database/index.ts +7 -0
- package/src/services/database/local-migrator.ts +30 -27
- package/src/services/database/local.test.ts +64 -0
- package/src/services/database/local.ts +484 -67
- package/src/services/database/plan-render.test.ts +159 -0
- package/src/services/database/plan-render.ts +108 -0
- package/src/services/database/relation-resolver.test.ts +467 -0
- package/src/services/database/relation-resolver.ts +0 -0
- package/src/services/database/remote.ts +222 -15
- package/src/services/database/sqlite-cache-engine.test.ts +616 -0
- package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
- package/src/services/database/sqlite-cache-engine.ts +1358 -0
- package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
- package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
- package/src/services/database/sqlite-lock-verify.test.ts +33 -0
- package/src/services/database/sqlite-lock-verify.ts +45 -0
- package/src/services/database/sqlite-open.test.ts +150 -0
- package/src/services/database/sqlite-open.ts +164 -0
- package/src/services/database/sqlite-plan-sql.test.ts +104 -0
- package/src/services/database/sqlite-plan-sql.ts +138 -0
- package/src/services/database/sqlite-projection.test.ts +99 -0
- package/src/services/database/sqlite-select.integration.test.ts +185 -0
- package/src/services/database/sqlite-select.test.ts +246 -0
- package/src/services/database/sqlite-select.ts +131 -0
- package/src/services/database/sqlite-transport.fixture.ts +30 -0
- package/src/services/database/sqlite-transport.ts +224 -0
- package/src/services/database/sqlite-worker.ts +437 -0
- package/src/services/database/surql-translate.ts +416 -0
- package/src/services/database/surreal-cache-engine.ts +161 -0
- package/src/services/logger/index.ts +3 -2
- package/src/services/persistence/localstorage.ts +2 -2
- package/src/services/persistence/resilient.ts +11 -4
- package/src/services/persistence/surrealdb.ts +10 -10
- package/src/services/stream-processor/index.ts +796 -84
- package/src/services/stream-processor/permissions.test.ts +47 -0
- package/src/services/stream-processor/permissions.ts +53 -0
- package/src/services/stream-processor/stream-processor.batch.test.ts +186 -0
- package/src/services/stream-processor/stream-processor.prime.test.ts +198 -0
- package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
- package/src/services/stream-processor/stream-processor.test.ts +1 -1
- package/src/services/stream-processor/wasm-types.ts +59 -3
- package/src/services/tabs/broker-client.ts +283 -0
- package/src/services/tabs/broker.test.ts +327 -0
- package/src/services/tabs/coordinator.test.ts +365 -0
- package/src/services/tabs/coordinator.ts +633 -0
- package/src/services/tabs/fake-ports.fixture.ts +112 -0
- package/src/services/tabs/leader-locks.ts +75 -0
- package/src/services/tabs/protocol.ts +258 -0
- package/src/services/tabs/support.ts +36 -0
- package/src/services/tabs/tabs-broker-worker.ts +640 -0
- package/src/sp00ky.auth-order.test.ts +112 -0
- package/src/sp00ky.init-query.test.ts +183 -0
- package/src/sp00ky.local-first.test.ts +60 -0
- package/src/sp00ky.ts +1854 -0
- package/src/types.ts +548 -13
- package/src/utils/blurhash.ts +90 -0
- package/src/utils/error-classification.test.ts +54 -0
- package/src/utils/error-classification.ts +25 -0
- package/src/utils/index.ts +79 -13
- package/src/utils/parser.test.ts +49 -120
- package/src/utils/parser.ts +32 -2
- package/src/utils/semver.test.ts +32 -0
- package/src/utils/semver.ts +30 -0
- package/src/utils/surql.ts +30 -18
- package/src/utils/withRetry.test.ts +1 -1
- package/tsdown.config.ts +86 -1
- package/src/spooky.ts +0 -395
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { RecordId } from 'surrealdb';
|
|
3
|
+
import { DataModule } from './index';
|
|
4
|
+
import type { QueryPlan } from '@spooky-sync/query-builder';
|
|
5
|
+
import type { QueryState, RecordVersionArray } from '../../types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A query's rows are its MEMBERSHIP — the id-set the server put in
|
|
9
|
+
* `_00_list_ref` — not "every locally cached body that matches the WHERE".
|
|
10
|
+
*
|
|
11
|
+
* Those two disagree whenever a row leaves a query's window while still existing
|
|
12
|
+
* upstream: `SyncEngine.handleRemovedRecords` keeps the local body and never
|
|
13
|
+
* re-fetches it, so a predicate re-scan finds that stale body still matching and
|
|
14
|
+
* keeps rendering the row. That is the "removed item comes back" bug, and it was
|
|
15
|
+
* permanent offline because membership used to live only on the session-salted
|
|
16
|
+
* `_00_query` row (wiped on reload) while bodies were durable.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const noop = () => {};
|
|
20
|
+
|
|
21
|
+
function makeLogger(): any {
|
|
22
|
+
const logger: any = { debug: noop, info: noop, warn: noop, error: noop, trace: noop };
|
|
23
|
+
logger.child = () => logger;
|
|
24
|
+
return logger;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const schema = { tables: [{ name: 'thread', columns: {} }] } as any;
|
|
28
|
+
|
|
29
|
+
const plan: QueryPlan = { table: 'thread', where: [['done', '=', false]] } as any;
|
|
30
|
+
|
|
31
|
+
function makeQueryState(
|
|
32
|
+
hash: string,
|
|
33
|
+
opts: {
|
|
34
|
+
remoteArray?: RecordVersionArray;
|
|
35
|
+
localArray?: RecordVersionArray;
|
|
36
|
+
membershipKnown?: boolean;
|
|
37
|
+
membershipKey?: string;
|
|
38
|
+
withPlan?: boolean;
|
|
39
|
+
} = {}
|
|
40
|
+
): QueryState {
|
|
41
|
+
return {
|
|
42
|
+
config: {
|
|
43
|
+
id: new RecordId('_00_query', hash),
|
|
44
|
+
surql: 'SELECT * FROM thread WHERE done = false;',
|
|
45
|
+
plan: opts.withPlan === false ? undefined : plan,
|
|
46
|
+
params: {},
|
|
47
|
+
localArray: opts.localArray ?? [],
|
|
48
|
+
remoteArray: opts.remoteArray ?? [],
|
|
49
|
+
membershipKnown: opts.membershipKnown,
|
|
50
|
+
membershipKey: opts.membershipKey,
|
|
51
|
+
ttl: '10m',
|
|
52
|
+
lastActiveAt: new Date(),
|
|
53
|
+
tableName: 'thread',
|
|
54
|
+
},
|
|
55
|
+
records: [],
|
|
56
|
+
ttlTimer: null,
|
|
57
|
+
ttlDurationMs: 0,
|
|
58
|
+
updateCount: 0,
|
|
59
|
+
lastUpdatedAt: null,
|
|
60
|
+
materializationSamples: [],
|
|
61
|
+
lastIngestLatencyMs: null,
|
|
62
|
+
errorCount: 0,
|
|
63
|
+
status: 'idle',
|
|
64
|
+
phaseSamples: {},
|
|
65
|
+
phaseLast: {},
|
|
66
|
+
registrationTimings: { parseMs: null, planMs: null, snapshotMs: null, wallMs: null },
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Local store holding three thread bodies, ALL of which match the query's
|
|
72
|
+
* predicate. `select` mimics the real engines: when `plan.ids` is set it returns
|
|
73
|
+
* exactly those ids (the membership path); otherwise it returns everything (the
|
|
74
|
+
* predicate-scan path).
|
|
75
|
+
*/
|
|
76
|
+
function makeLocal(pendingRows: Array<{ recordId: RecordId; mutationType: string }> = []) {
|
|
77
|
+
const bodies = new Map(
|
|
78
|
+
['a', 'b', 'c'].map((k) => [`thread:${k}`, { id: new RecordId('thread', k), title: k }])
|
|
79
|
+
);
|
|
80
|
+
const windowRows = new Map<string, { ids: RecordVersionArray; confirmed?: boolean }>();
|
|
81
|
+
const local: any = {
|
|
82
|
+
epoch: 1,
|
|
83
|
+
bodies,
|
|
84
|
+
windowRows,
|
|
85
|
+
select: vi.fn(async (p: QueryPlan) => {
|
|
86
|
+
if (p.ids) {
|
|
87
|
+
return (p.ids as RecordId[])
|
|
88
|
+
.map((id) => bodies.get(id.toString()))
|
|
89
|
+
.filter((r): r is NonNullable<typeof r> => r !== undefined);
|
|
90
|
+
}
|
|
91
|
+
return Array.from(bodies.values());
|
|
92
|
+
}),
|
|
93
|
+
query: vi.fn(async (sql: string, vars?: any) => {
|
|
94
|
+
if (sql.includes('_00_pending_mutations')) return [pendingRows];
|
|
95
|
+
// `createNewQuery` reads the `_00_query` row, then CREATEs it when absent.
|
|
96
|
+
// A reload always takes the absent path: the id is session-salted.
|
|
97
|
+
if (sql.includes('FROM ONLY')) return [null];
|
|
98
|
+
if (sql.startsWith('CREATE')) return [{ id: vars.id, ...vars.data }];
|
|
99
|
+
if (sql.startsWith('UPDATE')) return [null];
|
|
100
|
+
return [Array.from(bodies.values())];
|
|
101
|
+
}),
|
|
102
|
+
getById: vi.fn(async (_table: string, id: RecordId) => windowRows.get(String(id.id)) ?? null),
|
|
103
|
+
upsert: vi.fn(async (_t: string, id: RecordId, data: any) => {
|
|
104
|
+
windowRows.set(String(id.id), data);
|
|
105
|
+
}),
|
|
106
|
+
};
|
|
107
|
+
return local;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function setup(
|
|
111
|
+
stateOpts: Parameters<typeof makeQueryState>[1] = {},
|
|
112
|
+
pendingRows: Array<{ recordId: RecordId; mutationType: string }> = []
|
|
113
|
+
) {
|
|
114
|
+
const hash = 'h1';
|
|
115
|
+
const local = makeLocal(pendingRows);
|
|
116
|
+
const dm = new DataModule({ saveBatch: async () => {} } as any, local, schema, makeLogger(), 100);
|
|
117
|
+
const state = makeQueryState(hash, stateOpts);
|
|
118
|
+
(dm as any).activeQueries.set(hash, state);
|
|
119
|
+
return { dm, state, local, hash };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const ids = (rows: Array<Record<string, any>>) => rows.map((r) => String(r.id));
|
|
123
|
+
const materialize = (dm: DataModule<any>, state: QueryState, ssp?: RecordVersionArray) =>
|
|
124
|
+
(dm as any).materializeRecords(state, ssp) as Promise<Record<string, any>[]>;
|
|
125
|
+
|
|
126
|
+
describe('membership-authoritative rendering', () => {
|
|
127
|
+
it('hides a locally cached body that is absent from membership', async () => {
|
|
128
|
+
// `thread:c` still matches the predicate and its body is still cached, but
|
|
129
|
+
// the server dropped it from the window. It must not render.
|
|
130
|
+
const { dm, state } = setup({
|
|
131
|
+
membershipKnown: true,
|
|
132
|
+
remoteArray: [
|
|
133
|
+
['thread:a', 1],
|
|
134
|
+
['thread:b', 1],
|
|
135
|
+
],
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
expect(ids(await materialize(dm, state))).toEqual(['thread:a', 'thread:b']);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('renders an empty list when membership is known and empty', async () => {
|
|
142
|
+
const { dm, state } = setup({ membershipKnown: true, remoteArray: [] });
|
|
143
|
+
expect(await materialize(dm, state)).toEqual([]);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('falls back to the predicate scan when membership was never established', async () => {
|
|
147
|
+
// A query first run on this device: there is nothing authoritative to render
|
|
148
|
+
// from, so an offline first paint must still show the cached bodies.
|
|
149
|
+
const { dm, state, local } = setup({ membershipKnown: false });
|
|
150
|
+
expect(ids(await materialize(dm, state))).toEqual(['thread:a', 'thread:b', 'thread:c']);
|
|
151
|
+
expect(local.select).toHaveBeenCalledWith(
|
|
152
|
+
expect.objectContaining({ where: plan.where }),
|
|
153
|
+
expect.anything()
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('drops where/limit/offset and keeps orderBy on the membership path', async () => {
|
|
158
|
+
const { dm, state, local } = setup({ membershipKnown: true, remoteArray: [['thread:a', 1]] });
|
|
159
|
+
state.config.plan = { ...plan, limit: 50, offset: 0, orderBy: [['title', 'asc']] } as any;
|
|
160
|
+
|
|
161
|
+
await materialize(dm, state);
|
|
162
|
+
|
|
163
|
+
const passed = local.select.mock.calls.at(-1)![0] as QueryPlan;
|
|
164
|
+
expect(passed.where).toBeUndefined();
|
|
165
|
+
expect(passed.limit).toBeUndefined();
|
|
166
|
+
expect(passed.offset).toBeUndefined();
|
|
167
|
+
expect(passed.orderBy).toEqual([['title', 'asc']]);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe('optimistic writes', () => {
|
|
171
|
+
it('renders a pending create the server has not acknowledged yet', async () => {
|
|
172
|
+
// Not in remoteArray, but the SSP (fed every local write) says it matches.
|
|
173
|
+
const { dm, state } = setup(
|
|
174
|
+
{
|
|
175
|
+
membershipKnown: true,
|
|
176
|
+
remoteArray: [['thread:a', 1]],
|
|
177
|
+
localArray: [
|
|
178
|
+
['thread:a', 1],
|
|
179
|
+
['thread:c', 1],
|
|
180
|
+
],
|
|
181
|
+
},
|
|
182
|
+
[{ recordId: new RecordId('thread', 'c'), mutationType: 'create' }]
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
expect(ids(await materialize(dm, state))).toEqual(['thread:a', 'thread:c']);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('hides a pending update that moved a row OUT of the window', async () => {
|
|
189
|
+
// Pending write, but the SSP no longer lists it: the local body stopped
|
|
190
|
+
// matching. Unioning pending ids blindly would wrongly re-admit it.
|
|
191
|
+
const { dm, state } = setup(
|
|
192
|
+
{
|
|
193
|
+
membershipKnown: true,
|
|
194
|
+
remoteArray: [['thread:a', 1]],
|
|
195
|
+
localArray: [['thread:a', 1]],
|
|
196
|
+
},
|
|
197
|
+
[{ recordId: new RecordId('thread', 'c'), mutationType: 'update' }]
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
expect(ids(await materialize(dm, state))).toEqual(['thread:a']);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('hides a row the server still lists while our DELETE is queued', async () => {
|
|
204
|
+
const { dm, state } = setup(
|
|
205
|
+
{
|
|
206
|
+
membershipKnown: true,
|
|
207
|
+
remoteArray: [
|
|
208
|
+
['thread:a', 1],
|
|
209
|
+
['thread:b', 1],
|
|
210
|
+
],
|
|
211
|
+
},
|
|
212
|
+
[{ recordId: new RecordId('thread', 'b'), mutationType: 'delete' }]
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
expect(ids(await materialize(dm, state))).toEqual(['thread:a']);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('prefers the fresher sspArray over the persisted localArray', async () => {
|
|
219
|
+
const { dm, state } = setup(
|
|
220
|
+
{ membershipKnown: true, remoteArray: [['thread:a', 1]], localArray: [] },
|
|
221
|
+
[{ recordId: new RecordId('thread', 'c'), mutationType: 'create' }]
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
const rows = await materialize(dm, state, [
|
|
225
|
+
['thread:a', 1],
|
|
226
|
+
['thread:c', 1],
|
|
227
|
+
]);
|
|
228
|
+
expect(ids(rows)).toEqual(['thread:a', 'thread:c']);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
describe('render order', () => {
|
|
233
|
+
it('sorts an unordered query so both paints agree', async () => {
|
|
234
|
+
// `_00_list_ref` is selected without an ORDER BY, so membership arrives
|
|
235
|
+
// shuffled. The first paint came from the local scan in id order, so
|
|
236
|
+
// rendering server order here is what made lists visibly reorder about a
|
|
237
|
+
// second after load.
|
|
238
|
+
const { dm, hash, state } = setup();
|
|
239
|
+
state.config.plan = { table: 'thread', where: [['done', '=', false]] } as any;
|
|
240
|
+
|
|
241
|
+
const ids = await (dm as any).buildRenderIds(state.config, [
|
|
242
|
+
['thread:c', 1],
|
|
243
|
+
['thread:a', 1],
|
|
244
|
+
['thread:b', 1],
|
|
245
|
+
]);
|
|
246
|
+
|
|
247
|
+
expect(ids.map(String)).toEqual(['thread:a', 'thread:b', 'thread:c']);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it('leaves an explicitly ordered query to the engine', async () => {
|
|
251
|
+
const { dm, state } = setup();
|
|
252
|
+
state.config.plan = { table: 'thread', orderBy: [['created', 'desc']] } as any;
|
|
253
|
+
|
|
254
|
+
const ids = await (dm as any).buildRenderIds(state.config, [
|
|
255
|
+
['thread:c', 1],
|
|
256
|
+
['thread:a', 1],
|
|
257
|
+
]);
|
|
258
|
+
|
|
259
|
+
// Untouched: the engine applies the ORDER BY over the id set.
|
|
260
|
+
expect(ids.map(String)).toEqual(['thread:c', 'thread:a']);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it('preserves the slice order of a windowed query', async () => {
|
|
264
|
+
// For a window the id-set order IS the window: sorting it would reorder
|
|
265
|
+
// rows within the page.
|
|
266
|
+
const { dm, state } = setup();
|
|
267
|
+
state.config.surql = 'SELECT * FROM thread LIMIT 50 START 100;';
|
|
268
|
+
state.config.plan = { table: 'thread' } as any;
|
|
269
|
+
|
|
270
|
+
const ids = await (dm as any).buildRenderIds(state.config, [
|
|
271
|
+
['thread:c', 1],
|
|
272
|
+
['thread:a', 1],
|
|
273
|
+
]);
|
|
274
|
+
|
|
275
|
+
expect(ids.map(String)).toEqual(['thread:c', 'thread:a']);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
describe('durability across a reload', () => {
|
|
280
|
+
it('updateQueryRemoteArray latches membership and writes _00_window', async () => {
|
|
281
|
+
const { dm, state, local, hash } = setup({ membershipKey: 'stable-key' });
|
|
282
|
+
|
|
283
|
+
await dm.updateQueryRemoteArray(hash, [['thread:a', 1]]);
|
|
284
|
+
|
|
285
|
+
expect(state.config.membershipKnown).toBe(true);
|
|
286
|
+
expect(local.windowRows.get('stable-key')).toMatchObject({ ids: [['thread:a', 1]] });
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('seeds membership from _00_window under a different session salt', async () => {
|
|
290
|
+
// The reload case: `_00_query` is keyed by a `session::id()`-salted hash, so
|
|
291
|
+
// a reload always lands on a fresh row with empty arrays. Only the durable
|
|
292
|
+
// row, keyed session-independently, can carry the removal across.
|
|
293
|
+
const { dm, local } = setup({ membershipKey: 'stable-key' });
|
|
294
|
+
local.windowRows.set('stable-key', { ids: [['thread:a', 1]] });
|
|
295
|
+
|
|
296
|
+
const fresh = await (dm as any).createNewQuery({
|
|
297
|
+
recordId: new RecordId('_00_query', 'a-totally-different-salted-hash'),
|
|
298
|
+
surql: 'SELECT * FROM thread WHERE done = false;',
|
|
299
|
+
params: {},
|
|
300
|
+
ttl: '10m',
|
|
301
|
+
tableName: 'thread',
|
|
302
|
+
plan,
|
|
303
|
+
membershipKey: 'stable-key',
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
expect(fresh.config.membershipKnown).toBe(true);
|
|
307
|
+
expect(fresh.config.remoteArray).toEqual([['thread:a', 1]]);
|
|
308
|
+
// `thread:b`/`thread:c` bodies are still cached and still match, but are
|
|
309
|
+
// not in the persisted list — the reported bug, offline.
|
|
310
|
+
expect(ids(fresh.records)).toEqual(['thread:a']);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it('stays scan-based when no durable row exists', async () => {
|
|
314
|
+
const { dm } = setup({ membershipKey: 'never-written' });
|
|
315
|
+
|
|
316
|
+
const fresh = await (dm as any).createNewQuery({
|
|
317
|
+
recordId: new RecordId('_00_query', 'h-cold'),
|
|
318
|
+
surql: 'SELECT * FROM thread WHERE done = false;',
|
|
319
|
+
params: {},
|
|
320
|
+
ttl: '10m',
|
|
321
|
+
tableName: 'thread',
|
|
322
|
+
plan,
|
|
323
|
+
membershipKey: 'never-written',
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
expect(fresh.config.membershipKnown).toBeFalsy();
|
|
327
|
+
expect(ids(fresh.records)).toEqual(['thread:a', 'thread:b', 'thread:c']);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
// The `_00_query` row as read alongside the edges. `present: true` with
|
|
331
|
+
// `state: null` is a server that predates the marker.
|
|
332
|
+
const meta = (
|
|
333
|
+
over: Partial<{
|
|
334
|
+
present: boolean;
|
|
335
|
+
rowCount: number | null;
|
|
336
|
+
state: 'materializing' | 'ready' | null;
|
|
337
|
+
}> = {}
|
|
338
|
+
) => ({ present: true, rowCount: null, state: null, ...over });
|
|
339
|
+
|
|
340
|
+
it('ignores an empty id-set while the server is still publishing', async () => {
|
|
341
|
+
// The registration race: the SSP writes `state = materializing`, hands
|
|
342
|
+
// the view's initial edges to a coalescing flusher and answers
|
|
343
|
+
// `fn::query::register` before they land, so the read that follows says
|
|
344
|
+
// nothing about the query being empty. Believing it rendered a blank
|
|
345
|
+
// list AND persisted the blankness.
|
|
346
|
+
const { dm, state, local, hash } = setup({
|
|
347
|
+
membershipKey: 'stable-key',
|
|
348
|
+
remoteArray: [['thread:a', 1]],
|
|
349
|
+
membershipKnown: true,
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
const outcome = await dm.updateQueryRemoteArray(hash, [], {
|
|
353
|
+
meta: meta({ rowCount: 26, state: 'materializing' }),
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
expect(outcome).toBe('ignored');
|
|
357
|
+
expect(state.config.remoteArray).toEqual([['thread:a', 1]]);
|
|
358
|
+
expect(local.windowRows.has('stable-key')).toBe(false);
|
|
359
|
+
expect(state.serverState).toBe('materializing');
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it('ignores an empty id-set while the server still reports rows', async () => {
|
|
363
|
+
// `ready` but `rowCount 26` with no edges: the row and the edges
|
|
364
|
+
// disagree (a stranded view, metrics lag). No number of such reads may
|
|
365
|
+
// be taken as "empty".
|
|
366
|
+
const { dm, state, local, hash } = setup({
|
|
367
|
+
membershipKey: 'stable-key',
|
|
368
|
+
remoteArray: [['thread:a', 1]],
|
|
369
|
+
membershipKnown: true,
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
for (let i = 0; i < 5; i++) {
|
|
373
|
+
expect(
|
|
374
|
+
await dm.updateQueryRemoteArray(hash, [], { meta: meta({ rowCount: 26, state: 'ready' }) })
|
|
375
|
+
).toBe('ignored');
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
expect(state.config.remoteArray).toEqual([['thread:a', 1]]);
|
|
379
|
+
expect(local.windowRows.has('stable-key')).toBe(false);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it('believes an empty id-set the moment the server says ready with zero rows', async () => {
|
|
383
|
+
// The genuine transition — the last row left the window. The SSP flips
|
|
384
|
+
// `ready` inside the transaction that writes the edges, so this cannot
|
|
385
|
+
// be observed mid-publish. Honouring it is what stops a removed row
|
|
386
|
+
// resurrecting from the local body cache.
|
|
387
|
+
const { dm, state, local, hash } = setup({
|
|
388
|
+
membershipKey: 'stable-key',
|
|
389
|
+
remoteArray: [['thread:a', 1]],
|
|
390
|
+
membershipKnown: true,
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
const outcome = await dm.updateQueryRemoteArray(hash, [], {
|
|
394
|
+
meta: meta({ rowCount: 0, state: 'ready' }),
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
expect(outcome).toBe('applied');
|
|
398
|
+
expect(state.config.membershipKnown).toBe(true);
|
|
399
|
+
expect(state.config.remoteArray).toEqual([]);
|
|
400
|
+
expect(local.windowRows.get('stable-key')).toEqual(
|
|
401
|
+
expect.objectContaining({ ids: [], confirmed: true })
|
|
402
|
+
);
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
it('believes ready + zero rows with nothing held yet, too', async () => {
|
|
406
|
+
// A genuinely empty query on a cold device must not sit on a spinner
|
|
407
|
+
// waiting for rows that are never coming.
|
|
408
|
+
const { dm, state, hash } = setup({ membershipKey: 'stable-key' });
|
|
409
|
+
|
|
410
|
+
const outcome = await dm.updateQueryRemoteArray(hash, [], {
|
|
411
|
+
meta: meta({ rowCount: 0, state: 'ready' }),
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
expect(outcome).toBe('applied');
|
|
415
|
+
expect(state.config.membershipKnown).toBe(true);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it('treats a missing _00_query row as a lost view, never as an empty one', async () => {
|
|
419
|
+
// A TTL sweep, an SSP reset or a scheduler wipe took the row (and, with
|
|
420
|
+
// it, every edge). The client keeps what it renders, says so once, and
|
|
421
|
+
// asks for a re-registration. Believing the empty here is what blanked
|
|
422
|
+
// lists after every sweep and kept them blank across reloads.
|
|
423
|
+
const { dm, state, local, hash } = setup({
|
|
424
|
+
membershipKey: 'stable-key',
|
|
425
|
+
remoteArray: [['thread:a', 1]],
|
|
426
|
+
membershipKnown: true,
|
|
427
|
+
});
|
|
428
|
+
const onViewLost = vi.fn();
|
|
429
|
+
dm.onViewLost = onViewLost;
|
|
430
|
+
|
|
431
|
+
expect(await dm.updateQueryRemoteArray(hash, [], { meta: meta({ present: false }) })).toBe(
|
|
432
|
+
'view-lost'
|
|
433
|
+
);
|
|
434
|
+
expect(await dm.updateQueryRemoteArray(hash, [], { meta: meta({ present: false }) })).toBe(
|
|
435
|
+
'view-lost'
|
|
436
|
+
);
|
|
437
|
+
|
|
438
|
+
expect(onViewLost).toHaveBeenCalledTimes(1);
|
|
439
|
+
expect(onViewLost).toHaveBeenCalledWith(hash);
|
|
440
|
+
expect(state.viewLost).toBe(true);
|
|
441
|
+
expect(state.config.remoteArray).toEqual([['thread:a', 1]]);
|
|
442
|
+
expect(state.config.membershipKnown).toBe(true);
|
|
443
|
+
expect(local.windowRows.has('stable-key')).toBe(false);
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
it('clears the lost-view mark once a readable row is back', async () => {
|
|
447
|
+
const { dm, state, hash } = setup({
|
|
448
|
+
membershipKey: 'stable-key',
|
|
449
|
+
remoteArray: [['thread:a', 1]],
|
|
450
|
+
membershipKnown: true,
|
|
451
|
+
});
|
|
452
|
+
dm.onViewLost = vi.fn();
|
|
453
|
+
await dm.updateQueryRemoteArray(hash, [], { meta: meta({ present: false }) });
|
|
454
|
+
expect(state.viewLost).toBe(true);
|
|
455
|
+
|
|
456
|
+
// The re-registration republished the view.
|
|
457
|
+
const outcome = await dm.updateQueryRemoteArray(
|
|
458
|
+
hash,
|
|
459
|
+
[
|
|
460
|
+
['thread:a', 1],
|
|
461
|
+
['thread:b', 1],
|
|
462
|
+
],
|
|
463
|
+
{ meta: meta({ rowCount: 2, state: 'ready' }) }
|
|
464
|
+
);
|
|
465
|
+
|
|
466
|
+
expect(outcome).toBe('applied');
|
|
467
|
+
expect(state.viewLost).toBe(false);
|
|
468
|
+
expect(state.serverState).toBe('ready');
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
it('stays non-authoritative when nothing is held and the row is missing', async () => {
|
|
472
|
+
// A cold query whose row is not readable yet: nothing to lose, nothing
|
|
473
|
+
// to believe. The register lifecycle retries; no re-registration storm.
|
|
474
|
+
const { dm, state, hash } = setup({ membershipKey: 'stable-key' });
|
|
475
|
+
const onViewLost = vi.fn();
|
|
476
|
+
dm.onViewLost = onViewLost;
|
|
477
|
+
|
|
478
|
+
expect(await dm.updateQueryRemoteArray(hash, [], { meta: meta({ present: false }) })).toBe(
|
|
479
|
+
'ignored'
|
|
480
|
+
);
|
|
481
|
+
|
|
482
|
+
expect(onViewLost).not.toHaveBeenCalled();
|
|
483
|
+
expect(state.config.membershipKnown).toBeFalsy();
|
|
484
|
+
expect(state.viewLost).toBeFalsy();
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
it('treats an empty set that comes with no row information as a lost view', async () => {
|
|
488
|
+
const { dm, state, hash } = setup({
|
|
489
|
+
remoteArray: [['thread:a', 1]],
|
|
490
|
+
membershipKnown: true,
|
|
491
|
+
});
|
|
492
|
+
dm.onViewLost = vi.fn();
|
|
493
|
+
expect(await dm.updateQueryRemoteArray(hash, [])).toBe('view-lost');
|
|
494
|
+
expect(state.config.remoteArray).toEqual([['thread:a', 1]]);
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
it('judges a server that predates `state` on rowCount alone', async () => {
|
|
498
|
+
const { dm, state, hash } = setup({
|
|
499
|
+
membershipKey: 'stable-key',
|
|
500
|
+
remoteArray: [['thread:a', 1]],
|
|
501
|
+
membershipKnown: true,
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
expect(await dm.updateQueryRemoteArray(hash, [], { meta: meta({ rowCount: 3 }) })).toBe(
|
|
505
|
+
'ignored'
|
|
506
|
+
);
|
|
507
|
+
expect(await dm.updateQueryRemoteArray(hash, [], { meta: meta({ rowCount: null }) })).toBe(
|
|
508
|
+
'ignored'
|
|
509
|
+
);
|
|
510
|
+
expect(state.config.remoteArray).toEqual([['thread:a', 1]]);
|
|
511
|
+
|
|
512
|
+
expect(await dm.updateQueryRemoteArray(hash, [], { meta: meta({ rowCount: 0 }) })).toBe(
|
|
513
|
+
'applied'
|
|
514
|
+
);
|
|
515
|
+
expect(state.config.remoteArray).toEqual([]);
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
it('never grows to believe an unexplained empty on repeated reads', async () => {
|
|
519
|
+
// There is no retry budget any more: a server that cannot explain an
|
|
520
|
+
// empty must not be able to erase a device's data by repeating itself.
|
|
521
|
+
const { dm, state, local, hash } = setup({
|
|
522
|
+
membershipKey: 'stable-key',
|
|
523
|
+
remoteArray: [['thread:a', 1]],
|
|
524
|
+
membershipKnown: true,
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
for (let i = 0; i < 6; i++) {
|
|
528
|
+
await dm.updateQueryRemoteArray(hash, [], { meta: meta({ rowCount: null, state: 'ready' }) });
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
expect(state.config.remoteArray).toEqual([['thread:a', 1]]);
|
|
532
|
+
expect(local.windowRows.has('stable-key')).toBe(false);
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
it('applies an empty set the caller verified upstream', async () => {
|
|
536
|
+
// The LIVE removal path checked that every held id is gone from the
|
|
537
|
+
// server before asking: that is a real answer even without a row.
|
|
538
|
+
const { dm, state, local, hash } = setup({
|
|
539
|
+
membershipKey: 'stable-key',
|
|
540
|
+
remoteArray: [['thread:a', 1]],
|
|
541
|
+
membershipKnown: true,
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
const outcome = await dm.updateQueryRemoteArray(hash, [], { verifiedRemoval: true });
|
|
545
|
+
|
|
546
|
+
expect(outcome).toBe('applied');
|
|
547
|
+
expect(state.config.remoteArray).toEqual([]);
|
|
548
|
+
expect(local.windowRows.get('stable-key')).toEqual(
|
|
549
|
+
expect.objectContaining({ ids: [], confirmed: true })
|
|
550
|
+
);
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it('marks every applied set as confirmed', async () => {
|
|
554
|
+
// Every set that lands is server-backed now, so the durable mirror may
|
|
555
|
+
// always be trusted on the next boot, empty or not.
|
|
556
|
+
const { dm, local, hash } = setup({ membershipKey: 'stable-key' });
|
|
557
|
+
|
|
558
|
+
await dm.updateQueryRemoteArray(hash, [['thread:a', 1]]);
|
|
559
|
+
expect(local.windowRows.get('stable-key')).toMatchObject({ confirmed: true });
|
|
560
|
+
|
|
561
|
+
await dm.updateQueryRemoteArray(hash, [], { meta: meta({ rowCount: 0, state: 'ready' }) });
|
|
562
|
+
expect(local.windowRows.get('stable-key')).toEqual(
|
|
563
|
+
expect.objectContaining({ ids: [], confirmed: true })
|
|
564
|
+
);
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
it('seeds a known-empty membership from a confirmed empty durable row', async () => {
|
|
568
|
+
// The reload after a server-confirmed empty: the list must stay empty
|
|
569
|
+
// instead of re-admitting every cached body until the next poll blanks it.
|
|
570
|
+
const { dm, local } = setup({ membershipKey: 'stable-key' });
|
|
571
|
+
local.windowRows.set('stable-key', { ids: [], confirmed: true });
|
|
572
|
+
|
|
573
|
+
const fresh = await (dm as any).createNewQuery({
|
|
574
|
+
recordId: new RecordId('_00_query', 'h-confirmed-empty'),
|
|
575
|
+
surql: 'SELECT * FROM thread WHERE done = false;',
|
|
576
|
+
params: {},
|
|
577
|
+
ttl: '10m',
|
|
578
|
+
tableName: 'thread',
|
|
579
|
+
plan,
|
|
580
|
+
membershipKey: 'stable-key',
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
expect(fresh.config.membershipKnown).toBe(true);
|
|
584
|
+
expect(fresh.config.remoteArray).toEqual([]);
|
|
585
|
+
expect(fresh.records).toEqual([]);
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
it('does not seed membership from an empty durable row', async () => {
|
|
589
|
+
// Self-heals devices poisoned before the guard existed: an empty durable
|
|
590
|
+
// row is indistinguishable from "never had membership", so it must fall
|
|
591
|
+
// back to the scan rather than paint an empty list.
|
|
592
|
+
const { dm, local } = setup({ membershipKey: 'stable-key' });
|
|
593
|
+
local.windowRows.set('stable-key', { ids: [] });
|
|
594
|
+
|
|
595
|
+
const fresh = await (dm as any).createNewQuery({
|
|
596
|
+
recordId: new RecordId('_00_query', 'h-poisoned'),
|
|
597
|
+
surql: 'SELECT * FROM thread WHERE done = false;',
|
|
598
|
+
params: {},
|
|
599
|
+
ttl: '10m',
|
|
600
|
+
tableName: 'thread',
|
|
601
|
+
plan,
|
|
602
|
+
membershipKey: 'stable-key',
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
expect(fresh.config.membershipKnown).toBeFalsy();
|
|
606
|
+
expect(ids(fresh.records)).toEqual(['thread:a', 'thread:b', 'thread:c']);
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
it('derives the membership key without the session salt', async () => {
|
|
610
|
+
const { dm } = setup();
|
|
611
|
+
const key = () => (dm as any).calculateMembershipKey({ surql: 'X', params: {} });
|
|
612
|
+
|
|
613
|
+
const before = await key();
|
|
614
|
+
dm.setSessionId('session-two');
|
|
615
|
+
expect(await key()).toBe(before);
|
|
616
|
+
|
|
617
|
+
// ...whereas the `_00_query` hash intentionally moves with the session.
|
|
618
|
+
const salted = await (dm as any).calculateHash({ surql: 'X', params: {} });
|
|
619
|
+
dm.setSessionId('session-three');
|
|
620
|
+
expect(await (dm as any).calculateHash({ surql: 'X', params: {} })).not.toBe(salted);
|
|
621
|
+
});
|
|
622
|
+
});
|
|
623
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { RecordId } from 'surrealdb';
|
|
3
|
+
import { DataModule } from './index';
|
|
4
|
+
|
|
5
|
+
// A DELETE that landed in the local store (this tab's own, or one relayed
|
|
6
|
+
// from another tab) needs a forced re-materialize of the table's queries: the
|
|
7
|
+
// SSP may not emit a view update for it.
|
|
8
|
+
|
|
9
|
+
function makeLogger(): any {
|
|
10
|
+
const logger: any = { debug: () => {}, info: () => {}, warn: () => {}, error: () => {}, trace: () => {} };
|
|
11
|
+
logger.child = () => logger;
|
|
12
|
+
return logger;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const schema = { tables: [{ name: 'comment', columns: {} }, { name: 'thread', columns: {} }] } as any;
|
|
16
|
+
|
|
17
|
+
function state(hash: string, tableName: string): any {
|
|
18
|
+
return {
|
|
19
|
+
config: { id: new RecordId('_00_query', hash), tableName, localArray: [], remoteArray: [] },
|
|
20
|
+
records: [],
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe('DataModule.notifyTableQueries', () => {
|
|
25
|
+
it('re-materializes only the queries on that table, isolating failures', async () => {
|
|
26
|
+
const local: any = { epoch: 1, query: vi.fn(async () => [[]]) };
|
|
27
|
+
const dm = new DataModule({ saveBatch: async () => {} } as any, local, schema, makeLogger(), 100);
|
|
28
|
+
(dm as any).activeQueries.set('c1', state('c1', 'comment'));
|
|
29
|
+
(dm as any).activeQueries.set('c2', state('c2', 'comment'));
|
|
30
|
+
(dm as any).activeQueries.set('t1', state('t1', 'thread'));
|
|
31
|
+
const notified: string[] = [];
|
|
32
|
+
dm.notifyQuerySynced = vi.fn(async (hash: string) => {
|
|
33
|
+
notified.push(hash);
|
|
34
|
+
if (hash === 'c1') throw new Error('boom');
|
|
35
|
+
}) as any;
|
|
36
|
+
|
|
37
|
+
await dm.notifyTableQueries('comment');
|
|
38
|
+
|
|
39
|
+
expect(notified.sort()).toEqual(['c1', 'c2']);
|
|
40
|
+
});
|
|
41
|
+
});
|