@spooky-sync/core 0.0.1-canary.97 → 1.0.0-beta.1

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 +57 -21
  2. package/README.md +59 -16
  3. package/dist/index.d.ts +2096 -929
  4. package/dist/index.js +18217 -5019
  5. package/dist/live.d.ts +149 -0
  6. package/dist/live.js +739 -0
  7. package/dist/otel/index.d.ts +1 -1
  8. package/dist/pure.d.ts +401 -0
  9. package/dist/pure.js +24 -0
  10. package/dist/sqlite-open.js +392 -0
  11. package/dist/sqlite-worker.d.ts +1 -0
  12. package/dist/sqlite-worker.js +439 -0
  13. package/dist/sw.d.ts +207 -0
  14. package/dist/sw.js +393 -0
  15. package/dist/tabs-broker-worker.d.ts +8 -0
  16. package/dist/tabs-broker-worker.js +477 -0
  17. package/dist/types.d.ts +1968 -82
  18. package/dist/worker-edges.js +498 -0
  19. package/dist/worker-impersonation.js +60 -0
  20. package/dist/worker-ref-tables.d.ts +48 -0
  21. package/dist/worker-types.d.ts +112 -0
  22. package/dist/worker-types.js +73 -0
  23. package/package.json +34 -8
  24. package/scripts/check-broker-bundle.mjs +33 -0
  25. package/scripts/check-worker-entries.mjs +173 -0
  26. package/skills/sp00ky-core/SKILL.md +42 -8
  27. package/skills/sp00ky-core/references/config.md +8 -2
  28. package/src/boot/auth-flip.saga.test.ts +174 -0
  29. package/src/boot/auth-flip.saga.ts +77 -0
  30. package/src/boot/boot.saga.test.ts +203 -0
  31. package/src/boot/boot.saga.ts +146 -0
  32. package/src/boot/bucket-switch.saga.test.ts +191 -0
  33. package/src/boot/bucket-switch.saga.ts +151 -0
  34. package/src/boot/preload.saga.test.ts +96 -0
  35. package/src/boot/preload.saga.ts +45 -0
  36. package/src/bucket-blurhash.test.ts +148 -0
  37. package/src/client/bucket-handle.test.ts +195 -0
  38. package/src/client/bucket-handle.ts +352 -0
  39. package/src/client/router.test.ts +59 -0
  40. package/src/client/router.ts +104 -0
  41. package/src/client/runtime.test.ts +319 -0
  42. package/src/client/runtime.ts +307 -0
  43. package/src/client/services.test.ts +74 -0
  44. package/src/client/services.ts +437 -0
  45. package/src/client/sp00ky-client.test.ts +428 -0
  46. package/src/client/sp00ky-client.ts +737 -0
  47. package/src/index.ts +78 -0
  48. package/src/kernel/constants.test.ts +19 -0
  49. package/src/kernel/constants.ts +94 -0
  50. package/src/kernel/effects.test.ts +40 -0
  51. package/src/kernel/effects.ts +171 -0
  52. package/src/kernel/events.ts +60 -0
  53. package/src/kernel/interpreter.test.ts +116 -0
  54. package/src/kernel/interpreter.ts +148 -0
  55. package/src/kernel/saga.test.ts +89 -0
  56. package/src/kernel/saga.ts +73 -0
  57. package/src/live/index.test.ts +512 -0
  58. package/src/live/index.ts +1059 -0
  59. package/src/modules/app-release/index.test.ts +125 -0
  60. package/src/modules/app-release/index.ts +209 -0
  61. package/src/modules/auth/auth.local-first.test.ts +228 -0
  62. package/src/modules/auth/auth.session-recheck.test.ts +140 -0
  63. package/src/modules/auth/events/index.ts +7 -1
  64. package/src/modules/auth/impersonation-banner.ts +443 -0
  65. package/src/modules/auth/impersonation.integration.test.ts +209 -0
  66. package/src/modules/auth/impersonation.test.ts +377 -0
  67. package/src/modules/auth/impersonation.ts +179 -0
  68. package/src/modules/auth/index.ts +516 -34
  69. package/src/modules/channel/channel.test.ts +752 -0
  70. package/src/modules/channel/index.ts +1153 -0
  71. package/src/modules/channel/webrtc.ts +472 -0
  72. package/src/modules/crdt/acceptance.ts +75 -0
  73. package/src/modules/crdt/bytes.ts +50 -0
  74. package/src/modules/crdt/crdt-field.ts +82 -238
  75. package/src/modules/crdt/crdt.test.ts +546 -0
  76. package/src/modules/crdt/hash.ts +103 -0
  77. package/src/modules/crdt/index.ts +740 -271
  78. package/src/modules/crdt/loro-loader.ts +25 -0
  79. package/src/modules/crdt/mirror.ts +37 -0
  80. package/src/modules/devtools/flags.ts +349 -0
  81. package/src/modules/devtools/index.ts +661 -99
  82. package/src/modules/devtools/mutations.test.ts +92 -0
  83. package/src/modules/devtools/mutations.ts +160 -0
  84. package/src/modules/devtools/notify-throttle.test.ts +166 -0
  85. package/src/modules/devtools/state-shape.test.ts +263 -0
  86. package/src/modules/devtools/storage-info.test.ts +79 -0
  87. package/src/modules/devtools/storage-info.ts +168 -0
  88. package/src/modules/devtools/versions.ts +29 -0
  89. package/src/modules/feature-flag/index.test.ts +179 -1
  90. package/src/modules/feature-flag/index.ts +117 -10
  91. package/src/modules/ref-tables.test.ts +28 -1
  92. package/src/modules/ref-tables.ts +23 -1
  93. package/src/modules/web-push/index.test.ts +824 -0
  94. package/src/modules/web-push/index.ts +1027 -0
  95. package/src/mutation/ingest-outage.integration.test.ts +400 -0
  96. package/src/mutation/jobs.test.ts +25 -0
  97. package/src/mutation/jobs.ts +41 -0
  98. package/src/mutation/mutation-id.test.ts +37 -0
  99. package/src/mutation/mutation-id.ts +35 -0
  100. package/src/mutation/pending-writes.integration.test.ts +175 -0
  101. package/src/mutation/push.saga.test.ts +361 -0
  102. package/src/mutation/push.saga.ts +245 -0
  103. package/src/mutation/rows.test.ts +278 -0
  104. package/src/mutation/rows.ts +515 -0
  105. package/src/mutation/tray.saga.test.ts +63 -0
  106. package/src/mutation/tray.saga.ts +48 -0
  107. package/src/mutation/write.saga.test.ts +436 -0
  108. package/src/mutation/write.saga.ts +384 -0
  109. package/src/pure.ts +66 -0
  110. package/src/push/types.ts +174 -0
  111. package/src/query/anon-query-isolation.integration.test.ts +531 -0
  112. package/src/query/env.test.ts +23 -0
  113. package/src/query/env.ts +65 -0
  114. package/src/query/fetch.saga.test.ts +199 -0
  115. package/src/query/fetch.saga.ts +120 -0
  116. package/src/query/hash.test.ts +14 -0
  117. package/src/query/hash.ts +18 -0
  118. package/src/query/id-set-plan.test.ts +132 -0
  119. package/src/query/lifecycle.saga.test.ts +274 -0
  120. package/src/query/lifecycle.saga.ts +186 -0
  121. package/src/query/list-ref-live.integration.test.ts +203 -0
  122. package/src/query/materialize.saga.test.ts +200 -0
  123. package/src/query/materialize.saga.ts +102 -0
  124. package/src/query/materialize.test.ts +58 -0
  125. package/src/query/materialize.ts +57 -0
  126. package/src/query/membership.saga.test.ts +514 -0
  127. package/src/query/membership.saga.ts +339 -0
  128. package/src/query/membership.test.ts +151 -0
  129. package/src/query/membership.ts +234 -0
  130. package/src/query/register.saga.test.ts +369 -0
  131. package/src/query/register.saga.ts +293 -0
  132. package/src/query/render-set.test.ts +47 -0
  133. package/src/query/render-set.ts +71 -0
  134. package/src/query/session-guard.integration.test.ts +304 -0
  135. package/src/query/sql.test.ts +118 -0
  136. package/src/query/sql.ts +174 -0
  137. package/src/{modules/data → query}/window-query.test.ts +22 -4
  138. package/src/{modules/data → query}/window-query.ts +86 -8
  139. package/src/services/blobs/blob-cache.test.ts +359 -0
  140. package/src/services/blobs/blob-cache.ts +603 -0
  141. package/src/services/blobs/blob-manifest.ts +227 -0
  142. package/src/services/blobs/blob-store.test.ts +77 -0
  143. package/src/services/blobs/blob-store.ts +359 -0
  144. package/src/services/blobs/blob.fixture.ts +90 -0
  145. package/src/services/blobs/index.ts +70 -0
  146. package/src/services/database/cache-engine.ts +209 -0
  147. package/src/services/database/connection-supervisor.test.ts +318 -0
  148. package/src/services/database/connection-supervisor.ts +422 -0
  149. package/src/services/database/database.query-timeout.test.ts +140 -0
  150. package/src/services/database/database.responses.test.ts +43 -0
  151. package/src/services/database/database.ts +151 -56
  152. package/src/services/database/engine-factory.ts +33 -0
  153. package/src/services/database/errors.ts +34 -0
  154. package/src/services/database/index.ts +7 -0
  155. package/src/services/database/landed-body-clear.integration.test.ts +236 -0
  156. package/src/services/database/local-migrator.integration.test.ts +110 -0
  157. package/src/services/database/local-migrator.ts +54 -9
  158. package/src/services/database/local.test.ts +117 -0
  159. package/src/services/database/local.ts +364 -74
  160. package/src/services/database/plan-render.test.ts +159 -0
  161. package/src/services/database/plan-render.ts +108 -0
  162. package/src/services/database/relation-resolver.test.ts +484 -0
  163. package/src/services/database/relation-resolver.ts +0 -0
  164. package/src/services/database/remote.ts +283 -4
  165. package/src/services/database/sqlite-cache-engine.test.ts +628 -0
  166. package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
  167. package/src/services/database/sqlite-cache-engine.ts +1401 -0
  168. package/src/services/database/sqlite-column-reset.integration.test.ts +100 -0
  169. package/src/services/database/sqlite-column-reset.ts +91 -0
  170. package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
  171. package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
  172. package/src/services/database/sqlite-lock-verify.test.ts +33 -0
  173. package/src/services/database/sqlite-lock-verify.ts +45 -0
  174. package/src/services/database/sqlite-open.test.ts +150 -0
  175. package/src/services/database/sqlite-open.ts +164 -0
  176. package/src/services/database/sqlite-plan-sql.test.ts +164 -0
  177. package/src/services/database/sqlite-plan-sql.ts +196 -0
  178. package/src/services/database/sqlite-projection.test.ts +121 -0
  179. package/src/services/database/sqlite-select.integration.test.ts +185 -0
  180. package/src/services/database/sqlite-select.test.ts +261 -0
  181. package/src/services/database/sqlite-select.ts +131 -0
  182. package/src/services/database/sqlite-transport.fixture.ts +30 -0
  183. package/src/services/database/sqlite-transport.ts +229 -0
  184. package/src/services/database/sqlite-worker.ts +437 -0
  185. package/src/services/database/surql-translate.ts +417 -0
  186. package/src/services/database/surreal-cache-engine.ts +163 -0
  187. package/src/services/logger/index.test.ts +132 -0
  188. package/src/services/logger/index.ts +187 -6
  189. package/src/services/persistence/surrealdb.ts +2 -2
  190. package/src/services/stream-processor/index.ts +621 -93
  191. package/src/services/stream-processor/stream-processor.batch.test.ts +57 -7
  192. package/src/services/stream-processor/stream-processor.prime.test.ts +234 -0
  193. package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
  194. package/src/services/stream-processor/wasm-types.ts +41 -1
  195. package/src/services/tabs/broker-client.ts +284 -0
  196. package/src/services/tabs/broker.test.ts +381 -0
  197. package/src/services/tabs/coordinator.test.ts +386 -0
  198. package/src/services/tabs/coordinator.ts +646 -0
  199. package/src/services/tabs/fake-ports.fixture.ts +112 -0
  200. package/src/services/tabs/leader-locks.ts +75 -0
  201. package/src/services/tabs/protocol.ts +262 -0
  202. package/src/services/tabs/support.ts +36 -0
  203. package/src/services/tabs/tabs-broker-worker.ts +652 -0
  204. package/src/sp00ky.ts +5 -648
  205. package/src/state/client-state.ts +231 -0
  206. package/src/state/lifecycle.test.ts +184 -0
  207. package/src/state/lifecycle.ts +127 -0
  208. package/src/state/reducers.test.ts +337 -0
  209. package/src/state/reducers.ts +571 -0
  210. package/src/state/selectors.test.ts +203 -0
  211. package/src/state/selectors.ts +264 -0
  212. package/src/sw/index.test.ts +582 -0
  213. package/src/sw/index.ts +607 -0
  214. package/src/sw/store.ts +96 -0
  215. package/src/sync/connection.saga.test.ts +97 -0
  216. package/src/sync/connection.saga.ts +89 -0
  217. package/src/sync/live.saga.test.ts +229 -0
  218. package/src/sync/live.saga.ts +116 -0
  219. package/src/sync/policy.health.test.ts +56 -0
  220. package/src/{modules/sync/utils.test.ts → sync/policy.test.ts} +96 -11
  221. package/src/{modules/sync/utils.ts → sync/policy.ts} +186 -7
  222. package/src/sync/poll.saga.test.ts +51 -0
  223. package/src/sync/poll.saga.ts +36 -0
  224. package/src/sync/tabs.saga.test.ts +78 -0
  225. package/src/sync/tabs.saga.ts +96 -0
  226. package/src/testing/adapters.ts +137 -0
  227. package/src/testing/build.ts +63 -0
  228. package/src/testing/fake-services.ts +63 -0
  229. package/src/testing/run-pure.test.ts +92 -0
  230. package/src/testing/run-pure.ts +128 -0
  231. package/src/testing/services.ts +17 -0
  232. package/src/types.ts +568 -25
  233. package/src/utils/blurhash.ts +90 -0
  234. package/src/utils/edges.ts +57 -0
  235. package/src/utils/error-classification.test.ts +116 -0
  236. package/src/utils/error-classification.ts +74 -0
  237. package/src/utils/index.ts +58 -3
  238. package/src/utils/parser.test.ts +67 -102
  239. package/src/utils/parser.ts +62 -1
  240. package/src/utils/semver.test.ts +32 -0
  241. package/src/utils/semver.ts +30 -0
  242. package/src/utils/sha256.ts +15 -0
  243. package/src/utils/surql.ts +17 -15
  244. package/src/utils/utils.test.ts +3 -2
  245. package/tsdown.config.ts +64 -4
  246. package/vitest.config.ts +48 -0
  247. package/src/modules/cache/index.ts +0 -253
  248. package/src/modules/cache/types.ts +0 -19
  249. package/src/modules/crdt/crdt-hydration.test.ts +0 -206
  250. package/src/modules/data/data.status.test.ts +0 -108
  251. package/src/modules/data/data.test.ts +0 -58
  252. package/src/modules/data/index.ts +0 -1491
  253. package/src/modules/sync/engine.ts +0 -227
  254. package/src/modules/sync/events/index.ts +0 -84
  255. package/src/modules/sync/index.ts +0 -3
  256. package/src/modules/sync/queue/index.ts +0 -2
  257. package/src/modules/sync/queue/queue-down.ts +0 -90
  258. package/src/modules/sync/queue/queue-up.ts +0 -224
  259. package/src/modules/sync/scheduler.ts +0 -121
  260. package/src/modules/sync/sync.ts +0 -1244
  261. package/src/sp00ky.auth-order.test.ts +0 -65
@@ -1,206 +0,0 @@
1
- import { describe, it, expect, vi } from 'vitest';
2
- import { LoroDoc } from 'loro-crdt';
3
- import type { SchemaStructure } from '@spooky-sync/query-builder';
4
- import { CrdtManager } from './index';
5
- import type { LocalDatabaseService, RemoteDatabaseService } from '../../services/database/index';
6
- import type { Logger } from '../../services/logger/index';
7
-
8
- // Regression test for the offline-reload formatting-loss class of bug.
9
- //
10
- // Repro pattern: app uses `store: 'memory'` for the local SurrealDB (or
11
- // any scenario where the local row exists without a CRDT snapshot —
12
- // fresh device, post-sync-down with stale meta, …). On reload, the
13
- // editor mounts but stays empty until some unrelated edit fires the
14
- // parent LIVE feed. From the user's POV, formatting is gone.
15
- //
16
- // Fix: when `CrdtManager.open` finds no local snapshot, it must fetch
17
- // the parent row from remote (the snapshot lives inline on the row in
18
- // the consolidated design) and dispatch its CRDT field. This test pins
19
- // that behavior across both shapes — `@crdt`-only fields where the
20
- // snapshot IS the field value, and `@crdt @cursor` fields where the
21
- // snapshot lives at `<field>.state`.
22
-
23
- function silentLogger(): Logger {
24
- const noop = () => {};
25
- const fake: any = {};
26
- fake.info = noop;
27
- fake.warn = noop;
28
- fake.debug = noop;
29
- fake.error = noop;
30
- fake.trace = noop;
31
- fake.child = () => fake;
32
- return fake as Logger;
33
- }
34
-
35
- function singleTableSchema(opts: { cursor?: boolean } = {}): SchemaStructure {
36
- return {
37
- tables: [
38
- {
39
- name: 'thread',
40
- columns: {
41
- body: {
42
- type: 'string',
43
- optional: false,
44
- crdt: 'text',
45
- ...(opts.cursor ? { cursor: true } : {}),
46
- },
47
- },
48
- primaryKey: ['id'],
49
- },
50
- ],
51
- relationships: [],
52
- backends: {},
53
- };
54
- }
55
-
56
- function buildSnapshot(text: string): Uint8Array {
57
- const doc = new LoroDoc();
58
- doc.getText('text').insert(0, text);
59
- return doc.export({ mode: 'snapshot' });
60
- }
61
-
62
- function makeRemote(handlers: {
63
- selectRow?: () => unknown;
64
- }): RemoteDatabaseService {
65
- const remote = {
66
- query: vi.fn().mockImplementation(async (sql: string) => {
67
- if (sql.includes('LIVE SELECT')) return ['live-uuid'];
68
- if (sql.includes('SELECT * FROM ONLY')) {
69
- return [handlers.selectRow ? handlers.selectRow() : null];
70
- }
71
- return [];
72
- }),
73
- getClient: () =>
74
- ({
75
- liveOf: async () => ({
76
- subscribe: () => () => {},
77
- }),
78
- }) as any,
79
- } satisfies Partial<RemoteDatabaseService> as unknown as RemoteDatabaseService;
80
- return remote;
81
- }
82
-
83
- describe('CrdtManager.open hydration', () => {
84
- it('@crdt-only: fetches the parent row from remote when local is empty', async () => {
85
- const remoteSnapshot = buildSnapshot('hello world');
86
-
87
- const local = {
88
- query: vi.fn().mockImplementation(async (sql: string) => {
89
- if (sql.includes('SELECT VALUE body FROM ONLY')) return [null];
90
- return [];
91
- }),
92
- } satisfies Partial<LocalDatabaseService> as unknown as LocalDatabaseService;
93
-
94
- const remote = makeRemote({
95
- selectRow: () => ({ id: 'thread:abc', body: remoteSnapshot }),
96
- });
97
-
98
- const manager = new CrdtManager(singleTableSchema(), local, remote, silentLogger());
99
- manager.setSessionId('session-under-test');
100
-
101
- const field = await manager.open('thread', 'thread:abc', 'body');
102
-
103
- await vi.waitFor(() => {
104
- expect(field.getDoc().getText('text').toString()).toBe('hello world');
105
- });
106
-
107
- const sqls = (remote.query as ReturnType<typeof vi.fn>).mock.calls.map(
108
- (c) => c[0] as string
109
- );
110
- expect(sqls.some((s) => s.includes('SELECT * FROM ONLY'))).toBe(true);
111
- });
112
-
113
- it('@crdt @cursor: extracts the snapshot from the object shape', async () => {
114
- const remoteSnapshot = buildSnapshot('object shape');
115
-
116
- const local = {
117
- query: vi.fn().mockImplementation(async () => [null]),
118
- } satisfies Partial<LocalDatabaseService> as unknown as LocalDatabaseService;
119
-
120
- // Cursor-enabled field stores `{ state, cursors }`. The remote row
121
- // returns this shape; the manager must drill into `.state` for the
122
- // snapshot bytes.
123
- const remote = makeRemote({
124
- selectRow: () => ({
125
- id: 'thread:abc',
126
- body: { state: remoteSnapshot, cursors: {} },
127
- }),
128
- });
129
-
130
- const manager = new CrdtManager(
131
- singleTableSchema({ cursor: true }),
132
- local,
133
- remote,
134
- silentLogger()
135
- );
136
- manager.setSessionId('session-under-test');
137
-
138
- const field = await manager.open('thread', 'thread:abc', 'body');
139
-
140
- await vi.waitFor(() => {
141
- expect(field.getDoc().getText('text').toString()).toBe('object shape');
142
- });
143
- });
144
-
145
- it('skips the remote fetch when a local snapshot already exists', async () => {
146
- const localSnapshot = buildSnapshot('cached');
147
-
148
- const local = {
149
- query: vi.fn().mockImplementation(async (sql: string) => {
150
- if (sql.includes('SELECT VALUE body FROM ONLY')) return [localSnapshot];
151
- return [];
152
- }),
153
- } satisfies Partial<LocalDatabaseService> as unknown as LocalDatabaseService;
154
-
155
- const remote = makeRemote({});
156
-
157
- const manager = new CrdtManager(singleTableSchema(), local, remote, silentLogger());
158
- manager.setSessionId('session-under-test');
159
-
160
- const field = await manager.open('thread', 'thread:def', 'body');
161
-
162
- expect(field.getDoc().getText('text').toString()).toBe('cached');
163
-
164
- // Give any stray async work a tick to land before asserting absence.
165
- await new Promise((r) => setTimeout(r, 20));
166
-
167
- const sqls = (remote.query as ReturnType<typeof vi.fn>).mock.calls.map(
168
- (c) => c[0] as string
169
- );
170
- expect(sqls.some((s) => s.includes('SELECT * FROM ONLY'))).toBe(false);
171
- });
172
-
173
- // Regression: SurrealDB returns bytes nested inside a FLEXIBLE object as a
174
- // plain `number[]` from the local WASM engine (e.g. `state: [108, 111, 114,
175
- // 111, ...]` — the literal LoroDoc magic header bytes). The cursor-enabled
176
- // extractSnapshot path used to only accept Uint8Array and would silently
177
- // return undefined for the array form, leaving every editor empty and
178
- // killing realtime updates for `@crdt @cursor` fields.
179
- it('@crdt @cursor: accepts number[] as the state shape (local WASM transport)', async () => {
180
- const remoteSnapshot = buildSnapshot('from local wasm');
181
- const remoteAsArray = Array.from(remoteSnapshot) as unknown as number[];
182
-
183
- const local = {
184
- query: vi.fn().mockImplementation(async (sql: string) => {
185
- if (sql.includes('SELECT VALUE body FROM ONLY')) {
186
- return [{ state: remoteAsArray, cursors: {} }];
187
- }
188
- return [];
189
- }),
190
- } satisfies Partial<LocalDatabaseService> as unknown as LocalDatabaseService;
191
-
192
- const remote = makeRemote({});
193
-
194
- const manager = new CrdtManager(
195
- singleTableSchema({ cursor: true }),
196
- local,
197
- remote,
198
- silentLogger()
199
- );
200
- manager.setSessionId('session-under-test');
201
-
202
- const field = await manager.open('thread', 'thread:wasm', 'body');
203
-
204
- expect(field.getDoc().getText('text').toString()).toBe('from local wasm');
205
- });
206
- });
@@ -1,108 +0,0 @@
1
- import { describe, it, expect, beforeEach } from 'vitest';
2
- import { RecordId } from 'surrealdb';
3
- import { DataModule } from './index';
4
- import type { QueryState, QueryStatus } from '../../types';
5
-
6
- /**
7
- * Tests for the per-query fetch status (idle/fetching) added to DataModule:
8
- * setQueryStatus updates the query object and notifies both the DevTools
9
- * observer hook and subscribeStatus listeners.
10
- */
11
-
12
- function makeLogger(): any {
13
- const noop = () => {};
14
- const logger: any = { debug: noop, info: noop, warn: noop, error: noop, trace: noop };
15
- logger.child = () => logger;
16
- return logger;
17
- }
18
-
19
- function makeDataModule(): DataModule<any> {
20
- return new DataModule(
21
- {} as any, // cache — unused by status methods
22
- {} as any, // local — unused by status methods
23
- { tables: [] } as any, // schema — unused by status methods
24
- makeLogger(),
25
- 100
26
- );
27
- }
28
-
29
- function makeQueryState(hash: string): QueryState {
30
- return {
31
- config: {
32
- id: new RecordId('_00_query', hash),
33
- surql: 'SELECT * FROM user',
34
- params: {},
35
- localArray: [],
36
- remoteArray: [],
37
- ttl: '10m',
38
- lastActiveAt: new Date(),
39
- tableName: 'user',
40
- },
41
- records: [],
42
- ttlTimer: null,
43
- ttlDurationMs: 0,
44
- updateCount: 0,
45
- materializationSamples: [],
46
- lastIngestLatencyMs: null,
47
- errorCount: 0,
48
- status: 'idle',
49
- };
50
- }
51
-
52
- describe('DataModule query fetch status', () => {
53
- let dm: DataModule<any>;
54
- const hash = 'h1';
55
-
56
- beforeEach(() => {
57
- dm = makeDataModule();
58
- (dm as any).activeQueries.set(hash, makeQueryState(hash));
59
- });
60
-
61
- it('subscribeStatus with immediate reports the current status', () => {
62
- const seen: QueryStatus[] = [];
63
- dm.subscribeStatus(hash, (s) => seen.push(s), { immediate: true });
64
- expect(seen).toEqual(['idle']);
65
- });
66
-
67
- it('setQueryStatus updates the query object and notifies subscribers + observer', () => {
68
- const observed: Array<[string, QueryStatus]> = [];
69
- dm.onQueryStatusChange = (h, s) => observed.push([h, s]);
70
-
71
- const seen: QueryStatus[] = [];
72
- dm.subscribeStatus(hash, (s) => seen.push(s));
73
-
74
- dm.setQueryStatus(hash, 'fetching');
75
- expect((dm as any).activeQueries.get(hash).status).toBe('fetching');
76
- expect(seen).toEqual(['fetching']);
77
- expect(observed).toEqual([[hash, 'fetching']]);
78
-
79
- dm.setQueryStatus(hash, 'idle');
80
- expect(seen).toEqual(['fetching', 'idle']);
81
- expect(observed).toEqual([[hash, 'fetching'], [hash, 'idle']]);
82
- });
83
-
84
- it('setQueryStatus is a no-op when the status is unchanged', () => {
85
- const seen: QueryStatus[] = [];
86
- dm.subscribeStatus(hash, (s) => seen.push(s));
87
- dm.setQueryStatus(hash, 'idle'); // already idle
88
- expect(seen).toEqual([]);
89
- });
90
-
91
- it('setQueryStatus is a no-op for an unknown query', () => {
92
- let called = false;
93
- dm.onQueryStatusChange = () => {
94
- called = true;
95
- };
96
- dm.setQueryStatus('does-not-exist', 'fetching');
97
- expect(called).toBe(false);
98
- });
99
-
100
- it('unsubscribe stops further status notifications', () => {
101
- const seen: QueryStatus[] = [];
102
- const unsub = dm.subscribeStatus(hash, (s) => seen.push(s));
103
- dm.setQueryStatus(hash, 'fetching');
104
- unsub();
105
- dm.setQueryStatus(hash, 'idle');
106
- expect(seen).toEqual(['fetching']);
107
- });
108
- });
@@ -1,58 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { parseUpdateOptions } from './index';
3
-
4
- describe('parseUpdateOptions', () => {
5
- it('returns empty options when no debounce', () => {
6
- const result = parseUpdateOptions('user:1', { name: 'Alice' });
7
- expect(result).toEqual({});
8
- });
9
-
10
- it('returns empty options when options is undefined', () => {
11
- const result = parseUpdateOptions('user:1', { name: 'Alice' }, undefined);
12
- expect(result).toEqual({});
13
- });
14
-
15
- it('returns empty options when debounced is false', () => {
16
- const result = parseUpdateOptions('user:1', { name: 'Alice' }, { debounced: false });
17
- expect(result).toEqual({});
18
- });
19
-
20
- it('debounced: true uses default delay (200) and key = id', () => {
21
- const result = parseUpdateOptions('user:1', { name: 'Alice' }, { debounced: true });
22
- expect(result).toEqual({
23
- debounced: {
24
- delay: 200,
25
- key: 'user:1',
26
- },
27
- });
28
- });
29
-
30
- it('supports custom delay', () => {
31
- const result = parseUpdateOptions('user:1', { name: 'Alice' }, {
32
- debounced: { delay: 500 },
33
- });
34
- expect(result.debounced?.delay).toBe(500);
35
- });
36
-
37
- it('supports custom key type recordId', () => {
38
- const result = parseUpdateOptions('user:1', { name: 'Alice', age: 30 }, {
39
- debounced: { key: 'recordId' },
40
- });
41
- expect(result.debounced?.key).toBe('user:1');
42
- });
43
-
44
- it('key: recordId_x_fields generates composite key from id + sorted field names', () => {
45
- const result = parseUpdateOptions(
46
- 'user:1',
47
- { name: 'Alice', age: 30, email: 'a@b.com' },
48
- { debounced: { key: 'recordId_x_fields' } }
49
- );
50
- expect(result.debounced?.key).toBe('user:1::age#email#name');
51
- });
52
-
53
- it('uses defaults when debounced is object with no key or delay', () => {
54
- const result = parseUpdateOptions('user:1', { name: 'Alice' }, { debounced: {} });
55
- expect(result.debounced?.delay).toBe(200);
56
- expect(result.debounced?.key).toBe('user:1');
57
- });
58
- });