@spooky-sync/core 0.0.1-canary.98 → 1.0.0-beta.2
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 +57 -21
- package/README.md +59 -16
- package/dist/index.d.ts +2096 -929
- package/dist/index.js +18217 -5019
- 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/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 +1968 -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 +34 -8
- package/scripts/check-broker-bundle.mjs +33 -0
- package/scripts/check-worker-entries.mjs +173 -0
- package/skills/sp00ky-core/SKILL.md +42 -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 +352 -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 +437 -0
- package/src/client/sp00ky-client.test.ts +428 -0
- package/src/client/sp00ky-client.ts +737 -0
- package/src/index.ts +78 -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 +171 -0
- package/src/kernel/events.ts +60 -0
- package/src/kernel/interpreter.test.ts +116 -0
- package/src/kernel/interpreter.ts +148 -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/index.ts +1153 -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 +168 -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 +200 -0
- package/src/query/materialize.saga.ts +102 -0
- package/src/query/materialize.test.ts +58 -0
- package/src/query/materialize.ts +57 -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 +369 -0
- package/src/query/register.saga.ts +293 -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 +209 -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 +33 -0
- package/src/services/database/errors.ts +34 -0
- package/src/services/database/index.ts +7 -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/sqlite-cache-engine.test.ts +628 -0
- package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
- package/src/services/database/sqlite-cache-engine.ts +1401 -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 +229 -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 +36 -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 +568 -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 +64 -4
- package/vitest.config.ts +48 -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
|
@@ -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
|
-
});
|