@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
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import type { Saga } from '../kernel/saga';
|
|
2
|
+
import type { StatementResult } from '../kernel/effects';
|
|
3
|
+
import { fx } from '../kernel/effects';
|
|
4
|
+
import { ACK_GRACE_MS, backoffMs } from '../kernel/constants';
|
|
5
|
+
import type { ClientState, OutboxItem } from '../state/client-state';
|
|
6
|
+
import * as R from '../state/reducers';
|
|
7
|
+
import { classifySyncError } from '../utils/error-classification';
|
|
8
|
+
import type { SagaEnv } from '../query/env';
|
|
9
|
+
import { columnsFor } from '../query/env';
|
|
10
|
+
import { mutationOwnerTabId } from './mutation-id';
|
|
11
|
+
import {
|
|
12
|
+
buildFailedRow,
|
|
13
|
+
deletePendingRow,
|
|
14
|
+
hydrateRowData,
|
|
15
|
+
loadPendingRows,
|
|
16
|
+
moveToFailedTx,
|
|
17
|
+
parsePendingRow,
|
|
18
|
+
parseStoredRecordId,
|
|
19
|
+
planRevert,
|
|
20
|
+
remoteBatch,
|
|
21
|
+
toOutboxItem,
|
|
22
|
+
FAILED_TABLE,
|
|
23
|
+
type PendingMutationRow,
|
|
24
|
+
} from './rows';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Boot / bucket switch: mirror the outbox table into state. Rows that can
|
|
28
|
+
* never be replayed (a create without its payload) go straight to the tray.
|
|
29
|
+
*/
|
|
30
|
+
export function* loadOutbox(env: SagaEnv): Saga<void> {
|
|
31
|
+
const res = (yield fx.local.query(loadPendingRows())) as unknown[];
|
|
32
|
+
const raw = Array.isArray(res?.[0]) ? (res[0] as unknown[]) : [];
|
|
33
|
+
const items: OutboxItem[] = [];
|
|
34
|
+
const unreplayable: Array<Record<string, unknown>> = [];
|
|
35
|
+
for (const row of raw) {
|
|
36
|
+
const parsed = parsePendingRow(row);
|
|
37
|
+
if (parsed) items.push(toOutboxItem(parsed));
|
|
38
|
+
else if (row && typeof row === 'object') unreplayable.push(row as Record<string, unknown>);
|
|
39
|
+
}
|
|
40
|
+
yield fx.state.update(R.outboxReplace(items));
|
|
41
|
+
for (const row of unreplayable) {
|
|
42
|
+
const stub: PendingMutationRow = {
|
|
43
|
+
id: String(row.id ?? ''),
|
|
44
|
+
mutationType: (row.mutationType as PendingMutationRow['mutationType']) ?? 'create',
|
|
45
|
+
recordId: String(row.recordId ?? ''),
|
|
46
|
+
tableName: String(row.tableName ?? String(row.recordId ?? '').split(':')[0]),
|
|
47
|
+
createdAt: 0,
|
|
48
|
+
v: 1,
|
|
49
|
+
};
|
|
50
|
+
yield* rollback(env, stub, { message: 'pending mutation is not replayable', kind: 'unreplayable' });
|
|
51
|
+
}
|
|
52
|
+
yield* refreshFailedCount();
|
|
53
|
+
if (items.length > 0) yield fx.dispatch({ type: 'Drain' });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function* refreshFailedCount(): Saga<void> {
|
|
57
|
+
try {
|
|
58
|
+
const res = (yield fx.local.query(`SELECT count() FROM ${FAILED_TABLE} GROUP ALL`)) as unknown[];
|
|
59
|
+
const row = Array.isArray(res?.[0]) ? (res[0] as Array<{ count?: number }>)[0] : undefined;
|
|
60
|
+
yield fx.state.update(R.setFailedCount(typeof row?.count === 'number' ? row.count : 0));
|
|
61
|
+
} catch {
|
|
62
|
+
// The tray table may not exist yet on a fresh store; the count stays as is.
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const ERR = (r: StatementResult | undefined): string | null => (r && r.status === 'ERR' ? r.error : null);
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Drain the outbox: one serial lane, FIFO, up to `outboxBatchSize` statements
|
|
70
|
+
* per request, per-statement outcome. Accepted rows are acked (they stay in
|
|
71
|
+
* the overlay until membership names them), rejected rows are rolled back,
|
|
72
|
+
* a transport failure leaves the unsent tail queued behind a backoff timer.
|
|
73
|
+
*/
|
|
74
|
+
export function* drain(env: SagaEnv): Saga<void> {
|
|
75
|
+
for (;;) {
|
|
76
|
+
const state = (yield fx.state.read((s) => s)) as ClientState;
|
|
77
|
+
if (state.tabRole === 'follower') return;
|
|
78
|
+
const batch = state.outbox.filter((i) => i.status === 'pending').slice(0, env.outboxBatchSize);
|
|
79
|
+
if (batch.length === 0) return;
|
|
80
|
+
const rowsRes = (yield fx.local.query('SELECT * FROM $ids', { ids: batch.map((i) => parseStoredRecordId(i.id)) })) as unknown[];
|
|
81
|
+
const rawRows = Array.isArray(rowsRes?.[0]) ? (rowsRes[0] as unknown[]) : [];
|
|
82
|
+
const byId = new Map<string, PendingMutationRow>();
|
|
83
|
+
for (const raw of rawRows) {
|
|
84
|
+
const parsed = parsePendingRow(raw);
|
|
85
|
+
if (parsed) byId.set(parsed.id, parsed);
|
|
86
|
+
}
|
|
87
|
+
const rows: PendingMutationRow[] = [];
|
|
88
|
+
for (const item of batch) {
|
|
89
|
+
const row = byId.get(item.id);
|
|
90
|
+
if (row) rows.push(hydrateRowData(row, columnsFor(env, row.tableName)));
|
|
91
|
+
else yield fx.state.update(R.outboxRemove(item.id)); // row already gone from the store
|
|
92
|
+
}
|
|
93
|
+
if (rows.length === 0) continue;
|
|
94
|
+
|
|
95
|
+
const req = remoteBatch(rows);
|
|
96
|
+
let results: StatementResult[];
|
|
97
|
+
try {
|
|
98
|
+
const raw = (yield fx.remote.query(req.sql, req.vars, env.pushTimeoutMs)) as unknown;
|
|
99
|
+
// A response without statement results is a transport fault: nothing
|
|
100
|
+
// ran, everything stays queued (handled as `!r` below).
|
|
101
|
+
results = Array.isArray(raw) ? (raw as StatementResult[]) : [];
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (classifySyncError(error) === 'application') {
|
|
104
|
+
// The whole request was rejected before any statement ran: treat the
|
|
105
|
+
// head as rejected, keep the rest for the next round.
|
|
106
|
+
yield* rollback(env, rows[0], { message: error instanceof Error ? error.message : String(error), kind: 'application' });
|
|
107
|
+
yield fx.dispatch({ type: 'SyncOutcome', ok: false, error });
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const attempts = batch.find((i) => i.id === rows[0].id)!.attempts;
|
|
111
|
+
yield fx.state.update(R.outboxBumpAttempts(rows[0].id));
|
|
112
|
+
yield fx.dispatch({ type: 'SyncOutcome', ok: false, error });
|
|
113
|
+
yield fx.timer.set('outbox', backoffMs(attempts), { type: 'Drain' });
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const now = (yield fx.now()) as number;
|
|
118
|
+
let cutAt: number | null = null;
|
|
119
|
+
for (let i = 0; i < rows.length; i++) {
|
|
120
|
+
const row = rows[i];
|
|
121
|
+
const r = results[i];
|
|
122
|
+
if (!r) {
|
|
123
|
+
// Statements after a transport-level cut: they never ran.
|
|
124
|
+
cutAt = i;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
const err = ERR(r);
|
|
128
|
+
if (err === null) {
|
|
129
|
+
const del = deletePendingRow(row.id);
|
|
130
|
+
try {
|
|
131
|
+
yield fx.local.query(del.sql, del.vars);
|
|
132
|
+
} catch (error) {
|
|
133
|
+
yield fx.emit({ type: 'log', level: 'error', message: 'outbox row delete failed after a successful push', data: { id: row.id, error } });
|
|
134
|
+
}
|
|
135
|
+
yield fx.state.update(R.outboxAck(row.id, now));
|
|
136
|
+
yield fx.emit({ type: 'mutation:settled', mutationId: row.id, recordId: row.recordId, eventType: row.mutationType });
|
|
137
|
+
yield fx.emit({ type: 'tabs:broadcast', message: { type: 'mutation-settled', mutationId: row.id, recordId: row.recordId, eventType: row.mutationType } });
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (classifySyncError(new Error(err)) === 'network') {
|
|
141
|
+
cutAt = i;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
yield* rollback(env, row, { message: err, kind: 'application' });
|
|
145
|
+
}
|
|
146
|
+
yield fx.timer.set('ack-prune', ACK_GRACE_MS, { type: 'AckPrune' });
|
|
147
|
+
yield fx.dispatch({ type: 'SyncOutcome', ok: cutAt === null, error: cutAt === null ? undefined : 'push interrupted' });
|
|
148
|
+
if (cutAt !== null) {
|
|
149
|
+
const cut = rows[cutAt];
|
|
150
|
+
const attempts = batch.find((i) => i.id === cut.id)!.attempts;
|
|
151
|
+
yield fx.state.update(R.outboxBumpAttempts(cut.id));
|
|
152
|
+
yield fx.timer.set('outbox', backoffMs(attempts), { type: 'Drain' });
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Undo one rejected mutation. Order matters: the tray row is written and the
|
|
160
|
+
* pending row deleted in ONE transaction before any local revert, so a crash
|
|
161
|
+
* mid-way leaves the mutation in the tray, never silently re-applied.
|
|
162
|
+
*/
|
|
163
|
+
export function* rollback(
|
|
164
|
+
env: SagaEnv,
|
|
165
|
+
row: PendingMutationRow,
|
|
166
|
+
error: { message: string; kind: 'application' | 'unreplayable' }
|
|
167
|
+
): Saga<void> {
|
|
168
|
+
const attempts = ((yield fx.state.read((s) => s.outbox.find((i) => i.id === row.id)?.attempts)) as number | undefined) ?? 0;
|
|
169
|
+
const now = (yield fx.now()) as number;
|
|
170
|
+
let before: Record<string, unknown> | null = row.beforeRecord ?? null;
|
|
171
|
+
if (row.beforeRecord === undefined && row.mutationType !== 'create') {
|
|
172
|
+
// Legacy row: nothing captured locally, ask the server for the row.
|
|
173
|
+
try {
|
|
174
|
+
const res = (yield fx.remote.query('SELECT * FROM ONLY $id', { id: parseStoredRecordId(row.recordId) }, env.remoteTimeoutMs)) as StatementResult[];
|
|
175
|
+
const r = res?.[0];
|
|
176
|
+
before = r && r.status === 'OK' && r.result && typeof r.result === 'object' ? (r.result as Record<string, unknown>) : null;
|
|
177
|
+
} catch {
|
|
178
|
+
before = null;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const plan = planRevert(row, before);
|
|
182
|
+
const failed = buildFailedRow(row, error, before, attempts, now, plan.revert);
|
|
183
|
+
const move = moveToFailedTx(failed);
|
|
184
|
+
try {
|
|
185
|
+
yield fx.local.execute(move.query, move.vars);
|
|
186
|
+
} catch (err) {
|
|
187
|
+
yield fx.emit({ type: 'log', level: 'error', message: 'failed to move a rejected mutation to the tray', data: { id: row.id, err } });
|
|
188
|
+
}
|
|
189
|
+
yield fx.state.update(R.compose(R.outboxRemove(row.id), R.setFailedCount((yield fx.state.read((s) => s.failedCount)) as number + 1)));
|
|
190
|
+
if (plan.tx) {
|
|
191
|
+
try {
|
|
192
|
+
yield fx.local.execute(plan.tx.query, plan.tx.vars);
|
|
193
|
+
} catch (err) {
|
|
194
|
+
yield fx.emit({ type: 'log', level: 'error', message: 'local revert failed', data: { id: row.id, err } });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
try {
|
|
198
|
+
yield fx.ssp.ingest([plan.circuit]);
|
|
199
|
+
} catch (err) {
|
|
200
|
+
yield fx.emit({ type: 'log', level: 'warn', message: 'circuit revert failed', data: { id: row.id, err } });
|
|
201
|
+
}
|
|
202
|
+
const roleNow = (yield fx.state.read((s) => s.tabRole)) as ClientState['tabRole'];
|
|
203
|
+
if (roleNow !== 'solo') yield fx.emit({ type: 'tabs:broadcast', message: { type: 'ingest', records: [plan.circuit] } });
|
|
204
|
+
if (plan.circuit.op === 'DELETE') yield fx.state.update(R.deleteVersions([row.recordId]));
|
|
205
|
+
else if (before && typeof before._00_rv === 'number') yield fx.state.update(R.setVersions([[row.recordId, before._00_rv]]));
|
|
206
|
+
yield fx.state.update(R.markTableDirty(row.tableName));
|
|
207
|
+
const rolledBack = { type: 'mutation-rolled-back', mutationId: row.id, recordId: row.recordId, eventType: row.mutationType, error: error.message };
|
|
208
|
+
yield fx.emit({ type: 'mutation:rolled-back', mutationId: row.id, recordId: row.recordId, eventType: row.mutationType, error: error.message });
|
|
209
|
+
const [tabId, role] = (yield fx.state.read((s) => [s.tabId, s.tabRole])) as [string, ClientState['tabRole']];
|
|
210
|
+
const owner = mutationOwnerTabId(row.id);
|
|
211
|
+
if (role === 'leader' && owner && owner !== tabId) yield fx.emit({ type: 'tabs:sendTo', tabId: owner, message: rolledBack });
|
|
212
|
+
const count = (yield fx.state.read((s) => s.failedCount)) as number;
|
|
213
|
+
yield fx.emit({ type: 'tray:changed', count });
|
|
214
|
+
if (role === 'leader') yield fx.emit({ type: 'tabs:broadcast', message: { type: 'failed-mutations-changed', count } });
|
|
215
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { RecordId } from 'surrealdb';
|
|
3
|
+
import * as rows from './rows';
|
|
4
|
+
|
|
5
|
+
const rid = (t: string, i: string) => new RecordId(t, i);
|
|
6
|
+
|
|
7
|
+
describe('stored ids', () => {
|
|
8
|
+
it('strips ⟨⟩ escaping and reads the timestamp prefix', () => {
|
|
9
|
+
expect(rows.parseStoredRecordId('_00_pending_mutations:⟨1700000000000_0001_tab⟩')).toEqual(
|
|
10
|
+
rid('_00_pending_mutations', '1700000000000_0001_tab')
|
|
11
|
+
);
|
|
12
|
+
expect(rows.storedIdString(rid('_00_pending_mutations', 'x'))).toBe('_00_pending_mutations:x');
|
|
13
|
+
expect(rows.storedIdString('_00_pending_mutations:⟨x⟩')).toBe('_00_pending_mutations:x');
|
|
14
|
+
expect(rows.createdAtFromId('_00_pending_mutations:⟨1700000000000_0001_tab⟩')).toBe(1700000000000);
|
|
15
|
+
expect(rows.createdAtFromId('1700000000000_0001_tab')).toBe(1700000000000);
|
|
16
|
+
expect(rows.createdAtFromId('_00_pending_mutations:abc')).toBeNull();
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('parsePendingRow', () => {
|
|
21
|
+
it('reads v2 rows', () => {
|
|
22
|
+
const row = rows.parsePendingRow({
|
|
23
|
+
id: '_00_pending_mutations:⟨1700000000000_0001_tab⟩',
|
|
24
|
+
mutationType: 'update',
|
|
25
|
+
recordId: 'thing:1',
|
|
26
|
+
tableName: 'thing',
|
|
27
|
+
data: { a: 1 },
|
|
28
|
+
beforeRecord: { id: 'thing:1', a: 0 },
|
|
29
|
+
createdAt: 5,
|
|
30
|
+
v: 2,
|
|
31
|
+
});
|
|
32
|
+
expect(row).toEqual({
|
|
33
|
+
id: '_00_pending_mutations:1700000000000_0001_tab',
|
|
34
|
+
mutationType: 'update',
|
|
35
|
+
recordId: 'thing:1',
|
|
36
|
+
tableName: 'thing',
|
|
37
|
+
data: { a: 1 },
|
|
38
|
+
beforeRecord: { id: 'thing:1', a: 0 },
|
|
39
|
+
createdAt: 5,
|
|
40
|
+
v: 2,
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
it('tolerates legacy rows and rejects unreplayable ones', () => {
|
|
44
|
+
const legacy = rows.parsePendingRow({
|
|
45
|
+
id: '_00_pending_mutations:1700000000000_0001_tab',
|
|
46
|
+
mutationType: 'delete',
|
|
47
|
+
recordId: rid('thing', '1'),
|
|
48
|
+
});
|
|
49
|
+
expect(legacy).toMatchObject({ tableName: 'thing', createdAt: 1700000000000, v: 1, beforeRecord: undefined });
|
|
50
|
+
expect(rows.parsePendingRow({ id: '_00_pending_mutations:1', mutationType: 'create', recordId: 'thing:1' })).toBeNull();
|
|
51
|
+
expect(rows.parsePendingRow({ id: '_00_pending_mutations:abc', mutationType: 'delete', recordId: 'thing:1' })!.createdAt).toBe(0);
|
|
52
|
+
expect(rows.parsePendingRow(null)).toBeNull();
|
|
53
|
+
expect(rows.parsePendingRow({ mutationType: 'bogus', recordId: 'x' })).toBeNull();
|
|
54
|
+
expect(rows.parsePendingRow({ mutationType: 'delete', recordId: 5 })).toBeNull();
|
|
55
|
+
expect(rows.toOutboxItem(legacy!)).toEqual({
|
|
56
|
+
id: legacy!.id,
|
|
57
|
+
type: 'delete',
|
|
58
|
+
recordId: 'thing:1',
|
|
59
|
+
table: 'thing',
|
|
60
|
+
status: 'pending',
|
|
61
|
+
ackedAt: null,
|
|
62
|
+
attempts: 0,
|
|
63
|
+
});
|
|
64
|
+
expect(rows.loadPendingRows()).toBe('SELECT * FROM _00_pending_mutations ORDER BY id ASC');
|
|
65
|
+
expect(rows.loadFailedRows()).toBe('SELECT * FROM _00_failed_mutations ORDER BY failedAt ASC');
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('local write transactions', () => {
|
|
70
|
+
const base = { recordId: rid('thing', '1'), mutationId: rid('_00_pending_mutations', 'm'), table: 'thing', now: 7 };
|
|
71
|
+
it('create: row + v2 outbox entry, result index 0', () => {
|
|
72
|
+
const { query, vars } = rows.planCreateTx({ ...base, data: { a: 1, b: 'x' } });
|
|
73
|
+
expect(query.sql).toBe(
|
|
74
|
+
"BEGIN TRANSACTION;\nCREATE ONLY $id SET a = $data_a, b = $data_b;CREATE ONLY $mid SET mutationType = 'create', recordId = $id, tableName = $table, createdAt = $createdAt, v = 2, data = $data;\nCOMMIT TRANSACTION;"
|
|
75
|
+
);
|
|
76
|
+
expect(vars).toEqual({ id: base.recordId, mid: base.mutationId, table: 'thing', createdAt: 7, data: { a: 1, b: 'x' }, data_a: 1, data_b: 'x' });
|
|
77
|
+
expect(query.extract([null, 'row', 'outbox'])).toBe('row');
|
|
78
|
+
expect(rows.planCreateTx(base).vars.data).toEqual({});
|
|
79
|
+
});
|
|
80
|
+
it('update: rv bump, merge, outbox with beforeRecord, RETURN target', () => {
|
|
81
|
+
const { query, vars } = rows.planUpdateTx({ ...base, data: { a: 2 }, before: { id: 'thing:1', a: 1 } });
|
|
82
|
+
expect(query.sql).toContain('UPDATE $id SET _00_rv += 1;LET $updated = (UPDATE ONLY $id MERGE $data);');
|
|
83
|
+
expect(query.sql).toContain("mutationType = 'update', recordId = $id, tableName = $table, createdAt = $createdAt, v = 2, data = $data, beforeRecord = $before;RETURN {target: $updated}");
|
|
84
|
+
expect(vars.before).toEqual({ id: 'thing:1', a: 1 });
|
|
85
|
+
expect(rows.planUpdateTx(base).vars).toMatchObject({ data: {}, before: null });
|
|
86
|
+
});
|
|
87
|
+
it('delete: DELETE + outbox with beforeRecord', () => {
|
|
88
|
+
const { query, vars } = rows.planDeleteTx({ ...base, before: { id: 'thing:1' } });
|
|
89
|
+
expect(query.sql).toBe(
|
|
90
|
+
"BEGIN TRANSACTION;\nDELETE $id;CREATE ONLY $mid SET mutationType = 'delete', recordId = $id, tableName = $table, createdAt = $createdAt, v = 2, beforeRecord = $before;\nCOMMIT TRANSACTION;"
|
|
91
|
+
);
|
|
92
|
+
expect(vars.before).toEqual({ id: 'thing:1' });
|
|
93
|
+
expect(rows.planDeleteTx(base).vars.before).toBeNull();
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe('debounced update transactions', () => {
|
|
98
|
+
it('local-only update and deferred outbox row', () => {
|
|
99
|
+
const local = rows.planLocalOnlyUpdateTx({ recordId: rid('thing', '1'), data: { a: 1 } });
|
|
100
|
+
expect(local.query.sql).toBe('BEGIN TRANSACTION;\nUPDATE $id SET _00_rv += 1;LET $updated = (UPDATE ONLY $id MERGE $data);RETURN {target: $updated};\nCOMMIT TRANSACTION;');
|
|
101
|
+
expect(local.query.extract([null, 1, 2, { target: 'x' }])).toEqual({ target: 'x' });
|
|
102
|
+
const deferred = rows.planDeferredOutboxRowTx({ recordId: rid('thing', '1'), mutationId: rid('_00_pending_mutations', 'm'), table: 'thing', data: { a: 1 }, before: { a: 0 }, now: 3 });
|
|
103
|
+
expect(deferred.query.sql).toBe("BEGIN TRANSACTION;\nCREATE ONLY $mid SET mutationType = 'update', recordId = $id, tableName = $table, createdAt = $createdAt, v = 2, data = $data, beforeRecord = $before;\nCOMMIT TRANSACTION;");
|
|
104
|
+
expect(rows.planDeferredOutboxRowTx({ recordId: rid('thing', '1'), mutationId: rid('_00_pending_mutations', 'm'), table: 'thing', now: 3 }).vars).toMatchObject({ data: {}, before: null });
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe('remoteBatch', () => {
|
|
109
|
+
it('one statement per row, indexed vars, mixed types', () => {
|
|
110
|
+
const { sql, vars } = rows.remoteBatch([
|
|
111
|
+
{ id: 'm1', mutationType: 'create', recordId: 'thing:1', tableName: 'thing', data: { a: 1 }, createdAt: 0, v: 2 },
|
|
112
|
+
{ id: 'm2', mutationType: 'update', recordId: 'thing:2', tableName: 'thing', data: { b: 2 }, createdAt: 0, v: 2 },
|
|
113
|
+
{ id: 'm3', mutationType: 'delete', recordId: 'thing:3', tableName: 'thing', createdAt: 0, v: 2 },
|
|
114
|
+
{ id: 'm4', mutationType: 'update', recordId: 'thing:4', tableName: 'thing', createdAt: 0, v: 2 },
|
|
115
|
+
]);
|
|
116
|
+
expect(sql).toBe('CREATE ONLY $id0 SET a = $d0_a;\nUPDATE $id1 MERGE $data1;\nDELETE $id2;\nUPDATE $id3 MERGE $data3');
|
|
117
|
+
expect(vars).toEqual({
|
|
118
|
+
id0: rid('thing', '1'),
|
|
119
|
+
d0_a: 1,
|
|
120
|
+
id1: rid('thing', '2'),
|
|
121
|
+
data1: { b: 2 },
|
|
122
|
+
id2: rid('thing', '3'),
|
|
123
|
+
id3: rid('thing', '4'),
|
|
124
|
+
data3: {},
|
|
125
|
+
});
|
|
126
|
+
expect(rows.remoteBatch([{ id: 'm', mutationType: 'create', recordId: 'thing:9', tableName: 'thing', createdAt: 0, v: 2 }]).sql).toBe('CREATE ONLY $id0 SET ');
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('rollback plans', () => {
|
|
131
|
+
const pending = (type: rows.PendingMutationRow['mutationType']): rows.PendingMutationRow => ({
|
|
132
|
+
id: '_00_pending_mutations:m',
|
|
133
|
+
mutationType: type,
|
|
134
|
+
recordId: 'thing:1',
|
|
135
|
+
tableName: 'thing',
|
|
136
|
+
data: { a: 1 },
|
|
137
|
+
createdAt: 1,
|
|
138
|
+
v: 2,
|
|
139
|
+
});
|
|
140
|
+
it('create reverts by delete', () => {
|
|
141
|
+
const plan = rows.planRevert(pending('create'), null);
|
|
142
|
+
expect(plan.revert).toBe('full');
|
|
143
|
+
expect(plan.tx!.query.sql).toBe('BEGIN TRANSACTION;\nDELETE $id;\nCOMMIT TRANSACTION;');
|
|
144
|
+
expect(plan.circuit).toEqual({ table: 'thing', op: 'DELETE', id: 'thing:1', record: { a: 1 } });
|
|
145
|
+
expect(rows.planRevert({ ...pending('create'), data: undefined }, null).circuit.record).toEqual({});
|
|
146
|
+
});
|
|
147
|
+
it('update / delete restore beforeRecord, or are partial without it', () => {
|
|
148
|
+
const before = { id: 'thing:1', a: 0, _00_rv: 3 };
|
|
149
|
+
const upd = rows.planRevert(pending('update'), before);
|
|
150
|
+
expect(upd.tx!.query.sql).toBe('BEGIN TRANSACTION;\nUPSERT ONLY $id REPLACE $content;\nCOMMIT TRANSACTION;');
|
|
151
|
+
expect(upd.tx!.vars).toEqual({ id: rid('thing', '1'), content: { a: 0, _00_rv: 3 } });
|
|
152
|
+
expect(upd.circuit).toMatchObject({ op: 'UPDATE', record: { a: 0, _00_rv: 3, id: rid('thing', '1') } });
|
|
153
|
+
const del = rows.planRevert(pending('delete'), before);
|
|
154
|
+
expect(del.circuit.op).toBe('CREATE');
|
|
155
|
+
const partial = rows.planRevert(pending('update'), null);
|
|
156
|
+
expect(partial).toEqual({ tx: null, circuit: { table: 'thing', op: 'UPDATE', id: 'thing:1', record: {} }, revert: 'partial' });
|
|
157
|
+
});
|
|
158
|
+
it('failed rows: build, move tx, delete statements, parse', () => {
|
|
159
|
+
const failed = rows.buildFailedRow(pending('update'), { message: 'denied', kind: 'application' }, { id: 'thing:1' }, 2, 99, 'full');
|
|
160
|
+
expect(failed).toMatchObject({ id: '_00_pending_mutations:m', attempts: 2, failedAt: 99, revert: 'full', beforeRecord: { id: 'thing:1' } });
|
|
161
|
+
const { query, vars } = rows.moveToFailedTx(failed);
|
|
162
|
+
expect(query.sql).toBe('BEGIN TRANSACTION;\nCREATE ONLY $fid CONTENT $failed;DELETE $mid;\nCOMMIT TRANSACTION;');
|
|
163
|
+
expect(vars.fid).toEqual(rid('_00_failed_mutations', 'm'));
|
|
164
|
+
expect(vars.mid).toEqual(rid('_00_pending_mutations', 'm'));
|
|
165
|
+
expect((vars.failed as Record<string, unknown>).id).toBeUndefined();
|
|
166
|
+
expect(rows.deletePendingRow('_00_pending_mutations:m')).toEqual({ sql: 'DELETE $mid', vars: { mid: rid('_00_pending_mutations', 'm') } });
|
|
167
|
+
expect(rows.deleteFailedRow('_00_pending_mutations:m')).toEqual({ sql: 'DELETE $fid', vars: { fid: rid('_00_failed_mutations', 'm') } });
|
|
168
|
+
const parsed = rows.parseFailedRow({ ...failed, id: '_00_failed_mutations:⟨m⟩' });
|
|
169
|
+
expect(parsed).toMatchObject({ id: '_00_pending_mutations:m', mutationType: 'update', error: { message: 'denied', kind: 'application' }, revert: 'full' });
|
|
170
|
+
expect(rows.parseFailedRow({ id: '_00_failed_mutations:m', mutationType: 'delete', recordId: 'thing:2', error: { kind: 'unreplayable' }, revert: 'partial' })).toMatchObject({
|
|
171
|
+
tableName: 'thing',
|
|
172
|
+
error: { message: 'unknown', kind: 'unreplayable' },
|
|
173
|
+
attempts: 0,
|
|
174
|
+
createdAt: 0,
|
|
175
|
+
failedAt: 0,
|
|
176
|
+
revert: 'partial',
|
|
177
|
+
beforeRecord: null,
|
|
178
|
+
data: undefined,
|
|
179
|
+
});
|
|
180
|
+
expect(rows.parseFailedRow(null)).toBeNull();
|
|
181
|
+
expect(rows.parseFailedRow({ id: '_00_failed_mutations:m', mutationType: 'create', recordId: 'thing:1' })!.error).toEqual({ message: 'unknown', kind: 'application' });
|
|
182
|
+
expect(rows.parseFailedRow({ mutationType: 'nope', recordId: 'x' })).toBeNull();
|
|
183
|
+
expect(rows.parseFailedRow({ mutationType: 'delete', recordId: 1 })).toBeNull();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe('hydrateRowData', () => {
|
|
188
|
+
const columns = { owner: { recordId: true }, at: { dateTime: true }, n: {} } as any;
|
|
189
|
+
const base: rows.PendingMutationRow = { id: 'm', mutationType: 'create', recordId: 'thing:1', tableName: 'thing', createdAt: 0, v: 2 };
|
|
190
|
+
it('re-types record and datetime fields from the schema, keeps unknown keys, leaves rows without data or schema alone', () => {
|
|
191
|
+
const out = rows.hydrateRowData({ ...base, data: { owner: 'user:u1', at: '2026-01-02T00:00:00.000Z', n: 1, extra: 'x' } }, columns);
|
|
192
|
+
expect(out.data).toEqual({ owner: rid('user', 'u1'), at: new Date('2026-01-02T00:00:00.000Z'), n: 1, extra: 'x' });
|
|
193
|
+
expect(rows.hydrateRowData(base, columns)).toBe(base);
|
|
194
|
+
const noSchema = { ...base, data: { owner: 'user:u1' } };
|
|
195
|
+
expect(rows.hydrateRowData(noSchema, null)).toBe(noSchema);
|
|
196
|
+
});
|
|
197
|
+
it('sends a payload the schema rejects as it is', () => {
|
|
198
|
+
const bad = { ...base, data: { owner: 42 } };
|
|
199
|
+
expect(rows.hydrateRowData(bad, columns)).toBe(bad);
|
|
200
|
+
});
|
|
201
|
+
});
|