Skip to content

Commit

Permalink
waitForSyncWithServer shouldn't just wait
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn committed Oct 9, 2024
1 parent 7449eb5 commit 9aaeb37
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions test-e2e/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { valueOf } from '@comapeo/schema'
import { generate } from '@mapeo/mock-data'
import { execa } from 'execa'
import assert from 'node:assert/strict'
import test from 'node:test'
import { execa } from 'execa'
import { pEvent } from 'p-event'
import { MEMBER_ROLE_ID } from '../src/roles.js'
import createServer from '../src/server/app.js'
import {
Expand All @@ -15,6 +16,8 @@ import {
waitForSync,
} from './utils.js'
/** @import { MapeoManager } from '../src/mapeo-manager.js' */
/** @import { MapeoProject } from '../src/mapeo-project.js' */
/** @import { State as SyncState } from '../src/sync/sync-api.js' */

// TODO: test invalid base URL
// TODO: test bad requests
Expand Down Expand Up @@ -87,6 +90,15 @@ test('data can be synced via a server', async (t) => {
await managerAProject.$member.addServerPeer(serverBaseUrl, {
dangerouslyAllowInsecureConnections: true,
})
const serverDeviceIdPromise = managerAProject.$member
.getMany()
.then((members) => {
const serverMember = members.find(
(member) => member.deviceType === 'selfHostedServer'
)
assert(serverMember, 'Manager A must have a server member')
return serverMember.deviceId
})

// Add Manager B to project
const disconnectManagers = connectPeers(managers)
Expand Down Expand Up @@ -114,7 +126,8 @@ test('data can be synced via a server', async (t) => {
const observation = await managerAProject.observation.create(
valueOf(generate('observation')[0])
)
await waitForSyncWithServer()
const serverDeviceId = await serverDeviceIdPromise
await waitForSyncWithServer(managerAProject, serverDeviceId)
managerAProject.$sync.disconnectServers()
managerAProject.$sync.stop()
await assert.rejects(
Expand All @@ -125,7 +138,7 @@ test('data can be synced via a server', async (t) => {
// Manager B sees observation after syncing
managerBProject.$sync.connectServers()
managerBProject.$sync.start()
await waitForSyncWithServer()
await waitForSyncWithServer(managerBProject, serverDeviceId)
assert(
await managerBProject.observation.getByDocId(observation.docId),
'manager B now sees data'
Expand Down Expand Up @@ -212,9 +225,31 @@ function waitForNoPeersToBeConnected(manager) {
})
}

function waitForSyncWithServer() {
// TODO: This is fake!
return new Promise((resolve) => {
setTimeout(resolve, 3000)
})
/**
* @param {MapeoProject} project
* @param {string} serverDeviceId
* @returns {Promise<void>}
*/
async function waitForSyncWithServer(project, serverDeviceId) {
const initialState = project.$sync.getState()
if (isSyncedWithServer(initialState, serverDeviceId)) return
await pEvent(project.$sync, 'sync-state', (state) =>
isSyncedWithServer(state, serverDeviceId)
)
}

/**
* @param {SyncState} syncState
* @param {string} serverDeviceId
* @returns {boolean}
*/
function isSyncedWithServer(syncState, serverDeviceId) {
const serverSyncState = syncState.remoteDeviceSyncState[serverDeviceId]
return Boolean(
serverSyncState &&
serverSyncState.initial.want === 0 &&
serverSyncState.initial.wanted === 0 &&
serverSyncState.data.want === 0 &&
serverSyncState.data.wanted === 0
)
}

0 comments on commit 9aaeb37

Please sign in to comment.