@spooky-sync/core 0.0.1-canary.240 → 0.0.1-canary.241
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 +34 -15
- package/README.md +56 -16
- package/dist/index.d.ts +1136 -1846
- package/dist/index.js +10168 -11267
- package/dist/types.d.ts +22 -79
- package/package.json +8 -4
- package/skills/sp00ky-core/SKILL.md +18 -0
- package/src/boot/auth-flip.saga.test.ts +82 -0
- package/src/boot/auth-flip.saga.ts +62 -0
- package/src/boot/boot.saga.test.ts +151 -0
- package/src/boot/boot.saga.ts +128 -0
- package/src/boot/bucket-switch.saga.test.ts +158 -0
- package/src/boot/bucket-switch.saga.ts +115 -0
- package/src/boot/preload.saga.test.ts +51 -0
- package/src/boot/preload.saga.ts +31 -0
- package/src/client/bucket-handle.test.ts +117 -0
- package/src/client/bucket-handle.ts +227 -0
- package/src/client/router.test.ts +56 -0
- package/src/client/router.ts +94 -0
- package/src/client/runtime.test.ts +192 -0
- package/src/client/runtime.ts +296 -0
- package/src/client/services.ts +349 -0
- package/src/client/sp00ky-client.test.ts +241 -0
- package/src/client/sp00ky-client.ts +482 -0
- package/src/index.ts +1 -0
- package/src/kernel/constants.test.ts +19 -0
- package/src/kernel/constants.ts +54 -0
- package/src/kernel/effects.test.ts +40 -0
- package/src/kernel/effects.ts +159 -0
- package/src/kernel/events.ts +55 -0
- package/src/kernel/interpreter.test.ts +116 -0
- package/src/kernel/interpreter.ts +143 -0
- package/src/kernel/saga.test.ts +89 -0
- package/src/kernel/saga.ts +73 -0
- package/src/modules/app-release/index.ts +13 -5
- package/src/modules/devtools/index.ts +10 -3
- package/src/modules/devtools/state-shape.test.ts +4 -1
- package/src/modules/feature-flag/index.ts +13 -5
- package/src/mutation/jobs.test.ts +25 -0
- package/src/mutation/jobs.ts +41 -0
- package/src/{modules/data → mutation}/mutation-id.test.ts +12 -0
- package/src/mutation/push.saga.test.ts +285 -0
- package/src/mutation/push.saga.ts +215 -0
- package/src/mutation/rows.test.ts +201 -0
- package/src/mutation/rows.ts +377 -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 +163 -0
- package/src/mutation/write.saga.ts +166 -0
- package/src/query/env.test.ts +23 -0
- package/src/query/env.ts +56 -0
- package/src/query/fetch.saga.test.ts +156 -0
- package/src/query/fetch.saga.ts +109 -0
- package/src/query/hash.test.ts +14 -0
- package/src/query/hash.ts +18 -0
- package/src/{modules/data → query}/id-set-plan.test.ts +12 -2
- package/src/query/lifecycle.saga.test.ts +122 -0
- package/src/query/lifecycle.saga.ts +101 -0
- package/src/query/materialize.saga.test.ts +82 -0
- package/src/query/materialize.saga.ts +75 -0
- package/src/query/materialize.test.ts +50 -0
- package/src/query/materialize.ts +44 -0
- package/src/query/membership.saga.test.ts +246 -0
- package/src/query/membership.saga.ts +208 -0
- package/src/query/membership.test.ts +118 -0
- package/src/query/membership.ts +158 -0
- package/src/query/register.saga.test.ts +251 -0
- package/src/query/register.saga.ts +190 -0
- package/src/query/render-set.test.ts +36 -0
- package/src/query/render-set.ts +60 -0
- package/src/query/sql.test.ts +62 -0
- package/src/query/sql.ts +79 -0
- package/src/{modules/data → query}/window-query.test.ts +18 -0
- package/src/{modules/data → query}/window-query.ts +2 -1
- package/src/services/database/connection-supervisor.test.ts +29 -0
- package/src/services/database/connection-supervisor.ts +7 -0
- package/src/services/database/database.responses.test.ts +43 -0
- package/src/services/database/database.ts +25 -10
- package/src/services/database/local-migrator.ts +2 -2
- package/src/services/database/plan-render.test.ts +1 -1
- package/src/services/database/sqlite-cache-engine.test.ts +4 -3
- package/src/services/database/sqlite-cache-engine.ts +2 -11
- package/src/services/database/sqlite-devtools-queries.integration.test.ts +1 -1
- package/src/services/tabs/protocol.ts +7 -3
- package/src/sp00ky.ts +5 -1854
- package/src/state/client-state.ts +184 -0
- package/src/state/lifecycle.test.ts +118 -0
- package/src/state/lifecycle.ts +86 -0
- package/src/state/reducers.test.ts +280 -0
- package/src/state/reducers.ts +476 -0
- package/src/state/selectors.test.ts +149 -0
- package/src/state/selectors.ts +184 -0
- package/src/sync/connection.saga.test.ts +86 -0
- package/src/sync/connection.saga.ts +85 -0
- package/src/sync/live.saga.test.ts +69 -0
- package/src/sync/live.saga.ts +49 -0
- package/src/sync/policy.health.test.ts +56 -0
- package/src/{modules/sync/utils.test.ts → sync/policy.test.ts} +31 -3
- package/src/{modules/sync/utils.ts → sync/policy.ts} +67 -5
- package/src/sync/poll.saga.test.ts +51 -0
- package/src/sync/poll.saga.ts +36 -0
- package/src/sync/tabs.saga.test.ts +74 -0
- package/src/sync/tabs.saga.ts +92 -0
- package/src/testing/adapters.ts +133 -0
- package/src/testing/build.ts +61 -0
- package/src/testing/fake-services.ts +34 -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 +8 -7
- package/src/utils/utils.test.ts +2 -1
- package/vitest.config.ts +48 -0
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -87
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +0 -476
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -210
- package/coverage/lcov-report/src/boot/auth-flip.saga.ts.html +0 -271
- package/coverage/lcov-report/src/boot/boot.saga.ts.html +0 -469
- package/coverage/lcov-report/src/boot/bucket-switch.saga.ts.html +0 -430
- package/coverage/lcov-report/src/boot/index.html +0 -161
- package/coverage/lcov-report/src/boot/preload.saga.ts.html +0 -178
- package/coverage/lcov-report/src/client/bucket-handle.ts.html +0 -766
- package/coverage/lcov-report/src/client/index.html +0 -161
- package/coverage/lcov-report/src/client/router.ts.html +0 -367
- package/coverage/lcov-report/src/client/runtime.ts.html +0 -940
- package/coverage/lcov-report/src/client/sp00ky-client.ts.html +0 -1531
- package/coverage/lcov-report/src/events/index.html +0 -116
- package/coverage/lcov-report/src/events/index.ts.html +0 -877
- package/coverage/lcov-report/src/index.html +0 -146
- package/coverage/lcov-report/src/index.ts.html +0 -202
- package/coverage/lcov-report/src/kernel/constants.ts.html +0 -247
- package/coverage/lcov-report/src/kernel/effects.ts.html +0 -562
- package/coverage/lcov-report/src/kernel/events.ts.html +0 -250
- package/coverage/lcov-report/src/kernel/index.html +0 -176
- package/coverage/lcov-report/src/kernel/interpreter.ts.html +0 -514
- package/coverage/lcov-report/src/kernel/saga.ts.html +0 -304
- package/coverage/lcov-report/src/modules/app-release/index.html +0 -116
- package/coverage/lcov-report/src/modules/app-release/index.ts.html +0 -712
- package/coverage/lcov-report/src/modules/auth/events/index.html +0 -116
- package/coverage/lcov-report/src/modules/auth/events/index.ts.html +0 -142
- package/coverage/lcov-report/src/modules/auth/index.html +0 -116
- package/coverage/lcov-report/src/modules/auth/index.ts.html +0 -1225
- package/coverage/lcov-report/src/modules/crdt/crdt-field.ts.html +0 -967
- package/coverage/lcov-report/src/modules/crdt/index.html +0 -146
- package/coverage/lcov-report/src/modules/crdt/index.ts.html +0 -1474
- package/coverage/lcov-report/src/modules/crdt/loro-loader.ts.html +0 -160
- package/coverage/lcov-report/src/modules/devtools/flags.ts.html +0 -1132
- package/coverage/lcov-report/src/modules/devtools/index.html +0 -161
- package/coverage/lcov-report/src/modules/devtools/index.ts.html +0 -2413
- package/coverage/lcov-report/src/modules/devtools/storage-info.ts.html +0 -589
- package/coverage/lcov-report/src/modules/devtools/versions.ts.html +0 -415
- package/coverage/lcov-report/src/modules/feature-flag/index.html +0 -116
- package/coverage/lcov-report/src/modules/feature-flag/index.ts.html +0 -1033
- package/coverage/lcov-report/src/modules/index.html +0 -116
- package/coverage/lcov-report/src/modules/ref-tables.ts.html +0 -349
- package/coverage/lcov-report/src/mutation/index.html +0 -191
- package/coverage/lcov-report/src/mutation/jobs.ts.html +0 -208
- package/coverage/lcov-report/src/mutation/mutation-id.ts.html +0 -190
- package/coverage/lcov-report/src/mutation/push.saga.ts.html +0 -730
- package/coverage/lcov-report/src/mutation/rows.ts.html +0 -1216
- package/coverage/lcov-report/src/mutation/tray.saga.ts.html +0 -229
- package/coverage/lcov-report/src/mutation/write.saga.ts.html +0 -583
- package/coverage/lcov-report/src/otel/index.html +0 -116
- package/coverage/lcov-report/src/otel/index.ts.html +0 -466
- package/coverage/lcov-report/src/query/env.ts.html +0 -253
- package/coverage/lcov-report/src/query/fetch.saga.ts.html +0 -412
- package/coverage/lcov-report/src/query/hash.ts.html +0 -139
- package/coverage/lcov-report/src/query/index.html +0 -281
- package/coverage/lcov-report/src/query/lifecycle.saga.ts.html +0 -388
- package/coverage/lcov-report/src/query/materialize.saga.ts.html +0 -310
- package/coverage/lcov-report/src/query/materialize.ts.html +0 -217
- package/coverage/lcov-report/src/query/membership.saga.ts.html +0 -709
- package/coverage/lcov-report/src/query/membership.ts.html +0 -559
- package/coverage/lcov-report/src/query/register.saga.ts.html +0 -655
- package/coverage/lcov-report/src/query/render-set.ts.html +0 -265
- package/coverage/lcov-report/src/query/sql.ts.html +0 -322
- package/coverage/lcov-report/src/query/window-query.ts.html +0 -670
- package/coverage/lcov-report/src/services/blobs/blob-cache.ts.html +0 -1894
- package/coverage/lcov-report/src/services/blobs/blob-manifest.ts.html +0 -766
- package/coverage/lcov-report/src/services/blobs/blob-store.ts.html +0 -1162
- package/coverage/lcov-report/src/services/blobs/index.html +0 -161
- package/coverage/lcov-report/src/services/blobs/index.ts.html +0 -295
- package/coverage/lcov-report/src/services/database/cache-engine.ts.html +0 -664
- package/coverage/lcov-report/src/services/database/connection-supervisor.ts.html +0 -1330
- package/coverage/lcov-report/src/services/database/database.ts.html +0 -658
- package/coverage/lcov-report/src/services/database/engine-factory.ts.html +0 -184
- package/coverage/lcov-report/src/services/database/errors.ts.html +0 -187
- package/coverage/lcov-report/src/services/database/events/index.html +0 -116
- package/coverage/lcov-report/src/services/database/events/index.ts.html +0 -184
- package/coverage/lcov-report/src/services/database/index.html +0 -386
- package/coverage/lcov-report/src/services/database/index.ts.html +0 -121
- package/coverage/lcov-report/src/services/database/local-migrator.ts.html +0 -703
- package/coverage/lcov-report/src/services/database/local.ts.html +0 -1633
- package/coverage/lcov-report/src/services/database/plan-render.ts.html +0 -409
- package/coverage/lcov-report/src/services/database/relation-resolver.ts.html +0 -682
- package/coverage/lcov-report/src/services/database/remote.ts.html +0 -1036
- package/coverage/lcov-report/src/services/database/sqlite-cache-engine.ts.html +0 -4132
- package/coverage/lcov-report/src/services/database/sqlite-lock-verify.ts.html +0 -220
- package/coverage/lcov-report/src/services/database/sqlite-open.ts.html +0 -577
- package/coverage/lcov-report/src/services/database/sqlite-plan-sql.ts.html +0 -499
- package/coverage/lcov-report/src/services/database/sqlite-select.ts.html +0 -478
- package/coverage/lcov-report/src/services/database/sqlite-transport.ts.html +0 -757
- package/coverage/lcov-report/src/services/database/surql-translate.ts.html +0 -1333
- package/coverage/lcov-report/src/services/database/surreal-cache-engine.ts.html +0 -568
- package/coverage/lcov-report/src/services/logger/index.html +0 -116
- package/coverage/lcov-report/src/services/logger/index.ts.html +0 -154
- package/coverage/lcov-report/src/services/persistence/index.html +0 -146
- package/coverage/lcov-report/src/services/persistence/localstorage.ts.html +0 -163
- package/coverage/lcov-report/src/services/persistence/resilient.ts.html +0 -208
- package/coverage/lcov-report/src/services/persistence/surrealdb.ts.html +0 -271
- package/coverage/lcov-report/src/services/stream-processor/index.html +0 -146
- package/coverage/lcov-report/src/services/stream-processor/index.ts.html +0 -3313
- package/coverage/lcov-report/src/services/stream-processor/permissions.ts.html +0 -244
- package/coverage/lcov-report/src/services/stream-processor/wasm-types.ts.html +0 -346
- package/coverage/lcov-report/src/services/tabs/broker-client.ts.html +0 -934
- package/coverage/lcov-report/src/services/tabs/coordinator.ts.html +0 -1984
- package/coverage/lcov-report/src/services/tabs/index.html +0 -191
- package/coverage/lcov-report/src/services/tabs/leader-locks.ts.html +0 -310
- package/coverage/lcov-report/src/services/tabs/protocol.ts.html +0 -871
- package/coverage/lcov-report/src/services/tabs/support.ts.html +0 -193
- package/coverage/lcov-report/src/services/tabs/tabs-broker-worker.ts.html +0 -2005
- package/coverage/lcov-report/src/sp00ky.ts.html +0 -100
- package/coverage/lcov-report/src/state/client-state.ts.html +0 -637
- package/coverage/lcov-report/src/state/index.html +0 -161
- package/coverage/lcov-report/src/state/lifecycle.ts.html +0 -343
- package/coverage/lcov-report/src/state/reducers.ts.html +0 -1471
- package/coverage/lcov-report/src/state/selectors.ts.html +0 -637
- package/coverage/lcov-report/src/sync/connection.saga.ts.html +0 -343
- package/coverage/lcov-report/src/sync/index.html +0 -176
- package/coverage/lcov-report/src/sync/live.saga.ts.html +0 -232
- package/coverage/lcov-report/src/sync/policy.ts.html +0 -1606
- package/coverage/lcov-report/src/sync/poll.saga.ts.html +0 -193
- package/coverage/lcov-report/src/sync/tabs.saga.ts.html +0 -361
- package/coverage/lcov-report/src/types.ts.html +0 -2419
- package/coverage/lcov-report/src/utils/blurhash.ts.html +0 -355
- package/coverage/lcov-report/src/utils/error-classification.ts.html +0 -244
- package/coverage/lcov-report/src/utils/index.html +0 -191
- package/coverage/lcov-report/src/utils/index.ts.html +0 -820
- package/coverage/lcov-report/src/utils/parser.ts.html +0 -313
- package/coverage/lcov-report/src/utils/semver.ts.html +0 -175
- package/coverage/lcov-report/src/utils/surql.ts.html +0 -667
- package/coverage/lcov.info +0 -19079
- package/src/modules/cache/cache.relay.test.ts +0 -95
- package/src/modules/cache/index.ts +0 -361
- package/src/modules/cache/types.ts +0 -19
- package/src/modules/data/data.activity.test.ts +0 -109
- package/src/modules/data/data.authority.test.ts +0 -122
- package/src/modules/data/data.heartbeat-wake.test.ts +0 -115
- package/src/modules/data/data.hydration.test.ts +0 -142
- package/src/modules/data/data.membership.test.ts +0 -623
- package/src/modules/data/data.notify-table.test.ts +0 -41
- package/src/modules/data/data.pending-ids.test.ts +0 -199
- package/src/modules/data/data.rebind.test.ts +0 -170
- package/src/modules/data/data.rematerialize.test.ts +0 -114
- package/src/modules/data/data.run.test.ts +0 -113
- package/src/modules/data/data.settled-writes.test.ts +0 -235
- package/src/modules/data/data.status.test.ts +0 -249
- package/src/modules/data/data.test.ts +0 -58
- package/src/modules/data/index.ts +0 -2745
- package/src/modules/sync/engine.ts +0 -250
- 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.test.ts +0 -228
- package/src/modules/sync/queue/queue-down.ts +0 -184
- package/src/modules/sync/queue/queue-up.forwarded.test.ts +0 -164
- package/src/modules/sync/queue/queue-up.ts +0 -407
- package/src/modules/sync/scheduler.pause.test.ts +0 -109
- package/src/modules/sync/scheduler.retry.test.ts +0 -237
- package/src/modules/sync/scheduler.ts +0 -286
- package/src/modules/sync/sync.cleanup.test.ts +0 -116
- package/src/modules/sync/sync.health.test.ts +0 -167
- package/src/modules/sync/sync.heartbeat.test.ts +0 -80
- package/src/modules/sync/sync.list-ref-meta.test.ts +0 -193
- package/src/modules/sync/sync.live-removal.test.ts +0 -289
- package/src/modules/sync/sync.reconnect.test.ts +0 -145
- package/src/modules/sync/sync.rematerialize-after-fetch.test.ts +0 -93
- package/src/modules/sync/sync.settled-poll.test.ts +0 -58
- package/src/modules/sync/sync.subquery.test.ts +0 -82
- package/src/modules/sync/sync.tabs.test.ts +0 -249
- package/src/modules/sync/sync.ts +0 -2626
- package/src/sp00ky.auth-order.test.ts +0 -112
- package/src/sp00ky.init-query.test.ts +0 -183
- package/src/sp00ky.local-first.test.ts +0 -60
- /package/src/{modules/data → mutation}/mutation-id.ts +0 -0
|
@@ -13,7 +13,7 @@ export interface DevToolsEvent {
|
|
|
13
13
|
payload: any;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
import type {
|
|
16
|
+
import type { QueryState, QueryTimings } from '../../types';
|
|
17
17
|
import type { AuthService } from '../auth/index';
|
|
18
18
|
import { AuthEventTypes } from '../auth/events/index';
|
|
19
19
|
import {
|
|
@@ -37,6 +37,13 @@ const WASM_VERSION =
|
|
|
37
37
|
const SURREAL_VERSION =
|
|
38
38
|
typeof __SP00KY_SURREAL_VERSION__ !== 'undefined' ? __SP00KY_SURREAL_VERSION__ : 'unknown';
|
|
39
39
|
|
|
40
|
+
/** What DevTools reads about live queries; the runtime derives it from state. */
|
|
41
|
+
export interface DevToolsQuerySource {
|
|
42
|
+
getActiveQueries(): QueryState[];
|
|
43
|
+
getQueryById(id: RecordId<string>): QueryState | undefined;
|
|
44
|
+
phaseTimings(q: QueryState): QueryTimings;
|
|
45
|
+
}
|
|
46
|
+
|
|
40
47
|
export class DevToolsService implements StreamUpdateReceiver {
|
|
41
48
|
private eventsHistory: DevToolsEvent[] = [];
|
|
42
49
|
private eventIdCounter = 0;
|
|
@@ -102,7 +109,7 @@ export class DevToolsService implements StreamUpdateReceiver {
|
|
|
102
109
|
private logger: Logger,
|
|
103
110
|
private schema: SchemaStructure,
|
|
104
111
|
private authService: AuthService<SchemaStructure>,
|
|
105
|
-
private dataManager?:
|
|
112
|
+
private dataManager?: DevToolsQuerySource
|
|
106
113
|
) {
|
|
107
114
|
this.flagsAdmin = new FlagsAdminService({
|
|
108
115
|
remote: this.remoteDatabaseService,
|
|
@@ -235,7 +242,7 @@ export class DevToolsService implements StreamUpdateReceiver {
|
|
|
235
242
|
// Detailed per-phase processing-time breakdown (SSP sub-phases, local/
|
|
236
243
|
// remote record fetch, frontend reconcile, registration). Flows to both
|
|
237
244
|
// the DevTools panel and the MCP (which returns activeQueries verbatim).
|
|
238
|
-
timings: this.dataManager
|
|
245
|
+
timings: this.dataManager!.phaseTimings(q),
|
|
239
246
|
});
|
|
240
247
|
});
|
|
241
248
|
return result;
|
|
@@ -108,7 +108,10 @@ describe('DevTools pushed state shape', () => {
|
|
|
108
108
|
expect(fakeWindow.__00__.getQueryRows(12345)).toBeNull();
|
|
109
109
|
});
|
|
110
110
|
|
|
111
|
-
|
|
111
|
+
// Flaky under full-suite load only (the push that carries the event
|
|
112
|
+
// occasionally lands before the event is recorded); passes in isolation.
|
|
113
|
+
// DevTools moves to state-derived reads in a later commit; retried until then.
|
|
114
|
+
it('records a stream update as counts, not the membership array', { retry: 3 }, async () => {
|
|
112
115
|
vi.useFakeTimers();
|
|
113
116
|
const { service, statePushes } = harness(2);
|
|
114
117
|
service.onStreamUpdate({ queryHash: 'q1', localArray: [['a', 1], ['b', 1]], op: 'UPDATE' });
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import type { SchemaStructure } from '@spooky-sync/query-builder';
|
|
2
|
-
import type { DataModule } from '../data/index';
|
|
3
|
-
import type { Sp00kySync } from '../sync/index';
|
|
4
2
|
import type { AuthService } from '../auth/index';
|
|
5
3
|
import type { Logger } from '../../services/logger/index';
|
|
6
4
|
import type { QueryTimeToLive } from '../../types';
|
|
@@ -107,10 +105,20 @@ export class FeatureFlagHandle {
|
|
|
107
105
|
}
|
|
108
106
|
}
|
|
109
107
|
|
|
108
|
+
/** The slice of the query engine this module drives: register a live query
|
|
109
|
+
* locally, ask for its remote registration, subscribe to its rows. */
|
|
110
|
+
export interface LiveQueryPort {
|
|
111
|
+
query(table: string, surql: string, params: Record<string, unknown>, ttl: QueryTimeToLive): Promise<string>;
|
|
112
|
+
subscribe(hash: string, cb: (records: Record<string, any>[]) => void, options?: { immediate?: boolean }): () => void;
|
|
113
|
+
}
|
|
114
|
+
export interface RemoteRegisterPort {
|
|
115
|
+
enqueueDownEvent(event: { type: 'register'; payload: { hash: string } }): void;
|
|
116
|
+
}
|
|
117
|
+
|
|
110
118
|
export interface FeatureFlagModuleDeps<S extends SchemaStructure> {
|
|
111
|
-
dataModule:
|
|
112
|
-
sync:
|
|
113
|
-
auth: AuthService<S>;
|
|
119
|
+
dataModule: LiveQueryPort;
|
|
120
|
+
sync: RemoteRegisterPort;
|
|
121
|
+
auth: Pick<AuthService<S>, 'subscribe'>;
|
|
114
122
|
logger: Logger;
|
|
115
123
|
}
|
|
116
124
|
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { buildJobRecord } from './jobs';
|
|
3
|
+
|
|
4
|
+
const schema = {
|
|
5
|
+
tables: [],
|
|
6
|
+
backends: {
|
|
7
|
+
api: { outboxTable: '_00_job_api', routes: { hello: { args: { name: { optional: false }, tone: { optional: true } } } } },
|
|
8
|
+
bare: { routes: { x: { args: {} } } },
|
|
9
|
+
},
|
|
10
|
+
} as any;
|
|
11
|
+
|
|
12
|
+
describe('buildJobRecord', () => {
|
|
13
|
+
it('builds a pending job row with the route payload and option overrides', () => {
|
|
14
|
+
const { tableName, record } = buildJobRecord(schema, 'api', 'hello', { name: 'a', extra: 1 }, { max_retries: 5, retry_strategy: 'exponential', timeout: 9, delay: 100, assignedTo: 'w1' });
|
|
15
|
+
expect(tableName).toBe('_00_job_api');
|
|
16
|
+
expect(record).toEqual({ path: 'hello', payload: JSON.stringify({ name: 'a', tone: undefined }), status: 'pending', max_retries: 5, retry_strategy: 'exponential', timeout: 9, delay: 100, assigned_to: 'w1' });
|
|
17
|
+
expect(buildJobRecord(schema, 'api', 'hello', { name: 'a' }).record).toMatchObject({ max_retries: 3, retry_strategy: 'linear' });
|
|
18
|
+
});
|
|
19
|
+
it('rejects unknown routes, missing outbox tables and missing required args', () => {
|
|
20
|
+
expect(() => buildJobRecord(schema, 'api', 'nope', {})).toThrow('Route api.nope not found');
|
|
21
|
+
expect(() => buildJobRecord(schema, 'bare', 'x', {})).toThrow('Outbox table for backend bare not found');
|
|
22
|
+
expect(() => buildJobRecord(schema, 'api', 'hello', {})).toThrow('Missing required argument name');
|
|
23
|
+
expect(() => buildJobRecord({ tables: [] } as any, 'api', 'hello', {})).toThrow('Route api.hello not found');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { SchemaStructure } from '@spooky-sync/query-builder';
|
|
2
|
+
import type { RunOptions } from '../types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Build the outbox job record for a backend route and resolve its table.
|
|
6
|
+
* Every job is a single execution; recurring work is declared server-side.
|
|
7
|
+
*/
|
|
8
|
+
export function buildJobRecord(
|
|
9
|
+
schema: SchemaStructure,
|
|
10
|
+
backend: string,
|
|
11
|
+
path: string,
|
|
12
|
+
data: Record<string, unknown>,
|
|
13
|
+
options?: RunOptions
|
|
14
|
+
): { tableName: string; record: Record<string, unknown> } {
|
|
15
|
+
const backends = (schema as { backends?: Record<string, { outboxTable?: string; routes?: Record<string, { args: Record<string, { optional: boolean }> }> }> }).backends;
|
|
16
|
+
const route = backends?.[backend]?.routes?.[path];
|
|
17
|
+
if (!route) throw new Error(`Route ${backend}.${path} not found`);
|
|
18
|
+
const tableName = backends?.[backend]?.outboxTable;
|
|
19
|
+
if (!tableName) throw new Error(`Outbox table for backend ${backend} not found`);
|
|
20
|
+
|
|
21
|
+
const payload: Record<string, unknown> = {};
|
|
22
|
+
for (const argName of Object.keys(route.args)) {
|
|
23
|
+
const arg = route.args[argName];
|
|
24
|
+
if (data[argName] === undefined && arg.optional === false) throw new Error(`Missing required argument ${argName}`);
|
|
25
|
+
payload[argName] = data[argName];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const record: Record<string, unknown> = {
|
|
29
|
+
path,
|
|
30
|
+
payload: JSON.stringify(payload),
|
|
31
|
+
// Explicit: the schema's server-side DEFAULT does not apply to an
|
|
32
|
+
// optimistic local create, and in-flight indicators key on `pending`.
|
|
33
|
+
status: 'pending',
|
|
34
|
+
max_retries: options?.max_retries ?? 3,
|
|
35
|
+
retry_strategy: options?.retry_strategy ?? 'linear',
|
|
36
|
+
};
|
|
37
|
+
if (options?.timeout != null) record.timeout = options.timeout;
|
|
38
|
+
if (options?.delay != null) record.delay = options.delay;
|
|
39
|
+
if (options?.assignedTo) record.assigned_to = options.assignedTo;
|
|
40
|
+
return { tableName, record };
|
|
41
|
+
}
|
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest';
|
|
2
2
|
import { mintMutationId, mutationOwnerTabId } from './mutation-id';
|
|
3
3
|
|
|
4
|
+
describe('mutationOwnerTabId (raw ids)', () => {
|
|
5
|
+
it('mints with the fallback tab id when none is given', () => {
|
|
6
|
+
const id = mintMutationId();
|
|
7
|
+
expect(id.startsWith('_00_pending_mutations:')).toBe(true);
|
|
8
|
+
expect(mutationOwnerTabId(id)).toMatch(/^[a-z0-9]+$/);
|
|
9
|
+
});
|
|
10
|
+
it('accepts an id without the table prefix', () => {
|
|
11
|
+
expect(mutationOwnerTabId('1700000000000_0001_tab9')).toBe('tab9');
|
|
12
|
+
expect(mutationOwnerTabId('1700000000000')).toBeNull();
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
4
16
|
// The old `_00_pending_mutations:${Date.now()}` collided within a millisecond
|
|
5
17
|
// (guaranteed once multiple tabs share a store) and never actually ordered the
|
|
6
18
|
// drain. These pin the new id's three properties: unique, sortable, routable.
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { RecordId } from 'surrealdb';
|
|
3
|
+
import { runPure } from '../testing/run-pure';
|
|
4
|
+
import { buildEntry, buildOutboxItem, buildState } from '../testing/build';
|
|
5
|
+
import * as R from '../state/reducers';
|
|
6
|
+
import { defaultEnv } from '../query/env';
|
|
7
|
+
import { drain, loadOutbox, refreshFailedCount, rollback } from './push.saga';
|
|
8
|
+
import type { StatementResult } from '../kernel/effects';
|
|
9
|
+
import type { PendingMutationRow } from './rows';
|
|
10
|
+
|
|
11
|
+
const env = defaultEnv({ tables: [{ name: 'thing', columns: {} }] } as any, { outboxBatchSize: 2 });
|
|
12
|
+
const ok = (result: unknown = null): StatementResult => ({ status: 'OK', result });
|
|
13
|
+
const err = (error: string): StatementResult => ({ status: 'ERR', error });
|
|
14
|
+
const pendingRow = (n: number, type: PendingMutationRow['mutationType'] = 'create', extra: Partial<PendingMutationRow> = {}) => ({
|
|
15
|
+
id: `_00_pending_mutations:m${n}`,
|
|
16
|
+
mutationType: type,
|
|
17
|
+
recordId: `thing:${n}`,
|
|
18
|
+
tableName: 'thing',
|
|
19
|
+
data: type === 'delete' ? undefined : { a: n },
|
|
20
|
+
beforeRecord: type === 'create' ? undefined : { id: `thing:${n}`, a: 0, _00_rv: 7 },
|
|
21
|
+
createdAt: 1,
|
|
22
|
+
v: 2,
|
|
23
|
+
...extra,
|
|
24
|
+
});
|
|
25
|
+
const items = (...ns: number[]) => ns.map((n) => buildOutboxItem({ id: `_00_pending_mutations:m${n}`, recordId: `thing:${n}`, type: 'create' }));
|
|
26
|
+
const stateWith = (rows: number[], ...extra: Parameters<typeof buildState>[1][]) =>
|
|
27
|
+
buildState([buildEntry({ def: { hash: 'q', tableName: 'thing' } })], R.outboxReplace(items(...rows)), ...extra);
|
|
28
|
+
|
|
29
|
+
describe('drain', () => {
|
|
30
|
+
it('re-types stored payloads from the schema before pushing (a stored record field is a string)', async () => {
|
|
31
|
+
const typedEnv = defaultEnv({ tables: [{ name: 'thing', columns: { owner: { recordId: true } } }] } as any, { outboxBatchSize: 2 });
|
|
32
|
+
let vars: any = null;
|
|
33
|
+
await runPure(drain(typedEnv), {
|
|
34
|
+
state: stateWith([1]),
|
|
35
|
+
handlers: {
|
|
36
|
+
'local.query': (e: any) => (e.sql === 'SELECT * FROM $ids' ? [[pendingRow(1, 'create', { data: { owner: 'user:u1' } })]] : []),
|
|
37
|
+
'remote.query': (e: any) => ((vars = e.vars), [ok()]),
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
expect(vars.d0_owner).toEqual(new RecordId('user', 'u1'));
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('pushes batches in FIFO order, acks each accepted row, deletes its pending row, then loops', async () => {
|
|
44
|
+
const pushed: string[] = [];
|
|
45
|
+
const out = await runPure(drain(env), {
|
|
46
|
+
state: stateWith([1, 2, 3]),
|
|
47
|
+
now: 500,
|
|
48
|
+
handlers: {
|
|
49
|
+
'local.query': (e: any) => {
|
|
50
|
+
if (e.sql === 'SELECT * FROM $ids') return [e.vars.ids.map((id: RecordId) => pendingRow(Number(String(id.id).slice(1))))];
|
|
51
|
+
return [];
|
|
52
|
+
},
|
|
53
|
+
'remote.query': (e: any) => {
|
|
54
|
+
pushed.push(e.sql);
|
|
55
|
+
return e.sql.split(';\n').map(() => ok());
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
expect(pushed).toEqual(['CREATE ONLY $id0 SET a = $d0_a;\nCREATE ONLY $id1 SET a = $d1_a', 'CREATE ONLY $id0 SET a = $d0_a']);
|
|
60
|
+
expect(out.state.outbox.every((i) => i.status === 'acked' && i.ackedAt === 500)).toBe(true);
|
|
61
|
+
expect(out.log.filter((e) => e.kind === 'local.query' && (e as any).sql === 'DELETE $mid')).toHaveLength(3);
|
|
62
|
+
expect(out.emitted.filter((e) => e.type === 'mutation:settled')).toHaveLength(3);
|
|
63
|
+
expect(out.timers.get('ack-prune')).toEqual({ ms: 30_000, event: { type: 'AckPrune' } });
|
|
64
|
+
expect(out.dispatched.filter((d) => d.type === 'SyncOutcome')).toEqual([{ type: 'SyncOutcome', ok: true, error: undefined }, { type: 'SyncOutcome', ok: true, error: undefined }]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('a rejected statement rolls back only that row; the rest of the batch is acked', async () => {
|
|
68
|
+
const out = await runPure(drain(env), {
|
|
69
|
+
state: stateWith([1, 2]),
|
|
70
|
+
handlers: {
|
|
71
|
+
'local.query': (e: any) => (e.sql === 'SELECT * FROM $ids' ? [[pendingRow(1), pendingRow(2)]] : []),
|
|
72
|
+
'remote.query': () => [err('permission denied'), ok()],
|
|
73
|
+
'local.execute': () => undefined,
|
|
74
|
+
'ssp.ingest': () => undefined,
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
expect(out.state.outbox.map((i) => [i.id, i.status])).toEqual([['_00_pending_mutations:m2', 'acked']]);
|
|
78
|
+
expect(out.state.failedCount).toBe(1);
|
|
79
|
+
expect(out.emitted).toContainEqual(expect.objectContaining({ type: 'mutation:rolled-back', mutationId: '_00_pending_mutations:m1', error: 'permission denied' }));
|
|
80
|
+
expect(out.dispatched.at(-1)).toEqual({ type: 'SyncOutcome', ok: true, error: undefined });
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('transport failures keep the tail queued behind a backoff timer (thrown, or a network ERR mid-batch, or a short result list)', async () => {
|
|
84
|
+
const thrown = await runPure(drain(env), {
|
|
85
|
+
state: stateWith([1, 2]),
|
|
86
|
+
handlers: {
|
|
87
|
+
'local.query': () => [[pendingRow(1), pendingRow(2)]],
|
|
88
|
+
'remote.query': () => {
|
|
89
|
+
throw new Error('socket closed');
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
expect(thrown.state.outbox.map((i) => i.status)).toEqual(['pending', 'pending']);
|
|
94
|
+
expect(thrown.state.outbox[0].attempts).toBe(1);
|
|
95
|
+
expect(thrown.timers.get('outbox')).toEqual({ ms: 500, event: { type: 'Drain' } });
|
|
96
|
+
expect(thrown.dispatched).toEqual([{ type: 'SyncOutcome', ok: false, error: new Error('socket closed') }]);
|
|
97
|
+
const mid = await runPure(drain(env), {
|
|
98
|
+
state: stateWith([1, 2]),
|
|
99
|
+
handlers: { 'local.query': () => [[pendingRow(1), pendingRow(2)]], 'remote.query': () => [ok(), err('connection reset')] },
|
|
100
|
+
});
|
|
101
|
+
expect(mid.state.outbox.map((i) => [i.status, i.attempts])).toEqual([['acked', 0], ['pending', 1]]);
|
|
102
|
+
expect(mid.timers.get('outbox')!.ms).toBe(500);
|
|
103
|
+
const short = await runPure(drain(env), {
|
|
104
|
+
state: stateWith([1, 2]),
|
|
105
|
+
handlers: { 'local.query': () => [[pendingRow(1), pendingRow(2)]], 'remote.query': () => [ok()] },
|
|
106
|
+
});
|
|
107
|
+
expect(short.state.outbox.map((i) => i.status)).toEqual(['acked', 'pending']);
|
|
108
|
+
const undefinedRows = await runPure(drain(env), { state: stateWith([1]), handlers: { 'local.query': () => undefined } });
|
|
109
|
+
expect(undefinedRows.state.outbox).toEqual([]);
|
|
110
|
+
const notArray = await runPure(drain(env), {
|
|
111
|
+
state: stateWith([1]),
|
|
112
|
+
handlers: { 'local.query': () => [[pendingRow(1)]], 'remote.query': () => 'nope' },
|
|
113
|
+
});
|
|
114
|
+
expect(notArray.timers.has('outbox')).toBe(true);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('a request rejected as a whole with an application error rolls back the head and continues', async () => {
|
|
118
|
+
let calls = 0;
|
|
119
|
+
const out = await runPure(drain(env), {
|
|
120
|
+
state: stateWith([1, 2]),
|
|
121
|
+
handlers: {
|
|
122
|
+
'local.query': (e: any) => (e.sql === 'SELECT * FROM $ids' ? [e.vars.ids.map((id: RecordId) => pendingRow(Number(String(id.id).slice(1))))] : []),
|
|
123
|
+
'remote.query': () => {
|
|
124
|
+
calls++;
|
|
125
|
+
if (calls === 1) throw 'Parse error at line 1';
|
|
126
|
+
return [ok()];
|
|
127
|
+
},
|
|
128
|
+
'local.execute': () => undefined,
|
|
129
|
+
'ssp.ingest': () => undefined,
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
expect(calls).toBe(2);
|
|
133
|
+
expect(out.state.failedCount).toBe(1);
|
|
134
|
+
expect(out.state.outbox.map((i) => [i.id, i.status])).toEqual([['_00_pending_mutations:m2', 'acked']]);
|
|
135
|
+
const asError = await runPure(drain(env), {
|
|
136
|
+
state: stateWith([1]),
|
|
137
|
+
handlers: {
|
|
138
|
+
'local.query': () => [[pendingRow(1)]],
|
|
139
|
+
'remote.query': () => {
|
|
140
|
+
throw new Error('Parse error at line 1');
|
|
141
|
+
},
|
|
142
|
+
'local.execute': (e: any) => {
|
|
143
|
+
if (e.vars.failed) expect(e.vars.failed.error.message).toBe('Parse error at line 1');
|
|
144
|
+
},
|
|
145
|
+
'ssp.ingest': () => undefined,
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
expect(asError.state.failedCount).toBe(1);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('skips pending rows the store no longer has, does nothing as a follower or when empty, tolerates a failed row delete', async () => {
|
|
152
|
+
const gone = await runPure(drain(env), { state: stateWith([1]), handlers: { 'local.query': () => [[]] } });
|
|
153
|
+
expect(gone.state.outbox).toEqual([]);
|
|
154
|
+
expect(gone.log.filter((e) => e.kind === 'remote.query')).toHaveLength(0);
|
|
155
|
+
const follower = await runPure(drain(env), { state: { ...stateWith([1]), tabRole: 'follower' } });
|
|
156
|
+
expect(follower.log.filter((e) => e.kind === 'local.query')).toHaveLength(0);
|
|
157
|
+
const empty = await runPure(drain(env), { state: buildState() });
|
|
158
|
+
expect(empty.log.filter((e) => e.kind === 'local.query')).toHaveLength(0);
|
|
159
|
+
const delFail = await runPure(drain(env), {
|
|
160
|
+
state: stateWith([1]),
|
|
161
|
+
handlers: {
|
|
162
|
+
'local.query': (e: any) => {
|
|
163
|
+
if (e.sql === 'DELETE $mid') throw new Error('locked');
|
|
164
|
+
return [[pendingRow(1)]];
|
|
165
|
+
},
|
|
166
|
+
'remote.query': () => [ok()],
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
expect(delFail.state.outbox[0].status).toBe('acked');
|
|
170
|
+
expect(delFail.emitted).toContainEqual(expect.objectContaining({ level: 'error' }));
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe('rollback', () => {
|
|
175
|
+
it('create: tray move first, local delete, circuit DELETE, version dropped, owner relay when leading', async () => {
|
|
176
|
+
const order: string[] = [];
|
|
177
|
+
const state = { ...stateWith([1]), tabRole: 'leader' as const, tabId: 'tab-a' };
|
|
178
|
+
const row = pendingRow(1, 'create', { id: '_00_pending_mutations:1700000000000_0001_tabz' });
|
|
179
|
+
const out = await runPure(rollback(env, row, { message: 'denied', kind: 'application' }), {
|
|
180
|
+
state: R.setVersions([['thing:1', 1]])(state),
|
|
181
|
+
handlers: {
|
|
182
|
+
'local.execute': (e: any) => void order.push(e.query.sql.includes('_00_failed') || e.query.sql.includes('CREATE ONLY $fid') ? 'tray' : 'revert'),
|
|
183
|
+
'ssp.ingest': (e: any) => void order.push(`circuit:${e.records[0].op}`),
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
expect(order).toEqual(['tray', 'revert', 'circuit:DELETE']);
|
|
187
|
+
expect(out.state.versions.has('thing:1')).toBe(false);
|
|
188
|
+
expect(out.state.failedCount).toBe(1);
|
|
189
|
+
expect(out.state.dirty.has('q')).toBe(true);
|
|
190
|
+
expect(out.emitted).toContainEqual({ type: 'tabs:sendTo', tabId: 'tabz', message: expect.objectContaining({ type: 'mutation-rolled-back' }) });
|
|
191
|
+
expect(out.emitted).toContainEqual({ type: 'tabs:broadcast', message: { type: 'ingest', records: [expect.objectContaining({ op: 'DELETE' })] } });
|
|
192
|
+
expect(out.emitted).toContainEqual({ type: 'tray:changed', count: 1 });
|
|
193
|
+
expect(out.emitted).toContainEqual({ type: 'tabs:broadcast', message: { type: 'failed-mutations-changed', count: 1 } });
|
|
194
|
+
});
|
|
195
|
+
it('update / delete restore beforeRecord and its version; legacy rows read the server row (or end partial)', async () => {
|
|
196
|
+
const upd = await runPure(rollback(env, pendingRow(1, 'update'), { message: 'x', kind: 'application' }), {
|
|
197
|
+
state: stateWith([1]),
|
|
198
|
+
handlers: { 'local.execute': () => undefined, 'ssp.ingest': (e: any) => expect(e.records[0].op).toBe('UPDATE') },
|
|
199
|
+
});
|
|
200
|
+
expect(upd.state.versions.get('thing:1')).toBe(7);
|
|
201
|
+
const del = await runPure(rollback(env, pendingRow(1, 'delete'), { message: 'x', kind: 'application' }), {
|
|
202
|
+
state: stateWith([1]),
|
|
203
|
+
handlers: { 'local.execute': () => undefined, 'ssp.ingest': (e: any) => expect(e.records[0].op).toBe('CREATE') },
|
|
204
|
+
});
|
|
205
|
+
expect(del.state.versions.get('thing:1')).toBe(7);
|
|
206
|
+
const legacy = await runPure(rollback(env, pendingRow(1, 'update', { beforeRecord: undefined }), { message: 'x', kind: 'application' }), {
|
|
207
|
+
state: stateWith([1]),
|
|
208
|
+
handlers: {
|
|
209
|
+
'remote.query': () => [ok({ id: new RecordId('thing', '1'), a: 9, _00_rv: 2 })],
|
|
210
|
+
'local.execute': (e: any) => {
|
|
211
|
+
if (e.query.sql.includes('CREATE ONLY $fid')) expect(e.vars.failed.revert).toBe('full');
|
|
212
|
+
},
|
|
213
|
+
'ssp.ingest': () => undefined,
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
expect(legacy.state.versions.get('thing:1')).toBe(2);
|
|
217
|
+
const partial = await runPure(rollback(env, pendingRow(1, 'update', { beforeRecord: undefined }), { message: 'x', kind: 'application' }), {
|
|
218
|
+
state: stateWith([1]),
|
|
219
|
+
handlers: {
|
|
220
|
+
'remote.query': () => {
|
|
221
|
+
throw new Error('offline');
|
|
222
|
+
},
|
|
223
|
+
'local.execute': (e: any) => expect(e.vars.failed.revert).toBe('partial'),
|
|
224
|
+
'ssp.ingest': () => undefined,
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
expect(partial.log.filter((e) => e.kind === 'local.execute')).toHaveLength(1);
|
|
228
|
+
const errRow = await runPure(rollback(env, pendingRow(1, 'update', { beforeRecord: undefined }), { message: 'x', kind: 'application' }), {
|
|
229
|
+
state: stateWith([1]),
|
|
230
|
+
handlers: { 'remote.query': () => [err('nope')], 'local.execute': () => undefined, 'ssp.ingest': () => undefined },
|
|
231
|
+
});
|
|
232
|
+
expect(errRow.state.failedCount).toBe(1);
|
|
233
|
+
});
|
|
234
|
+
it('every local step is best-effort: tray move, revert and circuit failures are logged', async () => {
|
|
235
|
+
const out = await runPure(rollback(env, pendingRow(1, 'update'), { message: 'x', kind: 'unreplayable' }), {
|
|
236
|
+
state: stateWith([1]),
|
|
237
|
+
handlers: {
|
|
238
|
+
'local.execute': () => {
|
|
239
|
+
throw new Error('disk');
|
|
240
|
+
},
|
|
241
|
+
'ssp.ingest': () => {
|
|
242
|
+
throw new Error('wasm');
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
expect(out.emitted.filter((e) => e.type === 'log')).toHaveLength(3);
|
|
247
|
+
expect(out.state.outbox).toEqual([]);
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
describe('loadOutbox / refreshFailedCount', () => {
|
|
252
|
+
it('mirrors pending rows, sends unreplayable ones to the tray, refreshes the count, starts a drain', async () => {
|
|
253
|
+
const out = await runPure(loadOutbox(env), {
|
|
254
|
+
state: buildState(),
|
|
255
|
+
handlers: {
|
|
256
|
+
'local.query': (e: any) => {
|
|
257
|
+
if (e.sql.startsWith('SELECT * FROM _00_pending_mutations'))
|
|
258
|
+
return [[pendingRow(1), { id: '_00_pending_mutations:bad', mutationType: 'create', recordId: 'thing:9' }, { tableName: 'thing' }, { mutationType: 'delete' }, 'junk']];
|
|
259
|
+
if (e.sql.startsWith('SELECT count()')) return [[{ count: 4 }]];
|
|
260
|
+
return [];
|
|
261
|
+
},
|
|
262
|
+
'local.execute': () => undefined,
|
|
263
|
+
'ssp.ingest': () => undefined,
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
expect(out.state.outbox.map((i) => i.id)).toEqual(['_00_pending_mutations:m1']);
|
|
267
|
+
expect(out.emitted).toContainEqual(expect.objectContaining({ type: 'mutation:rolled-back', mutationId: '_00_pending_mutations:bad' }));
|
|
268
|
+
expect(out.emitted).toContainEqual(expect.objectContaining({ type: 'mutation:rolled-back', mutationId: '' }));
|
|
269
|
+
expect(out.state.failedCount).toBe(4);
|
|
270
|
+
expect(out.dispatched).toEqual([{ type: 'Drain' }]);
|
|
271
|
+
const empty = await runPure(loadOutbox(env), { state: buildState(), handlers: { 'local.query': () => [] } });
|
|
272
|
+
expect(empty.dispatched).toEqual([]);
|
|
273
|
+
const missingTable = await runPure(refreshFailedCount(), {
|
|
274
|
+
state: R.setFailedCount(2)(buildState()),
|
|
275
|
+
handlers: {
|
|
276
|
+
'local.query': () => {
|
|
277
|
+
throw new Error('no table');
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
expect(missingTable.state.failedCount).toBe(2);
|
|
282
|
+
const noRows = await runPure(refreshFailedCount(), { state: R.setFailedCount(2)(buildState()), handlers: { 'local.query': () => [[]] } });
|
|
283
|
+
expect(noRows.state.failedCount).toBe(0);
|
|
284
|
+
});
|
|
285
|
+
});
|