@spooky-sync/core 0.0.1-canary.32 → 0.0.1-canary.321

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.
Files changed (261) hide show
  1. package/AGENTS.md +81 -0
  2. package/README.md +56 -16
  3. package/dist/index.d.ts +2034 -137
  4. package/dist/index.js +15236 -2467
  5. package/dist/live.d.ts +141 -0
  6. package/dist/live.js +733 -0
  7. package/dist/otel/index.d.ts +1 -1
  8. package/dist/otel/index.js +3 -3
  9. package/dist/pure.d.ts +365 -0
  10. package/dist/pure.js +24 -0
  11. package/dist/sqlite-open.js +379 -0
  12. package/dist/sqlite-worker.d.ts +1 -0
  13. package/dist/sqlite-worker.js +439 -0
  14. package/dist/sw.d.ts +207 -0
  15. package/dist/sw.js +393 -0
  16. package/dist/tabs-broker-worker.d.ts +8 -0
  17. package/dist/tabs-broker-worker.js +472 -0
  18. package/dist/types.d.ts +1821 -38
  19. package/dist/worker-edges.js +470 -0
  20. package/dist/worker-impersonation.js +60 -0
  21. package/dist/worker-ref-tables.d.ts +48 -0
  22. package/dist/worker-types.d.ts +112 -0
  23. package/dist/worker-types.js +73 -0
  24. package/package.json +27 -7
  25. package/scripts/check-broker-bundle.mjs +33 -0
  26. package/scripts/check-worker-entries.mjs +172 -0
  27. package/skills/sp00ky-core/SKILL.md +31 -6
  28. package/skills/sp00ky-core/references/config.md +6 -0
  29. package/src/boot/auth-flip.saga.test.ts +135 -0
  30. package/src/boot/auth-flip.saga.ts +77 -0
  31. package/src/boot/boot.saga.test.ts +180 -0
  32. package/src/boot/boot.saga.ts +142 -0
  33. package/src/boot/bucket-switch.saga.test.ts +187 -0
  34. package/src/boot/bucket-switch.saga.ts +133 -0
  35. package/src/boot/preload.saga.test.ts +51 -0
  36. package/src/boot/preload.saga.ts +31 -0
  37. package/src/bucket-blurhash.test.ts +148 -0
  38. package/src/build-globals.d.ts +12 -0
  39. package/src/client/bucket-handle.test.ts +117 -0
  40. package/src/client/bucket-handle.ts +227 -0
  41. package/src/client/router.test.ts +58 -0
  42. package/src/client/router.ts +100 -0
  43. package/src/client/runtime.test.ts +195 -0
  44. package/src/client/runtime.ts +296 -0
  45. package/src/client/services.test.ts +60 -0
  46. package/src/client/services.ts +418 -0
  47. package/src/client/sp00ky-client.test.ts +303 -0
  48. package/src/client/sp00ky-client.ts +586 -0
  49. package/src/events/events.test.ts +2 -1
  50. package/src/events/index.ts +3 -0
  51. package/src/index.ts +69 -1
  52. package/src/kernel/constants.test.ts +19 -0
  53. package/src/kernel/constants.ts +80 -0
  54. package/src/kernel/effects.test.ts +40 -0
  55. package/src/kernel/effects.ts +163 -0
  56. package/src/kernel/events.ts +57 -0
  57. package/src/kernel/interpreter.test.ts +116 -0
  58. package/src/kernel/interpreter.ts +148 -0
  59. package/src/kernel/saga.test.ts +89 -0
  60. package/src/kernel/saga.ts +73 -0
  61. package/src/live/index.test.ts +501 -0
  62. package/src/live/index.ts +1039 -0
  63. package/src/modules/app-release/index.test.ts +125 -0
  64. package/src/modules/app-release/index.ts +209 -0
  65. package/src/modules/auth/auth.local-first.test.ts +169 -0
  66. package/src/modules/auth/events/index.ts +9 -2
  67. package/src/modules/auth/impersonation-banner.ts +443 -0
  68. package/src/modules/auth/impersonation.integration.test.ts +205 -0
  69. package/src/modules/auth/impersonation.test.ts +377 -0
  70. package/src/modules/auth/impersonation.ts +179 -0
  71. package/src/modules/auth/index.ts +466 -19
  72. package/src/modules/crdt/crdt-field.ts +294 -0
  73. package/src/modules/crdt/crdt-hydration.test.ts +210 -0
  74. package/src/modules/crdt/crdt-reconnect.test.ts +195 -0
  75. package/src/modules/crdt/index.ts +463 -0
  76. package/src/modules/crdt/loro-loader.ts +25 -0
  77. package/src/modules/devtools/flags.ts +349 -0
  78. package/src/modules/devtools/index.ts +746 -95
  79. package/src/modules/devtools/mutations.test.ts +92 -0
  80. package/src/modules/devtools/mutations.ts +160 -0
  81. package/src/modules/devtools/notify-throttle.test.ts +166 -0
  82. package/src/modules/devtools/state-shape.test.ts +263 -0
  83. package/src/modules/devtools/storage-info.test.ts +79 -0
  84. package/src/modules/devtools/storage-info.ts +168 -0
  85. package/src/modules/devtools/versions.test.ts +74 -0
  86. package/src/modules/devtools/versions.ts +110 -0
  87. package/src/modules/feature-flag/index.test.ts +298 -0
  88. package/src/modules/feature-flag/index.ts +316 -0
  89. package/src/modules/ref-tables.test.ts +93 -0
  90. package/src/modules/ref-tables.ts +91 -0
  91. package/src/modules/web-push/index.test.ts +824 -0
  92. package/src/modules/web-push/index.ts +1027 -0
  93. package/src/mutation/jobs.test.ts +25 -0
  94. package/src/mutation/jobs.ts +41 -0
  95. package/src/mutation/mutation-id.test.ts +37 -0
  96. package/src/mutation/mutation-id.ts +35 -0
  97. package/src/mutation/pending-writes.integration.test.ts +175 -0
  98. package/src/mutation/push.saga.test.ts +285 -0
  99. package/src/mutation/push.saga.ts +217 -0
  100. package/src/mutation/rows.test.ts +231 -0
  101. package/src/mutation/rows.ts +427 -0
  102. package/src/mutation/tray.saga.test.ts +63 -0
  103. package/src/mutation/tray.saga.ts +48 -0
  104. package/src/mutation/write.saga.test.ts +316 -0
  105. package/src/mutation/write.saga.ts +268 -0
  106. package/src/otel/index.ts +10 -7
  107. package/src/pure.ts +66 -0
  108. package/src/push/types.ts +174 -0
  109. package/src/query/anon-query-isolation.integration.test.ts +337 -0
  110. package/src/query/env.test.ts +23 -0
  111. package/src/query/env.ts +56 -0
  112. package/src/query/fetch.saga.test.ts +199 -0
  113. package/src/query/fetch.saga.ts +120 -0
  114. package/src/query/hash.test.ts +14 -0
  115. package/src/query/hash.ts +18 -0
  116. package/src/query/id-set-plan.test.ts +132 -0
  117. package/src/query/lifecycle.saga.test.ts +274 -0
  118. package/src/query/lifecycle.saga.ts +159 -0
  119. package/src/query/list-ref-live.integration.test.ts +203 -0
  120. package/src/query/materialize.saga.test.ts +115 -0
  121. package/src/query/materialize.saga.ts +84 -0
  122. package/src/query/materialize.test.ts +58 -0
  123. package/src/query/materialize.ts +57 -0
  124. package/src/query/membership.saga.test.ts +350 -0
  125. package/src/query/membership.saga.ts +278 -0
  126. package/src/query/membership.test.ts +118 -0
  127. package/src/query/membership.ts +201 -0
  128. package/src/query/register.saga.test.ts +311 -0
  129. package/src/query/register.saga.ts +229 -0
  130. package/src/query/render-set.test.ts +47 -0
  131. package/src/query/render-set.ts +71 -0
  132. package/src/query/sql.test.ts +112 -0
  133. package/src/query/sql.ts +149 -0
  134. package/src/query/window-query.test.ts +70 -0
  135. package/src/query/window-query.ts +195 -0
  136. package/src/services/blobs/blob-cache.test.ts +359 -0
  137. package/src/services/blobs/blob-cache.ts +603 -0
  138. package/src/services/blobs/blob-manifest.ts +227 -0
  139. package/src/services/blobs/blob-store.test.ts +77 -0
  140. package/src/services/blobs/blob-store.ts +359 -0
  141. package/src/services/blobs/blob.fixture.ts +90 -0
  142. package/src/services/blobs/index.ts +70 -0
  143. package/src/services/database/cache-engine.ts +199 -0
  144. package/src/services/database/connection-supervisor.test.ts +318 -0
  145. package/src/services/database/connection-supervisor.ts +422 -0
  146. package/src/services/database/database.query-timeout.test.ts +140 -0
  147. package/src/services/database/database.responses.test.ts +43 -0
  148. package/src/services/database/database.ts +146 -61
  149. package/src/services/database/engine-factory.ts +33 -0
  150. package/src/services/database/errors.ts +34 -0
  151. package/src/services/database/events/index.ts +2 -1
  152. package/src/services/database/index.ts +7 -0
  153. package/src/services/database/landed-body-clear.integration.test.ts +236 -0
  154. package/src/services/database/local-migrator.integration.test.ts +110 -0
  155. package/src/services/database/local-migrator.ts +59 -16
  156. package/src/services/database/local.test.ts +64 -0
  157. package/src/services/database/local.ts +487 -63
  158. package/src/services/database/plan-render.test.ts +159 -0
  159. package/src/services/database/plan-render.ts +108 -0
  160. package/src/services/database/relation-resolver.test.ts +484 -0
  161. package/src/services/database/relation-resolver.ts +0 -0
  162. package/src/services/database/remote.ts +273 -6
  163. package/src/services/database/sqlite-cache-engine.test.ts +617 -0
  164. package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
  165. package/src/services/database/sqlite-cache-engine.ts +1372 -0
  166. package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
  167. package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
  168. package/src/services/database/sqlite-lock-verify.test.ts +33 -0
  169. package/src/services/database/sqlite-lock-verify.ts +45 -0
  170. package/src/services/database/sqlite-open.test.ts +150 -0
  171. package/src/services/database/sqlite-open.ts +164 -0
  172. package/src/services/database/sqlite-plan-sql.test.ts +145 -0
  173. package/src/services/database/sqlite-plan-sql.ts +182 -0
  174. package/src/services/database/sqlite-projection.test.ts +121 -0
  175. package/src/services/database/sqlite-select.integration.test.ts +185 -0
  176. package/src/services/database/sqlite-select.test.ts +261 -0
  177. package/src/services/database/sqlite-select.ts +131 -0
  178. package/src/services/database/sqlite-transport.fixture.ts +30 -0
  179. package/src/services/database/sqlite-transport.ts +224 -0
  180. package/src/services/database/sqlite-worker.ts +437 -0
  181. package/src/services/database/surql-translate.ts +416 -0
  182. package/src/services/database/surreal-cache-engine.ts +161 -0
  183. package/src/services/logger/index.test.ts +132 -0
  184. package/src/services/logger/index.ts +189 -7
  185. package/src/services/persistence/localstorage.ts +2 -2
  186. package/src/services/persistence/resilient.ts +10 -3
  187. package/src/services/persistence/surrealdb.ts +4 -4
  188. package/src/services/stream-processor/index.ts +783 -68
  189. package/src/services/stream-processor/permissions.test.ts +47 -0
  190. package/src/services/stream-processor/permissions.ts +53 -0
  191. package/src/services/stream-processor/stream-processor.batch.test.ts +186 -0
  192. package/src/services/stream-processor/stream-processor.prime.test.ts +198 -0
  193. package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
  194. package/src/services/stream-processor/stream-processor.test.ts +1 -1
  195. package/src/services/stream-processor/wasm-types.ts +58 -2
  196. package/src/services/tabs/broker-client.ts +283 -0
  197. package/src/services/tabs/broker.test.ts +327 -0
  198. package/src/services/tabs/coordinator.test.ts +386 -0
  199. package/src/services/tabs/coordinator.ts +638 -0
  200. package/src/services/tabs/fake-ports.fixture.ts +112 -0
  201. package/src/services/tabs/leader-locks.ts +75 -0
  202. package/src/services/tabs/protocol.ts +262 -0
  203. package/src/services/tabs/support.ts +36 -0
  204. package/src/services/tabs/tabs-broker-worker.ts +640 -0
  205. package/src/sp00ky.ts +5 -395
  206. package/src/state/client-state.ts +200 -0
  207. package/src/state/lifecycle.test.ts +141 -0
  208. package/src/state/lifecycle.ts +99 -0
  209. package/src/state/reducers.test.ts +304 -0
  210. package/src/state/reducers.ts +526 -0
  211. package/src/state/selectors.test.ts +175 -0
  212. package/src/state/selectors.ts +222 -0
  213. package/src/sw/index.test.ts +582 -0
  214. package/src/sw/index.ts +607 -0
  215. package/src/sw/store.ts +96 -0
  216. package/src/sync/connection.saga.test.ts +97 -0
  217. package/src/sync/connection.saga.ts +89 -0
  218. package/src/sync/live.saga.test.ts +178 -0
  219. package/src/sync/live.saga.ts +107 -0
  220. package/src/sync/policy.health.test.ts +56 -0
  221. package/src/sync/policy.test.ts +663 -0
  222. package/src/sync/policy.ts +515 -0
  223. package/src/sync/poll.saga.test.ts +51 -0
  224. package/src/sync/poll.saga.ts +36 -0
  225. package/src/sync/tabs.saga.test.ts +78 -0
  226. package/src/sync/tabs.saga.ts +96 -0
  227. package/src/testing/adapters.ts +133 -0
  228. package/src/testing/build.ts +63 -0
  229. package/src/testing/fake-services.ts +48 -0
  230. package/src/testing/run-pure.test.ts +92 -0
  231. package/src/testing/run-pure.ts +128 -0
  232. package/src/testing/services.ts +17 -0
  233. package/src/types.ts +624 -10
  234. package/src/utils/blurhash.ts +90 -0
  235. package/src/utils/edges.ts +39 -0
  236. package/src/utils/error-classification.test.ts +54 -0
  237. package/src/utils/error-classification.ts +25 -0
  238. package/src/utils/index.ts +78 -12
  239. package/src/utils/parser.test.ts +67 -102
  240. package/src/utils/parser.ts +64 -2
  241. package/src/utils/semver.test.ts +32 -0
  242. package/src/utils/semver.ts +30 -0
  243. package/src/utils/sha256.ts +15 -0
  244. package/src/utils/surql.ts +30 -18
  245. package/src/utils/utils.test.ts +3 -2
  246. package/tsdown.config.ts +117 -3
  247. package/vitest.config.ts +48 -0
  248. package/src/modules/cache/index.ts +0 -241
  249. package/src/modules/cache/types.ts +0 -19
  250. package/src/modules/data/data.test.ts +0 -58
  251. package/src/modules/data/index.ts +0 -879
  252. package/src/modules/sync/engine.ts +0 -173
  253. package/src/modules/sync/events/index.ts +0 -77
  254. package/src/modules/sync/index.ts +0 -3
  255. package/src/modules/sync/queue/index.ts +0 -2
  256. package/src/modules/sync/queue/queue-down.ts +0 -89
  257. package/src/modules/sync/queue/queue-up.ts +0 -223
  258. package/src/modules/sync/scheduler.ts +0 -84
  259. package/src/modules/sync/sync.ts +0 -421
  260. package/src/modules/sync/utils.test.ts +0 -311
  261. package/src/modules/sync/utils.ts +0 -167
@@ -0,0 +1,89 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import type { Effect } from './effects';
3
+ import { fx } from './effects';
4
+ import { acquire, emptyLanes, release, runSaga } from './saga';
5
+ import type { Saga } from './saga';
6
+
7
+ describe('runSaga', () => {
8
+ it('threads effect results back into the generator in order', async () => {
9
+ const seen: Effect[] = [];
10
+ function* saga(): Saga<number> {
11
+ const a = (yield fx.now()) as number;
12
+ const b = (yield fx.hash('x')) as number;
13
+ return a + b;
14
+ }
15
+ const result = await runSaga(saga(), async (e) => {
16
+ seen.push(e);
17
+ return e.kind === 'now' ? 1 : 2;
18
+ });
19
+ expect(result).toBe(3);
20
+ expect(seen.map((e) => e.kind)).toEqual(['now', 'hash']);
21
+ });
22
+
23
+ it('throws a rejected effect into the saga so it can catch it', async () => {
24
+ function* saga(): Saga<string> {
25
+ try {
26
+ yield fx.now();
27
+ return 'unreachable';
28
+ } catch (err) {
29
+ return `caught:${(err as Error).message}`;
30
+ }
31
+ }
32
+ await expect(runSaga(saga(), async () => Promise.reject(new Error('boom')))).resolves.toBe('caught:boom');
33
+ });
34
+
35
+ it('propagates an error the saga does not catch', async () => {
36
+ function* saga(): Saga<void> {
37
+ yield fx.now();
38
+ }
39
+ await expect(runSaga(saga(), async () => Promise.reject(new Error('boom')))).rejects.toThrow('boom');
40
+ });
41
+
42
+ it('returns immediately for a saga that yields nothing', async () => {
43
+ // oxlint-disable-next-line require-yield
44
+ function* saga(): Saga<number> {
45
+ return 7;
46
+ }
47
+ await expect(runSaga(saga(), async () => undefined)).resolves.toBe(7);
48
+ });
49
+ });
50
+
51
+ describe('lanes', () => {
52
+ it('serial: first acquires, second waits, release hands over, final release frees', () => {
53
+ const lane = { kind: 'serial', key: 'outbox' } as const;
54
+ const a = acquire(emptyLanes(), lane);
55
+ expect(a.decision).toBe('start');
56
+ const b = acquire(a.state, lane);
57
+ expect(b.decision).toBe('wait');
58
+ const c = acquire(b.state, lane);
59
+ expect(c.decision).toBe('wait');
60
+ expect(c.state.waiting.get('outbox')).toBe(2);
61
+ const r1 = release(c.state, 'outbox');
62
+ expect(r1.startNext).toBe(true);
63
+ expect(r1.state.waiting.get('outbox')).toBe(1);
64
+ expect(r1.state.running.has('outbox')).toBe(true);
65
+ const r2 = release(r1.state, 'outbox');
66
+ expect(r2.startNext).toBe(true);
67
+ expect(r2.state.waiting.has('outbox')).toBe(false);
68
+ const r3 = release(r2.state, 'outbox');
69
+ expect(r3.startNext).toBe(false);
70
+ expect(r3.state.running.has('outbox')).toBe(false);
71
+ });
72
+
73
+ it('dedupe: a request during a run joins it and does not queue', () => {
74
+ const lane = { kind: 'dedupe', key: 'mat:h' } as const;
75
+ const a = acquire(emptyLanes(), lane);
76
+ const b = acquire(a.state, lane);
77
+ expect(b.decision).toBe('join');
78
+ expect(b.state).toBe(a.state);
79
+ const r = release(b.state, 'mat:h');
80
+ expect(r.startNext).toBe(false);
81
+ expect(r.state.running.size).toBe(0);
82
+ });
83
+
84
+ it('keys are independent', () => {
85
+ const a = acquire(emptyLanes(), { kind: 'serial', key: 'x' });
86
+ const b = acquire(a.state, { kind: 'serial', key: 'y' });
87
+ expect(b.decision).toBe('start');
88
+ });
89
+ });
@@ -0,0 +1,73 @@
1
+ import type { Effect } from './effects';
2
+
3
+ /**
4
+ * A saga is a generator that yields effects and receives their results. It
5
+ * holds no references to adapters or state, so it can be driven by the real
6
+ * interpreter or by `testing/run-pure` with canned results.
7
+ */
8
+ export type Saga<R> = Generator<Effect, R, any>;
9
+ export type Interpret = (effect: Effect) => Promise<unknown>;
10
+
11
+ /**
12
+ * Drive a saga to completion. A rejected effect is thrown back into the saga
13
+ * at the `yield`, so sagas use ordinary `try/catch`; an error the saga does
14
+ * not catch propagates to the caller.
15
+ */
16
+ export async function runSaga<R>(saga: Saga<R>, interpret: Interpret): Promise<R> {
17
+ let step = saga.next();
18
+ while (!step.done) {
19
+ let result: unknown;
20
+ try {
21
+ result = await interpret(step.value);
22
+ } catch (error) {
23
+ step = saga.throw(error);
24
+ continue;
25
+ }
26
+ step = saga.next(result);
27
+ }
28
+ return step.value;
29
+ }
30
+
31
+ /**
32
+ * Lanes bound concurrency without a queue object per lane:
33
+ * - `serial`: runs strictly one at a time per key, in arrival order.
34
+ * - `dedupe`: a request while one run is active joins that run instead of
35
+ * starting another.
36
+ * The bookkeeping is a pure value so the policy is unit-testable; the runtime
37
+ * owns the promises.
38
+ */
39
+ export type Lane = { kind: 'serial'; key: string } | { kind: 'dedupe'; key: string };
40
+
41
+ export interface LaneState {
42
+ readonly running: ReadonlySet<string>;
43
+ readonly waiting: ReadonlyMap<string, number>;
44
+ }
45
+
46
+ export const emptyLanes = (): LaneState => ({ running: new Set(), waiting: new Map() });
47
+
48
+ export type LaneDecision = 'start' | 'wait' | 'join';
49
+
50
+ export function acquire(state: LaneState, lane: Lane): { decision: LaneDecision; state: LaneState } {
51
+ if (!state.running.has(lane.key)) {
52
+ const running = new Set(state.running);
53
+ running.add(lane.key);
54
+ return { decision: 'start', state: { running, waiting: state.waiting } };
55
+ }
56
+ if (lane.kind === 'dedupe') return { decision: 'join', state };
57
+ const waiting = new Map(state.waiting);
58
+ waiting.set(lane.key, (waiting.get(lane.key) ?? 0) + 1);
59
+ return { decision: 'wait', state: { running: state.running, waiting } };
60
+ }
61
+
62
+ export function release(state: LaneState, key: string): { startNext: boolean; state: LaneState } {
63
+ const pending = state.waiting.get(key) ?? 0;
64
+ if (pending > 0) {
65
+ const waiting = new Map(state.waiting);
66
+ if (pending === 1) waiting.delete(key);
67
+ else waiting.set(key, pending - 1);
68
+ return { startNext: true, state: { running: state.running, waiting } };
69
+ }
70
+ const running = new Set(state.running);
71
+ running.delete(key);
72
+ return { startNext: false, state: { running, waiting: state.waiting } };
73
+ }
@@ -0,0 +1,501 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { RecordId, Uuid } from 'surrealdb';
3
+ import { QueryBuilder } from '@spooky-sync/query-builder';
4
+ import {
5
+ createLiveFeed,
6
+ liveEndpoint,
7
+ liveQueryInput,
8
+ LiveFeedError,
9
+ type LiveSurreal,
10
+ } from './index';
11
+ import { Sp00kyClient } from '../client/sp00ky-client';
12
+ import { Runtime } from '../client/runtime';
13
+ import { fakeAdapters } from '../testing/adapters';
14
+ import { fakeServiceBundle } from '../testing/fake-services';
15
+ import { runPure } from '../testing/run-pure';
16
+ import { defaultEnv } from '../query/env';
17
+ import { registerLocal, registerRemote } from '../query/register.saga';
18
+ import { emptyState } from '../state/client-state';
19
+ import { queryKey } from '../pure';
20
+ import { queryHashInput } from '../query/hash';
21
+ import { sha256Hex } from '../utils/sha256';
22
+
23
+ const schema = {
24
+ tables: [
25
+ {
26
+ name: 'notification',
27
+ columns: { user: { recordId: true }, read: {}, created_at: { dateTime: true } },
28
+ },
29
+ ],
30
+ relationships: [],
31
+ } as any;
32
+
33
+ function jwt(claims: Record<string, unknown>): string {
34
+ const b64 = (o: unknown) => Buffer.from(JSON.stringify(o)).toString('base64url');
35
+ return `${b64({ alg: 'HS512', typ: 'JWT' })}.${b64(claims)}.sig`;
36
+ }
37
+ const aliceToken = jwt({
38
+ ID: 'user:alice',
39
+ AC: 'account',
40
+ exp: Math.floor(Date.now() / 1000) + 3600,
41
+ });
42
+
43
+ const n1 = new RecordId('notification', 'n1');
44
+ const n2 = new RecordId('notification', 'n2');
45
+ const n3 = new RecordId('notification', 'n3');
46
+ const body = (rid: RecordId, extra: Record<string, unknown> = {}) => ({
47
+ id: rid,
48
+ user: new RecordId('user', 'alice'),
49
+ read: false,
50
+ title: `t-${rid.id}`,
51
+ ...extra,
52
+ });
53
+
54
+ /** A scripted stand-in for the `surrealdb` SDK client. */
55
+ class FakeSurreal {
56
+ calls: Array<{ sql: string; vars?: Record<string, any> }> = [];
57
+ edges: Array<{ out: RecordId; version: number }> = [];
58
+ bodies = new Map<string, Record<string, unknown>>();
59
+ meta: { rowCount: number; state: string } | null = { rowCount: 0, state: 'ready' };
60
+ liveCb: ((m: { action: string; value: unknown }) => void) | null = null;
61
+ killed = false;
62
+ closed = false;
63
+ authError: Error | null = null;
64
+ heartbeatGone = false;
65
+ liveId = new Uuid('018f0000-0000-7000-8000-000000000001');
66
+ connect = vi.fn(async () => true);
67
+ use = vi.fn(async () => true);
68
+ authenticate = vi.fn(async () => {
69
+ if (this.authError) throw this.authError;
70
+ return true;
71
+ });
72
+ close = vi.fn(async () => {
73
+ this.closed = true;
74
+ });
75
+
76
+ query(sql: string, vars?: Record<string, any>): Promise<unknown[]> {
77
+ this.calls.push({ sql, vars });
78
+ return Promise.resolve(this.answer(sql, vars));
79
+ }
80
+
81
+ answer(sql: string, vars?: Record<string, any>): unknown[] {
82
+ const snapshot = () => [this.edges, this.meta, []];
83
+ if (sql.startsWith('fn::query::register')) return [null, ...snapshot()];
84
+ if (sql.startsWith('SELECT out, version FROM')) return snapshot();
85
+ if (sql === 'SELECT * FROM $ids') {
86
+ const rows = (vars!.ids as RecordId[])
87
+ .map((rid) => this.bodies.get(`${rid.table.name}:${rid.id}`))
88
+ .filter(Boolean);
89
+ return [rows];
90
+ }
91
+ if (sql.startsWith('LIVE SELECT')) return [this.liveId];
92
+ if (sql.startsWith('fn::query::heartbeat')) return [this.heartbeatGone ? [] : [{ id: 'q' }]];
93
+ if (sql.startsWith('fn::query::unsubscribe'))
94
+ return sql.split(';').map(() => ({ released: true }));
95
+ if (sql.startsWith('KILL')) return [null];
96
+ if (sql.startsWith('RETURN')) return ['answer'];
97
+ throw new Error(`unscripted: ${sql}`);
98
+ }
99
+
100
+ liveOf(_id: unknown) {
101
+ return Promise.resolve({
102
+ subscribe: (cb: (m: { action: string; value: unknown }) => void) => {
103
+ this.liveCb = cb;
104
+ return () => {
105
+ this.liveCb = null;
106
+ };
107
+ },
108
+ kill: async () => {
109
+ this.killed = true;
110
+ },
111
+ });
112
+ }
113
+
114
+ events = new Map<string, Array<() => void>>();
115
+ subscribe(event: string, cb: () => void): () => void {
116
+ this.events.set(event, [...(this.events.get(event) ?? []), cb]);
117
+ return () =>
118
+ this.events.set(
119
+ event,
120
+ (this.events.get(event) ?? []).filter((x) => x !== cb)
121
+ );
122
+ }
123
+ fire(event: string): void {
124
+ for (const cb of this.events.get(event) ?? []) cb();
125
+ }
126
+
127
+ emit(action: string, value: unknown): void {
128
+ this.liveCb?.({ action, value });
129
+ }
130
+
131
+ put(
132
+ rid: RecordId,
133
+ version: number,
134
+ extra: Record<string, unknown> = {}
135
+ ): Record<string, unknown> {
136
+ const row = body(rid, extra);
137
+ this.bodies.set(`${rid.table.name}:${rid.id}`, row);
138
+ this.edges = [
139
+ ...this.edges.filter((e) => String(e.out.id) !== String(rid.id)),
140
+ { out: rid, version },
141
+ ];
142
+ return row;
143
+ }
144
+ }
145
+
146
+ const tick = (ms = 0) => new Promise((r) => setTimeout(r, ms));
147
+
148
+ function feedWith(fake: FakeSurreal, over: Partial<Parameters<typeof createLiveFeed>[0]> = {}) {
149
+ return createLiveFeed({
150
+ endpoint: 'https://db.example.com',
151
+ namespace: 'ns',
152
+ database: 'db',
153
+ token: aliceToken,
154
+ sessionId: 'sess',
155
+ surreal: fake as unknown as LiveSurreal,
156
+ ...over,
157
+ });
158
+ }
159
+
160
+ describe('createLiveFeed', () => {
161
+ it('registers exactly what the full client sends for the same query and session', async () => {
162
+ const q = new QueryBuilder(schema, 'notification')
163
+ .where({ user: 'user:alice', read: false } as any)
164
+ .build();
165
+
166
+ // The full client: its own registerInput, then the register sagas.
167
+ const services = fakeServiceBundle<any>();
168
+ const a = fakeAdapters({ services: { 'remote.connect': () => new Promise(() => {}) } as any });
169
+ const runtime = new Runtime({
170
+ env: defaultEnv(schema),
171
+ adapters: a.adapters,
172
+ logger: services.logger,
173
+ tabId: 't',
174
+ });
175
+ const client = new Sp00kyClient<any>(
176
+ {
177
+ database: { namespace: 'ns', database: 'db' },
178
+ schema,
179
+ schemaSurql: '',
180
+ logLevel: 'silent',
181
+ } as any,
182
+ {
183
+ services,
184
+ runtime,
185
+ }
186
+ );
187
+ const input = (client as any).registerInput('notification', q.innerQuery, '10m');
188
+ const env = defaultEnv(schema);
189
+ const local = await runPure(registerLocal(env, input), {
190
+ state: { ...emptyState({ tabId: 't' }), sessionId: 'sess', userId: 'user:alice' },
191
+ handlers: {
192
+ 'local.getById': () => null,
193
+ 'ssp.register': () => ({
194
+ localArray: [],
195
+ timings: { parseMs: 0, planMs: 0, snapshotMs: 0, wallMs: 0 },
196
+ }),
197
+ },
198
+ });
199
+ let sent: { sql: string; vars: Record<string, unknown> } | null = null;
200
+ await runPure(registerRemote(env, local.result), {
201
+ state: local.state,
202
+ handlers: {
203
+ 'remote.query': (e: any) => {
204
+ sent = { sql: e.sql, vars: e.vars };
205
+ throw new Error('captured');
206
+ },
207
+ },
208
+ });
209
+
210
+ const fake = new FakeSurreal();
211
+ const feed = feedWith(fake);
212
+ const sub = feed.subscribe(q, { onSet: () => {} });
213
+ await sub.ready;
214
+ const reg = fake.calls.find((c) => c.sql.startsWith('fn::query::register'))!;
215
+ expect(sent).not.toBeNull();
216
+ expect(reg.sql).toBe(sent!.sql);
217
+ expect(reg.vars).toEqual(sent!.vars);
218
+ expect(reg.sql).toContain('FROM $in->_00_list_ref_user_alice');
219
+ expect((reg.vars!.config as any).params.user).toBeInstanceOf(RecordId);
220
+ expect(sub.key).toBe(local.result);
221
+ expect(await queryKey({ surql: input.surql, params: input.params }, 'sess')).toBe(local.result);
222
+ expect(feed.userId).toBe('user:alice');
223
+ expect(fake.connect).toHaveBeenCalledWith('wss://db.example.com/rpc', expect.anything());
224
+ expect(fake.use).toHaveBeenCalledWith({ namespace: 'ns', database: 'db' });
225
+ expect(fake.authenticate).toHaveBeenCalledWith(aliceToken);
226
+ await feed.close();
227
+ await client.close();
228
+ });
229
+
230
+ it('loads the first set, lands FETCH bodies from LIVE, fetches bare ids, drops deletes', async () => {
231
+ const fake = new FakeSurreal();
232
+ const r1 = fake.put(n1, 1);
233
+ const feed = feedWith(fake);
234
+ const sets: unknown[][] = [];
235
+ const changes: any[] = [];
236
+ const sub = feed.subscribe(
237
+ { surql: 'SELECT * FROM notification WHERE user = $auth.id;' },
238
+ { onSet: (rows) => sets.push(rows), onChange: (c) => changes.push(c) }
239
+ );
240
+ expect(await sub.ready).toEqual([r1]);
241
+ expect(sets).toEqual([[r1]]);
242
+ expect(
243
+ fake.calls.some((c) => c.sql === 'LIVE SELECT * FROM _00_list_ref_user_alice FETCH out')
244
+ ).toBe(true);
245
+ const hash = sub.key!;
246
+ const inRid = new RecordId('_00_query', hash);
247
+
248
+ // Inline body (FETCH out): no extra read.
249
+ const reads = fake.calls.length;
250
+ const r2 = body(n2, { title: 'inline' });
251
+ fake.emit('CREATE', { in: inRid, out: r2, version: 1 });
252
+ await tick(30);
253
+ expect(fake.calls.length).toBe(reads);
254
+ expect(sets.at(-1)).toEqual([r1, r2]);
255
+ expect(changes.at(-1)).toEqual({ added: [r2], updated: [], removed: [] });
256
+
257
+ // An older version of a held row is ignored; a newer one updates.
258
+ fake.emit('UPDATE', { in: inRid, out: body(n2, { title: 'stale' }), version: 1 });
259
+ const r2b = fake.put(n2, 2, { title: 'newer' });
260
+ fake.emit('UPDATE', { in: inRid, out: r2b, version: 2 });
261
+ await tick(30);
262
+ expect(changes.at(-1)).toEqual({ added: [], updated: [r2b], removed: [] });
263
+
264
+ // Bare id (a server that ignored FETCH): the body is read.
265
+ const r3 = fake.put(n3, 4);
266
+ fake.emit('CREATE', { in: inRid, out: n3, version: 4 });
267
+ await tick(60);
268
+ expect(fake.calls.at(-1)!.sql).toBe('SELECT * FROM $ids');
269
+ expect(changes.at(-1)).toEqual({ added: [r3], updated: [], removed: [] });
270
+
271
+ // Another view's edge and subquery children are not ours.
272
+ fake.emit('CREATE', {
273
+ in: new RecordId('_00_query', 'other'),
274
+ out: body(new RecordId('notification', 'x')),
275
+ version: 1,
276
+ });
277
+ fake.emit('CREATE', {
278
+ in: inRid,
279
+ out: body(new RecordId('notification', 'child')),
280
+ version: 1,
281
+ parent: 'notification:n1',
282
+ });
283
+ await tick(30);
284
+ expect(sub.rows()).toHaveLength(3);
285
+
286
+ fake.edges = fake.edges.filter((e) => e.out.id !== 'n1');
287
+ fake.emit('DELETE', { in: inRid, out: n1, version: 1 });
288
+ await tick(30);
289
+ expect(changes.at(-1)).toEqual({ added: [], updated: [], removed: [r1] });
290
+ expect(sub.rows()).toEqual([r2b, r3]);
291
+
292
+ // `out: null` (unreadable or deleted row): membership is re-read and diffed.
293
+ fake.edges = fake.edges.filter((e) => e.out.id !== 'n3');
294
+ fake.emit('DELETE', { in: inRid, out: null, version: 4 });
295
+ await tick(120);
296
+ expect(sub.rows()).toEqual([r2b]);
297
+ expect(await feed.idle({ timeoutMs: 2000, quietMs: 20 })).toBe(true);
298
+ await feed.close();
299
+ });
300
+
301
+ it('shares one view between identical subscriptions and releases it with the last one', async () => {
302
+ const fake = new FakeSurreal();
303
+ fake.put(n1, 1);
304
+ const feed = feedWith(fake);
305
+ const a = feed.subscribe({ surql: 'SELECT * FROM notification;' }, { onSet: () => {} });
306
+ await a.ready;
307
+ const seen: unknown[][] = [];
308
+ const b = feed.subscribe(
309
+ { surql: 'SELECT * FROM notification;' },
310
+ { onSet: (rows) => seen.push(rows) }
311
+ );
312
+ await b.ready;
313
+ expect(seen).toHaveLength(1);
314
+ expect(fake.calls.filter((c) => c.sql.startsWith('fn::query::register'))).toHaveLength(1);
315
+ await a.unsubscribe();
316
+ expect(fake.calls.some((c) => c.sql.startsWith('fn::query::unsubscribe'))).toBe(false);
317
+ await b.unsubscribe();
318
+ const release = fake.calls.find((c) => c.sql.startsWith('fn::query::unsubscribe'))!;
319
+ expect(release.vars!.id).toEqual(new RecordId('_00_query', b.key!));
320
+ await feed.close();
321
+ });
322
+
323
+ it('close() kills LIVE, releases every view, closes the socket; idle waits for the first set', async () => {
324
+ const fake = new FakeSurreal();
325
+ fake.put(n1, 1);
326
+ const feed = feedWith(fake, { token: async () => aliceToken });
327
+ const one = feed.subscribe({ surql: 'SELECT * FROM notification;' }, { onSet: () => {} });
328
+ const two = feed.subscribe(
329
+ { surql: 'SELECT * FROM notification WHERE read = false;' },
330
+ { onSet: () => {} }
331
+ );
332
+ expect(await feed.idle({ timeoutMs: 3000, quietMs: 20 })).toBe(true);
333
+ expect(one.rows()).toHaveLength(1);
334
+ await feed.close();
335
+ expect(fake.killed).toBe(true);
336
+ const release = fake.calls.find((c) => c.sql.includes('fn::query::unsubscribe($id0)'))!;
337
+ expect(release.sql).toContain('fn::query::unsubscribe($id1)');
338
+ expect([release.vars!.id0, release.vars!.id1]).toEqual(
339
+ expect.arrayContaining([
340
+ new RecordId('_00_query', one.key!),
341
+ new RecordId('_00_query', two.key!),
342
+ ])
343
+ );
344
+ expect(fake.closed).toBe(true);
345
+ expect(feed.closed).toBe(true);
346
+ await feed.close();
347
+ await expect(feed.query('RETURN 1')).rejects.toBeInstanceOf(LiveFeedError);
348
+ expect(await feed.idle()).toBe(false);
349
+ });
350
+
351
+ it('a refused token calls onUnauthorized and rejects the subscription', async () => {
352
+ const fake = new FakeSurreal();
353
+ fake.authError = new Error('There was a problem with authentication');
354
+ const onUnauthorized = vi.fn();
355
+ const onError = vi.fn();
356
+ const feed = feedWith(fake, { onUnauthorized });
357
+ const sub = feed.subscribe(
358
+ { surql: 'SELECT * FROM notification;' },
359
+ { onSet: () => {}, onError }
360
+ );
361
+ await expect(sub.ready).rejects.toMatchObject({ code: 'unauthorized' });
362
+ expect(onUnauthorized).toHaveBeenCalledTimes(1);
363
+ expect(onError).toHaveBeenCalledTimes(1);
364
+ expect(fake.closed).toBe(true);
365
+ });
366
+
367
+ it('anonymous feeds use the anon list_ref table; query() runs on the feed connection', async () => {
368
+ const fake = new FakeSurreal();
369
+ const feed = feedWith(fake, { token: undefined });
370
+ expect(await feed.query('RETURN fn::push::info()')).toEqual(['answer']);
371
+ expect(fake.authenticate).not.toHaveBeenCalled();
372
+ const sub = feed.subscribe({ surql: 'SELECT * FROM notification;' }, { onSet: () => {} });
373
+ await sub.ready;
374
+ expect(feed.listRefTable).toBe('_00_list_ref_anon');
375
+ await feed.close();
376
+ });
377
+
378
+ it("an anonymous feed's LIVE hears only its own sockets, earlier ones included after a reconnect", async () => {
379
+ const fake = new FakeSurreal();
380
+ let sid = 0;
381
+ const answer = fake.answer.bind(fake);
382
+ fake.answer = (sql, vars) =>
383
+ sql === 'RETURN <string>session::id()' ? [`sid-${++sid}`] : answer(sql, vars);
384
+ const feed = feedWith(fake, { token: undefined });
385
+ const sub = feed.subscribe({ surql: 'SELECT * FROM notification;' }, { onSet: () => {} });
386
+ await sub.ready;
387
+ fake.fire('disconnected');
388
+ fake.fire('connected');
389
+ await tick(60);
390
+ const lives = fake.calls.filter((c) => c.sql.startsWith('LIVE SELECT'));
391
+ expect(lives.map((c) => c.sql)).toEqual([
392
+ 'LIVE SELECT * FROM _00_list_ref_anon WHERE clientId = <string>session::id() OR clientId IN $sids FETCH out',
393
+ 'LIVE SELECT * FROM _00_list_ref_anon WHERE clientId = <string>session::id() OR clientId IN $sids FETCH out',
394
+ ]);
395
+ // The view was registered on the first socket and is not re-registered,
396
+ // so its edges keep naming `sid-1`: the second LIVE must still hear them.
397
+ expect(lives[1].vars).toEqual({ sids: ['sid-1'] });
398
+ await feed.close();
399
+ });
400
+
401
+ it('heartbeats and re-registers a view the server reclaimed', async () => {
402
+ vi.useFakeTimers({ shouldAdvanceTime: true });
403
+ try {
404
+ const fake = new FakeSurreal();
405
+ fake.put(n1, 1);
406
+ const feed = feedWith(fake, { ttl: '20s' });
407
+ const sub = feed.subscribe({ surql: 'SELECT * FROM notification;' }, { onSet: () => {} });
408
+ await sub.ready;
409
+ fake.heartbeatGone = true;
410
+ await vi.advanceTimersByTimeAsync(10_000);
411
+ expect(fake.calls.some((c) => c.sql.startsWith('fn::query::heartbeat($id0)'))).toBe(true);
412
+ expect(fake.calls.filter((c) => c.sql.startsWith('fn::query::register'))).toHaveLength(2);
413
+ await feed.close();
414
+ } finally {
415
+ vi.useRealTimers();
416
+ }
417
+ });
418
+
419
+ it('after a reconnect: LIVE again and every view re-read (changes in the gap are not lost)', async () => {
420
+ const fake = new FakeSurreal();
421
+ fake.put(n1, 1);
422
+ const feed = feedWith(fake);
423
+ const changes: any[] = [];
424
+ const sub = feed.subscribe(
425
+ { surql: 'SELECT * FROM notification;' },
426
+ { onSet: () => {}, onChange: (c) => changes.push(c) }
427
+ );
428
+ await sub.ready;
429
+ fake.fire('disconnected');
430
+ const r2 = fake.put(n2, 1);
431
+ fake.fire('connected');
432
+ await tick(120);
433
+ expect(fake.calls.filter((c) => c.sql.startsWith('LIVE SELECT'))).toHaveLength(2);
434
+ expect(changes.at(-1)).toEqual({ added: [r2], updated: [], removed: [] });
435
+ await feed.close();
436
+ expect([...fake.events.values()].every((l) => l.length === 0)).toBe(true);
437
+ });
438
+
439
+ it('falls back to a plain LIVE (bodies read per edge) when the server refuses FETCH', async () => {
440
+ const fake = new FakeSurreal();
441
+ fake.put(n1, 1);
442
+ const answer = fake.answer.bind(fake);
443
+ fake.answer = (sql, vars) => {
444
+ if (sql.endsWith('FETCH out')) throw new Error('Parse error: FETCH not supported on LIVE');
445
+ return answer(sql, vars);
446
+ };
447
+ const onError = vi.fn();
448
+ const feed = feedWith(fake, { onError });
449
+ const sub = feed.subscribe({ surql: 'SELECT * FROM notification;' }, { onSet: () => {} });
450
+ await sub.ready;
451
+ expect(onError).toHaveBeenCalledTimes(1);
452
+ expect(fake.calls.filter((c) => c.sql.startsWith('LIVE SELECT')).map((c) => c.sql)).toEqual([
453
+ 'LIVE SELECT * FROM _00_list_ref_user_alice FETCH out',
454
+ 'LIVE SELECT * FROM _00_list_ref_user_alice',
455
+ ]);
456
+ const r2 = fake.put(n2, 1);
457
+ fake.emit('CREATE', { in: new RecordId('_00_query', sub.key!), out: n2, version: 1 });
458
+ await tick(60);
459
+ expect(sub.rows()).toEqual([body(n1), r2]);
460
+ await feed.close();
461
+ });
462
+
463
+ it('waits out a materializing view before delivering the first set', async () => {
464
+ const fake = new FakeSurreal();
465
+ fake.meta = { rowCount: 1, state: 'materializing' };
466
+ const feed = feedWith(fake);
467
+ const sub = feed.subscribe({ surql: 'SELECT * FROM notification;' }, { onSet: () => {} });
468
+ setTimeout(() => {
469
+ fake.put(n1, 1);
470
+ fake.meta = { rowCount: 1, state: 'ready' };
471
+ }, 200);
472
+ expect(await sub.ready).toHaveLength(1);
473
+ await feed.close();
474
+ });
475
+ });
476
+
477
+ describe('live feed helpers', () => {
478
+ it('normalizes endpoints and query inputs', async () => {
479
+ expect(liveEndpoint('https://h.example')).toBe('wss://h.example/rpc');
480
+ expect(liveEndpoint('http://localhost:8000/rpc')).toBe('ws://localhost:8000/rpc');
481
+ expect(liveEndpoint('wss://h/rpc')).toBe('wss://h/rpc');
482
+ expect(liveEndpoint('not a url')).toBe('not a url');
483
+ expect(liveQueryInput({ surql: 'SELECT * FROM thing WHERE a = $a', params: { a: 1 } })).toEqual(
484
+ {
485
+ table: 'thing',
486
+ surql: 'SELECT * FROM thing WHERE a = $a',
487
+ params: { a: 1 },
488
+ }
489
+ );
490
+ const inner = new QueryBuilder(schema, 'notification')
491
+ .where({ user: 'user:bob' } as any)
492
+ .build().innerQuery;
493
+ const fromInner = liveQueryInput(inner as any);
494
+ expect(fromInner.table).toBe('notification');
495
+ expect(fromInner.params.user).toBeInstanceOf(RecordId);
496
+ expect(() => liveQueryInput({} as any)).toThrow(TypeError);
497
+ expect(await queryKey({ surql: 'x', params: {} }, null)).toBe(
498
+ await sha256Hex(queryHashInput({ surql: 'x', params: {} }, null))
499
+ );
500
+ });
501
+ });