Skip to content

Commit e1430b0

Browse files
committed
fix(module-state): guard cancelled replication results, complete watchdog test mock
A watchdog-cancelled (or any cancelled) replication fires PouchDB's 'complete' event with no `docs` at all, crashing observePouchDbReplicate's result parsing with `Cannot read properties of undefined (reading 'map')`. Default to an empty array instead. Also add the missing `removeListener` mock to the watchdog test's hung replication stub - RxJS's teardown calls it for every event registered via `on()`, and its absence was throwing an UnsubscriptionError that left fake timers active and timed out the next test's afterEach hook.
1 parent fc578a7 commit e1430b0

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

packages/modules/state/src/__tests__/PouchDbSyncStorage.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('PouchDbSyncStorage', () => {
100100
// the exact failure mode the watchdog exists to recover from.
101101
const cancel = vi.fn();
102102
// biome-ignore lint/suspicious/noThenProperty: mocking PouchDB's Replication, which is genuinely thenable.
103-
const hungReplication = { on: vi.fn(), then: vi.fn(), cancel };
103+
const hungReplication = { on: vi.fn(), removeListener: vi.fn(), then: vi.fn(), cancel };
104104
const replicateFrom = vi
105105
.spyOn(localDb.replicate, 'from')
106106
.mockReturnValue(hungReplication as unknown as ReturnType<typeof localDb.replicate.from>);

packages/modules/state/src/storage/observe-pouch-db-replicate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export function observePouchDbReplicate<T extends AllowedValue = AllowedValue>(
2929
parse_docs: (doc: PouchDB.Core.ExistingDocument<{ value: T }>) => StorageItem<T>,
3030
): Observable<StateSyncEventType<T>> {
3131
const parse_replication_result = (change: PouchDB.Replication.ReplicationResult<{ value: T }>) => ({
32-
items: change.docs
32+
// A cancelled (e.g. watchdog-forced) replication fires 'complete' with no `docs` at all.
33+
items: (change.docs ?? [])
3334
// Convert replicated documents to the storage item shape expected by state events.
3435
.map((doc) => parse_docs(doc)),
3536
item_written: change.docs_written,

0 commit comments

Comments
 (0)