@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,167 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { Sp00kySync } from './sync';
|
|
3
|
+
|
|
4
|
+
// The idle list-ref poll is the only health signal that runs on a quiet page.
|
|
5
|
+
// These tests exercise `pollListRefForActiveQueries` -> `recordSyncOutcome` in
|
|
6
|
+
// isolation: a run of network-failed cycles degrades, and a single clean cycle
|
|
7
|
+
// recovers WITHOUT any mutation (the reported bug). Construction is cheap —
|
|
8
|
+
// Sp00kySync's constructor only stores refs and calls logger.child; no timers or
|
|
9
|
+
// I/O start until init(), which we never call.
|
|
10
|
+
|
|
11
|
+
const CONNECTION_UNAVAILABLE =
|
|
12
|
+
'You must be connected to a SurrealDB instance before performing this operation';
|
|
13
|
+
|
|
14
|
+
function makeSync(hashes: string[]) {
|
|
15
|
+
const logger: any = {
|
|
16
|
+
child: () => logger,
|
|
17
|
+
debug: () => {},
|
|
18
|
+
info: () => {},
|
|
19
|
+
warn: () => {},
|
|
20
|
+
error: () => {},
|
|
21
|
+
trace: () => {},
|
|
22
|
+
};
|
|
23
|
+
const remote: any = { query: vi.fn().mockResolvedValue([true]) };
|
|
24
|
+
const dataModule: any = { getActiveQueryHashes: () => hashes, getQueryByHash: () => undefined };
|
|
25
|
+
|
|
26
|
+
const sync = new Sp00kySync(
|
|
27
|
+
{} as any,
|
|
28
|
+
remote,
|
|
29
|
+
{} as any,
|
|
30
|
+
dataModule,
|
|
31
|
+
{} as any,
|
|
32
|
+
logger,
|
|
33
|
+
{ degradeAfterConsecutiveFailures: 3 }
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
// The poll reads every chunk of active hashes in one round trip
|
|
37
|
+
// (`fetchListRefSnapshots`) and lands each query's snapshot separately
|
|
38
|
+
// (`applyListRefSnapshot`). Stub both so the test controls per-cycle
|
|
39
|
+
// reachability. `poll` invokes the private method.
|
|
40
|
+
const fetch = vi.fn();
|
|
41
|
+
const apply = vi.fn().mockResolvedValue({ changed: false, outcome: 'unchanged' });
|
|
42
|
+
(sync as any).fetchListRefSnapshots = fetch;
|
|
43
|
+
(sync as any).applyListRefSnapshot = apply;
|
|
44
|
+
const snapshotsFor = (hs: string[]) =>
|
|
45
|
+
new Map(
|
|
46
|
+
hs.map((h) => [
|
|
47
|
+
h,
|
|
48
|
+
{
|
|
49
|
+
primary: [],
|
|
50
|
+
subquery: [],
|
|
51
|
+
rowCount: null,
|
|
52
|
+
meta: { present: true, rowCount: null, state: null },
|
|
53
|
+
},
|
|
54
|
+
])
|
|
55
|
+
);
|
|
56
|
+
fetch.mockImplementation(async (hs: string[]) => snapshotsFor(hs));
|
|
57
|
+
const poll = () => (sync as any).pollListRefForActiveQueries() as Promise<boolean>;
|
|
58
|
+
|
|
59
|
+
return { sync, remote, fetch, apply, snapshotsFor, poll };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
describe('sync health via idle poll', () => {
|
|
63
|
+
beforeEach(() => vi.clearAllMocks());
|
|
64
|
+
|
|
65
|
+
it('degrades after N consecutive network-failed poll cycles', async () => {
|
|
66
|
+
const { sync, fetch, poll } = makeSync(['h1']);
|
|
67
|
+
fetch.mockRejectedValue(new Error(CONNECTION_UNAVAILABLE));
|
|
68
|
+
|
|
69
|
+
await poll();
|
|
70
|
+
expect(sync.syncHealth.status).toBe('healthy');
|
|
71
|
+
await poll();
|
|
72
|
+
expect(sync.syncHealth.status).toBe('healthy');
|
|
73
|
+
await poll();
|
|
74
|
+
expect(sync.syncHealth.status).toBe('degraded');
|
|
75
|
+
expect(sync.syncHealth.kind).toBe('network');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('recovers on the next clean poll cycle — no mutation needed', async () => {
|
|
79
|
+
const { sync, fetch, snapshotsFor, poll } = makeSync(['h1']);
|
|
80
|
+
fetch.mockRejectedValue(new Error(CONNECTION_UNAVAILABLE));
|
|
81
|
+
await poll();
|
|
82
|
+
await poll();
|
|
83
|
+
await poll();
|
|
84
|
+
expect(sync.syncHealth.status).toBe('degraded');
|
|
85
|
+
|
|
86
|
+
// Connectivity returns; a plain idle poll (no user action) clears it.
|
|
87
|
+
fetch.mockImplementation(async (hs: string[]) => snapshotsFor(hs));
|
|
88
|
+
await poll();
|
|
89
|
+
expect(sync.syncHealth.status).toBe('healthy');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('does not degrade on application errors (server was reached)', async () => {
|
|
93
|
+
const { sync, fetch, poll } = makeSync(['h1']);
|
|
94
|
+
fetch.mockRejectedValue(new Error('Permission denied'));
|
|
95
|
+
await poll();
|
|
96
|
+
await poll();
|
|
97
|
+
await poll();
|
|
98
|
+
await poll();
|
|
99
|
+
expect(sync.syncHealth.status).toBe('healthy');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('counts a cycle whose round trip answered as reached even when a per-query apply fails', async () => {
|
|
103
|
+
const { sync, fetch, apply, snapshotsFor, poll } = makeSync(['h1', 'h2']);
|
|
104
|
+
// First degrade via all-network cycles.
|
|
105
|
+
fetch.mockRejectedValue(new Error(CONNECTION_UNAVAILABLE));
|
|
106
|
+
await poll();
|
|
107
|
+
await poll();
|
|
108
|
+
await poll();
|
|
109
|
+
expect(sync.syncHealth.status).toBe('degraded');
|
|
110
|
+
|
|
111
|
+
// The batched read answers (server reached); landing one of the two
|
|
112
|
+
// snapshots then fails on its own network error → still a reached cycle.
|
|
113
|
+
fetch.mockImplementation(async (hs: string[]) => snapshotsFor(hs));
|
|
114
|
+
apply
|
|
115
|
+
.mockResolvedValueOnce({ changed: true, outcome: 'applied' })
|
|
116
|
+
.mockRejectedValueOnce(new Error(CONNECTION_UNAVAILABLE));
|
|
117
|
+
await expect(poll()).resolves.toBe(true);
|
|
118
|
+
expect(sync.syncHealth.status).toBe('healthy');
|
|
119
|
+
expect(fetch).toHaveBeenLastCalledWith(['h1', 'h2']);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('probes RETURN true when there are no active queries', async () => {
|
|
123
|
+
const { sync, remote, poll } = makeSync([]);
|
|
124
|
+
remote.query.mockRejectedValue(new Error(CONNECTION_UNAVAILABLE));
|
|
125
|
+
await poll();
|
|
126
|
+
await poll();
|
|
127
|
+
await poll();
|
|
128
|
+
expect(remote.query).toHaveBeenCalledWith('RETURN true');
|
|
129
|
+
expect(sync.syncHealth.status).toBe('degraded');
|
|
130
|
+
|
|
131
|
+
remote.query.mockResolvedValue([true]);
|
|
132
|
+
await poll();
|
|
133
|
+
expect(sync.syncHealth.status).toBe('healthy');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('leaves everConnected false through a cold-start failure run', async () => {
|
|
137
|
+
const { sync, fetch, poll } = makeSync(['h1']);
|
|
138
|
+
expect(sync.syncHealth.everConnected).toBe(false);
|
|
139
|
+
|
|
140
|
+
// Server never reached: 3 failed cycles degrade, but this is the initial
|
|
141
|
+
// "connecting" phase, not a lost connection — everConnected stays false.
|
|
142
|
+
fetch.mockRejectedValue(new Error(CONNECTION_UNAVAILABLE));
|
|
143
|
+
await poll();
|
|
144
|
+
await poll();
|
|
145
|
+
await poll();
|
|
146
|
+
expect(sync.syncHealth.status).toBe('degraded');
|
|
147
|
+
expect(sync.syncHealth.everConnected).toBe(false);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('latches everConnected on the first success and keeps it through a later degrade', async () => {
|
|
151
|
+
const { sync, fetch, poll } = makeSync(['h1']);
|
|
152
|
+
|
|
153
|
+
// First successful round reaches the server: connecting phase is over.
|
|
154
|
+
await poll();
|
|
155
|
+
expect(sync.syncHealth.status).toBe('healthy');
|
|
156
|
+
expect(sync.syncHealth.everConnected).toBe(true);
|
|
157
|
+
|
|
158
|
+
// Connection later drops: degraded now reflects a REAL lost connection.
|
|
159
|
+
fetch.mockReset();
|
|
160
|
+
fetch.mockRejectedValue(new Error(CONNECTION_UNAVAILABLE));
|
|
161
|
+
await poll();
|
|
162
|
+
await poll();
|
|
163
|
+
await poll();
|
|
164
|
+
expect(sync.syncHealth.status).toBe('degraded');
|
|
165
|
+
expect(sync.syncHealth.everConnected).toBe(true);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { RecordId } from 'surrealdb';
|
|
3
|
+
import { Sp00kySync } from './sync';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `fn::query::heartbeat` is an `UPDATE $id SET ...`. Against a record that no
|
|
7
|
+
* longer exists it matches nothing and returns an empty array — it does NOT
|
|
8
|
+
* recreate the row. Verified against the deployed function:
|
|
9
|
+
*
|
|
10
|
+
* RETURN fn::query::heartbeat(_00_query:definitely_not_a_real_row_xyz);
|
|
11
|
+
* -- (0 rows)
|
|
12
|
+
*
|
|
13
|
+
* So an unchecked heartbeat cannot tell "refreshed" from "the row I am
|
|
14
|
+
* refreshing is gone", and a client whose row was reclaimed by the TTL sweep
|
|
15
|
+
* beats against nothing forever: no membership, no edges, no re-registration.
|
|
16
|
+
* The page then renders as though the data had been deleted — reported as
|
|
17
|
+
* "Game not found" on a game that was open and working.
|
|
18
|
+
*
|
|
19
|
+
* This is reachable in ordinary use: the sweep expires on `lastActiveAt + ttl`
|
|
20
|
+
* while the heartbeat runs on a timer browsers throttle hard in background
|
|
21
|
+
* tabs, so a second window left idle past its TTL is the normal way in.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
function makeSync(heartbeatResult: unknown) {
|
|
25
|
+
const logger: any = {
|
|
26
|
+
child: () => logger,
|
|
27
|
+
debug: () => {}, info: () => {}, warn: () => {}, error: () => {}, trace: () => {},
|
|
28
|
+
};
|
|
29
|
+
const remote: any = { query: vi.fn().mockResolvedValue(heartbeatResult) };
|
|
30
|
+
const queryState: any = { config: { id: new RecordId('_00_query', 'h1') } };
|
|
31
|
+
const dataModule: any = { getQueryByHash: vi.fn().mockReturnValue(queryState) };
|
|
32
|
+
|
|
33
|
+
const sync = new Sp00kySync({} as any, remote, {} as any, dataModule, {} as any, logger);
|
|
34
|
+
const enqueueDownEvent = vi.fn();
|
|
35
|
+
(sync as any).enqueueDownEvent = enqueueDownEvent;
|
|
36
|
+
|
|
37
|
+
return { sync, remote, dataModule, enqueueDownEvent };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('heartbeatQuery — noticing a reclaimed row', () => {
|
|
41
|
+
beforeEach(() => vi.clearAllMocks());
|
|
42
|
+
|
|
43
|
+
it('re-registers when the row it beat against is gone', async () => {
|
|
44
|
+
// `UPDATE` on a deleted record: one statement, zero updated records.
|
|
45
|
+
const { sync, enqueueDownEvent } = makeSync([[]]);
|
|
46
|
+
|
|
47
|
+
await sync.heartbeatQuery('h1');
|
|
48
|
+
|
|
49
|
+
expect(enqueueDownEvent).toHaveBeenCalledWith({
|
|
50
|
+
type: 'register',
|
|
51
|
+
payload: { hash: 'h1' },
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('does nothing extra on a healthy heartbeat', async () => {
|
|
56
|
+
const { sync, enqueueDownEvent } = makeSync([[{ id: 'x', lastActiveAt: 'now' }]]);
|
|
57
|
+
|
|
58
|
+
await sync.heartbeatQuery('h1');
|
|
59
|
+
|
|
60
|
+
expect(enqueueDownEvent).not.toHaveBeenCalled();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('does not re-register on an unrecognised result shape', async () => {
|
|
64
|
+
// Only an explicitly EMPTY update result means "the row is gone". Anything
|
|
65
|
+
// else — a driver returning null, a shape change — must not be read as
|
|
66
|
+
// deletion, or every heartbeat would re-register the whole working set.
|
|
67
|
+
for (const shape of [null, undefined, [], [null], ['unexpected']]) {
|
|
68
|
+
const { sync, enqueueDownEvent } = makeSync(shape);
|
|
69
|
+
await sync.heartbeatQuery('h1');
|
|
70
|
+
expect(enqueueDownEvent, `shape ${JSON.stringify(shape)}`).not.toHaveBeenCalled();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('still throws for a query that is no longer registered locally', async () => {
|
|
75
|
+
const { sync, dataModule } = makeSync([[]]);
|
|
76
|
+
dataModule.getQueryByHash.mockReturnValue(undefined);
|
|
77
|
+
|
|
78
|
+
await expect(sync.heartbeatQuery('gone')).rejects.toThrow();
|
|
79
|
+
});
|
|
80
|
+
});
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { RecordId } from 'surrealdb';
|
|
3
|
+
import { Sp00kySync } from './sync';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The list_ref readers (registration, poll, targeted re-read) read the
|
|
7
|
+
* `_00_query` row's `rowCount` and publish `state` next to the edges, dedupe
|
|
8
|
+
* duplicate edges, and hand the DataModule's verdict back to the poll so a
|
|
9
|
+
* rejected snapshot (SSP still publishing, view lost) leaves the query alone.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
function makeSync(states: Record<string, { held: Array<[string, number]> }>) {
|
|
13
|
+
const logger: any = {
|
|
14
|
+
child: () => logger,
|
|
15
|
+
debug: () => {},
|
|
16
|
+
info: () => {},
|
|
17
|
+
warn: () => {},
|
|
18
|
+
error: () => {},
|
|
19
|
+
trace: () => {},
|
|
20
|
+
};
|
|
21
|
+
const remote: any = { query: vi.fn() };
|
|
22
|
+
const queryStates: Record<string, any> = {};
|
|
23
|
+
for (const [hash, { held }] of Object.entries(states)) {
|
|
24
|
+
queryStates[hash] = {
|
|
25
|
+
config: {
|
|
26
|
+
id: new RecordId('_00_query', hash),
|
|
27
|
+
localArray: [],
|
|
28
|
+
remoteArray: held,
|
|
29
|
+
subqueryRemoteArray: undefined,
|
|
30
|
+
membershipKnown: held.length > 0,
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
const updateQueryRemoteArray = vi.fn().mockResolvedValue('applied');
|
|
35
|
+
const dataModule: any = {
|
|
36
|
+
getCurrentUserId: () => null,
|
|
37
|
+
getQueryByHash: (h: string) => queryStates[h],
|
|
38
|
+
getQueryById: (id: RecordId) => queryStates[String(id.id)],
|
|
39
|
+
getActiveQueryHashes: () => Object.keys(queryStates),
|
|
40
|
+
updateQueryRemoteArray,
|
|
41
|
+
scheduleRematerialize: vi.fn(),
|
|
42
|
+
notifyQuerySynced: vi.fn().mockResolvedValue(undefined),
|
|
43
|
+
};
|
|
44
|
+
const sync = new Sp00kySync({} as any, remote, {} as any, dataModule, {} as any, logger);
|
|
45
|
+
(sync as any).syncQuery = vi.fn().mockResolvedValue(undefined);
|
|
46
|
+
(sync as any).applySubqueryChildren = vi.fn().mockResolvedValue(undefined);
|
|
47
|
+
return { sync, remote, dataModule, queryStates, updateQueryRemoteArray };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const rid = (s: string) => {
|
|
51
|
+
const [table, id] = s.split(':');
|
|
52
|
+
return new RecordId(table, id);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
describe('list_ref snapshot meta', () => {
|
|
56
|
+
beforeEach(() => vi.clearAllMocks());
|
|
57
|
+
|
|
58
|
+
it('reads the row state next to the edges and dedupes duplicate edges (single form)', async () => {
|
|
59
|
+
const { sync, remote } = makeSync({ h1: { held: [] } });
|
|
60
|
+
remote.query.mockResolvedValue([
|
|
61
|
+
[
|
|
62
|
+
{ out: rid('thread:a'), version: 1 },
|
|
63
|
+
{ out: rid('thread:a'), version: 2 },
|
|
64
|
+
{ out: rid('thread:b'), version: 1 },
|
|
65
|
+
],
|
|
66
|
+
{ rowCount: 2, state: 'ready' },
|
|
67
|
+
[],
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
const snapshot = await (sync as any).fetchListRefSnapshot(rid('_00_query:h1'));
|
|
71
|
+
|
|
72
|
+
expect(snapshot.primary.sort()).toEqual([
|
|
73
|
+
['thread:a', 2],
|
|
74
|
+
['thread:b', 1],
|
|
75
|
+
]);
|
|
76
|
+
expect(snapshot.meta).toEqual({ present: true, rowCount: 2, state: 'ready' });
|
|
77
|
+
expect(snapshot.rowCount).toBe(2);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('reports a missing row as present: false', async () => {
|
|
81
|
+
const { sync, remote } = makeSync({ h1: { held: [] } });
|
|
82
|
+
remote.query.mockResolvedValue([[], null, []]);
|
|
83
|
+
const snapshot = await (sync as any).fetchListRefSnapshot(rid('_00_query:h1'));
|
|
84
|
+
expect(snapshot.meta).toEqual({ present: false, rowCount: null, state: null });
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('treats an unknown state value as no state (legacy server)', async () => {
|
|
88
|
+
const { sync, remote } = makeSync({ h1: { held: [] } });
|
|
89
|
+
remote.query.mockResolvedValue([[], { rowCount: 0 }, []]);
|
|
90
|
+
const snapshot = await (sync as any).fetchListRefSnapshot(rid('_00_query:h1'));
|
|
91
|
+
expect(snapshot.meta).toEqual({ present: true, rowCount: 0, state: null });
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('fills meta per query in the batched form and dedupes', async () => {
|
|
95
|
+
const { sync, remote } = makeSync({ h1: { held: [] }, h2: { held: [] } });
|
|
96
|
+
remote.query.mockResolvedValue([
|
|
97
|
+
[
|
|
98
|
+
{ in: rid('_00_query:h1'), out: rid('thread:a'), version: 1, parent: null },
|
|
99
|
+
{ in: rid('_00_query:h1'), out: rid('thread:a'), version: 3, parent: null },
|
|
100
|
+
{ in: rid('_00_query:h2'), out: rid('thread:z'), version: 1, parent: null },
|
|
101
|
+
],
|
|
102
|
+
[
|
|
103
|
+
{ id: rid('_00_query:h1'), rowCount: 1, state: 'materializing' },
|
|
104
|
+
{ id: rid('_00_query:h2'), rowCount: 1, state: 'ready' },
|
|
105
|
+
],
|
|
106
|
+
]);
|
|
107
|
+
|
|
108
|
+
const out = await (sync as any).fetchListRefSnapshots(['h1', 'h2']);
|
|
109
|
+
|
|
110
|
+
expect(out.get('h1').primary).toEqual([['thread:a', 3]]);
|
|
111
|
+
expect(out.get('h1').meta).toEqual({ present: true, rowCount: 1, state: 'materializing' });
|
|
112
|
+
expect(out.get('h2').meta).toEqual({ present: true, rowCount: 1, state: 'ready' });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('re-reads a held query one at a time when its row is missing from the batch', async () => {
|
|
116
|
+
const { sync, remote } = makeSync({ h1: { held: [['thread:a', 1]] }, h2: { held: [] } });
|
|
117
|
+
// Batch: no edges and no row for h1, which we hold rows for.
|
|
118
|
+
remote.query.mockResolvedValue([[], [{ id: rid('_00_query:h2'), rowCount: 0, state: 'ready' }]]);
|
|
119
|
+
const single = vi.fn().mockResolvedValue({
|
|
120
|
+
primary: [],
|
|
121
|
+
subquery: [],
|
|
122
|
+
rowCount: null,
|
|
123
|
+
meta: { present: false, rowCount: null, state: null },
|
|
124
|
+
});
|
|
125
|
+
(sync as any).fetchListRefSnapshot = single;
|
|
126
|
+
|
|
127
|
+
const out = await (sync as any).fetchListRefSnapshots(['h1', 'h2']);
|
|
128
|
+
|
|
129
|
+
expect(single).toHaveBeenCalled();
|
|
130
|
+
expect(out.get('h1').meta.present).toBe(false);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('applyListRefSnapshot', () => {
|
|
134
|
+
const snapshot = (primary: Array<[string, number]>, meta: any) => ({
|
|
135
|
+
primary,
|
|
136
|
+
subquery: [],
|
|
137
|
+
rowCount: meta.rowCount,
|
|
138
|
+
meta,
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('hands the verdict back and touches nothing when the set is rejected', async () => {
|
|
142
|
+
const { sync, dataModule, updateQueryRemoteArray } = makeSync({ h1: { held: [['thread:a', 1]] } });
|
|
143
|
+
updateQueryRemoteArray.mockResolvedValue('ignored');
|
|
144
|
+
|
|
145
|
+
const result = await (sync as any).applyListRefSnapshot(
|
|
146
|
+
'h1',
|
|
147
|
+
snapshot([], { present: true, rowCount: 1, state: 'materializing' })
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
expect(result).toEqual({ changed: false, outcome: 'ignored' });
|
|
151
|
+
expect(updateQueryRemoteArray).toHaveBeenCalledWith('h1', [], {
|
|
152
|
+
meta: { present: true, rowCount: 1, state: 'materializing' },
|
|
153
|
+
});
|
|
154
|
+
expect(dataModule.scheduleRematerialize).not.toHaveBeenCalled();
|
|
155
|
+
expect(dataModule.notifyQuerySynced).not.toHaveBeenCalled();
|
|
156
|
+
expect((sync as any).applySubqueryChildren).not.toHaveBeenCalled();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('does the same for a lost view', async () => {
|
|
160
|
+
const { sync, dataModule, updateQueryRemoteArray } = makeSync({ h1: { held: [['thread:a', 1]] } });
|
|
161
|
+
updateQueryRemoteArray.mockResolvedValue('view-lost');
|
|
162
|
+
const result = await (sync as any).applyListRefSnapshot(
|
|
163
|
+
'h1',
|
|
164
|
+
snapshot([], { present: false, rowCount: null, state: null })
|
|
165
|
+
);
|
|
166
|
+
expect(result).toEqual({ changed: false, outcome: 'view-lost' });
|
|
167
|
+
expect(dataModule.notifyQuerySynced).not.toHaveBeenCalled();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('re-materializes and re-renders removals when the set is applied', async () => {
|
|
171
|
+
const { sync, dataModule } = makeSync({ h1: { held: [['thread:a', 1], ['thread:b', 1]] } });
|
|
172
|
+
const result = await (sync as any).applyListRefSnapshot(
|
|
173
|
+
'h1',
|
|
174
|
+
snapshot([['thread:a', 1]], { present: true, rowCount: 1, state: 'ready' })
|
|
175
|
+
);
|
|
176
|
+
expect(result).toEqual({ changed: true, outcome: 'applied' });
|
|
177
|
+
expect(dataModule.scheduleRematerialize).toHaveBeenCalledWith('h1');
|
|
178
|
+
expect(dataModule.notifyQuerySynced).toHaveBeenCalledWith('h1');
|
|
179
|
+
expect((sync as any).applySubqueryChildren).toHaveBeenCalled();
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('is quiet when the server set equals what is held', async () => {
|
|
183
|
+
const { sync, dataModule, updateQueryRemoteArray } = makeSync({ h1: { held: [['thread:a', 1]] } });
|
|
184
|
+
const result = await (sync as any).applyListRefSnapshot(
|
|
185
|
+
'h1',
|
|
186
|
+
snapshot([['thread:a', 1]], { present: true, rowCount: 1, state: 'ready' })
|
|
187
|
+
);
|
|
188
|
+
expect(result).toEqual({ changed: false, outcome: 'unchanged' });
|
|
189
|
+
expect(updateQueryRemoteArray).not.toHaveBeenCalled();
|
|
190
|
+
expect(dataModule.scheduleRematerialize).not.toHaveBeenCalled();
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
});
|