@spooky-sync/core 0.0.1-canary.98 → 1.0.0-beta.10
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 -21
- package/README.md +59 -16
- package/dist/index.d.ts +2235 -908
- package/dist/index.js +20424 -5052
- package/dist/live.d.ts +149 -0
- package/dist/live.js +739 -0
- package/dist/otel/index.d.ts +1 -1
- package/dist/pure.d.ts +401 -0
- package/dist/pure.js +24 -0
- package/dist/rows-worker.d.ts +1 -0
- package/dist/rows-worker.js +2639 -0
- package/dist/sqlite-open.js +392 -0
- package/dist/sqlite-worker.d.ts +1 -0
- package/dist/sqlite-worker.js +439 -0
- package/dist/sw.d.ts +207 -0
- package/dist/sw.js +393 -0
- package/dist/tabs-broker-worker.d.ts +8 -0
- package/dist/tabs-broker-worker.js +477 -0
- package/dist/types.d.ts +2084 -82
- package/dist/worker-edges.js +498 -0
- package/dist/worker-impersonation.js +60 -0
- package/dist/worker-ref-tables.d.ts +48 -0
- package/dist/worker-types.d.ts +112 -0
- package/dist/worker-types.js +73 -0
- package/package.json +39 -8
- package/scripts/check-broker-bundle.mjs +33 -0
- package/scripts/check-worker-entries.mjs +186 -0
- package/skills/sp00ky-core/SKILL.md +44 -8
- package/skills/sp00ky-core/references/config.md +8 -2
- package/src/boot/auth-flip.saga.test.ts +174 -0
- package/src/boot/auth-flip.saga.ts +77 -0
- package/src/boot/boot.saga.test.ts +203 -0
- package/src/boot/boot.saga.ts +146 -0
- package/src/boot/bucket-switch.saga.test.ts +191 -0
- package/src/boot/bucket-switch.saga.ts +151 -0
- package/src/boot/preload.saga.test.ts +96 -0
- package/src/boot/preload.saga.ts +45 -0
- package/src/bucket-blurhash.test.ts +148 -0
- package/src/client/bucket-handle.test.ts +195 -0
- package/src/client/bucket-handle.ts +372 -0
- package/src/client/eager-paint.test.ts +82 -0
- package/src/client/router.test.ts +59 -0
- package/src/client/router.ts +104 -0
- package/src/client/runtime.test.ts +319 -0
- package/src/client/runtime.ts +307 -0
- package/src/client/services.test.ts +74 -0
- package/src/client/services.ts +440 -0
- package/src/client/sp00ky-client.test.ts +570 -0
- package/src/client/sp00ky-client.ts +875 -0
- package/src/index.ts +87 -0
- package/src/kernel/constants.test.ts +19 -0
- package/src/kernel/constants.ts +94 -0
- package/src/kernel/effects.test.ts +40 -0
- package/src/kernel/effects.ts +181 -0
- package/src/kernel/events.ts +60 -0
- package/src/kernel/interpreter.test.ts +133 -0
- package/src/kernel/interpreter.ts +151 -0
- package/src/kernel/saga.test.ts +89 -0
- package/src/kernel/saga.ts +73 -0
- package/src/live/index.test.ts +512 -0
- package/src/live/index.ts +1059 -0
- package/src/modules/app-release/index.test.ts +125 -0
- package/src/modules/app-release/index.ts +209 -0
- package/src/modules/auth/auth.local-first.test.ts +228 -0
- package/src/modules/auth/auth.session-recheck.test.ts +140 -0
- package/src/modules/auth/events/index.ts +7 -1
- package/src/modules/auth/impersonation-banner.ts +443 -0
- package/src/modules/auth/impersonation.integration.test.ts +209 -0
- package/src/modules/auth/impersonation.test.ts +377 -0
- package/src/modules/auth/impersonation.ts +179 -0
- package/src/modules/auth/index.ts +516 -34
- package/src/modules/channel/channel.test.ts +752 -0
- package/src/modules/channel/hub.test.ts +223 -0
- package/src/modules/channel/hub.ts +355 -0
- package/src/modules/channel/index.ts +1497 -0
- package/src/modules/channel/room.test.ts +340 -0
- package/src/modules/channel/room.ts +367 -0
- package/src/modules/channel/rooms.fixture.ts +383 -0
- package/src/modules/channel/share.test.ts +535 -0
- package/src/modules/channel/webrtc.ts +472 -0
- package/src/modules/crdt/acceptance.ts +75 -0
- package/src/modules/crdt/bytes.ts +50 -0
- package/src/modules/crdt/crdt-field.ts +82 -238
- package/src/modules/crdt/crdt.test.ts +546 -0
- package/src/modules/crdt/hash.ts +103 -0
- package/src/modules/crdt/index.ts +740 -271
- package/src/modules/crdt/loro-loader.ts +25 -0
- package/src/modules/crdt/mirror.ts +37 -0
- package/src/modules/devtools/flags.ts +349 -0
- package/src/modules/devtools/index.ts +661 -99
- package/src/modules/devtools/mutations.test.ts +92 -0
- package/src/modules/devtools/mutations.ts +160 -0
- package/src/modules/devtools/notify-throttle.test.ts +166 -0
- package/src/modules/devtools/state-shape.test.ts +263 -0
- package/src/modules/devtools/storage-info.test.ts +79 -0
- package/src/modules/devtools/storage-info.ts +169 -0
- package/src/modules/devtools/versions.ts +29 -0
- package/src/modules/feature-flag/index.test.ts +179 -1
- package/src/modules/feature-flag/index.ts +117 -10
- package/src/modules/ref-tables.test.ts +28 -1
- package/src/modules/ref-tables.ts +23 -1
- package/src/modules/web-push/index.test.ts +824 -0
- package/src/modules/web-push/index.ts +1027 -0
- package/src/mutation/ingest-outage.integration.test.ts +400 -0
- package/src/mutation/jobs.test.ts +25 -0
- package/src/mutation/jobs.ts +41 -0
- package/src/mutation/mutation-id.test.ts +37 -0
- package/src/mutation/mutation-id.ts +35 -0
- package/src/mutation/pending-writes.integration.test.ts +175 -0
- package/src/mutation/push.saga.test.ts +361 -0
- package/src/mutation/push.saga.ts +245 -0
- package/src/mutation/rows.test.ts +278 -0
- package/src/mutation/rows.ts +515 -0
- package/src/mutation/tray.saga.test.ts +63 -0
- package/src/mutation/tray.saga.ts +48 -0
- package/src/mutation/write.saga.test.ts +436 -0
- package/src/mutation/write.saga.ts +384 -0
- package/src/pure.ts +66 -0
- package/src/push/types.ts +174 -0
- package/src/query/anon-query-isolation.integration.test.ts +531 -0
- package/src/query/env.test.ts +23 -0
- package/src/query/env.ts +65 -0
- package/src/query/fetch.saga.test.ts +199 -0
- package/src/query/fetch.saga.ts +120 -0
- package/src/query/hash.test.ts +14 -0
- package/src/query/hash.ts +18 -0
- package/src/query/id-set-plan.test.ts +132 -0
- package/src/query/lifecycle.saga.test.ts +274 -0
- package/src/query/lifecycle.saga.ts +186 -0
- package/src/query/list-ref-live.integration.test.ts +203 -0
- package/src/query/materialize.saga.test.ts +216 -0
- package/src/query/materialize.saga.ts +83 -0
- package/src/query/materialize.test.ts +58 -0
- package/src/query/materialize.ts +133 -0
- package/src/query/membership.saga.test.ts +514 -0
- package/src/query/membership.saga.ts +339 -0
- package/src/query/membership.test.ts +151 -0
- package/src/query/membership.ts +234 -0
- package/src/query/register.saga.test.ts +430 -0
- package/src/query/register.saga.ts +330 -0
- package/src/query/render-set.test.ts +47 -0
- package/src/query/render-set.ts +71 -0
- package/src/query/session-guard.integration.test.ts +304 -0
- package/src/query/sql.test.ts +118 -0
- package/src/query/sql.ts +174 -0
- package/src/{modules/data → query}/window-query.test.ts +22 -4
- package/src/{modules/data → query}/window-query.ts +86 -8
- 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 +223 -0
- package/src/services/database/connection-supervisor.test.ts +318 -0
- package/src/services/database/connection-supervisor.ts +422 -0
- package/src/services/database/database.query-timeout.test.ts +140 -0
- package/src/services/database/database.responses.test.ts +43 -0
- package/src/services/database/database.ts +151 -56
- package/src/services/database/engine-factory.ts +40 -0
- package/src/services/database/errors.ts +34 -0
- package/src/services/database/index.ts +8 -0
- package/src/services/database/landed-body-clear.integration.test.ts +236 -0
- package/src/services/database/local-migrator.integration.test.ts +110 -0
- package/src/services/database/local-migrator.ts +54 -9
- package/src/services/database/local.test.ts +117 -0
- package/src/services/database/local.ts +364 -74
- 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 +484 -0
- package/src/services/database/relation-resolver.ts +0 -0
- package/src/services/database/remote.ts +283 -4
- package/src/services/database/rows-cache-engine.test.ts +1014 -0
- package/src/services/database/rows-cache-engine.ts +921 -0
- package/src/services/database/rows-exec.integration.test.ts +589 -0
- package/src/services/database/rows-exec.ts +1027 -0
- package/src/services/database/rows-prewarm.ts +225 -0
- package/src/services/database/rows-store.test.ts +635 -0
- package/src/services/database/rows-store.ts +1208 -0
- package/src/services/database/rows-transport.fixture.ts +151 -0
- package/src/services/database/rows-worker.ts +588 -0
- package/src/services/database/sqlite-cache-engine.test.ts +649 -0
- package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
- package/src/services/database/sqlite-cache-engine.ts +1422 -0
- package/src/services/database/sqlite-column-reset.integration.test.ts +100 -0
- package/src/services/database/sqlite-column-reset.ts +91 -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 +164 -0
- package/src/services/database/sqlite-plan-sql.ts +196 -0
- package/src/services/database/sqlite-projection.test.ts +121 -0
- package/src/services/database/sqlite-select.integration.test.ts +185 -0
- package/src/services/database/sqlite-select.test.ts +261 -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 +231 -0
- package/src/services/database/sqlite-worker.ts +437 -0
- package/src/services/database/surql-translate.ts +417 -0
- package/src/services/database/surreal-cache-engine.ts +163 -0
- package/src/services/logger/index.test.ts +132 -0
- package/src/services/logger/index.ts +187 -6
- package/src/services/persistence/surrealdb.ts +2 -2
- package/src/services/stream-processor/index.ts +621 -93
- package/src/services/stream-processor/stream-processor.batch.test.ts +57 -7
- package/src/services/stream-processor/stream-processor.prime.test.ts +234 -0
- package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
- package/src/services/stream-processor/wasm-types.ts +41 -1
- package/src/services/tabs/broker-client.ts +284 -0
- package/src/services/tabs/broker.test.ts +381 -0
- package/src/services/tabs/coordinator.test.ts +386 -0
- package/src/services/tabs/coordinator.ts +646 -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 +262 -0
- package/src/services/tabs/support.ts +38 -0
- package/src/services/tabs/tabs-broker-worker.ts +652 -0
- package/src/sp00ky.ts +5 -648
- package/src/state/client-state.ts +231 -0
- package/src/state/lifecycle.test.ts +184 -0
- package/src/state/lifecycle.ts +127 -0
- package/src/state/reducers.test.ts +337 -0
- package/src/state/reducers.ts +571 -0
- package/src/state/selectors.test.ts +203 -0
- package/src/state/selectors.ts +264 -0
- package/src/sw/index.test.ts +582 -0
- package/src/sw/index.ts +607 -0
- package/src/sw/store.ts +96 -0
- package/src/sync/connection.saga.test.ts +97 -0
- package/src/sync/connection.saga.ts +89 -0
- package/src/sync/live.saga.test.ts +229 -0
- package/src/sync/live.saga.ts +116 -0
- package/src/sync/policy.health.test.ts +56 -0
- package/src/{modules/sync/utils.test.ts → sync/policy.test.ts} +96 -11
- package/src/{modules/sync/utils.ts → sync/policy.ts} +186 -7
- package/src/sync/poll.saga.test.ts +51 -0
- package/src/sync/poll.saga.ts +36 -0
- package/src/sync/tabs.saga.test.ts +78 -0
- package/src/sync/tabs.saga.ts +96 -0
- package/src/testing/adapters.ts +137 -0
- package/src/testing/build.ts +63 -0
- package/src/testing/fake-services.ts +63 -0
- package/src/testing/run-pure.test.ts +92 -0
- package/src/testing/run-pure.ts +128 -0
- package/src/testing/services.ts +17 -0
- package/src/types.ts +601 -25
- package/src/utils/blurhash.ts +90 -0
- package/src/utils/edges.ts +57 -0
- package/src/utils/error-classification.test.ts +116 -0
- package/src/utils/error-classification.ts +74 -0
- package/src/utils/index.ts +58 -3
- package/src/utils/parser.test.ts +67 -102
- package/src/utils/parser.ts +62 -1
- package/src/utils/semver.test.ts +32 -0
- package/src/utils/semver.ts +30 -0
- package/src/utils/sha256.ts +15 -0
- package/src/utils/surql.ts +17 -15
- package/src/utils/utils.test.ts +3 -2
- package/tsdown.config.ts +105 -4
- package/vitest.config.ts +52 -0
- package/src/modules/cache/index.ts +0 -253
- package/src/modules/cache/types.ts +0 -19
- package/src/modules/crdt/crdt-hydration.test.ts +0 -206
- package/src/modules/data/data.status.test.ts +0 -108
- package/src/modules/data/data.test.ts +0 -58
- package/src/modules/data/index.ts +0 -1491
- package/src/modules/sync/engine.ts +0 -227
- package/src/modules/sync/events/index.ts +0 -84
- package/src/modules/sync/index.ts +0 -3
- package/src/modules/sync/queue/index.ts +0 -2
- package/src/modules/sync/queue/queue-down.ts +0 -90
- package/src/modules/sync/queue/queue-up.ts +0 -224
- package/src/modules/sync/scheduler.ts +0 -121
- package/src/modules/sync/sync.ts +0 -1244
- package/src/sp00ky.auth-order.test.ts +0 -65
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { ConnectionSupervisor } from './connection-supervisor';
|
|
3
|
+
import type { ReconnectConfig } from '../../types';
|
|
4
|
+
|
|
5
|
+
// The supervisor covers the two failure modes the SDK's own reconnect cannot:
|
|
6
|
+
// - it stopped trying (attempts exhausted, or its post-reconnect handshake
|
|
7
|
+
// threw and it terminated the engine) — nothing else would ever reconnect
|
|
8
|
+
// - the socket never closed at all (half-open: peer gone, readyState OPEN,
|
|
9
|
+
// no `close` event, so no reconnect is ever triggered)
|
|
10
|
+
|
|
11
|
+
const CONFIG: Required<ReconnectConfig> = {
|
|
12
|
+
attempts: -1,
|
|
13
|
+
retryDelayMax: 8_000,
|
|
14
|
+
heartbeatIntervalMs: 1_000,
|
|
15
|
+
heartbeatTimeoutMs: 500,
|
|
16
|
+
superviseRetryDelayMaxMs: 4_000,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function makeRemote() {
|
|
20
|
+
const handlers = new Map<string, Array<(...a: any[]) => void>>();
|
|
21
|
+
const state = { status: 'connected' as string };
|
|
22
|
+
const remote: any = {
|
|
23
|
+
getStatus: () => state.status,
|
|
24
|
+
getReconnectConfig: () => CONFIG,
|
|
25
|
+
subscribeConnection: (event: string, cb: (...a: any[]) => void) => {
|
|
26
|
+
const arr = handlers.get(event) ?? [];
|
|
27
|
+
arr.push(cb);
|
|
28
|
+
handlers.set(event, arr);
|
|
29
|
+
return () => {
|
|
30
|
+
handlers.set(
|
|
31
|
+
event,
|
|
32
|
+
(handlers.get(event) ?? []).filter((h) => h !== cb)
|
|
33
|
+
);
|
|
34
|
+
};
|
|
35
|
+
},
|
|
36
|
+
connect: vi.fn().mockImplementation(async () => {
|
|
37
|
+
state.status = 'connected';
|
|
38
|
+
}),
|
|
39
|
+
forceClose: vi.fn().mockImplementation(async () => {
|
|
40
|
+
state.status = 'disconnected';
|
|
41
|
+
emit('disconnected');
|
|
42
|
+
}),
|
|
43
|
+
query: vi.fn().mockResolvedValue([true]),
|
|
44
|
+
};
|
|
45
|
+
function emit(event: string, ...args: any[]) {
|
|
46
|
+
for (const cb of Array.from(handlers.get(event) ?? [])) cb(...args);
|
|
47
|
+
}
|
|
48
|
+
return { remote, emit, state, handlerCount: () => handlers.size };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const silentLogger: any = (() => {
|
|
52
|
+
const l: any = {
|
|
53
|
+
child: () => l,
|
|
54
|
+
debug: () => {},
|
|
55
|
+
info: () => {},
|
|
56
|
+
warn: () => {},
|
|
57
|
+
error: () => {},
|
|
58
|
+
trace: () => {},
|
|
59
|
+
};
|
|
60
|
+
return l;
|
|
61
|
+
})();
|
|
62
|
+
|
|
63
|
+
function makeSupervisor(remote: any) {
|
|
64
|
+
return new ConnectionSupervisor(remote, silentLogger, CONFIG);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
describe('ConnectionSupervisor', () => {
|
|
68
|
+
beforeEach(() => {
|
|
69
|
+
vi.useFakeTimers();
|
|
70
|
+
vi.clearAllMocks();
|
|
71
|
+
});
|
|
72
|
+
afterEach(() => {
|
|
73
|
+
vi.useRealTimers();
|
|
74
|
+
vi.unstubAllGlobals();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('reconnects after the SDK gives up, retrying on backoff until it succeeds', async () => {
|
|
78
|
+
const { remote, emit, state } = makeRemote();
|
|
79
|
+
// Two failures, then success — proves the loop keeps going rather than
|
|
80
|
+
// giving up like the SDK does.
|
|
81
|
+
remote.connect
|
|
82
|
+
.mockRejectedValueOnce(new Error('refused'))
|
|
83
|
+
.mockRejectedValueOnce(new Error('refused'))
|
|
84
|
+
.mockImplementationOnce(async () => {
|
|
85
|
+
state.status = 'connected';
|
|
86
|
+
emit('connected');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const sup = makeSupervisor(remote);
|
|
90
|
+
sup.start();
|
|
91
|
+
|
|
92
|
+
state.status = 'disconnected';
|
|
93
|
+
emit('disconnected');
|
|
94
|
+
expect(sup.connection).toBe('disconnected');
|
|
95
|
+
|
|
96
|
+
// 1s, then 2s, then 4s (capped at superviseRetryDelayMaxMs).
|
|
97
|
+
await vi.advanceTimersByTimeAsync(1_000);
|
|
98
|
+
expect(remote.connect).toHaveBeenCalledTimes(1);
|
|
99
|
+
expect(sup.connection).toBe('reconnecting');
|
|
100
|
+
|
|
101
|
+
await vi.advanceTimersByTimeAsync(2_000);
|
|
102
|
+
expect(remote.connect).toHaveBeenCalledTimes(2);
|
|
103
|
+
|
|
104
|
+
await vi.advanceTimersByTimeAsync(4_000);
|
|
105
|
+
expect(remote.connect).toHaveBeenCalledTimes(3);
|
|
106
|
+
expect(sup.connection).toBe('connected');
|
|
107
|
+
|
|
108
|
+
// Recovered: no further attempts.
|
|
109
|
+
await vi.advanceTimersByTimeAsync(30_000);
|
|
110
|
+
expect(remote.connect).toHaveBeenCalledTimes(3);
|
|
111
|
+
|
|
112
|
+
sup.dispose();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('does not reconnect while the SDK is still retrying', async () => {
|
|
116
|
+
const { remote, emit, state } = makeRemote();
|
|
117
|
+
const sup = makeSupervisor(remote);
|
|
118
|
+
sup.start();
|
|
119
|
+
|
|
120
|
+
state.status = 'reconnecting';
|
|
121
|
+
emit('reconnecting');
|
|
122
|
+
expect(sup.connection).toBe('reconnecting');
|
|
123
|
+
|
|
124
|
+
await vi.advanceTimersByTimeAsync(30_000);
|
|
125
|
+
// The SDK owns the socket during its own retry loop; racing it would open
|
|
126
|
+
// a second connection.
|
|
127
|
+
expect(remote.connect).not.toHaveBeenCalled();
|
|
128
|
+
|
|
129
|
+
sup.dispose();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('forces a teardown when a heartbeat never answers (half-open socket)', async () => {
|
|
133
|
+
const { remote, emit } = makeRemote();
|
|
134
|
+
// The defining symptom: the RPC never settles and no `close` ever fires.
|
|
135
|
+
remote.query.mockImplementation(() => new Promise(() => {}));
|
|
136
|
+
|
|
137
|
+
const sup = makeSupervisor(remote);
|
|
138
|
+
emit('connected');
|
|
139
|
+
sup.start();
|
|
140
|
+
|
|
141
|
+
await vi.advanceTimersByTimeAsync(CONFIG.heartbeatIntervalMs);
|
|
142
|
+
expect(remote.query).toHaveBeenCalledWith('RETURN true');
|
|
143
|
+
expect(remote.forceClose).not.toHaveBeenCalled();
|
|
144
|
+
|
|
145
|
+
// One failure is inconclusive — the probe shares a queue with ordinary
|
|
146
|
+
// traffic, so it re-probes rather than tearing down a possibly-fine socket.
|
|
147
|
+
await vi.advanceTimersByTimeAsync(CONFIG.heartbeatTimeoutMs);
|
|
148
|
+
expect(remote.forceClose).not.toHaveBeenCalled();
|
|
149
|
+
|
|
150
|
+
// The second consecutive failure is the one that tears it down.
|
|
151
|
+
await vi.advanceTimersByTimeAsync(5_000 + CONFIG.heartbeatTimeoutMs);
|
|
152
|
+
expect(remote.forceClose).toHaveBeenCalledTimes(1);
|
|
153
|
+
|
|
154
|
+
// The forced close published `disconnected`, so the revive loop takes over.
|
|
155
|
+
await vi.advanceTimersByTimeAsync(1_000);
|
|
156
|
+
expect(remote.connect).toHaveBeenCalled();
|
|
157
|
+
|
|
158
|
+
sup.dispose();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('does not tear down a healthy socket after a single slow probe', async () => {
|
|
162
|
+
// The regression this guards: the heartbeat rides the same serialized queue
|
|
163
|
+
// as every other RPC, so one busy window (a big sync burst) used to
|
|
164
|
+
// force-close a working connection — and the reconnect then re-registered
|
|
165
|
+
// every active query about a second later.
|
|
166
|
+
const { remote, emit } = makeRemote();
|
|
167
|
+
let calls = 0;
|
|
168
|
+
remote.query.mockImplementation(() => {
|
|
169
|
+
calls++;
|
|
170
|
+
// First probe hangs past its deadline, the next answers normally.
|
|
171
|
+
return calls === 1 ? new Promise(() => {}) : Promise.resolve(true);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
const sup = makeSupervisor(remote);
|
|
175
|
+
emit('connected');
|
|
176
|
+
sup.start();
|
|
177
|
+
|
|
178
|
+
await vi.advanceTimersByTimeAsync(CONFIG.heartbeatIntervalMs + CONFIG.heartbeatTimeoutMs);
|
|
179
|
+
expect(remote.forceClose).not.toHaveBeenCalled();
|
|
180
|
+
|
|
181
|
+
// The retry succeeds, so the socket survives and heartbeating continues.
|
|
182
|
+
await vi.advanceTimersByTimeAsync(5_000 + CONFIG.heartbeatIntervalMs * 2);
|
|
183
|
+
expect(remote.forceClose).not.toHaveBeenCalled();
|
|
184
|
+
expect(sup.connection).toBe('connected');
|
|
185
|
+
|
|
186
|
+
sup.dispose();
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('keeps heartbeating while the connection is healthy', async () => {
|
|
190
|
+
const { remote, emit } = makeRemote();
|
|
191
|
+
const sup = makeSupervisor(remote);
|
|
192
|
+
emit('connected');
|
|
193
|
+
sup.start();
|
|
194
|
+
|
|
195
|
+
await vi.advanceTimersByTimeAsync(CONFIG.heartbeatIntervalMs * 3 + 10);
|
|
196
|
+
expect(remote.query.mock.calls.length).toBeGreaterThanOrEqual(3);
|
|
197
|
+
expect(remote.forceClose).not.toHaveBeenCalled();
|
|
198
|
+
expect(sup.connection).toBe('connected');
|
|
199
|
+
|
|
200
|
+
sup.dispose();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('parks reconnects while the browser reports offline, and resumes on online', async () => {
|
|
204
|
+
const listeners = new Map<string, Array<() => void>>();
|
|
205
|
+
vi.stubGlobal('window', {
|
|
206
|
+
addEventListener: (e: string, cb: () => void) => {
|
|
207
|
+
listeners.set(e, [...(listeners.get(e) ?? []), cb]);
|
|
208
|
+
},
|
|
209
|
+
removeEventListener: () => {},
|
|
210
|
+
});
|
|
211
|
+
const { remote, emit, state } = makeRemote();
|
|
212
|
+
const sup = makeSupervisor(remote);
|
|
213
|
+
sup.start();
|
|
214
|
+
|
|
215
|
+
state.status = 'disconnected';
|
|
216
|
+
emit('disconnected');
|
|
217
|
+
listeners.get('offline')?.forEach((cb) => cb());
|
|
218
|
+
|
|
219
|
+
// Retrying a socket against a down interface only burns backoff.
|
|
220
|
+
await vi.advanceTimersByTimeAsync(30_000);
|
|
221
|
+
expect(remote.connect).not.toHaveBeenCalled();
|
|
222
|
+
|
|
223
|
+
// `online` is the strongest available hint a reconnect will now work, so it
|
|
224
|
+
// probes immediately rather than waiting out a backoff.
|
|
225
|
+
listeners.get('online')?.forEach((cb) => cb());
|
|
226
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
227
|
+
expect(remote.connect).toHaveBeenCalledTimes(1);
|
|
228
|
+
|
|
229
|
+
sup.dispose();
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('un-sticks the forced offline state when the SDK slept through the outage', async () => {
|
|
233
|
+
const listeners = new Map<string, Array<() => void>>();
|
|
234
|
+
vi.stubGlobal('window', {
|
|
235
|
+
addEventListener: (e: string, cb: () => void) => {
|
|
236
|
+
listeners.set(e, [...(listeners.get(e) ?? []), cb]);
|
|
237
|
+
},
|
|
238
|
+
removeEventListener: () => {},
|
|
239
|
+
});
|
|
240
|
+
const { remote, emit } = makeRemote();
|
|
241
|
+
const sup = makeSupervisor(remote);
|
|
242
|
+
emit('connected');
|
|
243
|
+
sup.start();
|
|
244
|
+
|
|
245
|
+
// The browser reports offline, but the SDK's own socket never notices: its
|
|
246
|
+
// status stays `connected` throughout, so it emits no transition at all.
|
|
247
|
+
listeners.get('offline')?.forEach((cb) => cb());
|
|
248
|
+
expect(sup.connection).toBe('disconnected');
|
|
249
|
+
|
|
250
|
+
// Back online. Nothing but the probe can correct the forced state here -
|
|
251
|
+
// there is no SDK event coming - and traffic flowing under a permanent
|
|
252
|
+
// "offline" indicator is exactly the bug.
|
|
253
|
+
listeners.get('online')?.forEach((cb) => cb());
|
|
254
|
+
await vi.advanceTimersByTimeAsync(10);
|
|
255
|
+
expect(remote.query).toHaveBeenCalled();
|
|
256
|
+
expect(sup.connection).toBe('connected');
|
|
257
|
+
|
|
258
|
+
sup.dispose();
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it('probes immediately when a hidden tab becomes visible', async () => {
|
|
262
|
+
const listeners = new Map<string, Array<() => void>>();
|
|
263
|
+
vi.stubGlobal('document', {
|
|
264
|
+
visibilityState: 'visible',
|
|
265
|
+
addEventListener: (e: string, cb: () => void) => {
|
|
266
|
+
listeners.set(e, [...(listeners.get(e) ?? []), cb]);
|
|
267
|
+
},
|
|
268
|
+
removeEventListener: () => {},
|
|
269
|
+
});
|
|
270
|
+
const { remote, emit } = makeRemote();
|
|
271
|
+
const sup = makeSupervisor(remote);
|
|
272
|
+
emit('connected');
|
|
273
|
+
sup.start();
|
|
274
|
+
|
|
275
|
+
listeners.get('visibilitychange')?.forEach((cb) => cb());
|
|
276
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
277
|
+
// Connected-looking socket: probe it rather than reconnect. A sleep/wake
|
|
278
|
+
// cycle is exactly how you get a socket that looks fine and isn't.
|
|
279
|
+
expect(remote.query).toHaveBeenCalledWith('RETURN true');
|
|
280
|
+
|
|
281
|
+
sup.dispose();
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it('dispose stops every timer and unsubscribes', async () => {
|
|
285
|
+
const { remote, emit, state } = makeRemote();
|
|
286
|
+
const sup = makeSupervisor(remote);
|
|
287
|
+
emit('connected');
|
|
288
|
+
sup.start();
|
|
289
|
+
|
|
290
|
+
sup.dispose();
|
|
291
|
+
state.status = 'disconnected';
|
|
292
|
+
emit('disconnected');
|
|
293
|
+
|
|
294
|
+
await vi.advanceTimersByTimeAsync(60_000);
|
|
295
|
+
expect(remote.connect).not.toHaveBeenCalled();
|
|
296
|
+
expect(remote.query).not.toHaveBeenCalled();
|
|
297
|
+
expect(vi.getTimerCount()).toBe(0);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('reports state changes to subscribers', () => {
|
|
301
|
+
const { remote, emit, state } = makeRemote();
|
|
302
|
+
const sup = makeSupervisor(remote);
|
|
303
|
+
sup.start();
|
|
304
|
+
|
|
305
|
+
const seen: string[] = [];
|
|
306
|
+
sup.subscribe((s) => seen.push(s));
|
|
307
|
+
// Fires immediately with the current value.
|
|
308
|
+
expect(seen).toEqual(['connected']);
|
|
309
|
+
|
|
310
|
+
state.status = 'reconnecting';
|
|
311
|
+
emit('reconnecting');
|
|
312
|
+
state.status = 'connected';
|
|
313
|
+
emit('connected');
|
|
314
|
+
expect(seen).toEqual(['connected', 'reconnecting', 'connected']);
|
|
315
|
+
|
|
316
|
+
sup.dispose();
|
|
317
|
+
});
|
|
318
|
+
});
|