@spooky-sync/core 0.0.1-canary.11 → 0.0.1-canary.111
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 +56 -0
- package/dist/index.d.ts +1033 -372
- package/dist/index.js +5490 -1331
- package/dist/otel/index.d.ts +21 -0
- package/dist/otel/index.js +86 -0
- package/dist/sqlite-worker.d.ts +1 -0
- package/dist/sqlite-worker.js +107 -0
- package/dist/types.d.ts +721 -0
- package/package.json +40 -9
- package/skills/sp00ky-core/SKILL.md +258 -0
- package/skills/sp00ky-core/references/auth.md +98 -0
- package/skills/sp00ky-core/references/config.md +76 -0
- package/src/build-globals.d.ts +12 -0
- package/src/events/events.test.ts +2 -1
- package/src/events/index.ts +3 -0
- package/src/index.ts +9 -2
- package/src/modules/auth/events/index.ts +2 -1
- package/src/modules/auth/index.ts +59 -20
- package/src/modules/cache/index.ts +77 -32
- package/src/modules/cache/types.ts +2 -2
- package/src/modules/crdt/crdt-field.ts +288 -0
- package/src/modules/crdt/crdt-hydration.test.ts +206 -0
- package/src/modules/crdt/index.ts +357 -0
- package/src/modules/data/data.rebind.test.ts +147 -0
- package/src/modules/data/data.status.test.ts +249 -0
- package/src/modules/data/index.ts +996 -125
- package/src/modules/data/window-query.test.ts +52 -0
- package/src/modules/data/window-query.ts +154 -0
- package/src/modules/devtools/index.ts +180 -28
- package/src/modules/devtools/versions.test.ts +74 -0
- package/src/modules/devtools/versions.ts +81 -0
- package/src/modules/feature-flag/index.test.ts +120 -0
- package/src/modules/feature-flag/index.ts +209 -0
- package/src/modules/ref-tables.test.ts +91 -0
- package/src/modules/ref-tables.ts +88 -0
- package/src/modules/sync/engine.ts +101 -37
- package/src/modules/sync/events/index.ts +9 -2
- package/src/modules/sync/queue/queue-down.ts +12 -5
- package/src/modules/sync/queue/queue-up.ts +29 -14
- package/src/modules/sync/scheduler.pause.test.ts +109 -0
- package/src/modules/sync/scheduler.ts +73 -7
- package/src/modules/sync/sync.health.test.ts +149 -0
- package/src/modules/sync/sync.ts +1017 -62
- package/src/modules/sync/utils.test.ts +269 -2
- package/src/modules/sync/utils.ts +182 -17
- package/src/otel/index.ts +127 -0
- package/src/services/database/cache-engine.ts +143 -0
- package/src/services/database/database.ts +11 -11
- package/src/services/database/engine-factory.ts +32 -0
- package/src/services/database/events/index.ts +2 -1
- package/src/services/database/index.ts +6 -0
- package/src/services/database/local-migrator.ts +27 -27
- package/src/services/database/local.test.ts +64 -0
- package/src/services/database/local.ts +452 -66
- package/src/services/database/plan-render.test.ts +85 -0
- package/src/services/database/plan-render.ts +86 -0
- package/src/services/database/relation-resolver.test.ts +413 -0
- package/src/services/database/relation-resolver.ts +0 -0
- package/src/services/database/remote.ts +13 -13
- package/src/services/database/sqlite-cache-engine.test.ts +85 -0
- package/src/services/database/sqlite-cache-engine.ts +693 -0
- package/src/services/database/sqlite-worker.ts +116 -0
- package/src/services/database/surql-translate.ts +291 -0
- package/src/services/database/surreal-cache-engine.ts +122 -0
- package/src/services/logger/index.ts +6 -101
- package/src/services/persistence/localstorage.ts +2 -2
- package/src/services/persistence/resilient.ts +41 -0
- package/src/services/persistence/surrealdb.ts +10 -10
- package/src/services/stream-processor/index.ts +295 -38
- package/src/services/stream-processor/permissions.test.ts +47 -0
- package/src/services/stream-processor/permissions.ts +53 -0
- package/src/services/stream-processor/stream-processor.batch.test.ts +136 -0
- package/src/services/stream-processor/stream-processor.reset.test.ts +104 -0
- package/src/services/stream-processor/stream-processor.test.ts +1 -1
- package/src/services/stream-processor/wasm-types.ts +18 -2
- package/src/sp00ky.auth-order.test.ts +92 -0
- package/src/sp00ky.ts +795 -0
- package/src/types.ts +231 -15
- package/src/utils/error-classification.test.ts +44 -0
- package/src/utils/error-classification.ts +7 -0
- package/src/utils/index.ts +35 -13
- package/src/utils/parser.ts +3 -2
- package/src/utils/surql.ts +24 -15
- package/src/utils/withRetry.test.ts +1 -1
- package/tsdown.config.ts +64 -1
- package/src/spooky.ts +0 -392
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi, afterEach } 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; beginFetching/endFetching
|
|
10
|
+
* refcount overlapping fetch cycles; flushPendingStreamUpdate lands the
|
|
11
|
+
* debounced result before a query flips back to idle.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
function makeLogger(): any {
|
|
15
|
+
const noop = () => {};
|
|
16
|
+
const logger: any = { debug: noop, info: noop, warn: noop, error: noop, trace: noop };
|
|
17
|
+
logger.child = () => logger;
|
|
18
|
+
return logger;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function makeDataModule(): DataModule<any> {
|
|
22
|
+
return new DataModule(
|
|
23
|
+
{} as any, // cache — unused by status methods
|
|
24
|
+
{} as any, // local — unused by status methods
|
|
25
|
+
{ tables: [] } as any, // schema — unused by status methods
|
|
26
|
+
makeLogger(),
|
|
27
|
+
100
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function makeQueryState(hash: string): QueryState {
|
|
32
|
+
return {
|
|
33
|
+
config: {
|
|
34
|
+
id: new RecordId('_00_query', hash),
|
|
35
|
+
surql: 'SELECT * FROM user',
|
|
36
|
+
params: {},
|
|
37
|
+
localArray: [],
|
|
38
|
+
remoteArray: [],
|
|
39
|
+
ttl: '10m',
|
|
40
|
+
lastActiveAt: new Date(),
|
|
41
|
+
tableName: 'user',
|
|
42
|
+
},
|
|
43
|
+
records: [],
|
|
44
|
+
ttlTimer: null,
|
|
45
|
+
ttlDurationMs: 0,
|
|
46
|
+
updateCount: 0,
|
|
47
|
+
lastUpdatedAt: null,
|
|
48
|
+
materializationSamples: [],
|
|
49
|
+
lastIngestLatencyMs: null,
|
|
50
|
+
errorCount: 0,
|
|
51
|
+
status: 'idle',
|
|
52
|
+
phaseSamples: {},
|
|
53
|
+
phaseLast: {},
|
|
54
|
+
registrationTimings: { parseMs: null, planMs: null, snapshotMs: null, wallMs: null },
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
describe('DataModule query fetch status', () => {
|
|
59
|
+
let dm: DataModule<any>;
|
|
60
|
+
const hash = 'h1';
|
|
61
|
+
|
|
62
|
+
beforeEach(() => {
|
|
63
|
+
dm = makeDataModule();
|
|
64
|
+
(dm as any).activeQueries.set(hash, makeQueryState(hash));
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('subscribeStatus with immediate reports the current status', () => {
|
|
68
|
+
const seen: QueryStatus[] = [];
|
|
69
|
+
dm.subscribeStatus(hash, (s) => seen.push(s), { immediate: true });
|
|
70
|
+
expect(seen).toEqual(['idle']);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('setQueryStatus updates the query object and notifies subscribers + observer', () => {
|
|
74
|
+
const observed: Array<[string, QueryStatus]> = [];
|
|
75
|
+
dm.onQueryStatusChange = (h, s) => observed.push([h, s]);
|
|
76
|
+
|
|
77
|
+
const seen: QueryStatus[] = [];
|
|
78
|
+
dm.subscribeStatus(hash, (s) => seen.push(s));
|
|
79
|
+
|
|
80
|
+
dm.setQueryStatus(hash, 'fetching');
|
|
81
|
+
expect((dm as any).activeQueries.get(hash).status).toBe('fetching');
|
|
82
|
+
expect(seen).toEqual(['fetching']);
|
|
83
|
+
expect(observed).toEqual([[hash, 'fetching']]);
|
|
84
|
+
|
|
85
|
+
dm.setQueryStatus(hash, 'idle');
|
|
86
|
+
expect(seen).toEqual(['fetching', 'idle']);
|
|
87
|
+
expect(observed).toEqual([[hash, 'fetching'], [hash, 'idle']]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('setQueryStatus is a no-op when the status is unchanged', () => {
|
|
91
|
+
const seen: QueryStatus[] = [];
|
|
92
|
+
dm.subscribeStatus(hash, (s) => seen.push(s));
|
|
93
|
+
dm.setQueryStatus(hash, 'idle'); // already idle
|
|
94
|
+
expect(seen).toEqual([]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('setQueryStatus is a no-op for an unknown query', () => {
|
|
98
|
+
let called = false;
|
|
99
|
+
dm.onQueryStatusChange = () => {
|
|
100
|
+
called = true;
|
|
101
|
+
};
|
|
102
|
+
dm.setQueryStatus('does-not-exist', 'fetching');
|
|
103
|
+
expect(called).toBe(false);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('unsubscribe stops further status notifications', () => {
|
|
107
|
+
const seen: QueryStatus[] = [];
|
|
108
|
+
const unsub = dm.subscribeStatus(hash, (s) => seen.push(s));
|
|
109
|
+
dm.setQueryStatus(hash, 'fetching');
|
|
110
|
+
unsub();
|
|
111
|
+
dm.setQueryStatus(hash, 'idle');
|
|
112
|
+
expect(seen).toEqual(['fetching']);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('DataModule beginFetching/endFetching refcount', () => {
|
|
117
|
+
let dm: DataModule<any>;
|
|
118
|
+
const hash = 'h1';
|
|
119
|
+
let seen: QueryStatus[];
|
|
120
|
+
|
|
121
|
+
beforeEach(() => {
|
|
122
|
+
dm = makeDataModule();
|
|
123
|
+
(dm as any).activeQueries.set(hash, makeQueryState(hash));
|
|
124
|
+
seen = [];
|
|
125
|
+
dm.subscribeStatus(hash, (s) => seen.push(s));
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('emits fetching on 0→1 and idle only on the last exit', () => {
|
|
129
|
+
dm.beginFetching(hash); // registration
|
|
130
|
+
dm.beginFetching(hash); // overlapping poll round
|
|
131
|
+
expect(seen).toEqual(['fetching']);
|
|
132
|
+
|
|
133
|
+
dm.endFetching(hash); // inner cycle finishes — must NOT emit idle
|
|
134
|
+
expect(seen).toEqual(['fetching']);
|
|
135
|
+
expect((dm as any).activeQueries.get(hash).status).toBe('fetching');
|
|
136
|
+
|
|
137
|
+
dm.endFetching(hash);
|
|
138
|
+
expect(seen).toEqual(['fetching', 'idle']);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('unbalanced endFetching settles to idle without going negative', () => {
|
|
142
|
+
dm.endFetching(hash); // already idle → no-op notification
|
|
143
|
+
expect(seen).toEqual([]);
|
|
144
|
+
dm.beginFetching(hash);
|
|
145
|
+
dm.endFetching(hash);
|
|
146
|
+
expect(seen).toEqual(['fetching', 'idle']);
|
|
147
|
+
expect((dm as any).fetchDepth.has(hash)).toBe(false);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
describe('DataModule pending stream-update flush', () => {
|
|
152
|
+
let dm: DataModule<any>;
|
|
153
|
+
const hash = 'h1';
|
|
154
|
+
let processed: any[];
|
|
155
|
+
|
|
156
|
+
const makeUpdate = (op: 'CREATE' | 'UPDATE' | 'DELETE') =>
|
|
157
|
+
({ queryHash: hash, op, localArray: [] }) as any;
|
|
158
|
+
|
|
159
|
+
beforeEach(() => {
|
|
160
|
+
vi.useFakeTimers();
|
|
161
|
+
dm = makeDataModule();
|
|
162
|
+
(dm as any).activeQueries.set(hash, makeQueryState(hash));
|
|
163
|
+
processed = [];
|
|
164
|
+
(dm as any).processStreamUpdate = async (u: any) => {
|
|
165
|
+
processed.push(u);
|
|
166
|
+
};
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
afterEach(() => {
|
|
170
|
+
vi.useRealTimers();
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('flushPendingStreamUpdate processes the debounced update exactly once', async () => {
|
|
174
|
+
const update = makeUpdate('CREATE');
|
|
175
|
+
await dm.onStreamUpdate(update);
|
|
176
|
+
expect(processed).toEqual([]); // still debounced
|
|
177
|
+
|
|
178
|
+
await dm.flushPendingStreamUpdate(hash);
|
|
179
|
+
expect(processed).toEqual([update]);
|
|
180
|
+
expect((dm as any).debounceTimers.has(hash)).toBe(false);
|
|
181
|
+
expect((dm as any).pendingStreamUpdates.has(hash)).toBe(false);
|
|
182
|
+
|
|
183
|
+
// The cancelled timer must not process it a second time.
|
|
184
|
+
await vi.runAllTimersAsync();
|
|
185
|
+
expect(processed).toEqual([update]);
|
|
186
|
+
|
|
187
|
+
// Nothing pending → flush is a no-op.
|
|
188
|
+
await dm.flushPendingStreamUpdate(hash);
|
|
189
|
+
expect(processed).toEqual([update]);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('the trailing-edge timer clears the pending entry itself', async () => {
|
|
193
|
+
const update = makeUpdate('UPDATE');
|
|
194
|
+
await dm.onStreamUpdate(update);
|
|
195
|
+
await vi.runAllTimersAsync();
|
|
196
|
+
expect(processed).toEqual([update]);
|
|
197
|
+
expect((dm as any).pendingStreamUpdates.has(hash)).toBe(false);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('a DELETE supersedes and drops the pending coalesced update', async () => {
|
|
201
|
+
const create = makeUpdate('CREATE');
|
|
202
|
+
const del = makeUpdate('DELETE');
|
|
203
|
+
await dm.onStreamUpdate(create);
|
|
204
|
+
await dm.onStreamUpdate(del); // immediate, drops the pending CREATE
|
|
205
|
+
expect(processed).toEqual([del]);
|
|
206
|
+
expect((dm as any).pendingStreamUpdates.has(hash)).toBe(false);
|
|
207
|
+
await vi.runAllTimersAsync();
|
|
208
|
+
expect(processed).toEqual([del]);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('finalizeDeregister clears pending update and fetch depth', async () => {
|
|
212
|
+
(dm as any).cache = { unregisterQuery: () => {} };
|
|
213
|
+
await dm.onStreamUpdate(makeUpdate('CREATE'));
|
|
214
|
+
dm.beginFetching(hash);
|
|
215
|
+
dm.finalizeDeregister(hash);
|
|
216
|
+
expect((dm as any).pendingStreamUpdates.has(hash)).toBe(false);
|
|
217
|
+
expect((dm as any).debounceTimers.has(hash)).toBe(false);
|
|
218
|
+
expect((dm as any).fetchDepth.has(hash)).toBe(false);
|
|
219
|
+
await vi.runAllTimersAsync();
|
|
220
|
+
expect(processed).toEqual([]);
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
describe('DataModule notifyQuerySynced', () => {
|
|
225
|
+
const hash = 'h1';
|
|
226
|
+
|
|
227
|
+
function makeDmWithLocal(rows: any[]): DataModule<any> {
|
|
228
|
+
const local: any = { query: async () => [rows] };
|
|
229
|
+
return new DataModule({} as any, local, { tables: [] } as any, makeLogger(), 100);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
it('notifies once per registration lifetime even when records are unchanged and updateCount > 0', async () => {
|
|
233
|
+
const dm = makeDmWithLocal([]);
|
|
234
|
+
const state = makeQueryState(hash);
|
|
235
|
+
state.updateCount = 7; // persisted from a previous registration
|
|
236
|
+
(dm as any).activeQueries.set(hash, state);
|
|
237
|
+
|
|
238
|
+
let notified = 0;
|
|
239
|
+
dm.subscribe(hash, () => {
|
|
240
|
+
notified++;
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
await dm.notifyQuerySynced(hash); // empty result, unchanged — must still emit
|
|
244
|
+
expect(notified).toBe(1);
|
|
245
|
+
|
|
246
|
+
await dm.notifyQuerySynced(hash); // already notified this lifetime → silent
|
|
247
|
+
expect(notified).toBe(1);
|
|
248
|
+
});
|
|
249
|
+
});
|