From a8cadd1e5366b7387749e5f9e6ed76a33931f38f Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sat, 22 Mar 2025 20:50:43 +0000 Subject: [PATCH] Fix test --- frontend/sync-client/src/index.ts | 2 +- frontend/sync-client/src/sync-client.ts | 3 +- .../sync-client/src/sync-operations/syncer.ts | 31 +++++++------------ frontend/test-client/src/agent/mock-client.ts | 15 ++++++++- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/frontend/sync-client/src/index.ts b/frontend/sync-client/src/index.ts index cb8a38a2..d40b5aab 100644 --- a/frontend/sync-client/src/index.ts +++ b/frontend/sync-client/src/index.ts @@ -7,7 +7,7 @@ export { export { Logger, LogLevel, LogLine } from "./tracing/logger"; export type { CheckConnectionResult } from "./services/sync-service"; export { type SyncSettings } from "./persistence/settings"; -export type { RelativePath } from "./persistence/database"; +export type { RelativePath, StoredDatabase } from "./persistence/database"; export type { FileSystemOperations } from "./file-operations/filesystem-operations"; export type { PersistenceProvider } from "./persistence/persistence"; diff --git a/frontend/sync-client/src/sync-client.ts b/frontend/sync-client/src/sync-client.ts index 93998d9d..ceadd8cf 100644 --- a/frontend/sync-client/src/sync-client.ts +++ b/frontend/sync-client/src/sync-client.ts @@ -173,8 +173,7 @@ export class SyncClient { } public async waitAndStop(): Promise { - await this.syncer.applyRemoteChangesLocally(); - await this.syncer.waitForSyncQueue(); + await this.syncer.waitUntilFinished(); this.stop(); } diff --git a/frontend/sync-client/src/sync-operations/syncer.ts b/frontend/sync-client/src/sync-operations/syncer.ts index bec932a0..1c3b064c 100644 --- a/frontend/sync-client/src/sync-operations/syncer.ts +++ b/frontend/sync-client/src/sync-operations/syncer.ts @@ -1,7 +1,6 @@ import type { Database, RelativePath } from "../persistence/database"; import type { SyncService } from "../services/sync-service"; import type { Logger } from "../tracing/logger"; -import type { SyncHistory } from "../tracing/sync-history"; import PQueue from "p-queue"; import { hash } from "../utils/hash"; import { v4 as uuidv4 } from "uuid"; @@ -9,7 +8,7 @@ import type { components } from "../services/types"; import type { Settings } from "../persistence/settings"; import type { FileOperations } from "../file-operations/file-operations"; import { findMatchingFile } from "../utils/find-matching-file"; -import { UnrestrictedSyncer } from "./unrestricted-syncer"; +import type { UnrestrictedSyncer } from "./unrestricted-syncer"; import { createPromise } from "../utils/create-promise"; import { SyncResetError } from "../services/sync-reset-error"; @@ -17,21 +16,18 @@ export class Syncer { private readonly remainingOperationsListeners: (( remainingOperations: number ) => void)[] = []; - private readonly syncQueue: PQueue; private runningScheduleSyncForOfflineChanges: Promise | undefined; private runningApplyRemoteChangesLocally: Promise | undefined; - private readonly internalSyncer: UnrestrictedSyncer; - public constructor( private readonly logger: Logger, private readonly database: Database, settings: Settings, private readonly syncService: SyncService, private readonly operations: FileOperations, - history: SyncHistory + private readonly internalSyncer: UnrestrictedSyncer ) { this.syncQueue = new PQueue({ concurrency: settings.getSettings().syncConcurrency @@ -49,15 +45,6 @@ export class Syncer { listener(this.syncQueue.size); }); }); - - this.internalSyncer = new UnrestrictedSyncer( - logger, - database, - settings, - syncService, - operations, - history - ); } public addRemainingOperationsListener( @@ -246,13 +233,17 @@ export class Syncer { } } - public async waitForSyncQueue(): Promise { - return this.syncQueue.onEmpty(); + public async reset(): Promise { + await this.waitUntilFinished(); + this.internalSyncer.reset(); } - public async reset(): Promise { - await this.syncQueue.onEmpty(); - this.internalSyncer.reset(); + public async waitUntilFinished(): Promise { + await Promise.allSettled([ + this.runningScheduleSyncForOfflineChanges, + this.runningApplyRemoteChangesLocally + ]); + return this.syncQueue.onEmpty(); } private async internalApplyRemoteChangesLocally(): Promise { diff --git a/frontend/test-client/src/agent/mock-client.ts b/frontend/test-client/src/agent/mock-client.ts index 793c3775..3913bb14 100644 --- a/frontend/test-client/src/agent/mock-client.ts +++ b/frontend/test-client/src/agent/mock-client.ts @@ -1,3 +1,4 @@ +import type { StoredDatabase } from "sync-client/dist/types/persistence/database"; import { assert } from "../utils/assert"; import { type RelativePath, @@ -9,7 +10,17 @@ import { export class MockClient implements FileSystemOperations { protected readonly localFiles = new Map(); protected client!: SyncClient; - protected data: object | undefined = undefined; + + protected data: Partial<{ + settings: Partial; + database: Partial; + }> = { + database: { + // Assume all clients start at the same time so there's no need to fetch + // any shared state. + hasInitialSyncCompleted: true + } + }; public constructor( private readonly initialSettings: Partial, @@ -37,6 +48,8 @@ export class MockClient implements FileSystemOperations { ); }) ); + + await this.client.start(); } public async listAllFiles(): Promise {