@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,289 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { RecordId } from 'surrealdb';
|
|
3
|
+
import { Sp00kySync } from './sync';
|
|
4
|
+
|
|
5
|
+
// A LIVE `_00_list_ref` DELETE is how one window learns another deleted a record.
|
|
6
|
+
//
|
|
7
|
+
// It is also how a view teardown arrives: the TTL sweep, an SSP reset, a
|
|
8
|
+
// scheduler wipe and a full republish all delete a view's edges, one DELETE per
|
|
9
|
+
// row, indistinguishable from a peer deleting rows. Obeying them on arrival
|
|
10
|
+
// blanked painted lists n-1 rows at a time. So removals are held briefly,
|
|
11
|
+
// coalesced per query, and VERIFIED upstream before membership moves: an id
|
|
12
|
+
// that no longer exists is dropped (its body is deleted inside the same sync
|
|
13
|
+
// round); anything else is settled by re-reading the server's whole set for the
|
|
14
|
+
// query, where a missing `_00_query` row means "view lost, keep everything".
|
|
15
|
+
//
|
|
16
|
+
// CREATE/UPDATE are applied on arrival as before.
|
|
17
|
+
|
|
18
|
+
const COALESCE_MS = 100;
|
|
19
|
+
|
|
20
|
+
function makeSync(
|
|
21
|
+
opts: {
|
|
22
|
+
membershipKnown?: boolean;
|
|
23
|
+
verify?: { confirmedRemovedIds?: string[]; stillRemoteIds?: string[] };
|
|
24
|
+
} = {}
|
|
25
|
+
) {
|
|
26
|
+
const logger: any = {
|
|
27
|
+
child: () => logger,
|
|
28
|
+
debug: () => {},
|
|
29
|
+
info: () => {},
|
|
30
|
+
warn: () => {},
|
|
31
|
+
error: () => {},
|
|
32
|
+
trace: () => {},
|
|
33
|
+
};
|
|
34
|
+
const queryId = new RecordId('_00_query', 'h1');
|
|
35
|
+
const queryState: any = {
|
|
36
|
+
config: {
|
|
37
|
+
id: queryId,
|
|
38
|
+
localArray: [
|
|
39
|
+
['thread:a', 1],
|
|
40
|
+
['thread:b', 1],
|
|
41
|
+
],
|
|
42
|
+
remoteArray: [
|
|
43
|
+
['thread:a', 1],
|
|
44
|
+
['thread:b', 1],
|
|
45
|
+
],
|
|
46
|
+
membershipKnown: opts.membershipKnown ?? true,
|
|
47
|
+
membershipKey: 'stable-key',
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const updateQueryRemoteArray = vi.fn(async (_h: string, next: any) => {
|
|
52
|
+
queryState.config.remoteArray = next;
|
|
53
|
+
return 'applied';
|
|
54
|
+
});
|
|
55
|
+
const notifyQuerySynced = vi.fn().mockResolvedValue(undefined);
|
|
56
|
+
const dataModule: any = {
|
|
57
|
+
scheduleRematerialize: vi.fn(),
|
|
58
|
+
getQueryById: vi.fn().mockReturnValue(queryState),
|
|
59
|
+
getQueryByHash: vi.fn().mockReturnValue(queryState),
|
|
60
|
+
updateQueryRemoteArray,
|
|
61
|
+
notifyQuerySynced,
|
|
62
|
+
getActiveQueryHashes: () => ['h1'],
|
|
63
|
+
getPendingRecordIds: async () => ({ writes: new Set(), deletes: new Set() }),
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const sync = new Sp00kySync(
|
|
67
|
+
{} as any,
|
|
68
|
+
{ query: vi.fn() } as any,
|
|
69
|
+
{} as any,
|
|
70
|
+
dataModule,
|
|
71
|
+
{} as any,
|
|
72
|
+
logger
|
|
73
|
+
);
|
|
74
|
+
// `runSyncForQuery` drives the engine + scheduler; both are out of scope here.
|
|
75
|
+
// It reports which removed ids were verified gone upstream and which still
|
|
76
|
+
// exist there.
|
|
77
|
+
const runSyncForQuery = vi.fn().mockResolvedValue({
|
|
78
|
+
confirmedRemovedIds: opts.verify?.confirmedRemovedIds ?? [],
|
|
79
|
+
stillRemoteIds: opts.verify?.stillRemoteIds ?? [],
|
|
80
|
+
});
|
|
81
|
+
(sync as any).runSyncForQuery = runSyncForQuery;
|
|
82
|
+
// The targeted server re-read a non-confirmed removal falls back to.
|
|
83
|
+
const refetch = vi.fn().mockResolvedValue({ changed: false, outcome: 'unchanged' });
|
|
84
|
+
(sync as any).refetchListRefForQuery = refetch;
|
|
85
|
+
|
|
86
|
+
const live = (action: 'CREATE' | 'UPDATE' | 'DELETE', id: string, version: number) =>
|
|
87
|
+
(sync as any).handleRemoteListRefChange(
|
|
88
|
+
action,
|
|
89
|
+
queryId,
|
|
90
|
+
new RecordId('thread', id),
|
|
91
|
+
version
|
|
92
|
+
) as Promise<void>;
|
|
93
|
+
// Let the coalescing window and the zero-delay re-read timer fire.
|
|
94
|
+
const settle = () => vi.advanceTimersByTimeAsync(COALESCE_MS + 10);
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
sync,
|
|
98
|
+
queryState,
|
|
99
|
+
updateQueryRemoteArray,
|
|
100
|
+
notifyQuerySynced,
|
|
101
|
+
runSyncForQuery,
|
|
102
|
+
refetch,
|
|
103
|
+
live,
|
|
104
|
+
settle,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
describe('LIVE list_ref removal → membership', () => {
|
|
109
|
+
beforeEach(() => {
|
|
110
|
+
vi.useFakeTimers();
|
|
111
|
+
vi.clearAllMocks();
|
|
112
|
+
});
|
|
113
|
+
afterEach(() => vi.useRealTimers());
|
|
114
|
+
|
|
115
|
+
it('does not move membership on arrival', async () => {
|
|
116
|
+
const { queryState, updateQueryRemoteArray, runSyncForQuery, live } = makeSync({
|
|
117
|
+
verify: { confirmedRemovedIds: ['thread:b'] },
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
await live('DELETE', 'b', 2);
|
|
121
|
+
|
|
122
|
+
expect(updateQueryRemoteArray).not.toHaveBeenCalled();
|
|
123
|
+
expect(runSyncForQuery).not.toHaveBeenCalled();
|
|
124
|
+
expect(queryState.config.remoteArray).toEqual([
|
|
125
|
+
['thread:a', 1],
|
|
126
|
+
['thread:b', 1],
|
|
127
|
+
]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('drops a removed id once it is verified gone upstream', async () => {
|
|
131
|
+
const { queryState, updateQueryRemoteArray, notifyQuerySynced, runSyncForQuery, refetch, live, settle } =
|
|
132
|
+
makeSync({ verify: { confirmedRemovedIds: ['thread:b'] } });
|
|
133
|
+
|
|
134
|
+
await live('DELETE', 'b', 2);
|
|
135
|
+
await settle();
|
|
136
|
+
|
|
137
|
+
expect(runSyncForQuery).toHaveBeenCalledTimes(1);
|
|
138
|
+
const [, diff] = runSyncForQuery.mock.calls[0];
|
|
139
|
+
expect(diff.added).toEqual([]);
|
|
140
|
+
expect(diff.updated).toEqual([]);
|
|
141
|
+
expect(diff.removed.map(String)).toEqual(['thread:b']);
|
|
142
|
+
expect(updateQueryRemoteArray).toHaveBeenCalledWith('h1', [['thread:a', 1]], {
|
|
143
|
+
verifiedRemoval: true,
|
|
144
|
+
});
|
|
145
|
+
expect(queryState.config.remoteArray).toEqual([['thread:a', 1]]);
|
|
146
|
+
// A removal-only diff gets no re-render for free: it has to be forced.
|
|
147
|
+
expect(notifyQuerySynced).toHaveBeenCalledWith('h1');
|
|
148
|
+
expect(refetch).not.toHaveBeenCalled();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('keeps a row that still exists upstream and re-reads the server set instead', async () => {
|
|
152
|
+
// The row left this view's edges but is still there: a membership change
|
|
153
|
+
// to be read back as a whole set, or a view teardown to be recognised by
|
|
154
|
+
// its missing `_00_query` row. Either way, not a blind removal.
|
|
155
|
+
const { queryState, updateQueryRemoteArray, notifyQuerySynced, refetch, live, settle } = makeSync({
|
|
156
|
+
verify: { stillRemoteIds: ['thread:b'] },
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
await live('DELETE', 'b', 2);
|
|
160
|
+
await settle();
|
|
161
|
+
|
|
162
|
+
expect(updateQueryRemoteArray).not.toHaveBeenCalled();
|
|
163
|
+
expect(notifyQuerySynced).not.toHaveBeenCalled();
|
|
164
|
+
expect(queryState.config.remoteArray).toEqual([
|
|
165
|
+
['thread:a', 1],
|
|
166
|
+
['thread:b', 1],
|
|
167
|
+
]);
|
|
168
|
+
expect(refetch).toHaveBeenCalledWith('h1');
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('coalesces a burst of removals into one verification', async () => {
|
|
172
|
+
// A sweep or a republish delivers a view's whole set within a few ms.
|
|
173
|
+
const { runSyncForQuery, live, settle } = makeSync({
|
|
174
|
+
verify: { confirmedRemovedIds: ['thread:a', 'thread:b'] },
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
await live('DELETE', 'a', 2);
|
|
178
|
+
await vi.advanceTimersByTimeAsync(30);
|
|
179
|
+
await live('DELETE', 'b', 2);
|
|
180
|
+
await live('DELETE', 'b', 2);
|
|
181
|
+
await settle();
|
|
182
|
+
|
|
183
|
+
expect(runSyncForQuery).toHaveBeenCalledTimes(1);
|
|
184
|
+
const [, diff] = runSyncForQuery.mock.calls[0];
|
|
185
|
+
expect(diff.removed.map(String).sort()).toEqual(['thread:a', 'thread:b']);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('falls back to a server re-read when the verification could not run', async () => {
|
|
189
|
+
// `handleRemovedRecords` answers with nothing on a failed existence check:
|
|
190
|
+
// neither confirmed nor still-remote. Nothing may be dropped on that.
|
|
191
|
+
const { updateQueryRemoteArray, refetch, live, settle } = makeSync({ verify: {} });
|
|
192
|
+
|
|
193
|
+
await live('DELETE', 'b', 2);
|
|
194
|
+
await settle();
|
|
195
|
+
|
|
196
|
+
expect(updateQueryRemoteArray).not.toHaveBeenCalled();
|
|
197
|
+
expect(refetch).toHaveBeenCalledWith('h1');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('does not force a re-render when rows were added (the stream update covers it)', async () => {
|
|
201
|
+
const { notifyQuerySynced, live } = makeSync();
|
|
202
|
+
await live('CREATE', 'c', 1);
|
|
203
|
+
expect(notifyQuerySynced).not.toHaveBeenCalled();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('adds a newly-arrived id to membership', async () => {
|
|
207
|
+
const { updateQueryRemoteArray, live } = makeSync();
|
|
208
|
+
|
|
209
|
+
await live('CREATE', 'c', 1);
|
|
210
|
+
|
|
211
|
+
expect(updateQueryRemoteArray).toHaveBeenCalledWith('h1', [
|
|
212
|
+
['thread:a', 1],
|
|
213
|
+
['thread:b', 1],
|
|
214
|
+
['thread:c', 1],
|
|
215
|
+
]);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// Every tab that ingested a write optimistically (its own, or one relayed
|
|
219
|
+
// from another tab) already holds the row at the server's version, so the
|
|
220
|
+
// fetch diff is empty. Membership still has to be recorded, or the row lives
|
|
221
|
+
// on the settled-write grace alone until the poll catches it.
|
|
222
|
+
it('adds membership for a row the circuit already holds at the server version', async () => {
|
|
223
|
+
const { queryState, updateQueryRemoteArray, runSyncForQuery, live } = makeSync();
|
|
224
|
+
queryState.config.localArray = [
|
|
225
|
+
['thread:a', 1],
|
|
226
|
+
['thread:b', 1],
|
|
227
|
+
['thread:c', 1],
|
|
228
|
+
];
|
|
229
|
+
|
|
230
|
+
await live('CREATE', 'c', 1);
|
|
231
|
+
|
|
232
|
+
expect(updateQueryRemoteArray).toHaveBeenCalledWith('h1', [
|
|
233
|
+
['thread:a', 1],
|
|
234
|
+
['thread:b', 1],
|
|
235
|
+
['thread:c', 1],
|
|
236
|
+
]);
|
|
237
|
+
// …without a refetch: the diff handed to the sync is empty.
|
|
238
|
+
expect(runSyncForQuery).toHaveBeenCalledWith('h1', {
|
|
239
|
+
added: [],
|
|
240
|
+
updated: [],
|
|
241
|
+
removed: [],
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('records a bumped version on UPDATE without rewriting an unchanged list', async () => {
|
|
246
|
+
const { updateQueryRemoteArray, live } = makeSync();
|
|
247
|
+
|
|
248
|
+
await live('UPDATE', 'b', 1);
|
|
249
|
+
expect(updateQueryRemoteArray).not.toHaveBeenCalled();
|
|
250
|
+
|
|
251
|
+
await live('UPDATE', 'b', 2);
|
|
252
|
+
expect(updateQueryRemoteArray).toHaveBeenCalledWith('h1', [
|
|
253
|
+
['thread:a', 1],
|
|
254
|
+
['thread:b', 2],
|
|
255
|
+
]);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('leaves membership alone while it is still unknown', async () => {
|
|
259
|
+
// Nothing authoritative has arrived yet, so there is no list to amend —
|
|
260
|
+
// registration will supply the whole thing shortly. The body delete of a
|
|
261
|
+
// verified-gone row still happens inside the sync round.
|
|
262
|
+
const { updateQueryRemoteArray, runSyncForQuery, live, settle } = makeSync({
|
|
263
|
+
membershipKnown: false,
|
|
264
|
+
verify: { confirmedRemovedIds: ['thread:b'] },
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
await live('DELETE', 'b', 2);
|
|
268
|
+
await settle();
|
|
269
|
+
|
|
270
|
+
expect(runSyncForQuery).toHaveBeenCalledTimes(1);
|
|
271
|
+
expect(updateQueryRemoteArray).not.toHaveBeenCalled();
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('is a no-op for an unknown query', async () => {
|
|
275
|
+
const { sync, updateQueryRemoteArray, runSyncForQuery, settle } = makeSync();
|
|
276
|
+
(sync as any).dataModule.getQueryById = vi.fn().mockReturnValue(undefined);
|
|
277
|
+
|
|
278
|
+
await (sync as any).handleRemoteListRefChange(
|
|
279
|
+
'DELETE',
|
|
280
|
+
new RecordId('_00_query', 'other'),
|
|
281
|
+
new RecordId('thread', 'b'),
|
|
282
|
+
2
|
|
283
|
+
);
|
|
284
|
+
await settle();
|
|
285
|
+
|
|
286
|
+
expect(updateQueryRemoteArray).not.toHaveBeenCalled();
|
|
287
|
+
expect(runSyncForQuery).not.toHaveBeenCalled();
|
|
288
|
+
});
|
|
289
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { Sp00kySync } from './sync';
|
|
3
|
+
|
|
4
|
+
// The reconnect handler re-registers active queries and re-issues the ref LIVE.
|
|
5
|
+
// It must fire on BOTH drop paths, and the distinction is the whole bug:
|
|
6
|
+
//
|
|
7
|
+
// - recovered drop: error -> reconnecting -> connected (no `disconnected`!)
|
|
8
|
+
// - gave-up drop: error -> disconnected -> (supervisor) -> connected
|
|
9
|
+
//
|
|
10
|
+
// Watching only `disconnected` therefore misses every *successful* reconnect —
|
|
11
|
+
// the common case — and leaves the dead server-side LIVE in place with the
|
|
12
|
+
// list_ref poll silently the only sync path.
|
|
13
|
+
|
|
14
|
+
/** Minimal stand-in for the SDK's event publisher. */
|
|
15
|
+
function makeClient() {
|
|
16
|
+
const listeners = new Map<string, Array<(...a: any[]) => void>>();
|
|
17
|
+
return {
|
|
18
|
+
subscribe(event: string, cb: (...a: any[]) => void) {
|
|
19
|
+
const arr = listeners.get(event) ?? [];
|
|
20
|
+
arr.push(cb);
|
|
21
|
+
listeners.set(event, arr);
|
|
22
|
+
return () => {
|
|
23
|
+
/* noop */
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
emit(event: string, ...args: any[]) {
|
|
27
|
+
for (const cb of listeners.get(event) ?? []) cb(...args);
|
|
28
|
+
},
|
|
29
|
+
liveOf: async () => ({ subscribe: () => () => {} }),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function makeSync(hashes: string[], userId: string | null = 'user:alice') {
|
|
34
|
+
const logger: any = {
|
|
35
|
+
child: () => logger,
|
|
36
|
+
debug: () => {},
|
|
37
|
+
info: () => {},
|
|
38
|
+
warn: () => {},
|
|
39
|
+
error: () => {},
|
|
40
|
+
trace: () => {},
|
|
41
|
+
};
|
|
42
|
+
const client = makeClient();
|
|
43
|
+
const status = { value: 'connected' as string };
|
|
44
|
+
const remote: any = {
|
|
45
|
+
query: vi.fn().mockResolvedValue(['live-uuid']),
|
|
46
|
+
getClient: () => client,
|
|
47
|
+
getStatus: () => status.value,
|
|
48
|
+
};
|
|
49
|
+
const dataModule: any = {
|
|
50
|
+
getActiveQueryHashes: () => hashes,
|
|
51
|
+
getCurrentUserId: () => userId,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const sync = new Sp00kySync({} as any, remote, {} as any, dataModule, {} as any, logger);
|
|
55
|
+
|
|
56
|
+
// `subscribeToReconnect` is normally wired from init(), which would also start
|
|
57
|
+
// timers and load the outbox. Call it directly instead.
|
|
58
|
+
(sync as any).currentUserId = userId;
|
|
59
|
+
(sync as any).subscribeToReconnect();
|
|
60
|
+
|
|
61
|
+
const enqueued: any[] = [];
|
|
62
|
+
(sync as any).scheduler = { enqueueDownEvent: (e: any) => enqueued.push(e) };
|
|
63
|
+
|
|
64
|
+
return { sync, client, remote, enqueued, status };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Let the `connected` handler's un-awaited `restartRefLiveQuery()` chain run. */
|
|
68
|
+
const flush = () => new Promise((r) => setTimeout(r, 0));
|
|
69
|
+
|
|
70
|
+
describe('sync reconnect re-subscription', () => {
|
|
71
|
+
beforeEach(() => vi.clearAllMocks());
|
|
72
|
+
|
|
73
|
+
it('re-registers queries and restarts LIVE on a recovered drop (reconnecting -> connected)', async () => {
|
|
74
|
+
const { client, remote, enqueued } = makeSync(['h1', 'h2']);
|
|
75
|
+
|
|
76
|
+
// The SDK's own reconnect: no `disconnected` is ever published.
|
|
77
|
+
client.emit('reconnecting');
|
|
78
|
+
client.emit('connected');
|
|
79
|
+
await flush();
|
|
80
|
+
|
|
81
|
+
expect(enqueued).toEqual([
|
|
82
|
+
{ type: 'register', payload: { hash: 'h1' } },
|
|
83
|
+
{ type: 'register', payload: { hash: 'h2' } },
|
|
84
|
+
]);
|
|
85
|
+
const sqls = (remote.query as any).mock.calls.map((c: any[]) => c[0]);
|
|
86
|
+
expect(sqls.some((s: string) => s.startsWith('LIVE SELECT'))).toBe(true);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('re-registers queries and restarts LIVE after the SDK gives up (disconnected -> connected)', async () => {
|
|
90
|
+
const { client, remote, enqueued } = makeSync(['h1']);
|
|
91
|
+
|
|
92
|
+
client.emit('disconnected');
|
|
93
|
+
client.emit('connected');
|
|
94
|
+
await flush();
|
|
95
|
+
|
|
96
|
+
expect(enqueued).toEqual([{ type: 'register', payload: { hash: 'h1' } }]);
|
|
97
|
+
const sqls = (remote.query as any).mock.calls.map((c: any[]) => c[0]);
|
|
98
|
+
expect(sqls.some((s: string) => s.startsWith('LIVE SELECT'))).toBe(true);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('does nothing on the initial connect', async () => {
|
|
102
|
+
const { client, remote, enqueued } = makeSync(['h1']);
|
|
103
|
+
|
|
104
|
+
client.emit('connected');
|
|
105
|
+
await flush();
|
|
106
|
+
|
|
107
|
+
expect(enqueued).toEqual([]);
|
|
108
|
+
expect(remote.query).not.toHaveBeenCalled();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('only reacts once per drop', async () => {
|
|
112
|
+
const { client, enqueued } = makeSync(['h1']);
|
|
113
|
+
|
|
114
|
+
client.emit('reconnecting');
|
|
115
|
+
client.emit('connected');
|
|
116
|
+
await flush();
|
|
117
|
+
// A spurious second `connected` (e.g. a re-published event) must not
|
|
118
|
+
// trigger a second refetch storm.
|
|
119
|
+
client.emit('connected');
|
|
120
|
+
await flush();
|
|
121
|
+
|
|
122
|
+
expect(enqueued).toHaveLength(1);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("does not KILL the dead session's LIVE uuid on reconnect", async () => {
|
|
126
|
+
const { sync, client, remote } = makeSync(['h1']);
|
|
127
|
+
// A LIVE was running before the drop. Its uuid belongs to the old
|
|
128
|
+
// WebSocket session, so KILLing it on the new socket is pointless work
|
|
129
|
+
// queued ahead of the restart that actually matters.
|
|
130
|
+
const unsub = vi.fn();
|
|
131
|
+
(sync as any).currentLiveQueryUuid = 'stale-uuid';
|
|
132
|
+
(sync as any).liveQueryUnsubscribe = unsub;
|
|
133
|
+
|
|
134
|
+
client.emit('reconnecting');
|
|
135
|
+
expect(unsub).toHaveBeenCalled();
|
|
136
|
+
expect((sync as any).currentLiveQueryUuid).toBeNull();
|
|
137
|
+
|
|
138
|
+
client.emit('connected');
|
|
139
|
+
await flush();
|
|
140
|
+
|
|
141
|
+
const sqls = (remote.query as any).mock.calls.map((c: any[]) => c[0]);
|
|
142
|
+
expect(sqls.some((q: string) => q.includes('KILL'))).toBe(false);
|
|
143
|
+
expect(sqls.some((q: string) => q.startsWith('LIVE SELECT'))).toBe(true);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { RecordId } from 'surrealdb';
|
|
3
|
+
import { Sp00kySync } from './sync';
|
|
4
|
+
|
|
5
|
+
// A LIVE (or poll) UPDATE for a row already in membership fetches the new body
|
|
6
|
+
// into the local store — and that can be the end of it: the circuit sees the
|
|
7
|
+
// same id-set and emits no stream update, and nothing else re-reads the query.
|
|
8
|
+
// Measured on staging: a `conversation` row reached the store at `_00_rv` 8
|
|
9
|
+
// while the query kept rendering version 2 for minutes. `runSyncForQuery`
|
|
10
|
+
// must therefore land a synthetic re-materialize whenever a fetch produced
|
|
11
|
+
// no stream update of its own.
|
|
12
|
+
|
|
13
|
+
function makeSync(opts: { streamUpdatePending: boolean }) {
|
|
14
|
+
const logger: any = {
|
|
15
|
+
child: () => logger,
|
|
16
|
+
debug: () => {},
|
|
17
|
+
info: () => {},
|
|
18
|
+
warn: () => {},
|
|
19
|
+
error: () => {},
|
|
20
|
+
trace: () => {},
|
|
21
|
+
};
|
|
22
|
+
const queryState: any = {
|
|
23
|
+
config: {
|
|
24
|
+
id: new RecordId('_00_query', 'h1'),
|
|
25
|
+
// Membership already knows the newer version; the circuit still holds 1.
|
|
26
|
+
localArray: [['conversation:c1', 1]],
|
|
27
|
+
remoteArray: [['conversation:c1', 8]],
|
|
28
|
+
membershipKnown: true,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
const calls: string[] = [];
|
|
32
|
+
const dataModule: any = {
|
|
33
|
+
getQueryByHash: vi.fn().mockReturnValue(queryState),
|
|
34
|
+
getPendingRecordIds: async () => ({ writes: new Set(), deletes: new Set() }),
|
|
35
|
+
beginFetching: vi.fn(() => calls.push('beginFetching')),
|
|
36
|
+
endFetching: vi.fn(() => calls.push('endFetching')),
|
|
37
|
+
recordRemoteFetch: vi.fn(),
|
|
38
|
+
updateQueryLocalArray: vi.fn(),
|
|
39
|
+
scheduleRematerialize: vi.fn(() => calls.push('scheduleRematerialize')),
|
|
40
|
+
// First flush reports whether a real stream update was pending; a second
|
|
41
|
+
// flush (after the synthetic schedule) always lands something.
|
|
42
|
+
flushPendingStreamUpdate: vi
|
|
43
|
+
.fn()
|
|
44
|
+
.mockImplementationOnce(async () => {
|
|
45
|
+
calls.push('flush');
|
|
46
|
+
return opts.streamUpdatePending;
|
|
47
|
+
})
|
|
48
|
+
.mockImplementation(async () => {
|
|
49
|
+
calls.push('flush');
|
|
50
|
+
return true;
|
|
51
|
+
}),
|
|
52
|
+
};
|
|
53
|
+
const sync = new Sp00kySync({} as any, { query: vi.fn() } as any, {} as any, dataModule, {} as any, logger);
|
|
54
|
+
(sync as any).syncEngine = {
|
|
55
|
+
syncRecords: vi.fn(async () => {
|
|
56
|
+
calls.push('syncRecords');
|
|
57
|
+
return { remoteFetchMs: 3, stillRemoteIds: [] };
|
|
58
|
+
}),
|
|
59
|
+
};
|
|
60
|
+
const run = () =>
|
|
61
|
+
(sync as any).runSyncForQuery('h1', {
|
|
62
|
+
added: [],
|
|
63
|
+
updated: [{ id: 'conversation:c1', version: 8 }],
|
|
64
|
+
removed: [],
|
|
65
|
+
}) as Promise<void>;
|
|
66
|
+
return { run, dataModule, calls };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
describe('runSyncForQuery re-materializes a fetched row the circuit stayed silent about', () => {
|
|
70
|
+
it('schedules and lands a synthetic re-materialize before going idle', async () => {
|
|
71
|
+
const { run, dataModule, calls } = makeSync({ streamUpdatePending: false });
|
|
72
|
+
await run();
|
|
73
|
+
expect(dataModule.scheduleRematerialize).toHaveBeenCalledWith('h1');
|
|
74
|
+
expect(dataModule.flushPendingStreamUpdate).toHaveBeenCalledTimes(2);
|
|
75
|
+
// The re-render lands BEFORE `endFetching`, so `idle` never races ahead
|
|
76
|
+
// of the rows it describes.
|
|
77
|
+
expect(calls).toEqual([
|
|
78
|
+
'beginFetching',
|
|
79
|
+
'syncRecords',
|
|
80
|
+
'flush',
|
|
81
|
+
'scheduleRematerialize',
|
|
82
|
+
'flush',
|
|
83
|
+
'endFetching',
|
|
84
|
+
]);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('leaves a real stream update alone', async () => {
|
|
88
|
+
const { run, dataModule } = makeSync({ streamUpdatePending: true });
|
|
89
|
+
await run();
|
|
90
|
+
expect(dataModule.scheduleRematerialize).not.toHaveBeenCalled();
|
|
91
|
+
expect(dataModule.flushPendingStreamUpdate).toHaveBeenCalledTimes(1);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, it, expect, vi, afterEach } from 'vitest';
|
|
2
|
+
import { Sp00kySync } from './sync';
|
|
3
|
+
|
|
4
|
+
// The list_ref poll backs off toward a 5s cap on a quiet page. A settled write
|
|
5
|
+
// that is still waiting for its membership edge must hold the poll at its base
|
|
6
|
+
// cadence instead: the row is rendered on the settled-write grace alone, so
|
|
7
|
+
// the edge has to be found on the first poll after it lands, not after the
|
|
8
|
+
// backoff has grown. Otherwise a missed LIVE notification during a server
|
|
9
|
+
// stall turns into "my message vanished" even though the edge arrived.
|
|
10
|
+
|
|
11
|
+
function makeSync(pending: () => boolean) {
|
|
12
|
+
const logger: any = {
|
|
13
|
+
child: () => logger,
|
|
14
|
+
debug: () => {},
|
|
15
|
+
info: () => {},
|
|
16
|
+
warn: () => {},
|
|
17
|
+
error: () => {},
|
|
18
|
+
trace: () => {},
|
|
19
|
+
};
|
|
20
|
+
const remote: any = { query: vi.fn().mockResolvedValue([true]) };
|
|
21
|
+
const dataModule: any = {
|
|
22
|
+
getActiveQueryHashes: () => ['h1'],
|
|
23
|
+
hasSettledWritesPending: pending,
|
|
24
|
+
};
|
|
25
|
+
const sync = new Sp00kySync({} as any, remote, {} as any, dataModule, {} as any, logger);
|
|
26
|
+
// A poll that never observes a change: the only thing driving the streak is
|
|
27
|
+
// the settled-write signal.
|
|
28
|
+
(sync as any).pollListRefForActiveQueries = vi.fn().mockResolvedValue(false);
|
|
29
|
+
return sync;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe('list_ref poll cadence while a settled write awaits membership', () => {
|
|
33
|
+
afterEach(() => vi.useRealTimers());
|
|
34
|
+
|
|
35
|
+
it('stays at the base cadence while a write is pending, then backs off', async () => {
|
|
36
|
+
vi.useFakeTimers();
|
|
37
|
+
let pending = true;
|
|
38
|
+
const sync = makeSync(() => pending);
|
|
39
|
+
const base = sync.refSyncIntervalMs;
|
|
40
|
+
|
|
41
|
+
(sync as any).startListRefPoll();
|
|
42
|
+
|
|
43
|
+
// Three quiet ticks with a write outstanding: no backoff at all.
|
|
44
|
+
for (let i = 0; i < 3; i++) {
|
|
45
|
+
await vi.advanceTimersByTimeAsync(base);
|
|
46
|
+
expect((sync as any).listRefIdleStreak).toBe(0);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Membership caught up: the ordinary idle backoff resumes.
|
|
50
|
+
pending = false;
|
|
51
|
+
await vi.advanceTimersByTimeAsync(base);
|
|
52
|
+
expect((sync as any).listRefIdleStreak).toBe(1);
|
|
53
|
+
await vi.advanceTimersByTimeAsync(base * 2);
|
|
54
|
+
expect((sync as any).listRefIdleStreak).toBe(2);
|
|
55
|
+
|
|
56
|
+
(sync as any).stopListRefPoll();
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { RecordId } from 'surrealdb';
|
|
3
|
+
import { Sp00kySync } from './sync';
|
|
4
|
+
import { buildSubqueryListRefSelect } from './utils';
|
|
5
|
+
import { encodeRecordId } from '../../utils/index';
|
|
6
|
+
|
|
7
|
+
// Guards the CLIENT half of `.related()` (author/comments) delivery: given
|
|
8
|
+
// subquery-child edges on the server (`_00_list_ref…` rows with
|
|
9
|
+
// `parent IS NOT NONE`), `syncSubqueryChildren` MUST pull the child bodies
|
|
10
|
+
// through the sync engine so a later re-materialization attaches them to the
|
|
11
|
+
// parent. If this stops forwarding the child ids, related fields come back
|
|
12
|
+
// empty (threads render with an "Anonymous" author, comments vanish) even
|
|
13
|
+
// though the server wrote the edges correctly.
|
|
14
|
+
|
|
15
|
+
function makeSync() {
|
|
16
|
+
const logger: any = {
|
|
17
|
+
child: () => logger,
|
|
18
|
+
debug: () => {}, info: () => {}, warn: () => {}, error: () => {}, trace: () => {},
|
|
19
|
+
};
|
|
20
|
+
const remote: any = { query: vi.fn() };
|
|
21
|
+
const queryId = new RecordId('_00_query', 'h1');
|
|
22
|
+
const queryState: any = { config: { id: queryId, subqueryRemoteArray: [] } };
|
|
23
|
+
const dataModule: any = { getQueryByHash: vi.fn().mockReturnValue(queryState) };
|
|
24
|
+
|
|
25
|
+
const sync = new Sp00kySync(
|
|
26
|
+
{} as any, remote, {} as any, dataModule, {} as any, logger,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
// syncEngine is constructed internally; replace it with a spy.
|
|
30
|
+
const syncRecords = vi.fn().mockResolvedValue(undefined);
|
|
31
|
+
(sync as any).syncEngine = { syncRecords };
|
|
32
|
+
// Pin the resolved per-user list_ref table (auth routing is out of scope here).
|
|
33
|
+
(sync as any).listRefTable = () => '_00_list_ref_user_x';
|
|
34
|
+
|
|
35
|
+
const run = (hash: string) => (sync as any).syncSubqueryChildren(hash) as Promise<void>;
|
|
36
|
+
return { remote, syncRecords, queryState, queryId, run };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe('syncSubqueryChildren — related-child delivery', () => {
|
|
40
|
+
beforeEach(() => vi.clearAllMocks());
|
|
41
|
+
|
|
42
|
+
it('reads the subquery edges (parent IS NOT NONE) scoped to the query id', async () => {
|
|
43
|
+
const { remote, run, queryId } = makeSync();
|
|
44
|
+
remote.query.mockResolvedValue([[]]);
|
|
45
|
+
|
|
46
|
+
await run('h1');
|
|
47
|
+
|
|
48
|
+
expect(remote.query).toHaveBeenCalledWith(
|
|
49
|
+
buildSubqueryListRefSelect('_00_list_ref_user_x'),
|
|
50
|
+
{ in: queryId }
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('forwards each subquery child id to the sync engine so its body is cached', async () => {
|
|
55
|
+
const { remote, syncRecords, run } = makeSync();
|
|
56
|
+
// Server has one author child edge for this query.
|
|
57
|
+
remote.query.mockResolvedValue([[
|
|
58
|
+
{ out: new RecordId('user', 'u'), version: 1 },
|
|
59
|
+
]]);
|
|
60
|
+
|
|
61
|
+
await run('h1');
|
|
62
|
+
|
|
63
|
+
expect(syncRecords).toHaveBeenCalledTimes(1);
|
|
64
|
+
const arg = syncRecords.mock.calls[0][0];
|
|
65
|
+
expect(arg.added).toHaveLength(1);
|
|
66
|
+
expect(encodeRecordId(arg.added[0].id)).toBe('user:u');
|
|
67
|
+
expect(arg.added[0].version).toBe(1);
|
|
68
|
+
expect(arg.removed).toEqual([]); // shared child bodies are never deleted here
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('is idempotent: unchanged edges do not refetch bodies', async () => {
|
|
72
|
+
const { remote, syncRecords, run, queryState } = makeSync();
|
|
73
|
+
queryState.config.subqueryRemoteArray = [['user:u', 1]];
|
|
74
|
+
remote.query.mockResolvedValue([[
|
|
75
|
+
{ out: new RecordId('user', 'u'), version: 1 },
|
|
76
|
+
]]);
|
|
77
|
+
|
|
78
|
+
await run('h1');
|
|
79
|
+
|
|
80
|
+
expect(syncRecords).not.toHaveBeenCalled();
|
|
81
|
+
});
|
|
82
|
+
});
|