Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class SyncStore {
const existingMaxCount = this.#perDeviceMaxSyncCount.get(deviceId)

if (typeof existingMaxCount === 'number' && existingMaxCount > 0) {
currentSyncCount = getDataSyncCountForDevice(deviceSyncState)
currentSyncCount += getDataSyncCountForDevice(deviceSyncState)
totalMaxSyncCount += existingMaxCount
}
}
Expand Down
67 changes: 67 additions & 0 deletions test/lib/sync.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @vitest-environment node
*/
import type { ComapeoProjectClientApi } from '@comapeo/ipc'
import { describe, expect, it } from 'vitest'

import { SyncStore, type SyncState } from '../../src/lib/sync.js'

type DeviceCounts = Record<string, { want: number; wanted: number }>

function createSyncState(devices: DeviceCounts): SyncState {
const remoteDeviceSyncState: SyncState['remoteDeviceSyncState'] = {}
for (const [deviceId, counts] of Object.entries(devices)) {
remoteDeviceSyncState[deviceId] = {
initial: { isSyncEnabled: true, want: 0, wanted: 0 },
data: { isSyncEnabled: true, ...counts },
}
}
return {
initial: { isSyncEnabled: true },
data: { isSyncEnabled: true },
remoteDeviceSyncState,
}
}

function createFakeProject(initialState: SyncState) {
let listener: ((state: SyncState) => void) | undefined
const project = {
$sync: {
getState: async () => initialState,
on: (_event: 'sync-state', fn: (state: SyncState) => void) => {
listener = fn
},
off: () => {
listener = undefined
},
},
}
return {
project: project as unknown as ComapeoProjectClientApi,
emitState: (state: SyncState) => listener?.(state),
}
}

describe('SyncStore data progress', () => {
it('is not complete while any syncing device still has blocks to exchange', async () => {
const { project, emitState } = createFakeProject(
createSyncState({
cloud: { want: 5, wanted: 0 },
phone: { want: 5, wanted: 0 },
}),
)
const store = new SyncStore(project)
const unsubscribe = store.subscribe(() => {})
await expect.poll(() => store.getStateSnapshot()).not.toBeNull()

emitState(
createSyncState({
cloud: { want: 5, wanted: 0 },
phone: { want: 0, wanted: 0 },
}),
)

expect(store.getDataProgressSnapshot()).toBe(0.5)
unsubscribe()
})
})
Loading