Integrate systemd CLI installer fix with current main

This commit is contained in:
vorotamoroz
2026-09-02 11:43:59 +00:00
138 changed files with 8751 additions and 4714 deletions
+39 -20
View File
@@ -1,7 +1,11 @@
import { LOG_LEVEL_INFO } from "octagonal-wheels/common/logger";
import type PouchDB from "pouchdb-core";
import type { SimpleStore } from "octagonal-wheels/databases/SimpleStoreBase";
import type { HasSettings, ObsidianLiveSyncSettings, EntryDoc } from "@vrtmrz/livesync-commonlib/compat/common/types";
import {
type HasSettings,
type ObsidianLiveSyncSettings,
type EntryDoc,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { __$checkInstanceBinding } from "@vrtmrz/livesync-commonlib/compat/dev/checks";
import type { Confirm } from "@vrtmrz/livesync-commonlib/compat/interfaces/Confirm";
import type { DatabaseFileAccess } from "@vrtmrz/livesync-commonlib/compat/interfaces/DatabaseFileAccess";
@@ -11,17 +15,13 @@ import type { StorageAccess } from "@vrtmrz/livesync-commonlib/compat/interfaces
import type { LiveSyncLocalDBEnv } from "@vrtmrz/livesync-commonlib/compat/pouchdb/LiveSyncLocalDB";
import type { LiveSyncCouchDBReplicatorEnv } from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import type { CheckPointInfo } from "@vrtmrz/livesync-commonlib/compat/replication/journal/JournalSyncTypes";
import type { LiveSyncJournalReplicatorEnv } from "@vrtmrz/livesync-commonlib/compat/replication/journal/LiveSyncJournalReplicatorEnv";
import type { LiveSyncReplicatorEnv } from "@vrtmrz/livesync-commonlib/compat/replication/LiveSyncAbstractReplicator";
import type { LiveSyncAbstractReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/LiveSyncAbstractReplicator";
import type { ReplicatorInstance } from "@vrtmrz/livesync-commonlib/replication";
import { useTargetFilters } from "@vrtmrz/livesync-commonlib/compat/serviceFeatures/targetFilter";
import { useRemoteConfigurationMigration } from "@vrtmrz/livesync-commonlib/compat/serviceFeatures/remoteConfig";
import type { ServiceContext } from "@vrtmrz/livesync-commonlib/context";
import type { InjectableServiceHub } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableServiceHub";
import { AbstractModule } from "./modules/AbstractModule";
import { ModulePeriodicProcess } from "./modules/core/ModulePeriodicProcess";
import { ModuleReplicator } from "./modules/core/ModuleReplicator";
import { ModuleReplicatorCouchDB } from "./modules/core/ModuleReplicatorCouchDB";
import { ModuleReplicatorMinIO } from "./modules/core/ModuleReplicatorMinIO";
import { ModuleConflictChecker } from "./modules/coreFeatures/ModuleConflictChecker";
import { ModuleConflictResolver } from "./modules/coreFeatures/ModuleConflictResolver";
import { ModuleResolvingMismatchedTweaks } from "./modules/coreFeatures/ModuleResolveMismatchedTweaks";
@@ -30,6 +30,16 @@ import type { ServiceModules } from "@vrtmrz/livesync-commonlib/compat/interface
import { ModuleBasicMenu } from "./modules/essential/ModuleBasicMenu";
import { usePrepareDatabaseForUse } from "@vrtmrz/livesync-commonlib/compat/serviceFeatures/prepareDatabaseForUse";
import type { Constructor } from "@vrtmrz/livesync-commonlib/compat/common/utils.type";
import { useReplicationScheduling, type ReplicationSchedulingControl } from "./serviceFeatures/replicationScheduling";
import { createCentralReplicatorProviderDefinitions } from "./common/replicatorProviders";
import { useReplicationFeature } from "./serviceFeatures/replication";
/** Focused views returned by serviceFeatures which the host may consume during composition. */
export interface LiveSyncCoreFeatureViews {
readonly replicationScheduling: ReplicationSchedulingControl;
}
type CompatibilityReplicatorView = ReplicatorInstance & Partial<LiveSyncAbstractReplicator>;
export class LiveSyncBaseCore<
T extends ServiceContext = ServiceContext,
@@ -37,8 +47,6 @@ export class LiveSyncBaseCore<
>
implements
LiveSyncLocalDBEnv,
LiveSyncReplicatorEnv,
LiveSyncJournalReplicatorEnv,
LiveSyncCouchDBReplicatorEnv,
HasSettings<ObsidianLiveSyncSettings>
{
@@ -74,18 +82,22 @@ export class LiveSyncBaseCore<
) => ServiceModules,
extraModuleInitialiser: (core: LiveSyncBaseCore<T, TCommands>) => AbstractModule[],
addOnsInitialiser: (core: LiveSyncBaseCore<T, TCommands>) => TCommands[],
featuresInitialiser: (core: LiveSyncBaseCore<T, TCommands>) => void
featuresInitialiser: (core: LiveSyncBaseCore<T, TCommands>, coreFeatureViews: LiveSyncCoreFeatureViews) => void
) {
this._services = serviceHub;
this.registerReplicatorProviders();
this._serviceModules = serviceModuleInitialiser(this, serviceHub);
const extraModules = extraModuleInitialiser(this);
this.registerModules(extraModules);
this.initialiseServiceFeatures();
featuresInitialiser(this);
const coreFeatureViews = this.initialiseServiceFeatures();
featuresInitialiser(this, coreFeatureViews);
const addOns = addOnsInitialiser(this);
for (const addOn of addOns) {
this._registerAddOn(addOn);
}
// Register host features and add-ons before replication, then bind
// legacy modules so lifecycle handlers observe the required order.
useReplicationFeature(this);
this.bindModuleFunctions();
}
/**
@@ -136,14 +148,17 @@ export class LiveSyncBaseCore<
this.modules.push(module);
}
/** Compose the current central providers before any lifecycle event can acquire one. */
private registerReplicatorProviders() {
this.services.replicator.registerReplicatorProviderDefinitions(
createCentralReplicatorProviderDefinitions(this)
);
}
public registerModules(extraModules: AbstractModule[] = []) {
this._registerModule(new ModuleLiveSyncMain(this));
this._registerModule(new ModuleConflictChecker(this));
this._registerModule(new ModuleReplicatorMinIO(this));
this._registerModule(new ModuleReplicatorCouchDB(this));
this._registerModule(new ModuleReplicator(this));
this._registerModule(new ModuleConflictResolver(this));
this._registerModule(new ModulePeriodicProcess(this));
this._registerModule(new ModuleResolvingMismatchedTweaks(this));
this._registerModule(new ModuleBasicMenu(this));
@@ -223,10 +238,11 @@ export class LiveSyncBaseCore<
}
/**
* @obsolete Use services.replication.getActiveReplicator instead. Get the active replicator instance. Note that there can be multiple replicators, but only one can be active at a time.
* @obsolete Use the provider context or a focused service operation instead.
* Provider-specific members on this compatibility view are optional.
*/
get replicator() {
return this.services.replicator.getActiveReplicator()!;
get replicator(): CompatibilityReplicatorView {
return this.services.replicator.getActiveReplicator() as CompatibilityReplicatorView;
}
/**
@@ -273,12 +289,15 @@ export class LiveSyncBaseCore<
* Initialise ServiceFeatures.
* (Please refer `serviceFeatures` for more details)
*/
initialiseServiceFeatures() {
initialiseServiceFeatures(): LiveSyncCoreFeatureViews {
useTargetFilters(this);
// enable target filter feature.
usePrepareDatabaseForUse(this);
// Migration to multiple remote configurations
useRemoteConfigurationMigration(this);
return Object.freeze({
replicationScheduling: useReplicationScheduling(this),
});
}
}
+7
View File
@@ -71,6 +71,9 @@ livesync-cli [database-path] [command] [args...]
- `init-settings` writes its target file. `setup`, `remote-add`, `remote-rm`, `remote-set`, and `remote-activate` write their settings changes without this option.
- All remaining commands leave the settings file unchanged by default.
- Temporary values used to suspend synchronisation or select a remote for one command are never written.
- `--compat-remote-admin-exit-zero`: Preserve the former zero exit code when `mark-resolved`, `lock-remote`, or `unlock-remote` returns a provider verification failure.
- Without this option, those commands return a non-zero exit code when verification fails.
- Invalid arguments, unknown remote IDs, and errors thrown while activating or mutating the remote remain errors with or without this option.
### Commands
@@ -96,6 +99,8 @@ livesync-cli [database-path] [command] [args...]
- `remote-status [remote-id]`: Show remote database status.
- `init-settings [file]`: Create a default settings file.
Remote-administration commands verify the resulting milestone state through the selected provider. The existing `[Verification]` lines remain suitable for scripts which inspect command output, while the default exit code now reflects whether that verification succeeded.
### Examples
```bash
@@ -338,6 +343,8 @@ Options:
--interval <N>, -i <N> (daemon only) Poll CouchDB every N seconds instead of using the _changes feed
--vault <path>, -V <path> (daemon/mirror) Path to vault directory, decoupled from database-path
--write-settings Write setting changes after a successful command
--compat-remote-admin-exit-zero
Preserve the former zero exit code when remote-administration verification fails
--help, -h Show this help message
Commands:
@@ -0,0 +1,137 @@
import type { StandardIo } from "@vrtmrz/livesync-commonlib/context";
import {
CENTRAL_REMOTE_ADMINISTRATION_ACTIONS,
CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS,
CENTRAL_REMOTE_ADMINISTRATION_OBSERVATION_KINDS,
isCentralRemoteAdministrationVerified,
type CentralRemoteAdministrationAction,
type CentralRemoteAdministrationResult,
} from "@vrtmrz/livesync-commonlib/replication";
import { activateRemoteConfiguration } from "@vrtmrz/livesync-commonlib/remote-configurations";
import { writeStderrLine } from "@/apps/cli/cliOutput";
import type { CLICommand, CLICommandContext, CLIOptions } from "./types";
const CENTRAL_REMOTE_ADMINISTRATION_ACTION_BY_COMMAND = Object.freeze({
"mark-resolved": CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.MARK_RESOLVED,
"lock-remote": CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.LOCK,
"unlock-remote": CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.UNLOCK,
} as const satisfies Partial<Record<CLICommand, CentralRemoteAdministrationAction>>);
export type CentralRemoteAdministrationCommand = keyof typeof CENTRAL_REMOTE_ADMINISTRATION_ACTION_BY_COMMAND;
/** Return whether a CLI command belongs to the central-remote administration category. */
export function isCentralRemoteAdministrationCommand(
command: CLICommand
): command is CentralRemoteAdministrationCommand {
return Object.prototype.hasOwnProperty.call(CENTRAL_REMOTE_ADMINISTRATION_ACTION_BY_COMMAND, command);
}
function detailMessage(detail: unknown): string {
return detail instanceof Error ? detail.message : String(detail);
}
function assertNeverCentralRemoteAdministrationFailureReason(reason: never): never {
throw new Error(`Unexpected central remote administration failure reason: ${String(reason)}`);
}
function reportMilestoneObservation(
standardIo: StandardIo,
observation: Extract<
CentralRemoteAdministrationResult["observation"],
{ kind: typeof CENTRAL_REMOTE_ADMINISTRATION_OBSERVATION_KINDS.MILESTONE }
>
): void {
standardIo.writeStderr(`[Verification] Remote Database: ${observation.locked ? "LOCKED" : "UNLOCKED"}\n`);
standardIo.writeStderr(
`[Verification] Current Device Node ID (${observation.nodeId}): ${observation.accepted ? "ACCEPTED" : "NOT ACCEPTED"}\n`
);
}
/** Map typed provider observations to the CLI's established verification output. */
function reportCentralRemoteAdministrationResult(
standardIo: StandardIo,
result: CentralRemoteAdministrationResult
): void {
if (result.observation?.kind === CENTRAL_REMOTE_ADMINISTRATION_OBSERVATION_KINDS.MILESTONE) {
reportMilestoneObservation(standardIo, result.observation);
return;
}
if (isCentralRemoteAdministrationVerified(result)) {
return;
}
const reason = result.reason;
switch (reason) {
case CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.NO_ACTIVE_REPLICATOR:
standardIo.writeStderr("[Verification] No active replicator found\n");
return;
case CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.CONNECTION_FAILED:
standardIo.writeStderr(
`[Verification] Failed to connect to the configured remote: ${detailMessage(result.detail)}\n`
);
return;
case CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.ACTIVE_CONFIGURATION_MISMATCH:
standardIo.writeStderr(
"[Verification] The active remote configuration changed before remote administration could begin.\n"
);
return;
case CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.MILESTONE_NOT_FOUND:
standardIo.writeStderr("[Verification] Milestone document not found on remote.\n");
return;
case CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.MILESTONE_READ_FAILED:
standardIo.writeStderr(
`[Verification] Failed to fetch milestone document: ${detailMessage(result.detail)}\n`
);
return;
case CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.LOCAL_IDENTITY_UNAVAILABLE:
standardIo.writeStderr("[Verification] Failed to initialise the current device identity.\n");
return;
case CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.CAPABILITY_NOT_IMPLEMENTED:
case CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.CAPABILITY_NOT_APPLICABLE:
standardIo.writeStderr("[Verification] Remote administration is unavailable for this provider.\n");
return;
case CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.POSTCONDITION_MISMATCH:
standardIo.writeStderr("[Verification] The requested remote state was not observed.\n");
return;
default:
return assertNeverCentralRemoteAdministrationFailureReason(reason);
}
}
/**
* Apply one provider-owned mutation and map its typed verification to CLI exit policy.
* Mutation exceptions deliberately escape this boundary.
*/
export async function runCentralRemoteAdministrationCommand(
options: CLIOptions,
context: CLICommandContext,
command: CentralRemoteAdministrationCommand
): Promise<boolean> {
const id = options.commandArgs[0]?.trim();
if (id) {
let switched = false;
await context.core.services.setting.updateSettings((currentSettings) => {
const activated = activateRemoteConfiguration(currentSettings, id);
if (activated) {
switched = true;
return activated;
}
return currentSettings;
}, false);
if (!switched) {
context.core.services.context.standardIo.writeStderr(
`[Info] Failed to temporarily activate remote configuration: ${id}\n`
);
return false;
}
await context.core.services.control.applySettings();
}
writeStderrLine(context.core.services.context.standardIo, `[Command] ${command}${id ? ` ${id}` : ""}`);
const action = CENTRAL_REMOTE_ADMINISTRATION_ACTION_BY_COMMAND[command];
const result = await context.core.services.replicator.runCentralRemoteAdministration({ action });
reportCentralRemoteAdministrationResult(context.core.services.context.standardIo, result);
return isCentralRemoteAdministrationVerified(result) || options.compatRemoteAdminExitZero === true;
}
@@ -1,5 +1,6 @@
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { createServiceContext } from "@vrtmrz/livesync-commonlib/context";
import { NO_INTERACTION } from "@vrtmrz/livesync-commonlib/replication";
import { runCommand } from "./runCommand";
import type { CLIOptions } from "./types";
@@ -38,7 +39,7 @@ function createCoreMock() {
currentSettings: vi.fn(() => ({ liveSync: true, syncOnStart: false })),
},
replication: {
replicate: vi.fn(async () => true),
replicateUnattended: vi.fn(async () => ({ status: "completed" as const })),
},
appLifecycle: {
onUnload: {
@@ -87,6 +88,17 @@ const baseContext = {
},
} as any;
function createDaemonContext(core: ReturnType<typeof createCoreMock>) {
return {
...baseContext,
core,
replicationScheduling: {
setExternalPollingMode: vi.fn(),
markInitialOneShotSatisfied: vi.fn(),
},
} as any;
}
describe("daemon command", () => {
beforeEach(() => {
vi.restoreAllMocks();
@@ -101,7 +113,7 @@ describe("daemon command", () => {
const core = createCoreMock();
vi.mocked(offlineScanner.performFullScan).mockResolvedValue(true);
await runCommand(makeDaemonOptions(), { ...baseContext, core });
await runCommand(makeDaemonOptions(), createDaemonContext(core));
expect(offlineScanner.performFullScan).toHaveBeenCalledTimes(1);
});
@@ -110,7 +122,7 @@ describe("daemon command", () => {
const core = createCoreMock();
vi.mocked(offlineScanner.performFullScan).mockResolvedValue(false);
const result = await runCommand(makeDaemonOptions(), { ...baseContext, core });
const result = await runCommand(makeDaemonOptions(), createDaemonContext(core));
expect(result).toBe(false);
});
@@ -120,9 +132,11 @@ describe("daemon command", () => {
vi.mocked(offlineScanner.performFullScan).mockResolvedValue(true);
const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout");
await runCommand(makeDaemonOptions(30), { ...baseContext, core });
const context = createDaemonContext(core);
await runCommand(makeDaemonOptions(30), context);
expect(setTimeoutSpy).toHaveBeenCalledTimes(1);
expect(context.replicationScheduling.setExternalPollingMode).toHaveBeenCalledWith(true);
// Interval should be in milliseconds (30s → 30000ms)
expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), 30000);
});
@@ -131,7 +145,7 @@ describe("daemon command", () => {
const core = createCoreMock();
vi.mocked(offlineScanner.performFullScan).mockResolvedValue(true);
await runCommand(makeDaemonOptions(10), { ...baseContext, core });
await runCommand(makeDaemonOptions(10), createDaemonContext(core));
expect(core.services.setting.applyPartial).toHaveBeenCalledWith(
expect.objectContaining({ suspendFileWatching: false }),
@@ -144,7 +158,7 @@ describe("daemon command", () => {
const core = createCoreMock();
vi.mocked(offlineScanner.performFullScan).mockResolvedValue(true);
await runCommand(makeDaemonOptions(), { ...baseContext, core });
await runCommand(makeDaemonOptions(), createDaemonContext(core));
expect(core.services.setting.applyPartial).toHaveBeenCalledWith(
expect.objectContaining({
@@ -164,7 +178,7 @@ describe("daemon command", () => {
}));
vi.mocked(offlineScanner.performFullScan).mockResolvedValue(true);
const result = await runCommand(makeDaemonOptions(), { ...baseContext, core });
const result = await runCommand(makeDaemonOptions(), createDaemonContext(core));
expect(result).toBe(true);
const warningCalls = core.services.context.standardIo.writeStderr.mock.calls.filter(
@@ -182,7 +196,7 @@ describe("daemon command", () => {
}));
vi.mocked(offlineScanner.performFullScan).mockResolvedValue(true);
await runCommand(makeDaemonOptions(), { ...baseContext, core });
await runCommand(makeDaemonOptions(), createDaemonContext(core));
const warningCalls = core.services.context.standardIo.writeStderr.mock.calls.filter(
([chunk]: [string | Uint8Array]) =>
@@ -194,37 +208,50 @@ describe("daemon command", () => {
it("calls replicate before performFullScan", async () => {
const core = createCoreMock();
const callOrder: string[] = [];
core.services.replication.replicate = vi.fn(async () => {
core.services.replication.replicateUnattended = vi.fn(async () => {
callOrder.push("replicate");
return true;
return { status: "completed" as const };
});
vi.mocked(offlineScanner.performFullScan).mockImplementation(async () => {
callOrder.push("performFullScan");
return true;
});
await runCommand(makeDaemonOptions(), { ...baseContext, core });
const context = createDaemonContext(core);
await runCommand(makeDaemonOptions(), context);
expect(callOrder).toEqual(["replicate", "performFullScan"]);
expect(core.services.replication.replicateUnattended).toHaveBeenCalledWith({
trigger: "daemon",
interaction: NO_INTERACTION,
});
expect(context.replicationScheduling.markInitialOneShotSatisfied).toHaveBeenCalledOnce();
});
it("returns false when initial replication fails", async () => {
const core = createCoreMock();
core.services.replication.replicate = vi.fn(async () => false);
core.services.replication.replicateUnattended = vi.fn(async () => ({
status: "failed" as const,
error: new Error("initial replication failed"),
}));
vi.mocked(offlineScanner.performFullScan).mockClear();
const result = await runCommand(makeDaemonOptions(), { ...baseContext, core });
const result = await runCommand(makeDaemonOptions(), createDaemonContext(core));
expect(result).toBe(false);
// performFullScan should NOT have been called
expect(offlineScanner.performFullScan).not.toHaveBeenCalled();
expect(core.services.replication.replicateUnattended).toHaveBeenCalledWith({
trigger: "daemon",
interaction: NO_INTERACTION,
});
});
it("polling mode: registers onUnload handler that clears timeout", async () => {
const core = createCoreMock();
vi.mocked(offlineScanner.performFullScan).mockResolvedValue(true);
await runCommand(makeDaemonOptions(10), { ...baseContext, core });
await runCommand(makeDaemonOptions(10), createDaemonContext(core));
// onUnload handler should have been registered
expect(core.services.appLifecycle.onUnload.addHandler).toHaveBeenCalledTimes(1);
@@ -242,17 +269,17 @@ describe("daemon command", () => {
// startup replicate (call 1) succeeds; poll calls 27 fail; call 8 succeeds.
let callCount = 0;
core.services.replication.replicate = vi.fn(async () => {
core.services.replication.replicateUnattended = vi.fn(async () => {
callCount++;
if (callCount === 1) return true; // initial startup replicate
if (callCount === 1) return { status: "completed" as const }; // initial startup replicate
if (callCount <= 7) throw new Error("network failure");
return true; // recovery
return { status: "completed" as const }; // recovery
});
const baseMs = 30 * 1000;
const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout");
await runCommand(makeDaemonOptions(30), { ...baseContext, core });
await runCommand(makeDaemonOptions(30), createDaemonContext(core));
// After runCommand returns the first setTimeout has been scheduled.
// setTimeoutSpy.mock.calls[0] is the initial schedule (baseMs).
@@ -297,14 +324,14 @@ describe("daemon command", () => {
// Make replicate succeed on the initial call (startup), then fail on the poll.
let callCount = 0;
core.services.replication.replicate = vi.fn(async () => {
core.services.replication.replicateUnattended = vi.fn(async () => {
callCount++;
if (callCount === 1) return true; // startup replicate
if (callCount === 1) return { status: "completed" as const }; // startup replicate
throw new Error("network failure");
});
const intervalMs = 30 * 1000;
await runCommand(makeDaemonOptions(30), { ...baseContext, core });
await runCommand(makeDaemonOptions(30), createDaemonContext(core));
// Advance time to trigger the first poll callback and flush its async work.
await vi.advanceTimersByTimeAsync(intervalMs);
+55 -65
View File
@@ -1,10 +1,9 @@
import type { LiveSyncBaseCore } from "@/LiveSyncBaseCore";
import { P2P_DEFAULT_SETTINGS } from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { ServiceContext } from "@vrtmrz/livesync-commonlib/context";
import { LiveSyncTrysteroReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/LiveSyncTrysteroReplicator";
import { compatGlobal } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions";
import { LiveSyncError } from "@vrtmrz/livesync-commonlib/compat/common/LSError";
import { getPeerConnectionStats } from "@vrtmrz/livesync-commonlib/compat/rpc/transports/DiagRTCPeerConnections.utils";
import type { P2PPeerConnectionMetrics, P2PServiceViews } from "@vrtmrz/livesync-commonlib/p2p";
import { fsPromises } from "@vrtmrz/livesync-commonlib/node";
type CLIP2PPeer = {
@@ -12,17 +11,13 @@ type CLIP2PPeer = {
name: string;
};
type CandidateSummary = {
id: string;
candidateType: string;
protocol: string;
relayProtocol: string;
};
type CLIP2PService = Pick<P2PServiceViews, "transportLifecycle" | "peerDirectory" | "targetedTransfer" | "diagnostics">;
function delay(ms: number): Promise<void> {
return new Promise((resolve) => compatGlobal.setTimeout(resolve, ms));
}
/** Parse a CLI timeout expressed as a finite, non-negative number of seconds. */
export function parseTimeoutSeconds(value: string, commandName: string): number {
const timeoutSec = Number(value);
if (!Number.isFinite(timeoutSec) || timeoutSec < 0) {
@@ -43,35 +38,36 @@ function validateP2PSettings(core: LiveSyncBaseCore<ServiceContext, never>) {
settings.P2P_IsHeadless = true;
}
async function createReplicator(core: LiveSyncBaseCore<ServiceContext, never>): Promise<LiveSyncTrysteroReplicator> {
function requireP2PService(
core: LiveSyncBaseCore<ServiceContext, never>,
service: CLIP2PService | undefined
): CLIP2PService {
validateP2PSettings(core);
const replicator = await core.services.replicator.getNewReplicator();
if (!replicator) {
throw new Error("Failed to create replicator instance. Ensure P2P is enabled in settings.");
if (!service) {
throw new Error("P2P service is not available. Ensure the P2P feature was composed for this CLI process.");
}
if (!(replicator instanceof LiveSyncTrysteroReplicator)) {
throw new Error("Unexpected replicator type. Expected LiveSyncTrysteroReplicator.");
}
return replicator;
return service;
}
function getSortedPeers(replicator: LiveSyncTrysteroReplicator): CLIP2PPeer[] {
return [...replicator.knownAdvertisements]
function getSortedPeers(service: Pick<P2PServiceViews, "peerDirectory">): CLIP2PPeer[] {
return [...service.peerDirectory.getPeers()]
.map((peer) => ({ peerId: peer.peerId, name: peer.name }))
.sort((a, b) => a.peerId.localeCompare(b.peerId));
}
/** Connect for a bounded discovery interval, return a stable peer ordering, and disconnect. */
export async function collectPeers(
core: LiveSyncBaseCore<ServiceContext, never>,
p2pService: CLIP2PService | undefined,
timeoutSec: number
): Promise<CLIP2PPeer[]> {
const replicator = await createReplicator(core);
await replicator.open();
const service = requireP2PService(core, p2pService);
await service.transportLifecycle.connect();
try {
await delay(timeoutSec * 1000);
return getSortedPeers(replicator);
return getSortedPeers(service);
} finally {
await replicator.close();
await service.transportLifecycle.disconnect();
}
}
@@ -90,32 +86,8 @@ function resolvePeer(peers: CLIP2PPeer[], peerToken: string): CLIP2PPeer | undef
return undefined;
}
function getReportValue<T extends string | number>(
report: Record<string, unknown> | undefined,
key: string
): T | "unknown" {
const value = report?.[key];
return typeof value === "string" || typeof value === "number" ? (value as T) : "unknown";
}
function summariseCandidate(reports: unknown[], candidateId: string): CandidateSummary | undefined {
if (candidateId === "unknown") {
return undefined;
}
const report = reports.map((r) => r as Record<string, unknown>).find((r) => r.id === candidateId);
if (!report) {
return undefined;
}
return {
id: candidateId,
candidateType: getReportValue<string>(report, "candidateType"),
protocol: getReportValue<string>(report, "protocol"),
relayProtocol: getReportValue<string>(report, "relayProtocol"),
};
}
async function writePeerConnectionStatsIfRequested(
replicator: LiveSyncTrysteroReplicator,
service: Pick<P2PServiceViews, "diagnostics">,
peer: CLIP2PPeer
): Promise<void> {
const outputPath = process.env.LIVESYNC_P2P_STATS_JSONL?.trim();
@@ -123,21 +95,30 @@ async function writePeerConnectionStatsIfRequested(
return;
}
const peerConnection = replicator.rawHost?.room?.getPeers()[peer.peerId];
const stats = peerConnection ? await getPeerConnectionStats(`cli-p2p-${peer.peerId}`, peerConnection) : undefined;
const localCandidate = summariseCandidate(stats?.reports ?? [], stats?.localCandidateId ?? "unknown");
const remoteCandidate = summariseCandidate(stats?.reports ?? [], stats?.remoteCandidateId ?? "unknown");
const stats = await service.diagnostics.getPeerConnectionMetrics(peer.peerId);
const payload = createPeerConnectionStatsPayload(peer, stats, new Date().toISOString());
await fsPromises.appendFile(outputPath, `${JSON.stringify(payload)}\n`, "utf8");
}
/** Build the stable JSONL record consumed by the P2P benchmark harnesses. */
export function createPeerConnectionStatsPayload(
peer: CLIP2PPeer,
stats: P2PPeerConnectionMetrics | undefined,
generatedAt: string
) {
const localCandidate = stats?.localCandidate;
const remoteCandidate = stats?.remoteCandidate;
const selectedPath =
localCandidate && remoteCandidate
? `${localCandidate.candidateType}<->${remoteCandidate.candidateType}`
: "unknown";
const payload = {
generatedAt: new Date().toISOString(),
generatedAt,
command: "p2p-sync",
peerId: peer.peerId,
peerName: peer.name,
candidatePathCollected: !!stats?.selectedPair,
candidatePathCollected: stats?.selectedPairPresent ?? false,
selectedPath,
selectedPair: stats
? {
@@ -155,23 +136,25 @@ async function writePeerConnectionStatsIfRequested(
localCandidate,
remoteCandidate,
};
await fsPromises.appendFile(outputPath, `${JSON.stringify(payload)}\n`, "utf8");
return payload;
}
/** Resolve one peer token, complete pull then push, and disconnect on every settlement. */
export async function syncWithPeer(
core: LiveSyncBaseCore<ServiceContext, never>,
p2pService: CLIP2PService | undefined,
peerToken: string,
timeoutSec: number
): Promise<CLIP2PPeer> {
const replicator = await createReplicator(core);
await replicator.open();
const service = requireP2PService(core, p2pService);
await service.transportLifecycle.connect();
try {
const timeoutMs = timeoutSec * 1000;
const start = Date.now();
let targetPeer: CLIP2PPeer | undefined;
while (Date.now() - start <= timeoutMs) {
const peers = getSortedPeers(replicator);
const peers = getSortedPeers(service);
targetPeer = resolvePeer(peers, peerToken);
if (targetPeer) {
break;
@@ -183,11 +166,14 @@ export async function syncWithPeer(
throw new Error(`Peer '${peerToken}' was not found within ${timeoutSec} seconds`);
}
const pullResult = await replicator.replicateFrom(targetPeer.peerId, false);
const pullResult = await service.targetedTransfer.pullFromPeer(targetPeer.peerId, { showNotice: false });
if (pullResult && "error" in pullResult && pullResult.error) {
throw pullResult.error instanceof Error ? pullResult.error : LiveSyncError.fromError(pullResult.error);
}
const pushResult = await replicator.requestSynchroniseToPeer(targetPeer.peerId);
if (!pullResult || pullResult.status !== "completed") {
throw LiveSyncError.fromError("P2P sync failed while pulling from peer");
}
const pushResult = await service.targetedTransfer.requestPushToPeer(targetPeer.peerId);
if (!pushResult || pushResult.ok !== true) {
const err: unknown = pushResult && "error" in pushResult ? pushResult.error : undefined;
throw err instanceof Error
@@ -195,15 +181,19 @@ export async function syncWithPeer(
: LiveSyncError.fromError(err ?? "P2P sync failed while requesting remote sync");
}
await writePeerConnectionStatsIfRequested(replicator, targetPeer);
await writePeerConnectionStatsIfRequested(service, targetPeer);
return targetPeer;
} finally {
await replicator.close();
await service.transportLifecycle.disconnect();
}
}
export async function openP2PHost(core: LiveSyncBaseCore<ServiceContext, never>): Promise<LiveSyncTrysteroReplicator> {
const replicator = await createReplicator(core);
await replicator.open();
return replicator;
/** Connect the headless P2P host and transfer transport ownership to the caller. */
export async function openP2PHost(
core: LiveSyncBaseCore<ServiceContext, never>,
p2pService: CLIP2PService | undefined
): Promise<CLIP2PService> {
const service = requireP2PService(core, p2pService);
await service.transportLifecycle.connect();
return service;
}
+131 -2
View File
@@ -1,5 +1,40 @@
import { describe, expect, it } from "vitest";
import { parseTimeoutSeconds } from "./p2p";
import { describe, expect, it, vi } from "vitest";
import { collectPeers, createPeerConnectionStatsPayload, parseTimeoutSeconds, syncWithPeer } from "./p2p";
function createCore() {
const settings = { P2P_Enabled: true, P2P_AppID: "app-id", P2P_IsHeadless: false };
return {
services: {
setting: { currentSettings: () => settings },
replicator: { getNewReplicator: vi.fn(() => Promise.reject(new Error("must not be called"))) },
},
} as never;
}
function createP2PService() {
const connect = vi.fn(async () => undefined);
const disconnect = vi.fn(async () => undefined);
const pullFromPeer = vi.fn(async () => ({ status: "completed" as const, ok: true as const }));
const requestPushToPeer = vi.fn(async () => ({ status: "completed" as const, ok: true as const }));
return {
service: {
transportLifecycle: { isConnected: false, connect, disconnect },
peerDirectory: {
getPeers: () => [{ peerId: "peer-a", name: "Peer A", platform: "test" }],
},
targetedTransfer: {
pullFromPeer,
requestPushToPeer,
synchroniseWithPeer: vi.fn(),
},
diagnostics: { requestStatus: vi.fn(), getPeerConnectionMetrics: vi.fn() },
},
connect,
disconnect,
pullFromPeer,
requestPushToPeer,
};
}
describe("p2p command helpers", () => {
it("accepts non-negative timeout", () => {
@@ -15,4 +50,98 @@ describe("p2p command helpers", () => {
"p2p-sync requires a non-negative timeout in seconds"
);
});
it("collects peers through service views without acquiring a concrete replicator", async () => {
const { service, connect, disconnect } = createP2PService();
await expect(collectPeers(createCore(), service as never, 0)).resolves.toEqual([
{ peerId: "peer-a", name: "Peer A" },
]);
expect(connect).toHaveBeenCalledOnce();
expect(disconnect).toHaveBeenCalledOnce();
});
it("synchronises through the targeted-transfer view", async () => {
const { service, pullFromPeer, requestPushToPeer } = createP2PService();
await expect(syncWithPeer(createCore(), service as never, "peer-a", 0)).resolves.toEqual({
peerId: "peer-a",
name: "Peer A",
});
expect(pullFromPeer).toHaveBeenCalledWith("peer-a", { showNotice: false });
expect(requestPushToPeer).toHaveBeenCalledWith("peer-a");
});
it("rejects a cancelled pull without requesting a peer push", async () => {
const { service, disconnect, pullFromPeer, requestPushToPeer } = createP2PService();
pullFromPeer.mockResolvedValue({ status: "cancelled" } as never);
await expect(syncWithPeer(createCore(), service as never, "peer-a", 0)).rejects.toBeDefined();
expect(requestPushToPeer).not.toHaveBeenCalled();
expect(disconnect).toHaveBeenCalledOnce();
});
it("preserves the benchmark diagnostics JSONL contract", () => {
expect(
createPeerConnectionStatsPayload(
{ peerId: "peer-a", name: "Peer A" },
{
selectedPairPresent: true,
selectedPairId: "pair-1",
state: "succeeded",
currentRoundTripTime: 0.01,
totalRoundTripTime: 0.1,
requestsSent: 3,
responsesReceived: 3,
packetsDiscardedOnSend: 0,
bytesSent: 100,
bytesReceived: 200,
localCandidate: {
id: "local-1",
candidateType: "host",
protocol: "udp",
relayProtocol: "unknown",
},
remoteCandidate: {
id: "remote-1",
candidateType: "relay",
protocol: "udp",
relayProtocol: "udp",
},
},
"2026-08-27T00:00:00.000Z"
)
).toEqual({
generatedAt: "2026-08-27T00:00:00.000Z",
command: "p2p-sync",
peerId: "peer-a",
peerName: "Peer A",
candidatePathCollected: true,
selectedPath: "host<->relay",
selectedPair: {
id: "pair-1",
state: "succeeded",
currentRoundTripTime: 0.01,
totalRoundTripTime: 0.1,
requestsSent: 3,
responsesReceived: 3,
packetsDiscardedOnSend: 0,
bytesSent: 100,
bytesReceived: 200,
},
localCandidate: {
id: "local-1",
candidateType: "host",
protocol: "udp",
relayProtocol: "unknown",
},
remoteCandidate: {
id: "remote-1",
candidateType: "relay",
protocol: "udp",
relayProtocol: "udp",
},
});
});
});
+68 -170
View File
@@ -2,12 +2,8 @@ import { decodeSettingsFromSetupURI } from "@vrtmrz/livesync-commonlib/compat/AP
import { configURIBase } from "@vrtmrz/livesync-commonlib/compat/common/models/shared.const";
import {
DEFAULT_SETTINGS,
MILESTONE_DOCID,
type FilePathWithPrefix,
type ObsidianLiveSyncSettings,
REMOTE_COUCHDB,
REMOTE_MINIO,
type EntryMilestoneInfo,
type EntryDoc,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { ConnectionStringParser } from "@vrtmrz/livesync-commonlib/compat/common/ConnectionString";
@@ -23,71 +19,27 @@ import { performFullScan } from "@vrtmrz/livesync-commonlib/compat/serviceFeatur
import { UnresolvedErrorManager } from "@vrtmrz/livesync-commonlib/compat/services/base/UnresolvedErrorManager";
import { compatGlobal } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions";
import { fsPromises as fs, path } from "@vrtmrz/livesync-commonlib/node";
import type { LiveSyncCouchDBReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import type { LiveSyncJournalReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/journal/LiveSyncJournalReplicator";
import { writeStderrLine, writeStdoutLine } from "@/apps/cli/cliOutput";
import {
CENTRAL_COMPATIBILITY_REJECTION_REASONS,
isReplicationCompleted,
NO_INTERACTION,
REPLICATION_PROGRESS_PRESENTATIONS,
REMOTE_RESOURCE_KINDS,
USER_INITIATED_REPLICATION_AUTHORITY,
} from "@vrtmrz/livesync-commonlib/replication";
import { withOwnedRemoteResource } from "@/common/ownedRemoteResource";
import {
isCentralRemoteAdministrationCommand,
runCentralRemoteAdministrationCommand,
} from "./centralRemoteAdministration";
function redactConnectionString(uri: string): string {
return uri.replace(/\/\/([^@/]+)@/u, "//***@");
}
async function verifyRemoteState(
core: CLICommandContext["core"],
settings: ObsidianLiveSyncSettings
): Promise<boolean> {
const { standardIo } = core.services.context;
const replicator = core.services.replicator.getActiveReplicator();
if (!replicator) {
standardIo.writeStderr("[Verification] No active replicator found\n");
return false;
}
if (!replicator.nodeid) {
await replicator.initializeDatabaseForReplication();
}
try {
let milestone: EntryMilestoneInfo | false | undefined = undefined;
if (settings.remoteType === REMOTE_COUCHDB) {
const dbRet = await (replicator as LiveSyncCouchDBReplicator).connectRemoteCouchDBWithSetting(
settings,
false,
true
);
if (typeof dbRet === "string") {
standardIo.writeStderr(`[Verification] Failed to connect to remote CouchDB: ${dbRet}\n`);
return false;
}
try {
milestone = await dbRet.db.get(MILESTONE_DOCID);
} finally {
await dbRet.db.close();
}
} else if (settings.remoteType === REMOTE_MINIO) {
milestone = await (replicator as LiveSyncJournalReplicator).client.downloadJson("_00000000-milestone.json");
}
if (milestone) {
const isLocked = !!milestone.locked;
const isAccepted = !!milestone.accepted_nodes?.includes(replicator.nodeid);
standardIo.writeStderr(`[Verification] Remote Database: ${isLocked ? "LOCKED" : "UNLOCKED"}\n`);
standardIo.writeStderr(
`[Verification] Current Device Node ID (${replicator.nodeid}): ${isAccepted ? "ACCEPTED" : "NOT ACCEPTED"}\n`
);
return true;
} else {
standardIo.writeStderr("[Verification] Milestone document not found on remote.\n");
return false;
}
} catch (e) {
const message = e instanceof Error ? e.message : String(e);
standardIo.writeStderr(`[Verification] Failed to fetch milestone document: ${message}\n`);
return false;
}
}
export async function runCommand(options: CLIOptions, context: CLICommandContext): Promise<boolean> {
const { databasePath, core, settingsPath } = context;
const { databasePath, core, replicationScheduling, settingsPath } = context;
const { standardIo } = core.services.context;
const vaultPath = context.vaultPath || databasePath;
@@ -95,19 +47,28 @@ export async function runCommand(options: CLIOptions, context: CLICommandContext
if (options.command === "daemon") {
const log = (msg: unknown) => writeStderrLine(standardIo, `[Daemon] ${String(msg)}`);
// The daemon owns its own recurring poller. Suppress the application
// resume starter and generic periodic timer before restoring settings.
replicationScheduling.setExternalPollingMode(!!options.interval);
// Skip the config mismatch dialog — the daemon cannot resolve it interactively
// and the default "Dismiss" action would block replication. The daemon should
// accept whatever configuration the remote has.
await core.services.setting.applyPartial({ disableCheckingConfigMismatch: true }, true);
// 1. Replicate CouchDB → local PouchDB so the mirror scan has content to work with.
log("Replicating from CouchDB...");
const replResult = await core.services.replication.replicate(true);
if (!replResult) {
writeStderrLine(standardIo, "[Daemon] Initial CouchDB replication failed, cannot continue");
// 1. Replicate the configured remote into the local database so the
// mirror scan has content to work with.
log("Replicating from remote...");
const replResult = await core.services.replication.replicateUnattended({
trigger: "daemon",
interaction: NO_INTERACTION,
});
if (!isReplicationCompleted(replResult)) {
writeStderrLine(standardIo, "[Daemon] Initial replication failed, cannot continue");
return false;
}
log("CouchDB replication complete");
replicationScheduling.markInitialOneShotSatisfied();
log("Initial replication complete");
// 2. Mirror scan to reconcile PouchDB ↔ local filesystem.
const errorManager = new UnresolvedErrorManager(core.services.appLifecycle, core.services.context.events);
@@ -129,8 +90,9 @@ export async function runCommand(options: CLIOptions, context: CLICommandContext
true
);
// applySettings fires the full lifecycle: onSuspending → onResumed.
// ModuleReplicatorCouchDB starts continuous replication on onResumed
// via fireAndForget.
// The provider-independent scheduling feature owns any eligible
// Continuous start; the daemon marker suppresses a duplicate
// sync-on-start OneShot.
await core.services.control.applySettings();
// Lifecycle events (onSuspending) may re-enable suspension flags.
// Clear them explicitly after the lifecycle completes. applyPartial
@@ -153,7 +115,13 @@ export async function runCommand(options: CLIOptions, context: CLICommandContext
const poll = async () => {
try {
await core.services.replication.replicate(true);
const result = await core.services.replication.replicateUnattended({
trigger: "daemon",
interaction: NO_INTERACTION,
});
if (!isReplicationCompleted(result)) {
throw new Error(`Daemon polling replication did not complete (${result.status}).`);
}
if (consecutiveFailures > 0) {
consecutiveFailures--;
currentIntervalMs = Math.max(currentIntervalMs / 2, baseIntervalMs);
@@ -182,11 +150,11 @@ export async function runCommand(options: CLIOptions, context: CLICommandContext
return true;
});
} else {
log("LiveSync mode: restoring sync settings and starting _changes feed");
log("LiveSync mode: restoring sync settings and starting continuous synchronisation where supported");
await restoreSyncSettings();
// The applySettings() lifecycle fires onResumed → ModuleReplicatorCouchDB which
// starts continuous replication via fireAndForget(openReplication). Don't call
// openReplication directly — it races with the handler and causes dedup/termination.
// The applySettings() lifecycle fires onResumed → the provider-
// independent scheduling feature, which starts Continuous when
// supported. Do not call a concrete Replicator directly.
log("LiveSync active");
const currentSettings = core.services.setting.currentSettings();
if (!currentSettings.liveSync && !currentSettings.syncOnStart) {
@@ -204,13 +172,20 @@ export async function runCommand(options: CLIOptions, context: CLICommandContext
if (options.command === "sync") {
writeStdoutLine(standardIo, "[Command] sync");
const result = await core.services.replication.replicate(true);
if (!result) {
const result = await core.services.replication.replicateUserInitiated({
trigger: "manual",
progressPresentation: REPLICATION_PROGRESS_PRESENTATIONS.NOTICE,
interaction: USER_INITIATED_REPLICATION_AUTHORITY,
});
if (!isReplicationCompleted(result)) {
// TODO: Standardise the logic for identifying the cause of replication
// failure so that every reason (locked DB, version mismatch, network
// error, etc.) is surfaced with a CLI-specific actionable message.
const replicator = core.services.replicator.getActiveReplicator();
if (replicator?.remoteLockedAndDeviceNotAccepted) {
const recoveryHint = result.status === "failed" ? result.recoveryHint : undefined;
if (
recoveryHint?.reason === CENTRAL_COMPATIBILITY_REJECTION_REASONS.NODE_LOCKED ||
recoveryHint?.reason === CENTRAL_COMPATIBILITY_REJECTION_REASONS.NODE_CLEANED
) {
writeStderrLine(
standardIo,
`[Error] The remote database is locked and this device is not yet accepted.\n` +
@@ -218,7 +193,7 @@ export async function runCommand(options: CLIOptions, context: CLICommandContext
);
}
}
return !!result;
return isReplicationCompleted(result);
}
if (options.command === "p2p-peers") {
@@ -227,7 +202,7 @@ export async function runCommand(options: CLIOptions, context: CLICommandContext
}
const timeoutSec = parseTimeoutSeconds(options.commandArgs[0], "p2p-peers");
writeStderrLine(standardIo, `[Command] p2p-peers timeout=${timeoutSec}s`);
const peers = await collectPeers(core, timeoutSec);
const peers = await collectPeers(core, context.p2pReplicator, timeoutSec);
if (peers.length > 0) {
standardIo.writeStdout(peers.map((peer) => `[peer]\t${peer.peerId}\t${peer.name}`).join("\n") + "\n");
}
@@ -244,14 +219,14 @@ export async function runCommand(options: CLIOptions, context: CLICommandContext
}
const timeoutSec = parseTimeoutSeconds(options.commandArgs[1], "p2p-sync");
writeStderrLine(standardIo, `[Command] p2p-sync peer=${peerToken} timeout=${timeoutSec}s`);
const peer = await syncWithPeer(core, peerToken, timeoutSec);
const peer = await syncWithPeer(core, context.p2pReplicator, peerToken, timeoutSec);
writeStderrLine(standardIo, `[Done] P2P sync completed with ${peer.name} (${peer.peerId})`);
return true;
}
if (options.command === "p2p-host") {
writeStderrLine(standardIo, "[Command] p2p-host");
await openP2PHost(core);
await openP2PHost(core, context.p2pReplicator);
writeStderrLine(standardIo, "[Ready] P2P host is running. Press Ctrl+C to stop.");
await new Promise(() => {});
return true;
@@ -757,88 +732,8 @@ export async function runCommand(options: CLIOptions, context: CLICommandContext
return true;
}
if (options.command === "mark-resolved") {
const id = options.commandArgs[0]?.trim();
if (id) {
let switched = false;
await core.services.setting.updateSettings((currentSettings) => {
const activated = activateRemoteConfiguration(currentSettings, id);
if (activated) {
switched = true;
return activated;
}
return currentSettings;
}, false);
if (!switched) {
standardIo.writeStderr(`[Info] Failed to temporarily activate remote configuration: ${id}\n`);
return false;
}
await core.services.control.applySettings();
}
writeStderrLine(standardIo, `[Command] mark-resolved${id ? ` ${id}` : ""}`);
await core.services.replication.markResolved();
const settings = core.services.setting.currentSettings();
await verifyRemoteState(core, settings);
return true;
}
if (options.command === "unlock-remote") {
const id = options.commandArgs[0]?.trim();
if (id) {
let switched = false;
await core.services.setting.updateSettings((currentSettings) => {
const activated = activateRemoteConfiguration(currentSettings, id);
if (activated) {
switched = true;
return activated;
}
return currentSettings;
}, false);
if (!switched) {
standardIo.writeStderr(`[Info] Failed to temporarily activate remote configuration: ${id}\n`);
return false;
}
await core.services.control.applySettings();
}
writeStderrLine(standardIo, `[Command] unlock-remote${id ? ` ${id}` : ""}`);
await core.services.replication.markUnlocked();
const settings = core.services.setting.currentSettings();
await verifyRemoteState(core, settings);
return true;
}
if (options.command === "lock-remote") {
const id = options.commandArgs[0]?.trim();
if (id) {
let switched = false;
await core.services.setting.updateSettings((currentSettings) => {
const activated = activateRemoteConfiguration(currentSettings, id);
if (activated) {
switched = true;
return activated;
}
return currentSettings;
}, false);
if (!switched) {
standardIo.writeStderr(`[Info] Failed to temporarily activate remote configuration: ${id}\n`);
return false;
}
await core.services.control.applySettings();
}
writeStderrLine(standardIo, `[Command] lock-remote${id ? ` ${id}` : ""}`);
await core.services.replication.markLocked();
const settings = core.services.setting.currentSettings();
await verifyRemoteState(core, settings);
return true;
if (isCentralRemoteAdministrationCommand(options.command)) {
return await runCentralRemoteAdministrationCommand(options, context, options.command);
}
if (options.command === "remote-status") {
@@ -863,13 +758,16 @@ export async function runCommand(options: CLIOptions, context: CLICommandContext
}
writeStderrLine(standardIo, `[Command] remote-status${id ? ` ${id}` : ""}`);
const replicator = core.services.replicator.getActiveReplicator();
if (!replicator) {
standardIo.writeStderr("[Error] No active replicator found\n");
const settings = core.services.setting.currentSettings();
const resource = await core.services.replicator.createRemoteResource(
REMOTE_RESOURCE_KINDS.CONNECTION,
settings
);
if (!resource) {
standardIo.writeStderr("[Error] Remote status is unavailable for the current provider\n");
return false;
}
const settings = core.services.setting.currentSettings();
const status = await replicator.getRemoteStatus(settings);
const status = await withOwnedRemoteResource(resource, (ownedResource) => ownedResource.getStatus());
if (status === false) {
standardIo.writeStderr("[Error] Failed to fetch remote status\n");
return false;
+249 -20
View File
@@ -2,10 +2,26 @@ import { fsPromises as fs, os, path } from "@vrtmrz/livesync-commonlib/node";
import * as processSetting from "@vrtmrz/livesync-commonlib/compat/API/processSetting";
import { ConnectionStringParser } from "@vrtmrz/livesync-commonlib/compat/common/ConnectionString";
import { configURIBase } from "@vrtmrz/livesync-commonlib/compat/common/models/shared.const";
import { DEFAULT_SETTINGS, REMOTE_COUCHDB, REMOTE_MINIO, REMOTE_P2P } from "@vrtmrz/livesync-commonlib/compat/common/types";
import {
DEFAULT_SETTINGS,
REMOTE_COUCHDB,
REMOTE_MINIO,
REMOTE_P2P,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { runCommand } from "./runCommand";
import type { CLIOptions } from "./types";
import {
CENTRAL_REMOTE_ADMINISTRATION_ACTIONS,
CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS,
CENTRAL_REMOTE_ADMINISTRATION_OBSERVATION_KINDS,
CENTRAL_REMOTE_ADMINISTRATION_RESULT_STATUSES,
REMOTE_RESOURCE_KINDS,
CENTRAL_COMPATIBILITY_REJECTION_REASONS,
REPLICATION_COMPLETED,
REPLICATION_PROGRESS_PRESENTATIONS,
replicationFailed,
} from "@vrtmrz/livesync-commonlib/replication";
function createStandardIoMock() {
return {
@@ -44,8 +60,26 @@ function createCoreMock() {
markResolved: vi.fn(async () => {}),
markUnlocked: vi.fn(async () => {}),
markLocked: vi.fn(async () => {}),
replicateUserInitiated: vi.fn(async () => REPLICATION_COMPLETED),
},
replicator: {
runCentralRemoteAdministration: vi.fn(async ({ action }) => ({
status: CENTRAL_REMOTE_ADMINISTRATION_RESULT_STATUSES.VERIFIED,
observation: {
kind: CENTRAL_REMOTE_ADMINISTRATION_OBSERVATION_KINDS.MILESTONE,
locked: action === CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.LOCK,
accepted: true,
nodeId: "test-node-id",
},
})),
createRemoteResource: vi.fn(async () => ({
check: vi.fn(async () => ({ ok: true as const })),
getStatus: vi.fn(async () => ({
db_name: "test-db",
doc_count: 42,
})),
dispose: vi.fn(async () => undefined),
})),
getActiveReplicator: vi.fn(() => ({
nodeid: "test-node-id",
initializeDatabaseForReplication: vi.fn(async () => {}),
@@ -93,6 +127,7 @@ function makeOptions(command: CLIOptions["command"], commandArgs: string[]): CLI
databasePath: "/tmp/vault",
verbose: false,
force: false,
compatRemoteAdminExitZero: false,
};
}
@@ -231,6 +266,42 @@ describe("runCommand abnormal cases", () => {
vi.restoreAllMocks();
});
it("retains visible progress for the interactive sync command", async () => {
const core = createCoreMock();
await expect(
runCommand(makeOptions("sync", []), {
...context,
core,
})
).resolves.toBe(true);
expect(core.services.replication.replicateUserInitiated).toHaveBeenCalledWith(
expect.objectContaining({ progressPresentation: REPLICATION_PROGRESS_PRESENTATIONS.NOTICE })
);
});
it("reports a lock from the exact sync outcome without inspecting a replacement Replicator", async () => {
const core = createCoreMock();
core.services.replication.replicateUserInitiated.mockResolvedValue(
replicationFailed(new Error("locked"), {
reason: CENTRAL_COMPATIBILITY_REJECTION_REASONS.NODE_LOCKED,
})
);
await expect(
runCommand(makeOptions("sync", []), {
...context,
core,
})
).resolves.toBe(false);
expect(core.services.context.standardIo.writeStderr).toHaveBeenCalledWith(
expect.stringContaining("remote database is locked")
);
expect(core.services.replicator.getActiveReplicator).not.toHaveBeenCalled();
});
it("pull returns false for non-existing path", async () => {
const core = createCoreMock();
core.serviceModules.fileHandler.dbToStorage.mockResolvedValue(false);
@@ -706,28 +777,158 @@ describe("runCommand abnormal cases", () => {
});
describe("mark-resolved and unlock-remote commands", () => {
it("reports a connection failure without claiming that every central remote is CouchDB", async () => {
const core = createCoreMock();
core.services.replicator.runCentralRemoteAdministration.mockResolvedValueOnce({
status: CENTRAL_REMOTE_ADMINISTRATION_RESULT_STATUSES.VERIFICATION_FAILED,
reason: CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.CONNECTION_FAILED,
detail: new Error("remote unavailable"),
});
const result = await runCommand(makeOptions("mark-resolved", []), {
...context,
core,
});
expect(result).toBe(false);
const verificationOutput = core.services.context.standardIo.writeStderr.mock.calls
.map(([chunk]: [string | Uint8Array]) =>
typeof chunk === "string" ? chunk : new TextDecoder().decode(chunk)
)
.join("");
expect(verificationOutput).toContain(
"[Verification] Failed to connect to the configured remote: remote unavailable\n"
);
expect(verificationOutput).not.toContain("CouchDB");
});
it("reports when the active remote configuration changes before administration begins", async () => {
const core = createCoreMock();
core.services.replicator.runCentralRemoteAdministration.mockResolvedValueOnce({
status: CENTRAL_REMOTE_ADMINISTRATION_RESULT_STATUSES.VERIFICATION_FAILED,
reason: CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.ACTIVE_CONFIGURATION_MISMATCH,
});
const result = await runCommand(makeOptions("mark-resolved", []), {
...context,
core,
});
expect(result).toBe(false);
const verificationOutput = core.services.context.standardIo.writeStderr.mock.calls
.map(([chunk]: [string | Uint8Array]) =>
typeof chunk === "string" ? chunk : new TextDecoder().decode(chunk)
)
.join("");
expect(verificationOutput).toContain(
"[Verification] The active remote configuration changed before remote administration could begin.\n"
);
});
it("fails by default when remote administration cannot verify its postcondition", async () => {
const core = createCoreMock();
core.services.replicator.runCentralRemoteAdministration.mockResolvedValueOnce({
status: CENTRAL_REMOTE_ADMINISTRATION_RESULT_STATUSES.VERIFICATION_FAILED,
reason: CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.NO_ACTIVE_REPLICATOR,
});
const result = await runCommand(makeOptions("mark-resolved", []), {
...context,
core,
});
expect(result).toBe(false);
});
it("preserves the historical zero exit for returned verification failures only when requested", async () => {
const core = createCoreMock();
core.services.replicator.runCentralRemoteAdministration.mockResolvedValueOnce({
status: CENTRAL_REMOTE_ADMINISTRATION_RESULT_STATUSES.VERIFICATION_FAILED,
reason: CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.NO_ACTIVE_REPLICATOR,
});
const result = await runCommand(
{ ...makeOptions("mark-resolved", []), compatRemoteAdminExitZero: true },
{
...context,
core,
}
);
expect(result).toBe(true);
});
it("does not hide a thrown remote mutation failure behind the compatibility option", async () => {
const core = createCoreMock();
const failure = new Error("mutation failed");
core.services.replicator.runCentralRemoteAdministration.mockRejectedValueOnce(failure);
await expect(
runCommand(
{ ...makeOptions("mark-resolved", []), compatRemoteAdminExitZero: true },
{
...context,
core,
}
)
).rejects.toBe(failure);
});
it("does not hide an unknown remote ID behind the compatibility option", async () => {
const core = createCoreMock();
const result = await runCommand(
{ ...makeOptions("mark-resolved", ["missing-remote"]), compatRemoteAdminExitZero: true },
{
...context,
core,
}
);
expect(result).toBe(false);
expect(core.services.replicator.runCentralRemoteAdministration).not.toHaveBeenCalled();
});
it("fails a lock command when the observed milestone remains unlocked", async () => {
const core = createCoreMock();
core.services.replicator.runCentralRemoteAdministration.mockResolvedValueOnce({
status: CENTRAL_REMOTE_ADMINISTRATION_RESULT_STATUSES.VERIFICATION_FAILED,
reason: CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.POSTCONDITION_MISMATCH,
observation: {
kind: CENTRAL_REMOTE_ADMINISTRATION_OBSERVATION_KINDS.MILESTONE,
locked: false,
accepted: true,
nodeId: "test-node-id",
},
});
const result = await runCommand(makeOptions("lock-remote", []), {
...context,
core,
});
expect(result).toBe(false);
const verificationOutput = core.services.context.standardIo.writeStderr.mock.calls
.map(([chunk]: [string | Uint8Array]) =>
typeof chunk === "string" ? chunk : new TextDecoder().decode(chunk)
)
.join("");
expect(verificationOutput).toContain("[Verification] Remote Database: UNLOCKED\n");
expect(verificationOutput).toContain("[Verification] Current Device Node ID (test-node-id): ACCEPTED\n");
});
it("mark-resolved without args runs on active database", async () => {
const core = createCoreMock();
const remoteDatabase = {
close: vi.fn(async () => undefined),
get: vi.fn(async () => ({
locked: false,
accepted_nodes: ["test-node-id"],
})),
};
core.services.replicator.getActiveReplicator.mockReturnValueOnce({
nodeid: "test-node-id",
initializeDatabaseForReplication: vi.fn(async () => undefined),
connectRemoteCouchDBWithSetting: vi.fn(async () => ({ db: remoteDatabase })),
});
const result = await runCommand(makeOptions("mark-resolved", []), {
...context,
core,
});
expect(result).toBe(true);
expect(core.services.replication.markResolved).toHaveBeenCalledTimes(1);
expect(core.services.replicator.runCentralRemoteAdministration).toHaveBeenCalledWith({
action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.MARK_RESOLVED,
});
expect(core.services.control.applySettings).not.toHaveBeenCalled();
expect(remoteDatabase.close).toHaveBeenCalledOnce();
expect(core.services.replication.markResolved).not.toHaveBeenCalled();
});
it("mark-resolved with remote-id temporarily activates it and runs markResolved", async () => {
@@ -745,7 +946,9 @@ describe("runCommand abnormal cases", () => {
core,
});
expect(result).toBe(true);
expect(core.services.replication.markResolved).toHaveBeenCalledTimes(1);
expect(core.services.replicator.runCentralRemoteAdministration).toHaveBeenCalledWith({
action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.MARK_RESOLVED,
});
expect(core.services.control.applySettings).toHaveBeenCalledTimes(1);
expect(settings.activeConfigurationId).toBe("r1");
expect(core.services.setting.updateSettings).toHaveBeenCalledWith(expect.any(Function), false);
@@ -758,7 +961,9 @@ describe("runCommand abnormal cases", () => {
core,
});
expect(result).toBe(true);
expect(core.services.replication.markUnlocked).toHaveBeenCalledTimes(1);
expect(core.services.replicator.runCentralRemoteAdministration).toHaveBeenCalledWith({
action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.UNLOCK,
});
expect(core.services.control.applySettings).not.toHaveBeenCalled();
});
@@ -777,7 +982,9 @@ describe("runCommand abnormal cases", () => {
core,
});
expect(result).toBe(true);
expect(core.services.replication.markUnlocked).toHaveBeenCalledTimes(1);
expect(core.services.replicator.runCentralRemoteAdministration).toHaveBeenCalledWith({
action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.UNLOCK,
});
expect(core.services.control.applySettings).toHaveBeenCalledTimes(1);
expect(settings.activeConfigurationId).toBe("r1");
expect(core.services.setting.updateSettings).toHaveBeenCalledWith(expect.any(Function), false);
@@ -790,7 +997,9 @@ describe("runCommand abnormal cases", () => {
core,
});
expect(result).toBe(true);
expect(core.services.replication.markLocked).toHaveBeenCalledTimes(1);
expect(core.services.replicator.runCentralRemoteAdministration).toHaveBeenCalledWith({
action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.LOCK,
});
expect(core.services.control.applySettings).not.toHaveBeenCalled();
});
@@ -809,7 +1018,9 @@ describe("runCommand abnormal cases", () => {
core,
});
expect(result).toBe(true);
expect(core.services.replication.markLocked).toHaveBeenCalledTimes(1);
expect(core.services.replicator.runCentralRemoteAdministration).toHaveBeenCalledWith({
action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.LOCK,
});
expect(core.services.control.applySettings).toHaveBeenCalledTimes(1);
expect(settings.activeConfigurationId).toBe("r1");
expect(core.services.setting.updateSettings).toHaveBeenCalledWith(expect.any(Function), false);
@@ -817,6 +1028,17 @@ describe("runCommand abnormal cases", () => {
it("remote-status without args outputs status of active remote configuration", async () => {
const core = createCoreMock();
const getStatus = vi.fn(async () => ({
db_name: "test-db",
doc_count: 42,
}));
const dispose = vi.fn(async () => undefined);
const createRemoteResource = vi.fn(async () => ({
check: vi.fn(),
getStatus,
dispose,
}));
core.services.replicator.createRemoteResource = createRemoteResource;
const stdout = captureStdout(core);
const result = await runCommand(makeOptions("remote-status", []), {
...context,
@@ -827,6 +1049,13 @@ describe("runCommand abnormal cases", () => {
const parsedStatus = JSON.parse(fullOutput);
expect(parsedStatus.db_name).toBe("test-db");
expect(parsedStatus.doc_count).toBe(42);
expect(createRemoteResource).toHaveBeenCalledWith(
REMOTE_RESOURCE_KINDS.CONNECTION,
core.services.setting.currentSettings()
);
expect(getStatus).toHaveBeenCalledOnce();
expect(dispose).toHaveBeenCalledOnce();
expect(core.services.replicator.getActiveReplicator).not.toHaveBeenCalled();
});
it("remote-status with remote-id temporarily activates it and outputs status", async () => {
+6 -1
View File
@@ -1,7 +1,8 @@
import { LiveSyncBaseCore } from "@/LiveSyncBaseCore";
import type { ObsidianLiveSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { NodeServiceContext } from "@/apps/cli/services/NodeServiceContext";
import type { UseP2PReplicatorResult } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/UseP2PReplicatorResult";
import type { UseP2PReplicatorResult } from "@vrtmrz/livesync-commonlib/p2p";
import type { ReplicationSchedulingControl } from "@/serviceFeatures/replicationScheduling";
export type CLICommand =
| "daemon"
@@ -41,6 +42,8 @@ export interface CLIOptions {
debug?: boolean;
force?: boolean;
writeSettings?: boolean;
/** Restore the former zero exit code after a returned remote-administration verification failure. */
compatRemoteAdminExitZero?: boolean;
command: CLICommand;
commandArgs: string[];
interval?: number;
@@ -50,6 +53,8 @@ export interface CLICommandContext {
databasePath: string;
vaultPath: string;
core: LiveSyncBaseCore<NodeServiceContext, never>;
/** Host-composition view used only to coordinate daemon-owned recurring work. */
replicationScheduling: ReplicationSchedulingControl;
/** Current-result contract owned by the P2P service feature. */
p2pReplicator?: UseP2PReplicatorResult;
settingsPath: string;
+24 -5
View File
@@ -23,8 +23,8 @@ import type { CLICommand, CLICommandContext, CLIOptions } from "./commands/types
import { getPathFromUXFileInfo } from "@vrtmrz/livesync-commonlib/compat/common/typeUtils";
import { stripAllPrefixes } from "@vrtmrz/livesync-commonlib/compat/string_and_binary/path";
import { IgnoreRules } from "./serviceModules/IgnoreRules";
import { useP2PReplicatorFeature } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/useP2PReplicatorFeature";
import type { UseP2PReplicatorResult } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/UseP2PReplicatorResult";
import { useP2PReplicatorFeature, type UseP2PReplicatorResult } from "@vrtmrz/livesync-commonlib/p2p";
import type { ReplicationSchedulingControl } from "@/serviceFeatures/replicationScheduling";
import { createNodeStandardIo, fsPromises as fs, path } from "@vrtmrz/livesync-commonlib/node";
import type { StandardIo } from "@vrtmrz/livesync-commonlib/context";
import { writeStderrLine, writeStdoutLine } from "./cliOutput";
@@ -103,6 +103,8 @@ Options:
(defaults to database-path; allows separate PouchDB and vault dirs)
--interval <N>, -i <N> (daemon only) Poll CouchDB every N seconds instead of using the _changes feed
--write-settings Write setting changes after a successful command
--compat-remote-admin-exit-zero
Preserve the former zero exit code when remote-administration verification fails
Examples:
livesync-cli ./my-database Run daemon (LiveSync mode)
@@ -153,6 +155,7 @@ export function parseArgs(standardIo: StandardIo = createNodeStandardIo()): CLIO
let debug = false;
let force = false;
let writeSettings = false;
let compatRemoteAdminExitZero = false;
let interval: number | undefined;
let command: CLICommand = "daemon";
const commandArgs: string[] = [];
@@ -212,6 +215,9 @@ export function parseArgs(standardIo: StandardIo = createNodeStandardIo()): CLIO
case "--write-settings":
writeSettings = true;
break;
case "--compat-remote-admin-exit-zero":
compatRemoteAdminExitZero = true;
break;
default: {
if (!databasePath) {
if (command === "daemon" && isCLICommand(token)) {
@@ -253,6 +259,7 @@ export function parseArgs(standardIo: StandardIo = createNodeStandardIo()): CLIO
debug,
force,
writeSettings,
compatRemoteAdminExitZero,
command,
commandArgs,
interval,
@@ -290,7 +297,10 @@ export async function main(
) {
const options = parseArgs(standardIo);
if (options.interval && options.command !== "daemon") {
writeStderrLine(standardIo, `Warning: --interval is only used in daemon mode, ignored for '${options.command}'`);
writeStderrLine(
standardIo,
`Warning: --interval is only used in daemon mode, ignored for '${options.command}'`
);
}
const avoidStdoutNoise =
options.command === "cat" ||
@@ -420,7 +430,10 @@ export async function main(
// In daemon mode the default handler must run so changes are applied to the filesystem.
if (options.command !== "daemon") {
serviceHubInstance.replication.processSynchroniseResult.addHandler(async () => {
writeStderrLine(standardIo, `[Info] Replication result received, but not processed automatically in CLI mode.`);
writeStderrLine(
standardIo,
`[Info] Replication result received, but not processed automatically in CLI mode.`
);
return await Promise.resolve(true);
}, -100);
}
@@ -472,6 +485,7 @@ export async function main(
// Create LiveSync core
let p2pReplicator: UseP2PReplicatorResult | undefined;
let replicationScheduling: ReplicationSchedulingControl | undefined;
const core = new LiveSyncBaseCore(
serviceHubInstance,
(core: LiveSyncBaseCore<NodeServiceContext, never>, serviceHub: InjectableServiceHub<NodeServiceContext>) => {
@@ -479,7 +493,8 @@ export async function main(
},
(core) => [],
() => [], // No add-ons
(core) => {
(core, coreFeatureViews) => {
replicationScheduling = coreFeatureViews.replicationScheduling;
// Register P2P replicator feature.
p2pReplicator = useP2PReplicatorFeature(core);
// Add target filter to prevent internal files are handled
@@ -511,6 +526,9 @@ export async function main(
}
}
);
if (!replicationScheduling) {
throw new Error("Replication scheduling was not provided during core feature composition.");
}
// Setup signal handlers for graceful shutdown
const shutdown = async (signal: string) => {
@@ -617,6 +635,7 @@ export async function main(
databasePath,
vaultPath,
core,
replicationScheduling,
p2pReplicator,
settingsPath,
originalSyncSettings,
+10
View File
@@ -69,6 +69,7 @@ describe("CLI parseArgs", () => {
const combined = standardIo.writeStdout.mock.calls.flat().join("");
expect(combined).toContain("Usage:");
expect(combined).toContain("livesync-cli <database-path> [options] <command> [command-args]");
expect(combined).toContain("--compat-remote-admin-exit-zero");
});
it("parses p2p-peers command and timeout", () => {
@@ -215,4 +216,13 @@ describe("CLI parseArgs", () => {
expect(parsed.writeSettings).toBe(true);
expect(parsed.commandArgs).toEqual([]);
});
it("parses the remote-administration exit compatibility option globally", () => {
process.argv = ["node", "livesync-cli", "./vault", "--compat-remote-admin-exit-zero", "mark-resolved"];
const parsed = parseArgs();
expect(parsed.command).toBe("mark-resolved");
expect(parsed.compatRemoteAdminExitZero).toBe(true);
expect(parsed.commandArgs).toEqual([]);
});
});
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "self-hosted-livesync-cli",
"private": true,
"version": "1.0.21-cli",
"version": "1.0.23-cli",
"main": "dist/index.cjs",
"type": "module",
"scripts": {
@@ -5,7 +5,7 @@ import { createNodeStandardIo } from "@vrtmrz/livesync-commonlib/node";
import { writeStderrLine } from "@/apps/cli/cliOutput";
import { main, type CliCommandRunner } from "@/apps/cli/main";
import { parseTimeoutSeconds } from "@/apps/cli/commands/p2p";
import { runP2PReplicatorReplacementProbe } from "./p2p-replicator-replacement";
import { runP2PReplicatorReplacementProbe } from "./p2p-replicator-replacement.test";
if (
typeof (compatGlobal as unknown as Record<string, unknown>).RTCPeerConnection === "undefined" &&
@@ -1,6 +1,6 @@
import type { FilePathWithPrefix } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { LiveSyncTrysteroReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/LiveSyncTrysteroReplicator";
import { compatGlobal } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions";
import type { P2PServiceViews } from "@vrtmrz/livesync-commonlib/p2p";
import type { CLICommandContext } from "@/apps/cli/commands/types";
import { openP2PHost } from "@/apps/cli/commands/p2p";
@@ -15,32 +15,35 @@ function describeError(value: unknown): string {
return value instanceof Error ? (value.stack ?? value.message) : String(value);
}
async function waitForServing(replicator: LiveSyncTrysteroReplicator, timeoutMs: number): Promise<void> {
type ProbeP2PService = Pick<P2PServiceViews, "transportLifecycle" | "peerDirectory" | "targetedTransfer">;
async function waitForServing(service: ProbeP2PService, timeoutMs: number): Promise<void> {
const started = Date.now();
while (Date.now() - started <= timeoutMs) {
if (replicator.server?.isServing) return;
if (service.transportLifecycle.isConnected) return;
await delay(200);
}
throw new Error("The replacement P2P replicator did not start serving within the timeout");
throw new Error("The stable P2P service did not start serving within the timeout");
}
async function waitForPeer(
replicator: LiveSyncTrysteroReplicator,
service: ProbeP2PService,
targetPeer: string,
timeoutMs: number
): Promise<{ peerId: string; name: string }> {
const started = Date.now();
while (Date.now() - started <= timeoutMs) {
const peer = replicator.knownAdvertisements.find(
(candidate) => candidate.name === targetPeer || candidate.peerId === targetPeer
);
const peer = service.peerDirectory
.getPeers()
.find((candidate) => candidate.name === targetPeer || candidate.peerId === targetPeer);
if (peer) return peer;
await delay(200);
}
const knownPeers = replicator.knownAdvertisements.map((peer) => `${peer.name} (${peer.peerId})`).join(", ");
throw new Error(
`Peer '${targetPeer}' was not discovered within the timeout. Known peers: ${knownPeers || "none"}`
);
const knownPeers = service.peerDirectory
.getPeers()
.map((peer) => `${peer.name} (${peer.peerId})`)
.join(", ");
throw new Error(`Peer '${targetPeer}' was not discovered within the timeout. Known peers: ${knownPeers || "none"}`);
}
function assertPullSucceeded(result: unknown): void {
@@ -50,15 +53,17 @@ function assertPullSucceeded(result: unknown): void {
}
async function communicateWithPeer(
replicator: LiveSyncTrysteroReplicator,
service: ProbeP2PService,
targetPeer: string,
timeoutMs: number
): Promise<{ peerId: string; name: string }> {
await replicator.open();
await waitForServing(replicator, timeoutMs);
const peer = await waitForPeer(replicator, targetPeer, timeoutMs);
assertPullSucceeded(await replicator.replicateFrom(peer.peerId, false));
const pushResult = await replicator.requestSynchroniseToPeer(peer.peerId);
if (!service.transportLifecycle.isConnected) {
await service.transportLifecycle.connect();
}
await waitForServing(service, timeoutMs);
const peer = await waitForPeer(service, targetPeer, timeoutMs);
assertPullSucceeded(await service.targetedTransfer.pullFromPeer(peer.peerId, { showNotice: false }));
const pushResult = await service.targetedTransfer.requestPushToPeer(peer.peerId);
if (!pushResult || pushResult.ok !== true) {
throw new Error(`P2P push failed: ${describeError(pushResult?.error)}`);
}
@@ -78,35 +83,39 @@ export async function runP2PReplicatorReplacementProbe(
throw new Error("The CLI did not expose its P2P service-feature result to the integration probe");
}
const firstReplicator = await openP2PHost(core);
if (p2pReplicator.replicator !== firstReplicator) {
throw new Error("The P2P service feature did not expose the newly created replicator");
const initialActiveReplicator = core.services.replicator.getActiveReplicator();
if (!initialActiveReplicator) {
throw new Error("The CLI did not activate the initial P2P Replicator adapter");
}
const compatibilityFacade = p2pReplicator.replicator;
const p2pService = await openP2PHost(core, p2pReplicator);
const firstPeer = await communicateWithPeer(firstReplicator, targetPeer, timeoutMs);
const firstPeer = await communicateWithPeer(p2pService, targetPeer, timeoutMs);
const initialised = await core.services.databaseEvents.initialiseDatabase(false, true, false);
if (!initialised) {
throw new Error("Database reinitialisation failed during the P2P replacement probe");
}
const replacementReplicator = p2pReplicator.replicator;
if (core.services.replicator.getActiveReplicator() !== replacementReplicator) {
throw new Error("ReplicatorService did not activate the P2P service feature's replacement replicator");
const replacementActiveReplicator = core.services.replicator.getActiveReplicator();
if (!replacementActiveReplicator) {
throw new Error("ReplicatorService did not activate a replacement P2P Replicator adapter");
}
if (replacementReplicator === firstReplicator) {
throw new Error("Database reinitialisation retained the previous P2P replicator instance");
if (replacementActiveReplicator === initialActiveReplicator) {
throw new Error("Database reinitialisation retained the previous active P2P Replicator adapter");
}
if (firstReplicator.server !== undefined) {
throw new Error("The previous P2P replicator remained open after replacement");
if (p2pReplicator.replicator !== compatibilityFacade) {
throw new Error("Database reinitialisation replaced the stable P2P service compatibility facade");
}
if (p2pService.transportLifecycle.isConnected) {
throw new Error("Database reinitialisation left the database-bound P2P room open");
}
const settings = core.services.setting.currentSettings();
settings.P2P_AutoStart = true;
await core.services.control.applySettings();
const resumedReplicator = p2pReplicator.replicator;
await waitForServing(resumedReplicator, timeoutMs);
if (firstReplicator.server !== undefined) {
throw new Error("A setting event reopened the previous P2P replicator");
await waitForServing(p2pService, timeoutMs);
if (p2pReplicator.replicator !== compatibilityFacade) {
throw new Error("A setting event replaced the stable P2P service compatibility facade");
}
const encoded = new TextEncoder().encode(noteContent);
@@ -118,7 +127,7 @@ export async function runP2PReplicatorReplacementProbe(
});
await core.serviceModules.fileHandler.storeFileToDB(notePath as FilePathWithPrefix, true);
const replacementPeer = await communicateWithPeer(resumedReplicator, targetPeer, timeoutMs);
const replacementPeer = await communicateWithPeer(p2pService, targetPeer, timeoutMs);
if (replacementPeer.name !== firstPeer.name) {
throw new Error(
`The replacement replicator reached '${replacementPeer.name}' instead of the original peer '${firstPeer.name}'`
@@ -126,7 +135,7 @@ export async function runP2PReplicatorReplacementProbe(
}
core.services.context.standardIo.writeStdout(
`[Probe] P2P replicator replaced, old transport stayed closed, and ${notePath} was sent through the replacement.\n`
`[Probe] The active P2P adapter was replaced, the stable service reopened, and ${notePath} was sent through it.\n`
);
return true;
}
+1
View File
@@ -8,6 +8,7 @@
"test:decoupled-vault": "deno test --env-file=.test.env -A --no-check test-decoupled-vault.ts",
"test:remote-commands": "deno test --env-file=.test.env -A --no-check test-remote-commands.ts",
"test:settings-writeback": "deno test -A --no-check test-settings-writeback.ts",
"test:remote-administration-exit-codes": "deno test -A --no-check test-remote-administration-exit-codes.ts",
"test:push-pull": "deno test --env-file=.test.env -A --no-check test-push-pull.ts",
"test:setup-put-cat": "deno test --env-file=.test.env -A --no-check test-setup-put-cat.ts",
"test:mirror": "deno test --env-file=.test.env -A --no-check test-mirror.ts",
@@ -143,8 +143,12 @@ export async function createCompressionBenchmarkDataset(options: {
);
await copyRepositoryFile("json", "package.json", "package.json");
await copyRepositoryFile("json", "manifest.json", "manifest.json");
await copyRepositoryFile("ts", "src/modules/core/ModuleReplicator.ts", "ModuleReplicator.ts");
await copyRepositoryFile("ts", "src/modules/core/ReplicateResultProcessor.ts", "ReplicateResultProcessor.ts");
await copyRepositoryFile("ts", "src/serviceFeatures/replication/index.ts", "replicationFeature.ts");
await copyRepositoryFile(
"ts",
"src/serviceFeatures/replication/ReplicateResultProcessor.ts",
"ReplicateResultProcessor.ts"
);
const markdownBytes = await Deno.readFile(join(repositoryRoot, "docs/settings.md"));
const gzipPath = join(datasetRoot, "gz", "settings.md.gz");
+1
View File
@@ -1,5 +1,6 @@
const TASKS = [
"test:settings-writeback",
"test:remote-administration-exit-codes",
"test:setup-put-cat",
"test:mirror",
"test:daemon",
@@ -79,7 +79,6 @@ Deno.test("benchmark cases record scope and limitations for paper use", () => {
);
}
});
Deno.test("CouchDB latency proxy applies half the requested RTT in each direction", async () => {
const backendPort = getFreePort();
const proxyPort = getFreePort();
@@ -156,8 +155,8 @@ Deno.test("compression benchmark dataset covers representative file kinds determ
"images/quick-setup/guide-quick-setup-first-setup-uri.png",
"package.json",
"manifest.json",
"src/modules/core/ModuleReplicator.ts",
"src/modules/core/ReplicateResultProcessor.ts",
"src/serviceFeatures/replication/index.ts",
"src/serviceFeatures/replication/ReplicateResultProcessor.ts",
];
try {
for (const [index, relativePath] of repositoryFiles.entries()) {
@@ -39,7 +39,7 @@ async function runReplacementProbe(
};
}
Deno.test("p2p lifecycle: replacement keeps real CLI communication on the current replicator", async () => {
Deno.test("p2p lifecycle: active-adapter replacement keeps real CLI communication on the stable service", async () => {
const relay = Deno.env.get("RELAY") ?? "ws://localhost:4000/";
const peersTimeout = Number(Deno.env.get("PEERS_TIMEOUT") ?? "20");
const syncTimeout = Number(Deno.env.get("SYNC_TIMEOUT") ?? "60");
@@ -82,11 +82,8 @@ Deno.test("p2p lifecycle: replacement keeps real CLI communication on the curren
try {
await host.waitUntilContains("P2P host is running", 20000);
const probe = await runReplacementProbe(probeVault, probeSettings, hostPeerName, probeTimeoutMs);
assert(
probe.code === 0,
`P2P replacement probe failed\nstdout: ${probe.stdout}\nstderr: ${probe.stderr}`
);
assertStringIncludes(probe.stdout, "[Probe] P2P replicator replaced");
assert(probe.code === 0, `P2P replacement probe failed\nstdout: ${probe.stdout}\nstderr: ${probe.stderr}`);
assertStringIncludes(probe.stdout, "[Probe] The active P2P adapter was replaced");
const syncResult = await runCli(
verifierVault,
@@ -0,0 +1,77 @@
import { assertEquals, assertStringIncludes } from "@std/assert";
import { TempDir } from "./helpers/temp.ts";
import { runCli } from "./helpers/cli.ts";
import { applyCouchdbSettings, applyP2pSettings, applyP2pTestTweaks, initSettingsFile } from "./helpers/settings.ts";
async function prepareFixture(prefix: string) {
const workDir = await TempDir.create(prefix);
const settingsFile = workDir.join("settings.json");
const databaseDir = workDir.join("database");
await Deno.mkdir(databaseDir, { recursive: true });
await initSettingsFile(settingsFile);
return { workDir, settingsFile, databaseDir };
}
Deno.test("remote administration process exit policy distinguishes returned verification failure", async () => {
const fixture = await prepareFixture("livesync-cli-remote-admin-exit");
await using workDir = fixture.workDir;
const { settingsFile, databaseDir } = fixture;
await applyP2pSettings(
settingsFile,
"remote-admin-exit-room",
"remote-admin-exit-passphrase",
"remote-admin-exit-tests",
"ws://127.0.0.1:1/",
"~.*",
"none"
);
await applyP2pTestTweaks(settingsFile, "remote-admin-exit-device", "remote-admin-exit-passphrase");
const defaultFailure = await runCli(databaseDir, "--settings", settingsFile, "mark-resolved");
assertEquals(defaultFailure.code, 1, defaultFailure.combined);
assertStringIncludes(
defaultFailure.combined,
"[Verification] Remote administration is unavailable for this provider."
);
assertStringIncludes(defaultFailure.combined, "[Error] Command 'mark-resolved' failed");
const compatibilitySuccess = await runCli(
databaseDir,
"--settings",
settingsFile,
"--compat-remote-admin-exit-zero",
"mark-resolved"
);
assertEquals(compatibilitySuccess.code, 0, compatibilitySuccess.combined);
assertStringIncludes(
compatibilitySuccess.combined,
"[Verification] Remote administration is unavailable for this provider."
);
assertStringIncludes(compatibilitySuccess.combined, "[Done] Command 'mark-resolved' completed");
});
Deno.test("remote administration compatibility does not hide a thrown mutation failure", async () => {
const fixture = await prepareFixture("livesync-cli-remote-admin-mutation");
await using workDir = fixture.workDir;
const { settingsFile, databaseDir } = fixture;
await applyCouchdbSettings(
settingsFile,
"http://127.0.0.1:1/",
"unreachable-user",
"unreachable-password",
"unreachable-database"
);
const mutationFailure = await runCli(
databaseDir,
"--settings",
settingsFile,
"--compat-remote-admin-exit-zero",
"mark-resolved"
);
assertEquals(mutationFailure.code, 1, mutationFailure.combined);
assertStringIncludes(mutationFailure.combined, "[Command] mark-resolved");
assertStringIncludes(mutationFailure.combined, "[Error] Failed to start:");
});
+18 -1
View File
@@ -146,6 +146,13 @@ export class WebAppRuntime {
return this.paneHost;
}
/**
* Import local files and complete the readiness boundary needed by optional P2P.
*
* An unconfigured central remote cannot use the normal offline-scan path, so
* the explicit WebApp scan completes the same post-scan finalisation without
* treating the central remote as configured.
*/
async scanLocalFiles(): Promise<boolean> {
const core = this.core;
const fileAccess = this.platformServiceModules?.vaultAccess;
@@ -171,7 +178,17 @@ export class WebAppRuntime {
this.addLog(`Failed to import ${path}: ${String(error)}`, LOG_LEVEL_NOTICE, "scan");
}
}
return succeeded;
if (!succeeded || core.services.appLifecycle.isReady()) {
return succeeded;
}
if (!(await core.services.databaseEvents.onDatabaseInitialised(false))) {
return false;
}
if (!(await core.services.fileProcessing.commitPendingFileEvents())) {
return false;
}
core.services.appLifecycle.markIsReady();
return true;
}
async start(): Promise<void> {
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "livesync-webapp",
"private": true,
"version": "1.0.21-webapp",
"version": "1.0.23-webapp",
"type": "module",
"description": "Browser-based Self-hosted LiveSync using FileSystem API",
"scripts": {
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "webpeer",
"private": true,
"version": "1.0.21-webpeer",
"version": "1.0.23-webpeer",
"type": "module",
"scripts": {
"dev": "vite",
+1 -1
View File
@@ -53,7 +53,7 @@ export class P2PCheckSession {
try {
await runtime.start();
await runtime.currentReplicator.makeSureOpened();
await runtime.p2p.transportLifecycle.connect();
} catch (error) {
await this.stop();
throw error;
+5 -12
View File
@@ -3,10 +3,9 @@ import { compatGlobal } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFu
import { EVENT_LAYOUT_READY } from "@vrtmrz/livesync-commonlib/compat/events/coreEvents";
import type { PeerStatus } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PReplicatorPaneCommon";
import { P2PLogCollector } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PLogCollector";
import type { LiveSyncTrysteroReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/LiveSyncTrysteroReplicator";
import type { UseP2PReplicatorResult } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/UseP2PReplicatorResult";
import { useP2PReplicatorFeature } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/useP2PReplicatorFeature";
import { ServiceContext, type LiveSyncEventHub } from "@vrtmrz/livesync-commonlib/context";
import type { P2PServiceViews } from "@vrtmrz/livesync-commonlib/p2p";
import { unique } from "octagonal-wheels/collection";
import type { SimpleStore } from "octagonal-wheels/databases/SimpleStoreBase";
@@ -48,7 +47,7 @@ function removeFromList(item: string, list: string): string {
export class WebPeerRuntime {
readonly context: ServiceContext;
readonly services: LiveSyncBrowserServiceHub<ServiceContext>;
readonly p2p: UseP2PReplicatorResult;
readonly p2p: P2PServiceViews;
readonly p2pLogCollector: P2PLogCollector;
readonly paneHost: P2PReplicatorPaneHost;
@@ -87,10 +86,6 @@ export class WebPeerRuntime {
return this.context.events;
}
get currentReplicator(): LiveSyncTrysteroReplicator {
return this.p2p.replicator;
}
get settings(): P2PSyncSetting {
return this.services.setting.currentSettings();
}
@@ -119,9 +114,7 @@ export class WebPeerRuntime {
}
this.services.appLifecycle.markIsReady();
this.events.emitEvent(EVENT_LAYOUT_READY);
if (this.settings.P2P_AutoStart && this.settings.P2P_Enabled) {
compatGlobal.setTimeout(() => void this.currentReplicator.open(), 100);
}
await this.services.appLifecycle.onResumed();
return this;
}
@@ -151,12 +144,12 @@ export class WebPeerRuntime {
this.menu = new Menu()
.addItem((item) =>
item.setTitle("📥 Only fetch").onClick(async () => {
await this.currentReplicator.replicateFrom(peer.peerId);
await this.p2p.targetedTransfer.pullFromPeer(peer.peerId);
})
)
.addItem((item) =>
item.setTitle("📤 Only send").onClick(async () => {
await this.currentReplicator.requestSynchroniseToPeer(peer.peerId);
await this.p2p.targetedTransfer.requestPushToPeer(peer.peerId);
})
)
.addSeparator()
+283
View File
@@ -0,0 +1,283 @@
import {
MILESTONE_DOCID,
type EntryMilestoneInfo,
type RemoteDBSettings,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { LiveSyncCouchDBReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import type { LiveSyncJournalReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/journal/LiveSyncJournalReplicator";
import {
CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS,
CENTRAL_REMOTE_ADMINISTRATION_OBSERVATION_KINDS,
applyCentralRemoteAdministrationMutation,
milestoneSatisfiesCentralRemoteAdministration,
centralRemoteAdministrationVerificationFailed,
centralRemoteAdministrationVerified,
supportedCapability,
type MilestoneCentralRemoteAdministrationObservation,
type CentralRemoteAdministrationFailureReason,
type CentralRemoteAdministrationRequest,
type CentralRemoteAdministrationReplicator,
type CentralRemoteAdministrationResult,
type CentralRemoteAdministrationRunner,
type ReplicatorInstance,
type SupportedCapability,
} from "@vrtmrz/livesync-commonlib/replication";
/**
* Central milestone administration shared by the two central providers.
*
* The provider definition selects a reader before mutation. CouchDB then owns
* a fresh verification connection, while Object Storage borrows the active
* Journal client. Local node identity is established before either mutation.
*/
const JOURNAL_MILESTONE_PATH = "_00000000-milestone.json";
/** A provider read result, including failures which settled without a throw. */
type CentralMilestoneReadResult =
| { readonly milestone: EntryMilestoneInfo | false | undefined }
| { readonly failureReason: CentralRemoteAdministrationFailureReason; readonly detail?: unknown };
/** A settings-bound postcondition reader prepared before remote mutation. */
type PreparedCentralMilestoneReader = () => Promise<CentralMilestoneReadResult>;
/** Select and validate the provider-specific reader without performing I/O. */
type CentralMilestoneReaderPreparer = (
replicator: CentralRemoteAdministrationReplicator,
setting: RemoteDBSettings
) => PreparedCentralMilestoneReader;
type CouchDBAdministrationReplicator = CentralRemoteAdministrationReplicator &
Pick<LiveSyncCouchDBReplicator, "connectRemoteCouchDBWithSetting" | "isMobile">;
type JournalAdministrationClient = Pick<LiveSyncJournalReplicator["client"], "downloadJsonWithResult">;
function isCentralRemoteAdministrationReplicator(
replicator: ReplicatorInstance
): replicator is CentralRemoteAdministrationReplicator {
return (
"nodeid" in replicator &&
typeof replicator.nodeid === "string" &&
"markRemoteResolved" in replicator &&
typeof replicator.markRemoteResolved === "function" &&
"markRemoteLocked" in replicator &&
typeof replicator.markRemoteLocked === "function"
);
}
async function ensureLocalNodeIdentity(
replicator: CentralRemoteAdministrationReplicator
): Promise<CentralRemoteAdministrationResult | undefined> {
if (replicator.nodeid) {
return undefined;
}
if ((await replicator.initializeDatabaseForReplication()) && replicator.nodeid) {
return undefined;
}
return centralRemoteAdministrationVerificationFailed(
CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.LOCAL_IDENTITY_UNAVAILABLE
);
}
function observeMilestone(
replicator: CentralRemoteAdministrationReplicator,
milestone: EntryMilestoneInfo
): MilestoneCentralRemoteAdministrationObservation {
return {
kind: CENTRAL_REMOTE_ADMINISTRATION_OBSERVATION_KINDS.MILESTONE,
locked: !!milestone.locked,
accepted: !!milestone.accepted_nodes?.includes(replicator.nodeid),
nodeId: replicator.nodeid,
};
}
function resultFromMilestone(
replicator: CentralRemoteAdministrationReplicator,
request: CentralRemoteAdministrationRequest,
milestone: EntryMilestoneInfo | false | undefined
): CentralRemoteAdministrationResult {
if (!milestone) {
return centralRemoteAdministrationVerificationFailed(
CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.MILESTONE_NOT_FOUND
);
}
const observation = observeMilestone(replicator, milestone);
return milestoneSatisfiesCentralRemoteAdministration(request.action, observation)
? centralRemoteAdministrationVerified(observation)
: centralRemoteAdministrationVerificationFailed(
CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.POSTCONDITION_MISMATCH,
{
observation,
}
);
}
/**
* Apply and verify the central milestone protocol without selecting a provider.
*
* The provider definition has already selected the reader preparer. Preparing
* it before mutation rejects incomplete composition before a remote write and
* binds any provider-owned client which must be used for postcondition reading.
*/
async function runCentralRemoteAdministration(
replicator: CentralRemoteAdministrationReplicator,
setting: RemoteDBSettings,
request: CentralRemoteAdministrationRequest,
prepareMilestoneReader: CentralMilestoneReaderPreparer
): Promise<CentralRemoteAdministrationResult> {
const identityFailure = await ensureLocalNodeIdentity(replicator);
if (identityFailure) return identityFailure;
const readMilestone = prepareMilestoneReader(replicator, setting);
await applyCentralRemoteAdministrationMutation(replicator, setting, request.action);
const readResult = await readMilestone();
if ("failureReason" in readResult) {
return centralRemoteAdministrationVerificationFailed(readResult.failureReason, { detail: readResult.detail });
}
return resultFromMilestone(replicator, request, readResult.milestone);
}
function requireCouchDBAdministrationOperations(
replicator: CentralRemoteAdministrationReplicator
): asserts replicator is CouchDBAdministrationReplicator {
if (
!("connectRemoteCouchDBWithSetting" in replicator) ||
typeof replicator.connectRemoteCouchDBWithSetting !== "function" ||
!("isMobile" in replicator) ||
typeof replicator.isMobile !== "function"
) {
throw new Error("The configured CouchDB administration adapter does not provide milestone access.");
}
}
function prepareCouchDBMilestoneReader(
replicator: CentralRemoteAdministrationReplicator,
setting: RemoteDBSettings
): PreparedCentralMilestoneReader {
requireCouchDBAdministrationOperations(replicator);
return async () => {
// This verification connection is fresh and owned by this read. It is
// always closed here rather than retained by the active Replicator.
let connection: Awaited<ReturnType<CouchDBAdministrationReplicator["connectRemoteCouchDBWithSetting"]>>;
try {
connection = await replicator.connectRemoteCouchDBWithSetting(setting, replicator.isMobile(), true);
} catch (error) {
return { failureReason: CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.CONNECTION_FAILED, detail: error };
}
if (typeof connection === "string") {
return {
failureReason: CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.CONNECTION_FAILED,
detail: connection,
};
}
let milestone: EntryMilestoneInfo | undefined;
let observationError: unknown;
try {
milestone = await connection.db.get<EntryMilestoneInfo>(MILESTONE_DOCID);
} catch (error) {
observationError = error;
}
try {
await connection.close();
} catch (error) {
observationError ??= error;
}
if (observationError !== undefined) {
return {
failureReason: CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.MILESTONE_READ_FAILED,
detail: observationError,
};
}
return { milestone };
};
}
function isJournalAdministrationClient(client: unknown): client is JournalAdministrationClient {
return (
typeof client === "object" &&
client !== null &&
"downloadJsonWithResult" in client &&
typeof client.downloadJsonWithResult === "function"
);
}
function requireJournalAdministrationClient(
replicator: CentralRemoteAdministrationReplicator
): JournalAdministrationClient {
if (!("client" in replicator) || !isJournalAdministrationClient(replicator.client)) {
throw new Error("The configured Object Storage administration adapter does not provide milestone access.");
}
return replicator.client;
}
function assertNeverJournalStorageRead(result: never): never {
throw new Error(`Unexpected Journal storage read result: ${String(result)}`);
}
function prepareObjectStorageMilestoneReader(
replicator: CentralRemoteAdministrationReplicator
): PreparedCentralMilestoneReader {
// The Journal client belongs to the active Replicator. This reader borrows
// it for the provider's distinct milestone path and must not dispose it.
const client = requireJournalAdministrationClient(replicator);
return async () => {
try {
const result = await client.downloadJsonWithResult<EntryMilestoneInfo>(JOURNAL_MILESTONE_PATH);
switch (result.status) {
case "available":
return { milestone: result.value };
case "not-found":
return { milestone: undefined };
case "unavailable":
return {
failureReason: CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.MILESTONE_READ_FAILED,
detail: result.error,
};
default:
return assertNeverJournalStorageRead(result);
}
} catch (error) {
return {
failureReason: CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.MILESTONE_READ_FAILED,
detail: error,
};
}
};
}
const runCouchDBCentralRemoteAdministration: CentralRemoteAdministrationRunner = async (
replicator,
setting,
request
) => {
if (!isCentralRemoteAdministrationReplicator(replicator)) {
return centralRemoteAdministrationVerificationFailed(
CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.CAPABILITY_NOT_APPLICABLE
);
}
return await runCentralRemoteAdministration(replicator, setting, request, prepareCouchDBMilestoneReader);
};
const runObjectStorageCentralRemoteAdministration: CentralRemoteAdministrationRunner = async (
replicator,
setting,
request
) => {
if (!isCentralRemoteAdministrationReplicator(replicator)) {
return centralRemoteAdministrationVerificationFailed(
CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.CAPABILITY_NOT_APPLICABLE
);
}
return await runCentralRemoteAdministration(replicator, setting, request, prepareObjectStorageMilestoneReader);
};
/** CouchDB mutation and milestone postcondition verification capability. */
export const COUCHDB_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY: SupportedCapability<CentralRemoteAdministrationRunner> =
supportedCapability(runCouchDBCentralRemoteAdministration);
/** Object Storage mutation and milestone postcondition verification capability. */
export const OBJECT_STORAGE_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY: SupportedCapability<CentralRemoteAdministrationRunner> =
supportedCapability(runObjectStorageCentralRemoteAdministration);
@@ -0,0 +1,260 @@
import { describe, expect, it, vi } from "vitest";
import { DEFAULT_SETTINGS, REMOTE_COUCHDB, REMOTE_MINIO } from "@vrtmrz/livesync-commonlib/compat/common/types";
import {
CENTRAL_REMOTE_ADMINISTRATION_ACTIONS,
CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS,
CENTRAL_REMOTE_ADMINISTRATION_OBSERVATION_KINDS,
CENTRAL_REMOTE_ADMINISTRATION_RESULT_STATUSES,
} from "@vrtmrz/livesync-commonlib/replication";
import {
COUCHDB_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY,
OBJECT_STORAGE_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY,
} from "./centralRemoteAdministration";
describe("central remote administration capabilities", () => {
it("mutates CouchDB, verifies the requested postcondition, and closes only the owned connection", async () => {
const rawDatabaseClose = vi.fn(async () => undefined);
const close = vi.fn(async () => undefined);
const database = {
get: vi.fn(async () => ({ locked: true, accepted_nodes: ["node-1"] })),
close: rawDatabaseClose,
};
const replicator = {
nodeid: "node-1",
initializeDatabaseForReplication: vi.fn(async () => true),
isMobile: vi.fn(() => false),
markRemoteLocked: vi.fn(async () => undefined),
markRemoteResolved: vi.fn(async () => undefined),
connectRemoteCouchDBWithSetting: vi.fn(async () => ({ db: database, close })),
};
const setting = { ...DEFAULT_SETTINGS, remoteType: REMOTE_COUCHDB };
const capability = COUCHDB_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY;
await expect(
capability.run(replicator as never, setting, { action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.LOCK })
).resolves.toEqual({
status: CENTRAL_REMOTE_ADMINISTRATION_RESULT_STATUSES.VERIFIED,
observation: {
kind: CENTRAL_REMOTE_ADMINISTRATION_OBSERVATION_KINDS.MILESTONE,
locked: true,
accepted: true,
nodeId: "node-1",
},
});
expect(replicator.markRemoteLocked).toHaveBeenCalledWith(setting, true, false);
expect(close).toHaveBeenCalledOnce();
expect(rawDatabaseClose).not.toHaveBeenCalled();
});
it("returns a typed CouchDB failure when the observed milestone does not satisfy the action", async () => {
const close = vi.fn(async () => undefined);
const replicator = {
nodeid: "node-1",
initializeDatabaseForReplication: vi.fn(async () => true),
isMobile: vi.fn(() => false),
markRemoteLocked: vi.fn(async () => undefined),
markRemoteResolved: vi.fn(async () => undefined),
connectRemoteCouchDBWithSetting: vi.fn(async () => ({
db: { get: vi.fn(async () => ({ locked: false, accepted_nodes: ["node-1"] })) },
close,
})),
};
const capability = COUCHDB_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY;
const result = await capability.run(
replicator as never,
{ ...DEFAULT_SETTINGS, remoteType: REMOTE_COUCHDB },
{
action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.LOCK,
}
);
expect(result).toMatchObject({
status: CENTRAL_REMOTE_ADMINISTRATION_RESULT_STATUSES.VERIFICATION_FAILED,
reason: CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.POSTCONDITION_MISMATCH,
observation: { kind: CENTRAL_REMOTE_ADMINISTRATION_OBSERVATION_KINDS.MILESTONE, locked: false },
});
expect(close).toHaveBeenCalledOnce();
});
it("does not mutate when initialisation succeeds without publishing a local node identity", async () => {
const replicator = {
nodeid: "",
initializeDatabaseForReplication: vi.fn(async () => true),
isMobile: vi.fn(() => false),
markRemoteLocked: vi.fn(async () => undefined),
markRemoteResolved: vi.fn(async () => undefined),
connectRemoteCouchDBWithSetting: vi.fn(async () => "must not connect"),
};
const capability = COUCHDB_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY;
await expect(
capability.run(
replicator as never,
{ ...DEFAULT_SETTINGS, remoteType: REMOTE_COUCHDB },
{ action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.MARK_RESOLVED }
)
).resolves.toEqual({
status: CENTRAL_REMOTE_ADMINISTRATION_RESULT_STATUSES.VERIFICATION_FAILED,
reason: CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.LOCAL_IDENTITY_UNAVAILABLE,
});
expect(replicator.markRemoteResolved).not.toHaveBeenCalled();
expect(replicator.connectRemoteCouchDBWithSetting).not.toHaveBeenCalled();
});
it("allows a CouchDB mutation exception to reject before verification", async () => {
const failure = new Error("write failed");
const replicator = {
nodeid: "node-1",
initializeDatabaseForReplication: vi.fn(async () => true),
isMobile: vi.fn(() => false),
markRemoteLocked: vi.fn(async () => {
throw failure;
}),
markRemoteResolved: vi.fn(async () => undefined),
connectRemoteCouchDBWithSetting: vi.fn(),
};
const capability = COUCHDB_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY;
await expect(
capability.run(
replicator as never,
{ ...DEFAULT_SETTINGS, remoteType: REMOTE_COUCHDB },
{
action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.UNLOCK,
}
)
).rejects.toBe(failure);
expect(replicator.connectRemoteCouchDBWithSetting).not.toHaveBeenCalled();
});
it("mutates Object Storage and verifies its milestone postcondition", async () => {
const milestone = { locked: false, accepted_nodes: ["node-1"] };
const downloadJson = vi.fn(async () => milestone);
const downloadJsonWithResult = vi.fn(async () => ({
status: "available" as const,
value: milestone,
}));
const replicator = {
nodeid: "node-1",
initializeDatabaseForReplication: vi.fn(async () => true),
markRemoteLocked: vi.fn(async () => undefined),
markRemoteResolved: vi.fn(async () => undefined),
client: { downloadJson, downloadJsonWithResult },
};
const setting = { ...DEFAULT_SETTINGS, remoteType: REMOTE_MINIO };
const capability = OBJECT_STORAGE_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY;
await expect(
capability.run(replicator as never, setting, {
action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.MARK_RESOLVED,
})
).resolves.toEqual({
status: CENTRAL_REMOTE_ADMINISTRATION_RESULT_STATUSES.VERIFIED,
observation: {
kind: CENTRAL_REMOTE_ADMINISTRATION_OBSERVATION_KINDS.MILESTONE,
locked: false,
accepted: true,
nodeId: "node-1",
},
});
expect(replicator.markRemoteResolved).toHaveBeenCalledWith(setting);
expect(downloadJsonWithResult).toHaveBeenCalledWith("_00000000-milestone.json");
expect(downloadJson).not.toHaveBeenCalled();
});
it("keeps a missing Object Storage milestone as an unverified postcondition", async () => {
const downloadJson = vi.fn(async () => false);
const downloadJsonWithResult = vi.fn(async () => ({ status: "not-found" as const }));
const replicator = {
nodeid: "node-1",
initializeDatabaseForReplication: vi.fn(async () => true),
markRemoteLocked: vi.fn(async () => undefined),
markRemoteResolved: vi.fn(async () => undefined),
client: { downloadJson, downloadJsonWithResult },
};
const result = await OBJECT_STORAGE_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY.run(
replicator as never,
{ ...DEFAULT_SETTINGS, remoteType: REMOTE_MINIO },
{ action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.MARK_RESOLVED }
);
expect(result).toEqual({
status: CENTRAL_REMOTE_ADMINISTRATION_RESULT_STATUSES.VERIFICATION_FAILED,
reason: CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.MILESTONE_NOT_FOUND,
});
expect(downloadJsonWithResult).toHaveBeenCalledWith("_00000000-milestone.json");
expect(downloadJson).not.toHaveBeenCalled();
});
it("returns a typed failure with diagnostic detail when Object Storage milestone reading is unavailable", async () => {
const diagnostic = new Error("object storage unavailable");
const downloadJson = vi.fn(async () => false);
const downloadJsonWithResult = vi.fn(async () => ({
status: "unavailable" as const,
error: diagnostic,
}));
const replicator = {
nodeid: "node-1",
initializeDatabaseForReplication: vi.fn(async () => true),
markRemoteLocked: vi.fn(async () => undefined),
markRemoteResolved: vi.fn(async () => undefined),
client: { downloadJson, downloadJsonWithResult },
};
const result = await OBJECT_STORAGE_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY.run(
replicator as never,
{ ...DEFAULT_SETTINGS, remoteType: REMOTE_MINIO },
{ action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.MARK_RESOLVED }
);
expect(result).toEqual({
status: CENTRAL_REMOTE_ADMINISTRATION_RESULT_STATUSES.VERIFICATION_FAILED,
reason: CENTRAL_REMOTE_ADMINISTRATION_FAILURE_REASONS.MILESTONE_READ_FAILED,
detail: diagnostic,
});
expect(downloadJsonWithResult).toHaveBeenCalledWith("_00000000-milestone.json");
expect(downloadJson).not.toHaveBeenCalled();
});
it("rejects an incomplete CouchDB milestone adapter before mutation", async () => {
const markRemoteLocked = vi.fn(async () => undefined);
const replicator = {
nodeid: "node-1",
initializeDatabaseForReplication: vi.fn(async () => true),
markRemoteLocked,
markRemoteResolved: vi.fn(async () => undefined),
};
await expect(
COUCHDB_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY.run(
replicator as never,
{ ...DEFAULT_SETTINGS, remoteType: REMOTE_COUCHDB },
{ action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.LOCK }
)
).rejects.toThrow("The configured CouchDB administration adapter does not provide milestone access.");
expect(markRemoteLocked).not.toHaveBeenCalled();
});
it("rejects an Object Storage adapter which only exposes lossy milestone reading", async () => {
const markRemoteResolved = vi.fn(async () => undefined);
const replicator = {
nodeid: "node-1",
initializeDatabaseForReplication: vi.fn(async () => true),
markRemoteLocked: vi.fn(async () => undefined),
markRemoteResolved,
client: { downloadJson: vi.fn(async () => false) },
};
await expect(
OBJECT_STORAGE_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY.run(
replicator as never,
{ ...DEFAULT_SETTINGS, remoteType: REMOTE_MINIO },
{ action: CENTRAL_REMOTE_ADMINISTRATION_ACTIONS.MARK_RESOLVED }
)
).rejects.toThrow("The configured Object Storage administration adapter does not provide milestone access.");
expect(markRemoteResolved).not.toHaveBeenCalled();
});
});
@@ -177,6 +177,7 @@ describe("packaged Commonlib compatibility gate", () => {
databaseService: {},
fileProcessingService: { commitPendingFileEvents: vi.fn().mockResolvedValue(true) },
replicatorService: {
acquireActiveReplicatorContext: vi.fn().mockResolvedValue(undefined),
getActiveReplicator: () => ({ openReplication }),
runFiniteReplicationActivity,
},
@@ -9791,6 +9791,10 @@ export const allMessages: Readonly<Record<string, Readonly<Record<string, string
zh: "仅供测试 - 通过同步文件的较新副本来解决文件冲突,这可能会覆盖修改过的文件。请注意 ",
"zh-tw": "僅供測試 —— 透過同步較新的檔案版本解決衝突,這可能會覆寫已修改的檔案,請注意。",
},
"The connection test cannot add a signalling relay while P2P is active. Use the active relay settings, or disconnect P2P before testing.":
{
def: "The connection test cannot add a signalling relay while P2P is active. Use the active relay settings, or disconnect P2P before testing.",
},
"The connection to the server has been configured successfully. As the next step,": {
def: "The connection to the server has been configured successfully. As the next step,",
es: "La conexión con el servidor se ha configurado correctamente. Como paso siguiente,",
+1
View File
@@ -1062,6 +1062,7 @@
"Target patterns": "Target patterns",
"Test Settings and Continue": "Test Settings and Continue",
"Testing only - Resolve file conflicts by syncing newer copies of the file, this can overwrite modified files. Be Warned.": "Testing only - Resolve file conflicts by syncing newer copies of the file, this can overwrite modified files. Be Warned.",
"The connection test cannot add a signalling relay while P2P is active. Use the active relay settings, or disconnect P2P before testing.": "The connection test cannot add a signalling relay while P2P is active. Use the active relay settings, or disconnect P2P before testing.",
"The connection to the server has been configured successfully. As the next step,": "The connection to the server has been configured successfully. As the next step,",
"The delay for consecutive on-demand fetches": "The delay for consecutive on-demand fetches",
"The files in this Vault are almost identical to the server's.": "The files in this Vault are almost identical to the server's.",
+1
View File
@@ -362,6 +362,7 @@ Export: Export
"Failed to connect to the server: ${reason}": "Failed to connect to the server: ${reason}"
Failed to connect to the server. Please check your settings.: Failed to connect to the server. Please check your settings.
"Failed to connect to the signalling relay: ${reason}": "Failed to connect to the signalling relay: ${reason}"
The connection test cannot add a signalling relay while P2P is active. Use the active relay settings, or disconnect P2P before testing.: The connection test cannot add a signalling relay while P2P is active. Use the active relay settings, or disconnect P2P before testing.
Failed to create replicator instance.: Failed to create replicator instance.
Failed to parse Setup-URI.: Failed to parse Setup-URI.
"Failed:": "Failed:"
+18
View File
@@ -0,0 +1,18 @@
/**
* Run a finite operation with a flow-owned remote resource and release it
* after either success or failure.
*
* Resource implementations make `dispose()` idempotent. This helper makes the
* caller's ownership boundary explicit and prevents finite flows from leaking
* a provider-owned resource when their operation rejects.
*/
export async function withOwnedRemoteResource<TResource extends { dispose(): Promise<void> }, TResult>(
resource: TResource,
operation: (ownedResource: TResource) => Promise<TResult>
): Promise<TResult> {
try {
return await operation(resource);
} finally {
await resource.dispose();
}
}
@@ -0,0 +1,28 @@
import { describe, expect, it, vi } from "vitest";
import { withOwnedRemoteResource } from "./ownedRemoteResource";
describe("flow-owned remote resources", () => {
it("disposes a resource after a successful finite operation", async () => {
const dispose = vi.fn(async () => undefined);
const resource = { dispose };
await expect(
withOwnedRemoteResource(resource, async (owned) => (owned === resource ? "done" : "wrong"))
).resolves.toBe("done");
expect(dispose).toHaveBeenCalledOnce();
});
it("disposes a resource when the finite operation rejects", async () => {
const dispose = vi.fn(async () => undefined);
const error = new Error("resource operation failed");
await expect(
withOwnedRemoteResource({ dispose }, async () => {
throw error;
})
).rejects.toBe(error);
expect(dispose).toHaveBeenCalledOnce();
});
});
@@ -0,0 +1,102 @@
import type { RemoteDBSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
type EndpointProjection = readonly [kind: "url" | "invalid-url", value: string];
/**
* Compare the effective endpoint rather than inconsequential URI spelling.
* Fragments are not sent, query order is immaterial, and redundant trailing
* slashes do not bind a different adapter. Invalid input is retained verbatim
* and tagged so comparison remains deterministic and fails closed.
*/
function projectEndpoint(value: string): EndpointProjection {
try {
const endpoint = new URL(value);
endpoint.hash = "";
endpoint.searchParams.sort();
while (endpoint.pathname.length > 1 && endpoint.pathname.endsWith("/")) {
endpoint.pathname = endpoint.pathname.slice(0, -1);
}
return ["url", endpoint.toString()];
} catch {
return ["invalid-url", value];
}
}
/**
* Mirror the effective custom-header parser: trim each first name/value pair,
* ignore incomplete lines, and let the last duplicate name win. Sorting the
* resulting entries prevents line order alone from replacing a Replicator.
*/
function projectHeaders(value: string): readonly (readonly [name: string, value: string])[] {
const headers = new Map<string, string>();
for (const line of value.split("\n")) {
const [name, headerValue] = line.split(":", 2).map((part) => part.trim());
if (name && headerValue) {
headers.set(name, headerValue);
}
}
return [...headers.entries()].sort(([leftName, leftValue], [rightName, rightValue]) => {
const nameOrder = leftName.localeCompare(rightName);
return nameOrder || leftValue.localeCompare(rightValue);
});
}
function projectRemoteSecurity(settings: RemoteDBSettings) {
return settings.encrypt
? ([
"encrypted",
settings.passphrase,
settings.useDynamicIterationCount,
settings.E2EEAlgorithm,
settings.permitEmptyPassphrase,
] as const)
: (["plain"] as const);
}
/**
* Project the effective CouchDB connection settings to a private comparison identity.
* The returned value can contain credentials and must not be logged, persisted, or displayed.
*/
export function getCouchDBReplicatorConfigurationIdentity(settings: RemoteDBSettings): string {
const authentication = settings.useJWT
? ([
"jwt",
settings.jwtAlgorithm,
settings.jwtKey,
settings.jwtKid,
settings.jwtSub,
settings.jwtExpDuration,
] as const)
: (["basic", settings.couchDB_USER, settings.couchDB_PASSWORD] as const);
return JSON.stringify([
"couchdb",
projectEndpoint(settings.couchDB_URI),
settings.couchDB_DBNAME,
authentication,
projectHeaders(settings.couchDB_CustomHeaders),
settings.useRequestAPI,
settings.disableRequestURI,
projectRemoteSecurity(settings),
settings.enableCompression,
]);
}
/**
* Project the effective Object Storage connection settings to a private comparison identity.
* The returned value can contain credentials and must not be logged, persisted, or displayed.
*/
export function getObjectStorageReplicatorConfigurationIdentity(settings: RemoteDBSettings): string {
return JSON.stringify([
"s3",
projectEndpoint(settings.endpoint),
settings.bucket,
settings.bucketPrefix,
settings.region,
settings.accessKey,
settings.secretKey,
settings.forcePathStyle,
settings.useCustomRequestHandler,
projectHeaders(settings.bucketCustomHeaders),
projectRemoteSecurity(settings),
]);
}
@@ -0,0 +1,174 @@
import { describe, expect, it } from "vitest";
import type { ObsidianLiveSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { createNewVaultSettings } from "@vrtmrz/livesync-commonlib/settings";
import {
getCouchDBReplicatorConfigurationIdentity,
getObjectStorageReplicatorConfigurationIdentity,
} from "./replicatorConfigurationIdentity";
describe("active Replicator configuration identity", () => {
function configuredSettings(overrides: Partial<ObsidianLiveSyncSettings> = {}): ObsidianLiveSyncSettings {
return Object.assign(createNewVaultSettings(), {
activeConfigurationId: "profile-a",
couchDB_URI: "https://couch.example.test/base",
couchDB_USER: "alice",
couchDB_PASSWORD: "secret-a",
couchDB_DBNAME: "vault",
couchDB_CustomHeaders: "X-Second: two\nX-First: one",
endpoint: "https://objects.example.test/base",
accessKey: "alice",
secretKey: "secret-a",
bucket: "vault",
bucketPrefix: "notes/",
region: "auto",
bucketCustomHeaders: "X-Second: two\nX-First: one",
encrypt: true,
passphrase: "encryption-a",
useDynamicIterationCount: false,
permitEmptyPassphrase: false,
enableCompression: false,
...overrides,
});
}
it.each([
["couchDB_URI", "https://other.example.test/base"],
["couchDB_DBNAME", "other-vault"],
["couchDB_USER", "bob"],
["couchDB_PASSWORD", "secret-b"],
["couchDB_CustomHeaders", "X-First: changed"],
["useRequestAPI", true],
["disableRequestURI", true],
["encrypt", false],
["passphrase", "encryption-b"],
["useDynamicIterationCount", true],
["E2EEAlgorithm", ""],
["permitEmptyPassphrase", true],
["enableCompression", true],
] satisfies Array<[keyof ObsidianLiveSyncSettings, ObsidianLiveSyncSettings[keyof ObsidianLiveSyncSettings]]>)(
"detects a CouchDB %s change",
(key, value) => {
const settings = configuredSettings();
expect(getCouchDBReplicatorConfigurationIdentity({ ...settings, [key]: value })).not.toBe(
getCouchDBReplicatorConfigurationIdentity(settings)
);
}
);
it("ignores persisted central profile identity when the effective connection settings match", () => {
const settings = configuredSettings({ activeConfigurationId: "profile-a" });
const otherProfile = { ...settings, activeConfigurationId: "profile-b" };
expect(getCouchDBReplicatorConfigurationIdentity(otherProfile)).toBe(
getCouchDBReplicatorConfigurationIdentity(settings)
);
expect(getObjectStorageReplicatorConfigurationIdentity(otherProfile)).toBe(
getObjectStorageReplicatorConfigurationIdentity(settings)
);
});
it("projects only the active CouchDB authentication mode", () => {
const basic = configuredSettings({ useJWT: false, jwtKey: "inactive-a" });
expect(getCouchDBReplicatorConfigurationIdentity({ ...basic, jwtKey: "inactive-b" })).toBe(
getCouchDBReplicatorConfigurationIdentity(basic)
);
const jwt = configuredSettings({
useJWT: true,
jwtAlgorithm: "HS256",
jwtKey: "jwt-a",
jwtKid: "kid-a",
jwtSub: "subject-a",
jwtExpDuration: 5,
});
expect(getCouchDBReplicatorConfigurationIdentity({ ...jwt, couchDB_PASSWORD: "inactive" })).toBe(
getCouchDBReplicatorConfigurationIdentity(jwt)
);
expect(getCouchDBReplicatorConfigurationIdentity({ ...jwt, jwtKey: "jwt-b" })).not.toBe(
getCouchDBReplicatorConfigurationIdentity(jwt)
);
});
it.each([
["endpoint", "https://other.example.test/base"],
["bucket", "other-vault"],
["bucketPrefix", "archive/"],
["region", "eu-west-1"],
["accessKey", "bob"],
["secretKey", "secret-b"],
["forcePathStyle", false],
["useCustomRequestHandler", true],
["bucketCustomHeaders", "X-First: changed"],
["encrypt", false],
["passphrase", "encryption-b"],
["useDynamicIterationCount", true],
["E2EEAlgorithm", ""],
["permitEmptyPassphrase", true],
] satisfies Array<[keyof ObsidianLiveSyncSettings, ObsidianLiveSyncSettings[keyof ObsidianLiveSyncSettings]]>)(
"detects an Object Storage %s change",
(key, value) => {
const settings = configuredSettings();
expect(getObjectStorageReplicatorConfigurationIdentity({ ...settings, [key]: value })).not.toBe(
getObjectStorageReplicatorConfigurationIdentity(settings)
);
}
);
it("normalises endpoint and header representation without using the setup URI grammar", () => {
const settings = configuredSettings();
const couchIdentity = getCouchDBReplicatorConfigurationIdentity(settings);
const objectStorageIdentity = getObjectStorageReplicatorConfigurationIdentity(settings);
expect(
getCouchDBReplicatorConfigurationIdentity({
...settings,
couchDB_URI: "https://couch.example.test:443/base/",
couchDB_CustomHeaders: "X-First: one\nX-Second: two",
})
).toBe(couchIdentity);
expect(
getObjectStorageReplicatorConfigurationIdentity({
...settings,
endpoint: "https://objects.example.test:443/base/",
bucketCustomHeaders: "X-First: one\nX-Second: two",
})
).toBe(objectStorageIdentity);
});
it("ignores inactive remote-security credentials", () => {
const settings = configuredSettings({ encrypt: false, passphrase: "inactive-a" });
expect(
getCouchDBReplicatorConfigurationIdentity({
...settings,
passphrase: "inactive-b",
useDynamicIterationCount: !settings.useDynamicIterationCount,
E2EEAlgorithm: "",
permitEmptyPassphrase: !settings.permitEmptyPassphrase,
})
).toBe(getCouchDBReplicatorConfigurationIdentity(settings));
expect(
getObjectStorageReplicatorConfigurationIdentity({
...settings,
passphrase: "inactive-b",
useDynamicIterationCount: !settings.useDynamicIterationCount,
E2EEAlgorithm: "",
permitEmptyPassphrase: !settings.permitEmptyPassphrase,
})
).toBe(getObjectStorageReplicatorConfigurationIdentity(settings));
});
it("keeps malformed endpoints deterministic and scoped", () => {
const settings = configuredSettings({ couchDB_URI: "not a URL", endpoint: "also not a URL" });
expect(() => getCouchDBReplicatorConfigurationIdentity(settings)).not.toThrow();
expect(() => getObjectStorageReplicatorConfigurationIdentity(settings)).not.toThrow();
expect(
getCouchDBReplicatorConfigurationIdentity({ ...settings, couchDB_URI: "different invalid URL" })
).not.toBe(getCouchDBReplicatorConfigurationIdentity(settings));
const unrelatedPluginChange = { ...settings, displayLanguage: "ja" };
expect(getObjectStorageReplicatorConfigurationIdentity(unrelatedPluginChange)).toBe(
getObjectStorageReplicatorConfigurationIdentity(settings)
);
});
});
+161
View File
@@ -0,0 +1,161 @@
import { REMOTE_COUCHDB, REMOTE_MINIO, type RemoteDBSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import {
CAPABILITY_NOT_APPLICABLE,
CENTRAL_REMOTE_REPLICATION_READINESS,
NO_INTERACTION,
REPLICATION_PROGRESS_PRESENTATIONS,
REMOTE_RESOURCE_KINDS,
defineReplicatorProviderDefinitions,
supportedOpenReplicationContinuous,
replicationBlocked,
replicationFailed,
supportedStopActiveTransfer,
supportedCapability,
type ReplicatorProviderDefinitionMap,
type ReplicationOutcome,
type ReplicatorInstance,
type UserInitiatedOneShotRunner,
type UnattendedOneShotRunner,
} from "@vrtmrz/livesync-commonlib/replication";
import {
LiveSyncCouchDBReplicator,
type LiveSyncCouchDBReplicatorEnv,
} from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import { LiveSyncJournalReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/journal/LiveSyncJournalReplicator";
import {
getCouchDBReplicatorConfigurationIdentity,
getObjectStorageReplicatorConfigurationIdentity,
} from "./replicatorConfigurationIdentity";
import {
createCouchDBConnectionProbeFactory,
createCouchDBPreferredTweakProbeFactory,
createCouchDBSecuritySeedResourceFactory,
createCouchDBSynchronisationInformationResourceFactory,
createObjectStorageConnectionProbeFactory,
createObjectStoragePreferredTweakProbeFactory,
createObjectStorageSecuritySeedResourceFactory,
} from "./replicatorResources";
import {
COUCHDB_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY,
OBJECT_STORAGE_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY,
} from "./centralRemoteAdministration";
/** Host environment sufficient to construct every current central provider. */
export type CentralReplicatorProviderHost = LiveSyncCouchDBReplicatorEnv;
/** Minimal operation required by both central one-shot adapters. */
interface OneShotOutcomeReplicator extends ReplicatorInstance {
openOneShotReplicationWithOutcome(setting: RemoteDBSettings, showResult: boolean): Promise<ReplicationOutcome>;
}
/** Narrow structurally so the shared adapter does not depend on either concrete provider class. */
function isOneShotOutcomeReplicator(instance: ReplicatorInstance): instance is OneShotOutcomeReplicator {
return (
"openOneShotReplicationWithOutcome" in instance &&
typeof instance.openOneShotReplicationWithOutcome === "function"
);
}
async function runOneShotWithOutcome(
instance: ReplicatorInstance,
setting: RemoteDBSettings,
showResult: boolean
): Promise<ReplicationOutcome> {
if (!isOneShotOutcomeReplicator(instance)) {
return replicationFailed(new Error("The configured provider does not implement one-shot replication."));
}
return await instance.openOneShotReplicationWithOutcome(setting, showResult);
}
// Manual and unattended wrappers share the provider transfer operation, but
// keep interaction authority and result presentation explicit at this boundary.
const couchDBUserInitiatedOneShot: UserInitiatedOneShotRunner = async (instance, setting, request) => {
return await runOneShotWithOutcome(
instance,
setting,
request.progressPresentation === REPLICATION_PROGRESS_PRESENTATIONS.NOTICE
);
};
const couchDBUnattendedOneShot: UnattendedOneShotRunner = async (instance, setting, request) => {
if (request.interaction.kind !== NO_INTERACTION.kind) return replicationBlocked("interaction-required");
return await runOneShotWithOutcome(instance, setting, false);
};
const objectStorageUserInitiatedOneShot: UserInitiatedOneShotRunner = async (instance, setting, request) => {
return await runOneShotWithOutcome(
instance,
setting,
request.progressPresentation === REPLICATION_PROGRESS_PRESENTATIONS.NOTICE
);
};
const objectStorageUnattendedOneShot: UnattendedOneShotRunner = async (instance, setting, request) => {
if (request.interaction.kind !== NO_INTERACTION.kind) return replicationBlocked("interaction-required");
return await runOneShotWithOutcome(instance, setting, false);
};
/**
* Build the complete, deliberately concrete central-provider matrix for one
* LiveSync host. This closed composition is not a runtime provider registry.
*/
export function createCentralReplicatorProviderDefinitions(
host: CentralReplicatorProviderHost
): ReplicatorProviderDefinitionMap {
return defineReplicatorProviderDefinitions([REMOTE_COUCHDB, REMOTE_MINIO] as const, {
[REMOTE_COUCHDB]: {
kind: REMOTE_COUCHDB,
diagnosticName: "CouchDB",
readiness: CENTRAL_REMOTE_REPLICATION_READINESS,
isConfigured: (settings) =>
settings.remoteType === REMOTE_COUCHDB &&
!!settings.couchDB_URI?.trim() &&
!!settings.couchDB_DBNAME?.trim(),
configurationIdentity: getCouchDBReplicatorConfigurationIdentity,
create: () => Promise.resolve(new LiveSyncCouchDBReplicator(host)),
remoteResources: {
[REMOTE_RESOURCE_KINDS.CONNECTION]: supportedCapability(createCouchDBConnectionProbeFactory(host)),
[REMOTE_RESOURCE_KINDS.PREFERRED_TWEAK]: supportedCapability(
createCouchDBPreferredTweakProbeFactory(host)
),
[REMOTE_RESOURCE_KINDS.SECURITY_SEED]: supportedCapability(
createCouchDBSecuritySeedResourceFactory(host)
),
[REMOTE_RESOURCE_KINDS.SYNCHRONISATION_INFORMATION]: supportedCapability(
createCouchDBSynchronisationInformationResourceFactory(host)
),
},
centralRemoteAdministration: COUCHDB_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY,
userInitiatedOneShot: supportedCapability(couchDBUserInitiatedOneShot),
unattendedOneShot: supportedCapability(couchDBUnattendedOneShot),
continuous: supportedOpenReplicationContinuous(),
stopActiveTransfer: supportedStopActiveTransfer(),
},
[REMOTE_MINIO]: {
kind: REMOTE_MINIO,
diagnosticName: "Object Storage",
readiness: CENTRAL_REMOTE_REPLICATION_READINESS,
isConfigured: (settings) =>
settings.remoteType === REMOTE_MINIO && !!settings.endpoint?.trim() && !!settings.bucket?.trim(),
configurationIdentity: getObjectStorageReplicatorConfigurationIdentity,
create: () => Promise.resolve(new LiveSyncJournalReplicator(host)),
remoteResources: {
[REMOTE_RESOURCE_KINDS.CONNECTION]: supportedCapability(
createObjectStorageConnectionProbeFactory(host)
),
[REMOTE_RESOURCE_KINDS.PREFERRED_TWEAK]: supportedCapability(
createObjectStoragePreferredTweakProbeFactory(host)
),
[REMOTE_RESOURCE_KINDS.SECURITY_SEED]: supportedCapability(
createObjectStorageSecuritySeedResourceFactory(host)
),
[REMOTE_RESOURCE_KINDS.SYNCHRONISATION_INFORMATION]: CAPABILITY_NOT_APPLICABLE,
},
centralRemoteAdministration: OBJECT_STORAGE_CENTRAL_REMOTE_ADMINISTRATION_CAPABILITY,
userInitiatedOneShot: supportedCapability(objectStorageUserInitiatedOneShot),
unattendedOneShot: supportedCapability(objectStorageUnattendedOneShot),
continuous: CAPABILITY_NOT_APPLICABLE,
stopActiveTransfer: supportedStopActiveTransfer(),
},
});
}
+248
View File
@@ -0,0 +1,248 @@
import { describe, expect, it, vi } from "vitest";
import { REMOTE_COUCHDB, REMOTE_MINIO } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { createNewVaultSettings } from "@vrtmrz/livesync-commonlib/settings";
import {
CAPABILITY_SUPPORT_KINDS,
NO_INTERACTION,
REPLICATION_COMPLETED,
REPLICATION_PROGRESS_PRESENTATIONS,
REMOTE_RESOURCE_KINDS,
USER_INITIATED_REPLICATION_AUTHORITY,
} from "@vrtmrz/livesync-commonlib/replication";
const constructorMocks = vi.hoisted(() => ({
couchDB: vi.fn(),
couchDBOneShot: vi.fn(async (..._args: unknown[]) => REPLICATION_COMPLETED),
objectStorage: vi.fn(),
objectStorageOneShot: vi.fn(async (..._args: unknown[]) => REPLICATION_COMPLETED),
}));
vi.mock("@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator", () => ({
LiveSyncCouchDBReplicator: class {
constructor(host: unknown) {
constructorMocks.couchDB(host);
}
openOneShotReplicationWithOutcome(...args: unknown[]) {
return constructorMocks.couchDBOneShot(...args);
}
},
}));
vi.mock("@vrtmrz/livesync-commonlib/compat/replication/journal/LiveSyncJournalReplicator", () => ({
LiveSyncJournalReplicator: class {
constructor(host: unknown) {
constructorMocks.objectStorage(host);
}
openOneShotReplicationWithOutcome(...args: unknown[]) {
return constructorMocks.objectStorageOneShot(...args);
}
},
}));
import { createCentralReplicatorProviderDefinitions } from "./replicatorProviders";
describe("central Replicator provider definitions", () => {
it("keeps the retained remote-resource catalogue bounded", () => {
expect
.soft(Object.values(REMOTE_RESOURCE_KINDS).sort())
.toEqual(["connection", "preferred-tweak", "security-seed", "synchronisation-information"].sort());
});
it("composes CouchDB and Object Storage policies outside LiveSyncBaseCore", async () => {
const host = {} as Parameters<typeof createCentralReplicatorProviderDefinitions>[0];
const definitions = createCentralReplicatorProviderDefinitions(host);
const couchDB = definitions.get(REMOTE_COUCHDB)!;
const objectStorage = definitions.get(REMOTE_MINIO)!;
expect([...definitions.keys()]).toEqual([REMOTE_COUCHDB, REMOTE_MINIO]);
expect("sameKindReconciliation" in couchDB).toBe(false);
expect("sameKindReconciliation" in objectStorage).toBe(false);
expect(
couchDB.isConfigured(
Object.assign(createNewVaultSettings(), {
remoteType: REMOTE_COUCHDB,
couchDB_URI: "https://couch.example.test",
couchDB_DBNAME: "vault",
})
)
).toBe(true);
expect(
objectStorage.isConfigured(
Object.assign(createNewVaultSettings(), {
remoteType: REMOTE_MINIO,
endpoint: "https://objects.example.test",
bucket: "vault",
})
)
).toBe(true);
await couchDB.create(createNewVaultSettings());
await objectStorage.create(createNewVaultSettings());
expect(constructorMocks.couchDB).toHaveBeenCalledWith(host);
expect(constructorMocks.objectStorage).toHaveBeenCalledWith(host);
});
it("rejects incomplete and wrong-kind settings before construction", () => {
const definitions = createCentralReplicatorProviderDefinitions({} as never);
const couchDB = definitions.get(REMOTE_COUCHDB)!;
const objectStorage = definitions.get(REMOTE_MINIO)!;
expect(couchDB.isConfigured(Object.assign(createNewVaultSettings(), { remoteType: REMOTE_MINIO }))).toBe(false);
expect(
objectStorage.isConfigured(Object.assign(createNewVaultSettings(), { remoteType: REMOTE_COUCHDB }))
).toBe(false);
});
it("declares the retained owned resources and cohesive optional administration", () => {
const definitions = createCentralReplicatorProviderDefinitions({} as never);
const couchResources = definitions.get(REMOTE_COUCHDB)?.remoteResources;
const objectResources = definitions.get(REMOTE_MINIO)?.remoteResources;
const couchAdministration = definitions.get(REMOTE_COUCHDB)?.centralRemoteAdministration;
const objectAdministration = definitions.get(REMOTE_MINIO)?.centralRemoteAdministration;
expect(Object.keys(couchResources ?? {}).sort()).toEqual(Object.values(REMOTE_RESOURCE_KINDS).sort());
expect(Object.keys(objectResources ?? {}).sort()).toEqual(Object.values(REMOTE_RESOURCE_KINDS).sort());
expect(couchResources?.[REMOTE_RESOURCE_KINDS.CONNECTION].kind).toBe(CAPABILITY_SUPPORT_KINDS.SUPPORTED);
expect(couchResources?.[REMOTE_RESOURCE_KINDS.PREFERRED_TWEAK].kind).toBe(CAPABILITY_SUPPORT_KINDS.SUPPORTED);
expect(couchResources?.[REMOTE_RESOURCE_KINDS.SECURITY_SEED].kind).toBe(CAPABILITY_SUPPORT_KINDS.SUPPORTED);
expect(couchResources?.[REMOTE_RESOURCE_KINDS.SYNCHRONISATION_INFORMATION].kind).toBe(
CAPABILITY_SUPPORT_KINDS.SUPPORTED
);
expect(objectResources?.[REMOTE_RESOURCE_KINDS.SECURITY_SEED].kind).toBe(CAPABILITY_SUPPORT_KINDS.SUPPORTED);
expect(objectResources?.[REMOTE_RESOURCE_KINDS.SYNCHRONISATION_INFORMATION].kind).toBe(
CAPABILITY_SUPPORT_KINDS.NOT_APPLICABLE
);
expect(couchAdministration?.kind).toBe(CAPABILITY_SUPPORT_KINDS.SUPPORTED);
expect(objectAdministration?.kind).toBe(CAPABILITY_SUPPORT_KINDS.SUPPORTED);
expect("activeRemoteReads" in definitions.get(REMOTE_COUCHDB)!).toBe(false);
expect("fullTransfers" in definitions.get(REMOTE_COUCHDB)!).toBe(false);
});
it("dispatches central finite work through provider-local attempt results", async () => {
const definitions = createCentralReplicatorProviderDefinitions({} as never);
const couchDB = definitions.get(REMOTE_COUCHDB)!;
const objectStorage = definitions.get(REMOTE_MINIO)!;
const setting = createNewVaultSettings();
const couchInstance = await couchDB.create(setting);
const objectInstance = await objectStorage.create(setting);
if (!couchInstance || !objectInstance) throw new Error("Provider construction failed");
if (couchDB.userInitiatedOneShot.kind !== CAPABILITY_SUPPORT_KINDS.SUPPORTED) {
throw new Error("CouchDB OneShot is unavailable");
}
if (objectStorage.unattendedOneShot.kind !== CAPABILITY_SUPPORT_KINDS.SUPPORTED) {
throw new Error("Object Storage OneShot is unavailable");
}
await expect(
couchDB.userInitiatedOneShot.run(couchInstance, setting, {
trigger: "manual",
progressPresentation: REPLICATION_PROGRESS_PRESENTATIONS.NOTICE,
interaction: USER_INITIATED_REPLICATION_AUTHORITY,
})
).resolves.toBe(REPLICATION_COMPLETED);
await expect(
objectStorage.unattendedOneShot.run(objectInstance, setting, {
trigger: "resume",
interaction: NO_INTERACTION,
})
).resolves.toBe(REPLICATION_COMPLETED);
expect(constructorMocks.couchDBOneShot).toHaveBeenCalledWith(setting, true);
expect(constructorMocks.objectStorageOneShot).toHaveBeenCalledWith(setting, false);
});
it.each([
["CouchDB", REMOTE_COUCHDB],
["Object Storage", REMOTE_MINIO],
] as const)("maps %s progress presentation independently of recovery authority", async (label, remoteType) => {
const definitions = createCentralReplicatorProviderDefinitions({} as never);
const definition = definitions.get(remoteType)!;
if (definition.userInitiatedOneShot.kind !== CAPABILITY_SUPPORT_KINDS.SUPPORTED) {
throw new Error(`${label} OneShot is unavailable`);
}
const setting = Object.assign(createNewVaultSettings(), { remoteType });
const openOneShotReplicationWithOutcome = vi.fn(async () => REPLICATION_COMPLETED);
const instance = {
initializeDatabaseForReplication: vi.fn(async () => true),
openReplication: vi.fn(async () => true),
terminateSync: vi.fn(),
closeReplication: vi.fn(),
openOneShotReplicationWithOutcome,
};
await definition.userInitiatedOneShot.run(instance, setting, {
trigger: "manual",
progressPresentation: REPLICATION_PROGRESS_PRESENTATIONS.QUIET,
interaction: USER_INITIATED_REPLICATION_AUTHORITY,
});
await definition.userInitiatedOneShot.run(instance, setting, {
trigger: "manual",
progressPresentation: REPLICATION_PROGRESS_PRESENTATIONS.NOTICE,
interaction: USER_INITIATED_REPLICATION_AUTHORITY,
});
expect(openOneShotReplicationWithOutcome).toHaveBeenNthCalledWith(1, setting, false);
expect(openOneShotReplicationWithOutcome).toHaveBeenNthCalledWith(2, setting, true);
});
it("dispatches central finite work through the declared operation rather than constructor identity", async () => {
const definitions = createCentralReplicatorProviderDefinitions({} as never);
const couchDB = definitions.get(REMOTE_COUCHDB)!;
const objectStorage = definitions.get(REMOTE_MINIO)!;
const setting = createNewVaultSettings();
const createStructuralOneShotReplicator = () => ({
initializeDatabaseForReplication: vi.fn(async () => true),
openReplication: vi.fn(async () => true),
terminateSync: vi.fn(),
closeReplication: vi.fn(),
openOneShotReplicationWithOutcome: vi.fn(async () => REPLICATION_COMPLETED),
});
const couchInstance = createStructuralOneShotReplicator();
const objectStorageInstance = createStructuralOneShotReplicator();
if (couchDB.userInitiatedOneShot.kind !== CAPABILITY_SUPPORT_KINDS.SUPPORTED) {
throw new Error("CouchDB OneShot is unavailable");
}
if (objectStorage.unattendedOneShot.kind !== CAPABILITY_SUPPORT_KINDS.SUPPORTED) {
throw new Error("Object Storage OneShot is unavailable");
}
const couchOutcome = await couchDB.userInitiatedOneShot.run(couchInstance, setting, {
trigger: "manual",
progressPresentation: REPLICATION_PROGRESS_PRESENTATIONS.NOTICE,
interaction: USER_INITIATED_REPLICATION_AUTHORITY,
});
const objectStorageOutcome = await objectStorage.unattendedOneShot.run(objectStorageInstance, setting, {
trigger: "resume",
interaction: NO_INTERACTION,
});
expect.soft(couchOutcome).toBe(REPLICATION_COMPLETED);
expect.soft(objectStorageOutcome).toBe(REPLICATION_COMPLETED);
expect(couchInstance.openOneShotReplicationWithOutcome).toHaveBeenCalledWith(setting, true);
expect(objectStorageInstance.openOneShotReplicationWithOutcome).toHaveBeenCalledWith(setting, false);
});
it("rejects a one-shot adapter whose Replicator does not declare the required operation", async () => {
const definitions = createCentralReplicatorProviderDefinitions({} as never);
const couchDB = definitions.get(REMOTE_COUCHDB)!;
const setting = createNewVaultSettings();
const incompleteInstance = {
initializeDatabaseForReplication: vi.fn(async () => true),
openReplication: vi.fn(async () => true),
terminateSync: vi.fn(),
closeReplication: vi.fn(),
};
if (couchDB.userInitiatedOneShot.kind !== CAPABILITY_SUPPORT_KINDS.SUPPORTED) {
throw new Error("CouchDB OneShot is unavailable");
}
const outcome = await couchDB.userInitiatedOneShot.run(incompleteInstance, setting, {
trigger: "manual",
progressPresentation: REPLICATION_PROGRESS_PRESENTATIONS.NOTICE,
interaction: USER_INITIATED_REPLICATION_AUTHORITY,
});
expect(outcome.status).toBe("failed");
expect(incompleteInstance.openReplication).not.toHaveBeenCalled();
});
});
+333
View File
@@ -0,0 +1,333 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { LOG_LEVEL_NOTICE, REMOTE_COUCHDB, REMOTE_MINIO } from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { ObsidianLiveSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { createNewVaultSettings } from "@vrtmrz/livesync-commonlib/settings";
const mocks = vi.hoisted(() => ({
logger: vi.fn(),
couchDB: [] as Array<{
host: unknown;
isMobile: ReturnType<typeof vi.fn>;
connectRemoteCouchDBWithSetting: ReturnType<typeof vi.fn>;
getRemoteStatus: ReturnType<typeof vi.fn>;
getRemotePreferredTweakValues: ReturnType<typeof vi.fn>;
getReplicationPBKDF2Salt: ReturnType<typeof vi.fn>;
closeReplication: ReturnType<typeof vi.fn>;
}>,
objectStorage: [] as Array<{
host: unknown;
tryConnectRemote: ReturnType<typeof vi.fn>;
getRemoteStatus: ReturnType<typeof vi.fn>;
getRemotePreferredTweakValues: ReturnType<typeof vi.fn>;
getReplicationPBKDF2Salt: ReturnType<typeof vi.fn>;
closeReplication: ReturnType<typeof vi.fn>;
}>,
checkSyncInfo: vi.fn(async () => true),
}));
vi.mock("@vrtmrz/livesync-commonlib/compat/common/logger", async (importOriginal) => {
const actual = await importOriginal<typeof import("@vrtmrz/livesync-commonlib/compat/common/logger")>();
return { ...actual, Logger: mocks.logger };
});
vi.mock("@vrtmrz/livesync-commonlib/compat/pouchdb/negotiation", () => ({
checkSyncInfo: mocks.checkSyncInfo,
}));
vi.mock("@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator", () => ({
LiveSyncCouchDBReplicator: class {
host: unknown;
isMobile = vi.fn(() => false);
connectRemoteCouchDBWithSetting = vi.fn();
getRemoteStatus = vi.fn();
getRemotePreferredTweakValues = vi.fn();
getReplicationPBKDF2Salt = vi.fn();
closeReplication = vi.fn();
constructor(host: unknown) {
this.host = host;
mocks.couchDB.push(this);
}
},
}));
vi.mock("@vrtmrz/livesync-commonlib/compat/replication/journal/LiveSyncJournalReplicator", () => ({
LiveSyncJournalReplicator: class {
host: unknown;
tryConnectRemote = vi.fn();
getRemoteStatus = vi.fn();
getRemotePreferredTweakValues = vi.fn();
getReplicationPBKDF2Salt = vi.fn();
closeReplication = vi.fn();
constructor(host: unknown) {
this.host = host;
mocks.objectStorage.push(this);
}
},
}));
import {
createCouchDBConnectionProbeFactory,
createCouchDBPreferredTweakProbeFactory,
createCouchDBSecuritySeedResourceFactory,
createCouchDBSynchronisationInformationResourceFactory,
createObjectStorageConnectionProbeFactory,
createObjectStoragePreferredTweakProbeFactory,
createObjectStorageSecuritySeedResourceFactory,
} from "./replicatorResources";
function createSettings(overrides: Partial<ObsidianLiveSyncSettings> = {}): ObsidianLiveSyncSettings {
return Object.assign(createNewVaultSettings(), {
remoteType: REMOTE_COUCHDB,
couchDB_URI: "https://couch.example.test",
couchDB_DBNAME: "vault",
endpoint: "https://objects.example.test",
bucket: "vault",
...overrides,
});
}
describe("replicator probe factories", () => {
beforeEach(() => {
mocks.couchDB.length = 0;
mocks.objectStorage.length = 0;
mocks.checkSyncInfo.mockReset().mockResolvedValue(true);
mocks.logger.mockClear();
});
it("binds a CouchDB connection probe to a shallow settings snapshot and closes its owned connection", async () => {
const host = { name: "host" };
const source = createSettings();
const snapshot = { ...source };
const probe = await createCouchDBConnectionProbeFactory(host as never)(source);
const replicator = mocks.couchDB[0];
const close = vi.fn(async () => undefined);
const databaseClose = vi.fn(async () => undefined);
replicator.isMobile.mockReturnValue(true);
replicator.connectRemoteCouchDBWithSetting.mockResolvedValue({
db: { close: databaseClose },
info: {},
close,
});
source.couchDB_URI = "https://changed.example.test";
expect(await probe.check({ createIfMissing: false, showResult: true })).toEqual({ ok: true });
expect(replicator.connectRemoteCouchDBWithSetting).toHaveBeenCalledWith(snapshot, true, false, false);
expect(replicator.connectRemoteCouchDBWithSetting.mock.calls[0][0]).not.toBe(source);
expect(close).toHaveBeenCalledOnce();
expect(databaseClose).not.toHaveBeenCalled();
});
it("maps a CouchDB connection error string and delegates status to the same snapshot", async () => {
const source = createSettings();
const snapshot = { ...source };
const probe = await createCouchDBConnectionProbeFactory({} as never)(source);
const replicator = mocks.couchDB[0];
replicator.connectRemoteCouchDBWithSetting.mockResolvedValue("connection failed");
expect(await probe.check()).toEqual({ ok: false, reason: "connection failed" });
const status = { estimatedSize: 12 };
replicator.getRemoteStatus.mockResolvedValue(status);
source.couchDB_DBNAME = "changed-vault";
expect(await probe.getStatus()).toBe(status);
expect(replicator.getRemoteStatus).toHaveBeenCalledWith(snapshot);
});
it("emits a result Notice only for an explicitly visible successful CouchDB probe", async () => {
const probe = await createCouchDBConnectionProbeFactory({} as never)(createSettings());
const replicator = mocks.couchDB[0];
replicator.connectRemoteCouchDBWithSetting.mockResolvedValue({
info: { db_name: "vault" },
close: vi.fn(async () => undefined),
});
await expect(probe.check({ showResult: true })).resolves.toEqual({ ok: true });
expect(mocks.logger).toHaveBeenCalledTimes(1);
expect(mocks.logger).toHaveBeenCalledWith("Connected to vault successfully", LOG_LEVEL_NOTICE);
mocks.logger.mockClear();
await expect(probe.check()).resolves.toEqual({ ok: true });
expect(mocks.logger).not.toHaveBeenCalled();
});
it("emits a result Notice only for an explicitly visible CouchDB connection failure", async () => {
const reason = "connection failed";
const translatedFailure = "translated CouchDB connection failure";
const translate = vi.fn(() => translatedFailure);
const settings = createSettings();
const probe = await createCouchDBConnectionProbeFactory({ services: { context: { translate } } } as never)(
settings
);
const replicator = mocks.couchDB[0];
replicator.connectRemoteCouchDBWithSetting.mockResolvedValue(reason);
await expect(probe.check({ showResult: true })).resolves.toEqual({ ok: false, reason });
expect(mocks.logger).toHaveBeenCalledTimes(1);
expect(translate).toHaveBeenCalledWith("liveSyncReplicator.couldNotConnectTo", {
uri: settings.couchDB_URI,
name: settings.couchDB_DBNAME,
db: reason,
});
expect(mocks.logger).toHaveBeenCalledWith(translatedFailure, LOG_LEVEL_NOTICE);
mocks.logger.mockClear();
translate.mockClear();
await expect(probe.check()).resolves.toEqual({ ok: false, reason });
expect(mocks.logger).not.toHaveBeenCalled();
expect(translate).not.toHaveBeenCalled();
});
it("creates an unpublished Object Storage replicator for each probe and normalises connection results", async () => {
const host = { name: "host" };
const source = createSettings({ remoteType: REMOTE_MINIO });
const snapshot = { ...source };
const factory = createObjectStorageConnectionProbeFactory(host as never);
const firstProbe = await factory(source);
const secondProbe = await factory(source);
expect(mocks.objectStorage).toHaveLength(2);
const firstReplicator = mocks.objectStorage[0];
firstReplicator.tryConnectRemote.mockResolvedValue(true);
source.endpoint = "https://changed.example.test";
expect(await firstProbe.check()).toEqual({ ok: true });
expect(firstReplicator.tryConnectRemote).toHaveBeenCalledWith(snapshot, false);
const secondReplicator = mocks.objectStorage[1];
secondReplicator.tryConnectRemote.mockResolvedValue(false);
expect(await secondProbe.check({ showResult: true })).toEqual({ ok: false });
expect(secondReplicator.tryConnectRemote).toHaveBeenCalledWith(snapshot, true);
const error = new Error("storage offline");
secondReplicator.tryConnectRemote.mockRejectedValue(error);
expect(await secondProbe.check()).toEqual({ ok: false, reason: error });
});
it("delegates Object Storage status and preferred-tweak reads to the trial snapshot", async () => {
const source = createSettings({ remoteType: REMOTE_MINIO });
const snapshot = { ...source };
const connectionProbe = await createObjectStorageConnectionProbeFactory({} as never)(source);
const preferredProbe = await createObjectStoragePreferredTweakProbeFactory({} as never)(source);
const connectionReplicator = mocks.objectStorage[0];
const preferredReplicator = mocks.objectStorage[1];
const status = { estimatedSize: 42 };
const preferred = { status: "unsupported" } as const;
connectionReplicator.getRemoteStatus.mockResolvedValue(status);
preferredReplicator.getRemotePreferredTweakValues.mockResolvedValue(preferred);
source.bucket = "changed-vault";
expect(await connectionProbe.getStatus()).toBe(status);
expect(await preferredProbe.read()).toBe(preferred);
expect(connectionReplicator.getRemoteStatus).toHaveBeenCalledWith(snapshot);
expect(preferredReplicator.getRemotePreferredTweakValues).toHaveBeenCalledWith(snapshot);
});
it("shares one successful asynchronous disposal promise for every probe kind", async () => {
const couchProbe = await createCouchDBPreferredTweakProbeFactory({} as never)(createSettings());
const objectProbe = await createObjectStoragePreferredTweakProbeFactory({} as never)(
createSettings({ remoteType: REMOTE_MINIO })
);
const couchReplicator = mocks.couchDB[0];
const objectReplicator = mocks.objectStorage[0];
const couchDisposal = couchProbe.dispose();
expect(couchProbe.dispose()).toBe(couchDisposal);
const objectDisposal = objectProbe.dispose();
expect(objectProbe.dispose()).toBe(objectDisposal);
await Promise.all([couchDisposal, objectDisposal]);
expect(couchReplicator.closeReplication).toHaveBeenCalledOnce();
expect(objectReplicator.closeReplication).toHaveBeenCalledOnce();
});
it("shares a rejected disposal promise and never retries closeReplication", async () => {
const probe = await createObjectStorageConnectionProbeFactory({} as never)(
createSettings({ remoteType: REMOTE_MINIO })
);
const replicator = mocks.objectStorage[0];
const failure = new Error("close failed");
replicator.closeReplication.mockImplementation(() => {
throw failure;
});
const disposal = probe.dispose();
expect(probe.dispose()).toBe(disposal);
await expect(disposal).rejects.toBe(failure);
expect(replicator.closeReplication).toHaveBeenCalledOnce();
});
it("reads the Security Seed from a settings snapshot and disposes its private Replicator", async () => {
const couchSettings = createSettings();
const couchSnapshot = { ...couchSettings };
const objectSettings = createSettings({ remoteType: REMOTE_MINIO });
const objectSnapshot = { ...objectSettings };
const couchResource = await createCouchDBSecuritySeedResourceFactory({} as never)(couchSettings);
const objectResource = await createObjectStorageSecuritySeedResourceFactory({} as never)(objectSettings);
const couchReplicator = mocks.couchDB[0];
const objectReplicator = mocks.objectStorage[0];
const couchSeed = new Uint8Array([1]);
const objectSeed = new Uint8Array([2]);
couchReplicator.getReplicationPBKDF2Salt.mockResolvedValue(couchSeed);
objectReplicator.getReplicationPBKDF2Salt.mockResolvedValue(objectSeed);
couchSettings.couchDB_URI = "https://changed.example.test";
objectSettings.endpoint = "https://changed.example.test";
await expect(couchResource.read()).resolves.toBe(couchSeed);
await expect(objectResource.read()).resolves.toBe(objectSeed);
expect(couchReplicator.getReplicationPBKDF2Salt).toHaveBeenCalledWith(couchSnapshot, true);
expect(objectReplicator.getReplicationPBKDF2Salt).toHaveBeenCalledWith(objectSnapshot, true);
await Promise.all([couchResource.dispose(), objectResource.dispose()]);
expect(couchReplicator.closeReplication).toHaveBeenCalledOnce();
expect(objectReplicator.closeReplication).toHaveBeenCalledOnce();
});
it("checks synchronisation information through an owned connection and disposes the private Replicator", async () => {
const settings = createSettings();
const snapshot = { ...settings };
const resource = await createCouchDBSynchronisationInformationResourceFactory({} as never)(settings);
const replicator = mocks.couchDB[0];
const database = { close: vi.fn() };
const close = vi.fn(async () => undefined);
replicator.connectRemoteCouchDBWithSetting.mockResolvedValue({ db: database, close });
settings.couchDB_DBNAME = "changed-vault";
await expect(resource.check()).resolves.toBe(true);
expect(replicator.connectRemoteCouchDBWithSetting).toHaveBeenCalledWith(snapshot, false, true);
expect(mocks.checkSyncInfo).toHaveBeenCalledWith(database);
expect(close).toHaveBeenCalledOnce();
expect(database.close).not.toHaveBeenCalled();
await resource.dispose();
expect(replicator.closeReplication).toHaveBeenCalledOnce();
});
it("preserves a CouchDB connection or setup failure for the settings flow to report", async () => {
const reason = "connection failed";
const resource = await createCouchDBSynchronisationInformationResourceFactory({} as never)(createSettings());
const replicator = mocks.couchDB[0];
replicator.connectRemoteCouchDBWithSetting.mockResolvedValue(reason);
await expect(resource.check()).rejects.toMatchObject({ message: reason });
});
it("closes the owned connection when synchronisation-information verification rejects", async () => {
const resource = await createCouchDBSynchronisationInformationResourceFactory({} as never)(createSettings());
const replicator = mocks.couchDB[0];
const database = { close: vi.fn() };
const close = vi.fn(async () => undefined);
const failure = new Error("verification failed");
replicator.connectRemoteCouchDBWithSetting.mockResolvedValue({ db: database, close });
mocks.checkSyncInfo.mockRejectedValue(failure);
await expect(resource.check()).rejects.toBe(failure);
expect(close).toHaveBeenCalledOnce();
expect(database.close).not.toHaveBeenCalled();
await resource.dispose();
expect(replicator.closeReplication).toHaveBeenCalledOnce();
});
});
@@ -0,0 +1,104 @@
import type { RemoteDBSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import type {
ConnectionProbeFactory,
RemoteConnectionProbe,
RemoteConnectionProbeOptions,
} from "@vrtmrz/livesync-commonlib/replication";
import {
LiveSyncCouchDBReplicator,
type LiveSyncCouchDBReplicatorEnv,
} from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import { LOG_LEVEL_NOTICE, Logger } from "@vrtmrz/livesync-commonlib/compat/common/logger";
import { LiveSyncJournalReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/journal/LiveSyncJournalReplicator";
import { createReplicatorDisposer, snapshotRemoteSettings } from "./shared";
/** Host environment sufficient to construct either central connection probe. */
export type ConnectionResourceHost = LiveSyncCouchDBReplicatorEnv;
function createCouchDBConnectionProbe(
replicator: LiveSyncCouchDBReplicator,
snapshot: RemoteDBSettings,
host: ConnectionResourceHost
): RemoteConnectionProbe {
const dispose = createReplicatorDisposer(replicator);
return {
check: async (options: RemoteConnectionProbeOptions = {}) => {
const connection = await replicator.connectRemoteCouchDBWithSetting(
snapshot,
replicator.isMobile(),
options.createIfMissing ?? true,
false
);
if (typeof connection === "string") {
if (options.showResult) {
Logger(
host.services.context.translate("liveSyncReplicator.couldNotConnectTo", {
uri: snapshot.couchDB_URI,
name: snapshot.couchDB_DBNAME,
db: connection,
}),
LOG_LEVEL_NOTICE
);
}
return { ok: false, reason: connection };
}
try {
if (options.showResult) {
Logger(`Connected to ${connection.info.db_name} successfully`, LOG_LEVEL_NOTICE);
}
return { ok: true };
} finally {
await connection.close();
}
},
getStatus: () => replicator.getRemoteStatus(snapshot),
dispose,
};
}
function createObjectStorageConnectionProbe(
replicator: LiveSyncJournalReplicator,
snapshot: RemoteDBSettings
): RemoteConnectionProbe {
const dispose = createReplicatorDisposer(replicator);
return {
check: async (options: RemoteConnectionProbeOptions = {}) => {
try {
const connected = await replicator.tryConnectRemote(snapshot, options.showResult ?? false);
return connected ? { ok: true } : { ok: false };
} catch (error) {
return { ok: false, reason: error };
}
},
getStatus: () => replicator.getRemoteStatus(snapshot),
dispose,
};
}
/**
* Build an unpublished CouchDB connection probe for one host.
*
* The probe owns both its concrete Replicator and each connection it opens. It
* never publishes that Replicator as the active provider instance. A caller
* may request the established result Notice explicitly; ordinary probes remain
* silent.
*/
export function createCouchDBConnectionProbeFactory(host: ConnectionResourceHost): ConnectionProbeFactory {
return (setting) => {
const snapshot = snapshotRemoteSettings(setting);
return Promise.resolve(createCouchDBConnectionProbe(new LiveSyncCouchDBReplicator(host), snapshot, host));
};
}
/**
* Build an unpublished Object Storage connection probe for one host.
*
* The probe owns its concrete Replicator and never publishes or replaces the
* active provider instance.
*/
export function createObjectStorageConnectionProbeFactory(host: ConnectionResourceHost): ConnectionProbeFactory {
return (setting) => {
const snapshot = snapshotRemoteSettings(setting);
return Promise.resolve(createObjectStorageConnectionProbe(new LiveSyncJournalReplicator(host), snapshot));
};
}
+16
View File
@@ -0,0 +1,16 @@
export {
createCouchDBConnectionProbeFactory,
createObjectStorageConnectionProbeFactory,
type ConnectionResourceHost,
} from "./connection";
export {
createCouchDBPreferredTweakProbeFactory,
createObjectStoragePreferredTweakProbeFactory,
type PreferredTweakResourceHost,
} from "./preferredTweak";
export {
createCouchDBSecuritySeedResourceFactory,
createObjectStorageSecuritySeedResourceFactory,
type SecuritySeedResourceHost,
} from "./securitySeed";
export { createCouchDBSynchronisationInformationResourceFactory } from "./synchronisationInformation";
@@ -0,0 +1,43 @@
import type { RemoteDBSettings, RemotePreferredTweakResult } from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { PreferredTweakProbe, PreferredTweakProbeFactory } from "@vrtmrz/livesync-commonlib/replication";
import {
LiveSyncCouchDBReplicator,
type LiveSyncCouchDBReplicatorEnv,
} from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import { LiveSyncJournalReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/journal/LiveSyncJournalReplicator";
import { createReplicatorDisposer, snapshotRemoteSettings, type ResourceReplicator } from "./shared";
/** Host environment sufficient to construct either preferred-tweak probe. */
export type PreferredTweakResourceHost = LiveSyncCouchDBReplicatorEnv;
interface PreferredTweakReplicator extends ResourceReplicator {
getRemotePreferredTweakValues(setting: RemoteDBSettings): Promise<RemotePreferredTweakResult>;
}
function createPreferredTweakProbe(
replicator: PreferredTweakReplicator,
snapshot: RemoteDBSettings
): PreferredTweakProbe {
return {
read: () => replicator.getRemotePreferredTweakValues(snapshot),
dispose: createReplicatorDisposer(replicator),
};
}
/** Build an unpublished, independently disposed CouchDB preferred-tweak probe. */
export function createCouchDBPreferredTweakProbeFactory(host: PreferredTweakResourceHost): PreferredTweakProbeFactory {
return (setting) => {
const snapshot = snapshotRemoteSettings(setting);
return Promise.resolve(createPreferredTweakProbe(new LiveSyncCouchDBReplicator(host), snapshot));
};
}
/** Build an unpublished, independently disposed Object Storage preferred-tweak probe. */
export function createObjectStoragePreferredTweakProbeFactory(
host: PreferredTweakResourceHost
): PreferredTweakProbeFactory {
return (setting) => {
const snapshot = snapshotRemoteSettings(setting);
return Promise.resolve(createPreferredTweakProbe(new LiveSyncJournalReplicator(host), snapshot));
};
}
@@ -0,0 +1,41 @@
import type { RemoteDBSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { SecuritySeedResourceFactory } from "@vrtmrz/livesync-commonlib/replication";
import {
LiveSyncCouchDBReplicator,
type LiveSyncCouchDBReplicatorEnv,
} from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import { LiveSyncJournalReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/journal/LiveSyncJournalReplicator";
import { createReplicatorDisposer, snapshotRemoteSettings, type ResourceReplicator } from "./shared";
/** Host environment sufficient to construct either Security Seed resource. */
export type SecuritySeedResourceHost = LiveSyncCouchDBReplicatorEnv;
/** Minimal private Replicator surface required by a Security Seed resource. */
interface SecuritySeedReplicator extends ResourceReplicator {
getReplicationPBKDF2Salt(setting: RemoteDBSettings, refresh?: boolean): Promise<Uint8Array<ArrayBuffer>>;
}
function createSecuritySeedResourceFactory(
createReplicator: () => SecuritySeedReplicator
): SecuritySeedResourceFactory {
return (setting) => {
const snapshot = snapshotRemoteSettings(setting);
const replicator = createReplicator();
return Promise.resolve({
read: () => replicator.getReplicationPBKDF2Salt(snapshot, true),
dispose: createReplicatorDisposer(replicator),
});
};
}
/** Build an unpublished, independently disposed CouchDB Security Seed resource. */
export function createCouchDBSecuritySeedResourceFactory(host: SecuritySeedResourceHost): SecuritySeedResourceFactory {
return createSecuritySeedResourceFactory(() => new LiveSyncCouchDBReplicator(host));
}
/** Build an unpublished, independently disposed Object Storage Security Seed resource. */
export function createObjectStorageSecuritySeedResourceFactory(
host: SecuritySeedResourceHost
): SecuritySeedResourceFactory {
return createSecuritySeedResourceFactory(() => new LiveSyncJournalReplicator(host));
}
+28
View File
@@ -0,0 +1,28 @@
import type { RemoteDBSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
/**
* Closeable surface of a concrete Replicator owned by one private resource.
*
* It deliberately exposes no active-provider controls: the resource may use
* the helper for one bounded operation, then must dispose it without
* publishing or replacing the active Replicator.
*/
export interface ResourceReplicator {
closeReplication(): void | Promise<void>;
}
/** Create one idempotent asynchronous disposer for a private Replicator. */
export function createReplicatorDisposer(replicator: ResourceReplicator): () => Promise<void> {
let disposal: Promise<void> | undefined;
return () => {
if (disposal === undefined) {
disposal = Promise.resolve().then(() => replicator.closeReplication());
}
return disposal;
};
}
/** Fence a finite resource from later edits to its source settings object. */
export function snapshotRemoteSettings(setting: RemoteDBSettings): RemoteDBSettings {
return { ...setting };
}
@@ -0,0 +1,42 @@
import type { SynchronisationInformationResourceFactory } from "@vrtmrz/livesync-commonlib/replication";
import {
LiveSyncCouchDBReplicator,
type LiveSyncCouchDBReplicatorEnv,
} from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import { checkSyncInfo } from "@vrtmrz/livesync-commonlib/compat/pouchdb/negotiation";
import { createReplicatorDisposer, snapshotRemoteSettings } from "./shared";
/**
* Build an unpublished CouchDB synchronisation-information verifier.
*
* The resource owns its concrete Replicator and connection, and cannot replace
* the active provider instance. Its check resolves to `false` only for observed
* incompatibility; connection, setup, and verification failures reject so the
* caller can report an operational failure separately.
*/
export function createCouchDBSynchronisationInformationResourceFactory(
host: LiveSyncCouchDBReplicatorEnv
): SynchronisationInformationResourceFactory {
return (setting) => {
const snapshot = snapshotRemoteSettings(setting);
const replicator = new LiveSyncCouchDBReplicator(host);
return Promise.resolve({
check: async () => {
const connection = await replicator.connectRemoteCouchDBWithSetting(
snapshot,
replicator.isMobile(),
true
);
if (typeof connection === "string") {
throw new Error(connection);
}
try {
return await checkSyncInfo(connection.db);
} finally {
await connection.close();
}
},
dispose: createReplicatorDisposer(replicator),
});
};
}
+9 -1
View File
@@ -24,6 +24,10 @@
import { LOG_LEVEL_NOTICE, Logger } from "octagonal-wheels/common/logger";
import type { LiveSyncBaseCore } from "@/LiveSyncBaseCore.ts";
import { $msg as translateMessage } from "@/common/translation";
import {
REPLICATION_PROGRESS_PRESENTATIONS,
USER_INITIATED_REPLICATION_AUTHORITY,
} from "@vrtmrz/livesync-commonlib/replication";
export let plugin: ObsidianLiveSyncPlugin;
export let core :LiveSyncBaseCore;
// $: core = plugin.core;
@@ -104,7 +108,11 @@
await requestUpdate();
}
async function replicate() {
await core.services.replication.replicate(true);
await core.services.replication.replicateUserInitiated({
trigger: "manual",
progressPresentation: REPLICATION_PROGRESS_PRESENTATIONS.NOTICE,
interaction: USER_INITIATED_REPLICATION_AUTHORITY,
});
}
function selectAllNewest(selectMode: boolean) {
selectNewestPulse++;
@@ -17,6 +17,7 @@ import { serialized } from "octagonal-wheels/concurrency/lock_v2";
import { arrayToChunkedArray } from "octagonal-wheels/collection";
import { EVENT_ANALYSE_DB_USAGE, EVENT_REQUEST_PERFORM_GC_V3, eventHub } from "@/common/events";
import type { LiveSyncCouchDBReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import type { ReplicatorInstance } from "@vrtmrz/livesync-commonlib/replication";
import { delay } from "@vrtmrz/livesync-commonlib/compat/common/utils";
import { isNotFoundError } from "@vrtmrz/livesync-commonlib/compat/common/utils.doc";
import { ensureLocalDatabaseMaintenancePrerequisites } from "./maintenancePrerequisites";
@@ -29,6 +30,31 @@ type NoteDocumentID = DocumentID;
type Rev = string;
type ChunkUsageMap = Map<NoteDocumentID, Map<Rev, Set<ChunkID>>>;
type CouchDBCompactionReplicator = ReplicatorInstance &
Pick<LiveSyncCouchDBReplicator, "connectRemoteCouchDBWithSetting">;
type CouchDBGarbageCollectionReplicator = ReplicatorInstance &
Pick<LiveSyncCouchDBReplicator, "getConnectedDeviceList" | "openOneShotReplication">;
function canCompactCouchDBRemote(replicator: ReplicatorInstance): replicator is CouchDBCompactionReplicator {
return (
"connectRemoteCouchDBWithSetting" in replicator &&
typeof replicator.connectRemoteCouchDBWithSetting === "function"
);
}
function canRunCouchDBGarbageCollection(
replicator: ReplicatorInstance
): replicator is CouchDBGarbageCollectionReplicator {
return (
"getConnectedDeviceList" in replicator &&
typeof replicator.getConnectedDeviceList === "function" &&
"openOneShotReplication" in replicator &&
typeof replicator.openOneShotReplication === "function"
);
}
export class LocalDatabaseMaintenance extends LiveSyncCommands {
onunload(): void {
// NO OP.
@@ -737,7 +763,8 @@ Success: ${successCount}, Errored: ${errored}`;
}
async compactDatabase() {
const replicator = this.core.replicator as LiveSyncCouchDBReplicator;
const replicator = this.core.replicator;
if (!canCompactCouchDBRemote(replicator)) return;
const remote = await replicator.connectRemoteCouchDBWithSetting(this.settings, false, false, true);
if (!remote) {
this._notice("Failed to connect to remote for compaction.", "gc-compact");
@@ -840,8 +867,9 @@ Success: ${successCount}, Errored: ${errored}`;
// }
// }
async gcv3() {
const replicator = this.core.replicator;
if (this.settings.remoteType !== REMOTE_COUCHDB || !canRunCouchDBGarbageCollection(replicator)) return;
if (!(await this.ensureAvailable("Garbage Collection"))) return;
const replicator = this.core.replicator as LiveSyncCouchDBReplicator;
// Start one-shot replication to ensure all changes are synced before GC.
const r0 = await replicator.openOneShotReplication(this.settings, false, false, "sync");
if (!r0) {
@@ -854,7 +882,7 @@ Success: ${successCount}, Errored: ${errored}`;
// Delete the chunk, but first verify the following:
// Fetch the list of accepted nodes from the replicator.
const OPTION_CANCEL = "Cancel Garbage Collection";
const info = await this.core.replicator.getConnectedDeviceList();
const info = await replicator.getConnectedDeviceList();
if (!info) {
this._notice("No connected device information found. Cancelling Garbage Collection.");
return;
@@ -1,15 +1,19 @@
import { App, Modal } from "@/deps.ts";
import P2POpenReplicationPane from "./P2POpenReplicationPane.svelte";
import { mount, unmount } from "svelte";
import type { LiveSyncTrysteroReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/LiveSyncTrysteroReplicator";
import type { P2PServiceViews } from "@vrtmrz/livesync-commonlib/p2p";
/**
* Reports action completion so the pane does not infer success merely from a
* settled Promise.
*/
export type P2POpenReplicationModalCallback = {
onSync: (peerId: string) => Promise<void>;
onSyncAndClose: (peerId: string) => Promise<void>;
onSync: (peerId: string) => Promise<boolean>;
onSyncAndClose: (peerId: string) => Promise<boolean>;
};
export class P2POpenReplicationModal extends Modal {
liveSyncReplicator: LiveSyncTrysteroReplicator;
p2p: P2PServiceViews;
callback?: P2POpenReplicationModalCallback;
component?: ReturnType<typeof mount>;
showResult: boolean;
@@ -19,7 +23,7 @@ export class P2POpenReplicationModal extends Modal {
constructor(
app: App,
liveSyncReplicator: LiveSyncTrysteroReplicator,
p2p: P2PServiceViews,
callback?: P2POpenReplicationModalCallback,
showResult: boolean = false,
title: string = "P2P Replication",
@@ -27,7 +31,7 @@ export class P2POpenReplicationModal extends Modal {
rebuildMode: boolean = false
) {
super(app);
this.liveSyncReplicator = liveSyncReplicator;
this.p2p = p2p;
this.callback = callback;
this.showResult = showResult;
this.title = title;
@@ -35,17 +39,20 @@ export class P2POpenReplicationModal extends Modal {
this.rebuildMode = rebuildMode;
}
async onSync(peerId: string) {
async onSync(peerId: string): Promise<boolean> {
if (this.callback?.onSync) {
await this.callback.onSync(peerId);
return await this.callback.onSync(peerId);
}
return false;
}
async onSyncAndClose(peerId: string) {
async onSyncAndClose(peerId: string): Promise<boolean> {
let completed = false;
if (this.callback?.onSyncAndClose) {
await this.callback.onSyncAndClose(peerId);
completed = await this.callback.onSyncAndClose(peerId);
}
this.close();
return completed;
}
override onOpen() {
@@ -57,7 +64,7 @@ export class P2POpenReplicationModal extends Modal {
this.component = mount(P2POpenReplicationPane, {
target: contentEl,
props: {
liveSyncReplicator: this.liveSyncReplicator,
p2p: this.p2p,
onSync: (peerId: string) => this.onSync(peerId),
onSyncAndClose: (peerId: string) => this.onSyncAndClose(peerId),
onClose: () => this.close(),
@@ -9,29 +9,28 @@
// import type { TrysteroReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/TrysteroReplicator";
import { LOG_LEVEL_NOTICE, LOG_LEVEL_INFO } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { Logger } from "@vrtmrz/livesync-commonlib/compat/common/logger";
import type { LiveSyncTrysteroReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/LiveSyncTrysteroReplicator";
import type { P2PServiceViews } from "@vrtmrz/livesync-commonlib/p2p";
import { delay, fireAndForget } from "octagonal-wheels/promises";
import P2PServerStatusCard from "./P2PServerStatusCard.svelte";
import { $msg as translateMessage } from "@/common/translation";
interface Props {
liveSyncReplicator: LiveSyncTrysteroReplicator;
onSync: (_peerId: string) => Promise<void>;
onSyncAndClose: (_peerId: string) => Promise<void>;
p2p: P2PServiceViews;
onSync: (_peerId: string) => Promise<boolean>;
onSyncAndClose: (_peerId: string) => Promise<boolean>;
onClose: () => void;
showResult: boolean;
rebuildMode?: boolean;
}
let { onSync, onSyncAndClose, onClose, showResult, liveSyncReplicator, rebuildMode = false }: Props = $props();
const getLiveSyncReplicator = () => liveSyncReplicator;
let { onSync, onSyncAndClose, onClose, showResult, p2p, rebuildMode = false }: Props = $props();
let serverInfo = $state<P2PServerInfo | undefined>(undefined);
let syncingPeerId = $state<string | null>(null);
const logLevel = $derived(showResult ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO);
async function requestServerStatus() {
await liveSyncReplicator.requestStatus();
p2p.diagnostics.requestStatus();
eventHub.emitEvent(EVENT_REQUEST_STATUS);
}
onMount(() => {
@@ -50,8 +49,8 @@
try {
syncingPeerId = peerId;
Logger(`Starting sync with ${peerId}`, logLevel);
await onSync(peerId);
Logger(`Sync completed with ${peerId}`, logLevel);
const completed = await onSync(peerId);
if (completed) Logger(`Sync completed with ${peerId}`, logLevel);
} catch (e) {
Logger(`Error during sync: ${e instanceof Error ? e.message : String(e)}`, logLevel);
} finally {
@@ -62,8 +61,8 @@
try {
syncingPeerId = peerId;
Logger(`Starting sync with ${peerId}`, logLevel);
await onSyncAndClose(peerId);
Logger(`Sync completed with ${peerId}`, logLevel);
const completed = await onSyncAndClose(peerId);
if (completed) Logger(`Sync completed with ${peerId}`, logLevel);
} catch (e) {
Logger(`Error during sync: ${e instanceof Error ? e.message : String(e)}`, logLevel);
} finally {
@@ -73,7 +72,7 @@
async function disconnect() {
try {
await liveSyncReplicator.close();
await p2p.transportLifecycle.disconnect();
Logger("Signalling connection closed.", logLevel);
} catch (e) {
Logger(`Failed to close signalling connection: ${e instanceof Error ? e.message : String(e)}`, logLevel);
@@ -100,7 +99,7 @@
</script>
<div class="p2p-container">
<P2PServerStatusCard {getLiveSyncReplicator} showBroadcastToggle={false} />
<P2PServerStatusCard {p2p} showBroadcastToggle={false} />
<div class="peers-section">
<h3>{translateMessage("Available Peers")}</h3>
@@ -2,21 +2,24 @@ import type { App } from "@/deps.ts";
import { Logger } from "@vrtmrz/livesync-commonlib/compat/common/logger";
import { LOG_LEVEL_NOTICE, LOG_LEVEL_INFO } from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { LiveSyncTrysteroReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/LiveSyncTrysteroReplicator";
import type { P2PServiceViews } from "@vrtmrz/livesync-commonlib/p2p";
import { P2POpenReplicationModal } from "./P2POpenReplicationModal";
/**
* Creates an openReplicationUI factory for Obsidian environments.
* Returns a per-replicator closure that opens the P2P Replication modal
* and performs bidirectional sync (pull then push on success).
* Create the Obsidian-owned interactive P2P entry for stable service views.
*
* Peer selection belongs to the host UI rather than the concrete compatibility
* Replicator. The returned operation opens the modal and performs bidirectional
* synchronisation, pulling before pushing, through the targeted-transfer view.
*
* Usage:
* const factory = createOpenReplicationUI(app);
* useP2PReplicatorFeature(core, factory);
* const createInteractiveReplication = createOpenReplicationUI(app);
* const openInteractiveReplication = createInteractiveReplication(p2p);
*/
export function createOpenReplicationUI(
app: App
): (replicator: LiveSyncTrysteroReplicator) => (showResult: boolean) => Promise<boolean | void> {
return (replicator: LiveSyncTrysteroReplicator) =>
): (p2p: P2PServiceViews) => (showResult: boolean) => Promise<boolean | void> {
return (p2p: P2PServiceViews) =>
(showResult: boolean): Promise<boolean | void> => {
const logLevel = showResult ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO;
return new Promise<boolean | void>((resolve) => {
@@ -36,20 +39,25 @@ export function createOpenReplicationUI(
activeSynchronisations++;
try {
// Pull first, then push only when the pull succeeds.
const pullResult = await replicator.replicateFrom(peerId, showResult);
if (!pullResult?.ok) {
const pullResult = await p2p.targetedTransfer.pullFromPeer(peerId, {
showNotice: showResult,
});
if (pullResult.status !== "completed" || !pullResult.ok) {
sessionResult = false;
return;
return false;
}
const pushResult = await replicator.requestSynchroniseToPeer(peerId);
sessionResult = pushResult?.ok ?? true;
if (sessionResult && closeConnection) await replicator.close();
const pushResult = await p2p.targetedTransfer.requestPushToPeer(peerId);
const completed = pushResult.status === "completed" && pushResult.ok === true;
sessionResult = completed;
if (completed && closeConnection) await p2p.transportLifecycle.disconnect();
return completed;
} catch (e) {
Logger(
`Error in bidirectional sync with ${peerId}: ${e instanceof Error ? e.message : String(e)}`,
logLevel
);
sessionResult = false;
return false;
} finally {
activeSynchronisations--;
settleClosedSession();
@@ -57,7 +65,7 @@ export function createOpenReplicationUI(
};
const modal = new P2POpenReplicationModal(
app,
replicator,
p2p,
{
onSync: (peerId: string) => synchronise(peerId, false),
onSyncAndClose: (peerId: string) => synchronise(peerId, true),
@@ -81,12 +89,12 @@ export function createOpenReplicationUI(
*
* Usage:
* const factory = createOpenRebuildUI(app);
* useP2PReplicatorFeature(core, createOpenReplicationUI(app), factory);
* useP2PReplicatorFeature(core, openReplicationUIFactory, factory);
*/
export function createOpenRebuildUI(
app: App
): (replicator: LiveSyncTrysteroReplicator) => (showResult: boolean) => Promise<boolean | void> {
return (replicator: LiveSyncTrysteroReplicator) =>
): (replicator: LiveSyncTrysteroReplicator, p2p: P2PServiceViews) => (showResult: boolean) => Promise<boolean | void> {
return (replicator: LiveSyncTrysteroReplicator, p2p: P2PServiceViews) =>
(showResult: boolean): Promise<boolean | void> => {
const logLevel = showResult ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO;
return new Promise<boolean | void>((resolve) => {
@@ -113,12 +121,14 @@ export function createOpenRebuildUI(
Logger(`Rebuilding from peer ${peerId}`, logLevel);
const result = await replicator.replicateFrom(peerId, showResult, true);
sessionResult = result?.ok ?? false;
return sessionResult;
} catch (e) {
Logger(
`Error in rebuild from ${peerId}: ${e instanceof Error ? e.message : String(e)}`,
logLevel
);
sessionResult = false;
return false;
} finally {
try {
replicator.clearOnSetup();
@@ -132,7 +142,7 @@ export function createOpenRebuildUI(
const modal = new P2POpenReplicationModal(
app,
replicator,
p2p,
{
onSync: doRebuild,
onSyncAndClose: doRebuild,
@@ -2,9 +2,10 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
const modalState = vi.hoisted(() => ({
instances: [] as Array<{
p2p: unknown;
callback: {
onSync: (peerId: string) => Promise<void>;
onSyncAndClose: (peerId: string) => Promise<void>;
onSync: (peerId: string) => Promise<boolean>;
onSyncAndClose: (peerId: string) => Promise<boolean>;
};
onClosed?: () => void;
open: ReturnType<typeof vi.fn>;
@@ -15,18 +16,20 @@ vi.mock("@/deps.ts", () => ({ App: class {} }));
vi.mock("./P2POpenReplicationModal", () => ({
P2POpenReplicationModal: class {
p2p;
callback;
onClosed;
open = vi.fn();
constructor(
_app: unknown,
_replicator: unknown,
p2p: unknown,
callback: (typeof modalState.instances)[number]["callback"],
_showResult: boolean,
_title?: string,
onClosed?: () => void
) {
this.p2p = p2p;
this.callback = callback;
this.onClosed = onClosed;
modalState.instances.push(this);
@@ -38,23 +41,38 @@ import { createOpenRebuildUI, createOpenReplicationUI } from "./P2PReplicationUI
function createReplicator() {
return {
replicateFrom: vi.fn(async () => ({ ok: true })),
requestSynchroniseToPeer: vi.fn(async () => ({ ok: true })),
replicateFrom: vi.fn(async () => ({ status: "completed" as const, ok: true as const })),
requestSynchroniseToPeer: vi.fn(async () => ({ status: "completed" as const, ok: true as const })),
close: vi.fn(async () => undefined),
setOnSetup: vi.fn(),
clearOnSetup: vi.fn(),
} as any;
}
function createP2PServiceViews() {
return {
transportLifecycle: {
disconnect: vi.fn(async () => undefined),
},
targetedTransfer: {
pullFromPeer: vi.fn(async () => ({ status: "completed" as const, ok: true as const })),
requestPushToPeer: vi.fn(async () => ({ status: "completed" as const, ok: true as const })),
},
diagnostics: {},
} as any;
}
describe("createOpenReplicationUI", () => {
beforeEach(() => {
modalState.instances.length = 0;
});
it("settles a cancelled peer-selection session when the modal closes", async () => {
const session = createOpenReplicationUI({} as any)(createReplicator())(true);
const p2p = createP2PServiceViews();
const session = createOpenReplicationUI({} as any)(p2p)(true);
const modal = modalState.instances[0];
expect(modal.p2p).toBe(p2p);
expect(modal.onClosed).toBeTypeOf("function");
modal.onClosed?.();
@@ -62,36 +80,49 @@ describe("createOpenReplicationUI", () => {
});
it("keeps repeated synchronisation inside the session boundary until the modal closes", async () => {
const replicator = createReplicator();
const session = createOpenReplicationUI({} as any)(replicator)(true);
const p2p = createP2PServiceViews();
const session = createOpenReplicationUI({} as any)(p2p)(true);
const modal = modalState.instances[0];
let settled = false;
void session.finally(() => {
settled = true;
});
await modal.callback.onSync("peer-a");
await expect(modal.callback.onSync("peer-a")).resolves.toBe(true);
await Promise.resolve();
expect(settled).toBe(false);
await modal.callback.onSync("peer-b");
expect(replicator.replicateFrom).toHaveBeenCalledTimes(2);
expect(replicator.requestSynchroniseToPeer).toHaveBeenCalledTimes(2);
expect(p2p.targetedTransfer.pullFromPeer).toHaveBeenCalledTimes(2);
expect(p2p.targetedTransfer.requestPushToPeer).toHaveBeenCalledTimes(2);
modal.onClosed?.();
await expect(session).resolves.toBe(true);
});
it("routes ordinary peer transfer through the stable targeted-transfer view", async () => {
const p2p = createP2PServiceViews();
const session = createOpenReplicationUI({} as any)(p2p)(true);
const modal = modalState.instances[0];
await modal.callback.onSync("peer-a");
modal.onClosed?.();
await expect(session).resolves.toBe(true);
expect(p2p.targetedTransfer.pullFromPeer).toHaveBeenCalledWith("peer-a", { showNotice: true });
expect(p2p.targetedTransfer.requestPushToPeer).toHaveBeenCalledWith("peer-a");
});
it("waits for an in-flight synchronisation when the modal closes", async () => {
let finishPull!: (value: { ok: boolean }) => void;
const replicator = createReplicator();
replicator.replicateFrom.mockImplementation(
let finishPull!: (value: { status: "completed"; ok: true }) => void;
const p2p = createP2PServiceViews();
p2p.targetedTransfer.pullFromPeer.mockImplementation(
async () =>
await new Promise<{ ok: boolean }>((resolve) => {
await new Promise<{ status: "completed"; ok: true }>((resolve) => {
finishPull = resolve;
})
);
const session = createOpenReplicationUI({} as any)(replicator)(true);
const session = createOpenReplicationUI({} as any)(p2p)(true);
const modal = modalState.instances[0];
let settled = false;
void session.finally(() => {
@@ -104,19 +135,19 @@ describe("createOpenReplicationUI", () => {
expect(settled).toBe(false);
finishPull({ ok: true });
finishPull({ status: "completed", ok: true });
await synchronisation;
await expect(session).resolves.toBe(true);
});
it("closes the P2P connection after a successful sync-and-close action", async () => {
const replicator = createReplicator();
const session = createOpenReplicationUI({} as any)(replicator)(true);
const p2p = createP2PServiceViews();
const session = createOpenReplicationUI({} as any)(p2p)(true);
const modal = modalState.instances[0];
await modal.callback.onSyncAndClose("peer-a");
expect(replicator.close).toHaveBeenCalledOnce();
expect(p2p.transportLifecycle.disconnect).toHaveBeenCalledOnce();
let settled = false;
void session.finally(() => {
settled = true;
@@ -127,6 +158,19 @@ describe("createOpenReplicationUI", () => {
modal.onClosed?.();
await expect(session).resolves.toBe(true);
});
it("returns a cancelled peer push as non-success to the presentation boundary", async () => {
const p2p = createP2PServiceViews();
p2p.targetedTransfer.requestPushToPeer.mockResolvedValue({ status: "cancelled" } as never);
const session = createOpenReplicationUI({} as any)(p2p)(true);
const modal = modalState.instances[0];
const actionResult = await modal.callback.onSync("peer-a");
modal.onClosed?.();
expect(actionResult).toBe(false);
await expect(session).resolves.toBe(false);
});
});
describe("createOpenRebuildUI", () => {
@@ -135,15 +179,15 @@ describe("createOpenRebuildUI", () => {
});
it("waits for an in-flight rebuild when the modal closes", async () => {
let finishPull!: (value: { ok: boolean }) => void;
let finishPull!: (value: { status: "completed"; ok: true }) => void;
const replicator = createReplicator();
replicator.replicateFrom.mockImplementation(
async () =>
await new Promise<{ ok: boolean }>((resolve) => {
await new Promise<{ status: "completed"; ok: true }>((resolve) => {
finishPull = resolve;
})
);
const session = createOpenRebuildUI({} as any)(replicator)(true);
const session = createOpenRebuildUI({} as any)(replicator, createP2PServiceViews())(true);
const modal = modalState.instances[0];
let settled = false;
void session.finally(() => {
@@ -156,8 +200,8 @@ describe("createOpenRebuildUI", () => {
expect(settled).toBe(false);
finishPull({ ok: true });
await rebuild;
finishPull({ status: "completed", ok: true });
await expect(rebuild).resolves.toBe(true);
await expect(session).resolves.toBe(true);
expect(replicator.setOnSetup).toHaveBeenCalledOnce();
expect(replicator.replicateFrom).toHaveBeenCalledWith("peer-a", true, true);
@@ -166,7 +210,7 @@ describe("createOpenRebuildUI", () => {
it("does not complete Fetch when the rebuild dialogue closes without selecting a peer", async () => {
const replicator = createReplicator();
const session = createOpenRebuildUI({} as any)(replicator)(true);
const session = createOpenRebuildUI({} as any)(replicator, createP2PServiceViews())(true);
const modal = modalState.instances[0];
modal.onClosed?.();
@@ -13,7 +13,6 @@
type PeerInfo,
type P2PServerInfo,
EVENT_SERVER_STATUS,
EVENT_REQUEST_STATUS,
EVENT_P2P_REPLICATOR_STATUS,
} from "@vrtmrz/livesync-commonlib/compat/replication/trystero/TrysteroReplicatorP2PServer";
import type { P2PReplicatorStatus } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/TrysteroReplicator";
@@ -29,7 +28,6 @@
let services = $derived(host.services);
let events = $derived(services.context.events);
const currentSettings = () => services.setting.currentSettings() as P2PSyncSetting;
const currentReplicator = () => host.p2p.replicator;
const initialSettings = { ...currentSettings() } as P2PSyncSetting;
let settings = $state<P2PSyncSetting>(initialSettings);
@@ -146,7 +144,7 @@
replicatorInfo = status;
});
applyLoadSettings(currentSettings(), true);
events.emitEvent(EVENT_REQUEST_STATUS);
host.p2p.diagnostics.requestStatus();
return () => {
r();
rx();
@@ -223,16 +221,16 @@
}
async function openServer() {
await currentReplicator().open();
await host.p2p.transportLifecycle.connect();
}
async function closeServer() {
await currentReplicator().close();
await host.p2p.transportLifecycle.disconnect();
}
function startBroadcasting() {
currentReplicator().enableBroadcastChanges();
host.p2p.changeRelay.enableBroadcastChanges();
}
function stopBroadcasting() {
currentReplicator().disableBroadcastChanges();
host.p2p.changeRelay.disableBroadcastChanges();
}
const initialDialogStatusKey = `p2p-dialog-status`;
@@ -1,12 +1,20 @@
import type { RequiredServices } from "@vrtmrz/livesync-commonlib/compat/interfaces/ServiceModule";
import type { P2PServiceViews } from "@vrtmrz/livesync-commonlib/p2p";
import type { PeerStatus } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PReplicatorPaneCommon";
import type { UseP2PReplicatorResult } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/UseP2PReplicatorResult";
export type P2PReplicatorHandle = Pick<UseP2PReplicatorResult, "replicator">;
/**
* The shared pane only needs the contracts which represent its visible
* actions. In particular, it must not receive the compatibility Replicator
* facade, whose lifecycle methods can bypass the stable P2P service owner.
*/
export type P2PReplicatorPaneP2P = Pick<
P2PServiceViews,
"transportLifecycle" | "peerDirectory" | "peerAdmission" | "targetedTransfer" | "changeRelay" | "diagnostics"
>;
/** Host capabilities consumed by the shared P2P pane. */
export interface P2PReplicatorPaneHost {
readonly services: RequiredServices<"API" | "config" | "setting" | "vault">;
readonly p2p: P2PReplicatorHandle;
readonly p2p: P2PReplicatorPaneP2P;
readonly showPeerMenu?: (peer: PeerStatus, event: MouseEvent) => void;
}
@@ -8,7 +8,7 @@ import { LOG_LEVEL_NOTICE, REMOTE_P2P } from "@vrtmrz/livesync-commonlib/compat/
import { Logger } from "@vrtmrz/livesync-commonlib/compat/common/logger";
import type { PeerStatus } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PReplicatorPaneCommon";
import type { LiveSyncBaseCore } from "@/LiveSyncBaseCore.ts";
import type { P2PPaneParams } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/UseP2PReplicatorResult";
import type { P2PServiceViews } from "@vrtmrz/livesync-commonlib/p2p";
export const VIEW_TYPE_P2P = "p2p-replicator";
function addToList(item: string, list: string) {
@@ -31,7 +31,7 @@ function removeFromList(item: string, list: string) {
export class P2PReplicatorPaneView extends SvelteItemView {
core: LiveSyncBaseCore;
private _p2pResult: P2PPaneParams;
private _p2p: P2PServiceViews;
override icon = "waypoints";
title: string = "";
override navigation = false;
@@ -39,21 +39,18 @@ export class P2PReplicatorPaneView extends SvelteItemView {
override getIcon(): string {
return "waypoints";
}
get replicator() {
return this._p2pResult.replicator;
}
async replicateFrom(peer: PeerStatus) {
await this.replicator.replicateFrom(peer.peerId);
await this._p2p.targetedTransfer.pullFromPeer(peer.peerId);
}
async replicateTo(peer: PeerStatus) {
await this.replicator.requestSynchroniseToPeer(peer.peerId);
await this._p2p.targetedTransfer.requestPushToPeer(peer.peerId);
}
async getRemoteConfig(peer: PeerStatus) {
Logger(
`Requesting remote config for ${peer.name}. Please input the passphrase on the remote device`,
LOG_LEVEL_NOTICE
);
const remoteConfig = await this.replicator.getRemoteConfig(peer.peerId);
const remoteConfig = await this._p2p.configurationExchange.getRemoteConfiguration(peer.peerId);
if (remoteConfig) {
Logger(`Remote config for ${peer.name} is retrieved successfully`);
const DROP = "Yes, and drop local database";
@@ -122,10 +119,10 @@ And you can also drop the local database to rebuild from the remote device.`,
await this.core.services.setting.applyPartial(currentSetting, true);
}
m?: Menu;
constructor(leaf: WorkspaceLeaf, core: LiveSyncBaseCore, p2pResult: P2PPaneParams) {
constructor(leaf: WorkspaceLeaf, core: LiveSyncBaseCore, p2p: P2PServiceViews) {
super(leaf);
this.core = core;
this._p2pResult = p2pResult;
this._p2p = p2p;
}
private showPeerMenu(peer: PeerStatus, event: MouseEvent): void {
@@ -187,7 +184,7 @@ And you can also drop the local database to rebuild from the remote device.`,
props: {
host: {
services: this.core.services,
p2p: this._p2pResult,
p2p: this._p2p,
showPeerMenu: (peer: PeerStatus, event: MouseEvent) => this.showPeerMenu(peer, event),
},
},
@@ -9,19 +9,19 @@
EVENT_P2P_REPLICATOR_STATUS,
} from "@vrtmrz/livesync-commonlib/compat/replication/trystero/TrysteroReplicatorP2PServer";
import { EVENT_SETTING_SAVED } from "@vrtmrz/livesync-commonlib/compat/events/coreEvents";
import type { LiveSyncTrysteroReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/LiveSyncTrysteroReplicator";
import type { P2PServiceViews } from "@vrtmrz/livesync-commonlib/p2p";
import type { P2PReplicatorStatus } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/TrysteroReplicator";
import { extractP2PRoomSuffix } from "@vrtmrz/livesync-commonlib/compat/common/utils";
import type { LiveSyncBaseCore } from "@/LiveSyncBaseCore";
import { $msg as translateMessage } from "@/common/translation";
interface Props {
getLiveSyncReplicator: () => LiveSyncTrysteroReplicator;
p2p: P2PServiceViews;
showBroadcastToggle?: boolean;
core?: LiveSyncBaseCore;
}
let { getLiveSyncReplicator, showBroadcastToggle = true, core }: Props = $props();
let { p2p, showBroadcastToggle = true, core }: Props = $props();
let serverInfo = $state<P2PServerInfo | undefined>(undefined);
let replicatorStatus = $state<P2PReplicatorStatus | undefined>(undefined);
// Later setting changes arrive through EVENT_SETTING_SAVED; these values only seed local state at mount time.
@@ -31,25 +31,25 @@
let useDiagRTC = $state<boolean>(initialSettings?.P2P_useDiagRTC ?? false);
async function requestServerStatus() {
await Promise.resolve(getLiveSyncReplicator().requestStatus());
p2p.diagnostics.requestStatus();
eventHub.emitEvent(EVENT_REQUEST_STATUS);
}
async function onOpenConnection() {
await getLiveSyncReplicator().makeSureOpened();
await p2p.transportLifecycle.connect();
await requestServerStatus();
}
async function onDisconnect() {
await getLiveSyncReplicator().close();
await p2p.transportLifecycle.disconnect();
await requestServerStatus();
}
function toggleBroadcast() {
if (replicatorStatus?.isBroadcasting) {
getLiveSyncReplicator().disableBroadcastChanges();
p2p.changeRelay.disableBroadcastChanges();
} else {
getLiveSyncReplicator().enableBroadcastChanges();
p2p.changeRelay.enableBroadcastChanges();
}
}
@@ -8,7 +8,7 @@
EVENT_P2P_REPLICATOR_PROGRESS,
type P2PServerInfo,
} from "@vrtmrz/livesync-commonlib/compat/replication/trystero/TrysteroReplicatorP2PServer";
import type { LiveSyncTrysteroReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/LiveSyncTrysteroReplicator";
import type { P2PServiceViews } from "@vrtmrz/livesync-commonlib/p2p";
import type { P2PReplicatorStatus, P2PReplicationReport } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/TrysteroReplicator";
import { delay, fireAndForget } from "octagonal-wheels/promises";
import P2PServerStatusCard from "./P2PServerStatusCard.svelte";
@@ -23,7 +23,6 @@
} from "@vrtmrz/livesync-commonlib/remote-configurations";
import { extractP2PRoomSuffix } from "@vrtmrz/livesync-commonlib/compat/common/utils";
import { SetupManager } from "@/modules/features/SetupManager";
import SetupRemoteP2P from "@/modules/features/SetupWizard/dialogs/SetupRemoteP2P.svelte";
import { Menu } from "@/deps";
import { $msg as translateMessage } from "@/common/translation";
import {
@@ -33,11 +32,11 @@
} from "./p2pPeerSettings";
interface Props {
getLiveSyncReplicator: () => LiveSyncTrysteroReplicator;
p2p: P2PServiceViews;
core: LiveSyncBaseCore;
}
let { getLiveSyncReplicator, core }: Props = $props();
let { p2p, core }: Props = $props();
let serverInfo = $state<P2PServerInfo | undefined>(undefined);
let replicatorInfo = $state<P2PReplicatorStatus | undefined>(undefined);
let decidingPeerId = $state<string | null>(null);
@@ -121,7 +120,7 @@
}
async function requestServerStatus() {
await getLiveSyncReplicator().requestStatus();
p2p.diagnostics.requestStatus();
eventHub.emitEvent(EVENT_REQUEST_STATUS);
}
@@ -213,9 +212,8 @@
async function createAndSelectP2PRemote() {
const setupManager = core.getModule(SetupManager);
const dialogManager = setupManager.dialogManager;
const currentSettings = core.services.setting.currentSettings();
const p2pConf = await dialogManager.openWithExplicitCancel(SetupRemoteP2P, currentSettings);
const p2pConf = await setupManager.openP2PSetup(currentSettings);
if (p2pConf === "cancelled" || typeof p2pConf !== "object" || !p2pConf) {
return;
}
@@ -296,7 +294,7 @@
) {
decidingPeerId = peer.peerId;
try {
await getLiveSyncReplicator().makeDecision({
await p2p.peerAdmission.makeDecision({
peerId: peer.peerId,
name: peer.name,
decision,
@@ -311,7 +309,7 @@
async function revokeDecision(peer: P2PServerInfo["knownAdvertisements"][number]) {
decidingPeerId = peer.peerId;
try {
await getLiveSyncReplicator().revokeDecision({
await p2p.peerAdmission.revokeDecision({
peerId: peer.peerId,
name: peer.name,
});
@@ -324,10 +322,7 @@
async function startReplication(peer: P2PServerInfo["knownAdvertisements"][number]) {
replicatingPeerId = peer.peerId;
try {
const pullResult = await getLiveSyncReplicator().replicateFrom(peer.peerId, true);
if (pullResult?.ok) {
await getLiveSyncReplicator().requestSynchroniseToPeer(peer.peerId);
}
await p2p.targetedTransfer.synchroniseWithPeer(peer.peerId, true);
await requestServerStatus();
} finally {
replicatingPeerId = null;
@@ -347,9 +342,9 @@
return;
}
if (isWatching(peerId)) {
getLiveSyncReplicator().unwatchPeer(peerId);
p2p.changeRelay.unwatchPeer(peerId);
} else {
getLiveSyncReplicator().watchPeer(peerId);
p2p.changeRelay.watchPeer(peerId);
}
}
@@ -455,7 +450,7 @@
</p>
{/if}
<P2PServerStatusCard {getLiveSyncReplicator} {core} />
<P2PServerStatusCard {p2p} {core} />
<div class="peers-section">
<div class="peers-header">
@@ -2,21 +2,21 @@ import { WorkspaceLeaf } from "@/deps.ts";
import { mount } from "svelte";
import { SvelteItemView } from "@/common/SvelteItemView.ts";
import type { LiveSyncBaseCore } from "@/LiveSyncBaseCore.ts";
import type { P2PPaneParams } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/UseP2PReplicatorResult";
import type { P2PServiceViews } from "@vrtmrz/livesync-commonlib/p2p";
import P2PServerStatusPane from "./P2PServerStatusPane.svelte";
export const VIEW_TYPE_P2P_SERVER_STATUS = "p2p-server-status";
export class P2PServerStatusPaneView extends SvelteItemView {
core: LiveSyncBaseCore;
private _p2pResult: P2PPaneParams;
private readonly p2p: P2PServiceViews;
override icon = "waypoints";
override navigation = false;
constructor(leaf: WorkspaceLeaf, core: LiveSyncBaseCore, p2pResult: P2PPaneParams) {
constructor(leaf: WorkspaceLeaf, core: LiveSyncBaseCore, p2p: P2PServiceViews) {
super(leaf);
this.core = core;
this._p2pResult = p2pResult;
this.p2p = p2p;
}
override getIcon(): string {
@@ -35,7 +35,7 @@ export class P2PServerStatusPaneView extends SvelteItemView {
return mount(P2PServerStatusPane, {
target,
props: {
getLiveSyncReplicator: () => this._p2pResult.replicator,
p2p: this.p2p,
core: this.core,
},
});
@@ -1,17 +1,16 @@
<script lang="ts">
import { AcceptedStatus, type PeerStatus } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PReplicatorPaneCommon";
import type { P2PReplicatorHandle } from "./P2PReplicatorPaneHost";
import type { P2PReplicatorPaneP2P } from "./P2PReplicatorPaneHost";
import { $msg as translateMessage } from "@/common/translation";
interface Props {
peerStatus: PeerStatus;
p2p: P2PReplicatorHandle;
p2p: P2PReplicatorPaneP2P;
showPeerMenu?: (peer: PeerStatus, event: MouseEvent) => void;
}
let { peerStatus, p2p, showPeerMenu }: Props = $props();
let peer = $derived(peerStatus);
const currentReplicator = () => p2p.replicator;
function select<T extends PropertyKey, U, V = undefined>(
d: T,
@@ -72,7 +71,7 @@
let isNew = $derived.by(() => peer.accepted === AcceptedStatus.UNKNOWN);
function makeDecision(isAccepted: boolean, isTemporary: boolean) {
currentReplicator().makeDecision({
void p2p.peerAdmission.makeDecision({
peerId: peer.peerId,
name: peer.name,
decision: isAccepted,
@@ -80,7 +79,7 @@
});
}
function revokeDecision() {
currentReplicator().revokeDecision({
void p2p.peerAdmission.revokeDecision({
peerId: peer.peerId,
name: peer.name,
});
@@ -99,14 +98,14 @@
return attrs;
});
function startWatching() {
currentReplicator().watchPeer(peer.peerId);
p2p.changeRelay.watchPeer(peer.peerId);
}
function stopWatching() {
currentReplicator().unwatchPeer(peer.peerId);
p2p.changeRelay.unwatchPeer(peer.peerId);
}
function sync() {
void currentReplicator().sync(peer.peerId, false);
void p2p.targetedTransfer.synchroniseWithPeer(peer.peerId, false);
}
function moreMenu(evt: MouseEvent) {
@@ -24,14 +24,6 @@ export const REVIEW_HARNESS_SCENARIOS = [
mode: "guided",
access: "device-local-state",
},
{
id: "p2p-composition",
title: "P2P composition",
description:
"Checks that the Obsidian host and P2P interface still resolve the current Commonlib replicator.",
mode: "automatic",
access: "read-only",
},
{
id: "vault-round-trip",
title: "Vault fixture round trip",
@@ -74,7 +74,6 @@ describe("Review Harness contract", () => {
expect(REVIEW_HARNESS_SCENARIO_IDS).toEqual([
"settings-lifecycle",
"compatibility-review",
"p2p-composition",
"vault-round-trip",
]);
});
@@ -20,11 +20,6 @@ export interface ReviewHarnessRuntime {
isCompatibilityReviewInitialised(): boolean;
getCompatibilityPause(): CompatibilityPause | undefined;
openCompatibilityReview(): Promise<void>;
getP2PComposition(): {
readonly first: unknown;
readonly second: unknown;
readonly expectedServices: unknown;
};
runVaultRoundTrip(): Promise<ReviewHarnessScenarioResult>;
readContinuation(): string | null;
writeContinuation(value: string): void;
@@ -63,37 +58,6 @@ function initialResults(): Record<ReviewHarnessScenarioId, ReviewHarnessScenario
>;
}
function inspectP2PComposition(input: ReturnType<ReviewHarnessRuntime["getP2PComposition"]>): ReviewHarnessScenarioResult {
if (input.first !== input.second) {
return {
status: "failed",
detail: "Two consecutive reads resolved different P2P replicators without a lifecycle transition.",
observations: [],
};
}
if (typeof input.first !== "object" || input.first === null) {
return {
status: "failed",
detail: "The P2P composition did not expose a current replicator.",
observations: [],
};
}
const env = "env" in input.first ? input.first.env : undefined;
const services = typeof env === "object" && env !== null && "services" in env ? env.services : undefined;
if (services !== input.expectedServices) {
return {
status: "failed",
detail: "The current P2P replicator is not bound to the active Obsidian services.",
observations: [],
};
}
return {
status: "passed",
detail: "The live P2P result resolves the current replicator and active Obsidian services.",
observations: [],
};
}
export class ReviewHarnessController {
private readonly results = initialResults();
private readonly transcript: ReviewHarnessTranscriptEntry[] = [];
@@ -163,9 +127,7 @@ export class ReviewHarnessController {
}
async runAutomaticScenarios(): Promise<void> {
for (const id of ["settings-lifecycle", "p2p-composition"] as const) {
await this.runScenario(id);
}
await this.runScenario("settings-lifecycle");
}
async runAllScenarios(): Promise<void> {
@@ -195,8 +157,6 @@ export class ReviewHarnessController {
settings: this.runtime.getSettings(),
newVaultSettings: this.runtime.getNewVaultSettings(),
});
} else if (id === "p2p-composition") {
result = inspectP2PComposition(this.runtime.getP2PComposition());
} else if (id === "vault-round-trip") {
result = await this.runtime.runVaultRoundTrip();
} else {
@@ -43,8 +43,6 @@ function createRuntime(): ReviewHarnessRuntime & {
continuation: string | null;
events: string[];
} {
const services = {};
const replicator = { env: { services } };
const runtime: ReviewHarnessRuntime & {
compatibilityReviewInitialised: boolean;
compatibilityPause: CompatibilityPause | undefined;
@@ -77,7 +75,6 @@ function createRuntime(): ReviewHarnessRuntime & {
runtime.events.push("open-compatibility-review");
runtime.compatibilityPause = undefined;
}),
getP2PComposition: () => ({ first: replicator, second: replicator, expectedServices: services }),
runVaultRoundTrip: vi.fn(async () => ({
status: "passed" as const,
detail: "The owned fixture tree was exercised and removed.",
@@ -110,14 +107,13 @@ function createRuntime(): ReviewHarnessRuntime & {
}
describe("ReviewHarnessController", () => {
it("runs the automatic settings and P2P composition checks", async () => {
it("runs the automatic settings check", async () => {
const runtime = createRuntime();
const controller = new ReviewHarnessController(runtime);
await controller.runAutomaticScenarios();
expect(controller.snapshot().results["settings-lifecycle"].status).toBe("passed");
expect(controller.snapshot().results["p2p-composition"].status).toBe("passed");
expect(controller.snapshot().results["vault-round-trip"].status).toBe("idle");
expect(controller.snapshot().results["compatibility-review"].status).toBe("idle");
});
+6 -9
View File
@@ -39,8 +39,7 @@ import { useSetupProtocolFeature } from "./serviceFeatures/setupObsidian/setupPr
import { useSetupQRCodeFeature } from "@/serviceFeatures/setupObsidian/qrCode";
import { useSetupURIFeature } from "@/serviceFeatures/setupObsidian/setupUri";
import { useSetupManagerHandlersFeature } from "./serviceFeatures/setupObsidian/setupManagerHandlers.ts";
import { useP2PReplicatorFeature } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/useP2PReplicatorFeature";
import { useP2PReplicatorCommands } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/useP2PReplicatorCommands";
import { useP2PReplicatorCommands, useP2PReplicatorFeature } from "@vrtmrz/livesync-commonlib/p2p";
import { useP2PReplicatorUI } from "./serviceFeatures/useP2PReplicatorUI.ts";
import { useReviewHarness } from "./serviceFeatures/useReviewHarness.ts";
import { createOpenReplicationUI, createOpenRebuildUI } from "./features/P2PSync/P2PReplicator/P2PReplicationUI.ts";
@@ -179,13 +178,15 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
const curriedFeature = () => featuresInitialiser(core);
core.services.appLifecycle.onLayoutReady.addHandler(curriedFeature);
const setupManager = core.getModule(SetupManager);
const createInteractiveP2PReplication = createOpenReplicationUI(this.app);
const replicator = useP2PReplicatorFeature(
core,
createOpenReplicationUI(this.app),
(_compatibilityReplicator, p2p) => createInteractiveP2PReplication(p2p),
createOpenRebuildUI(this.app)
);
setupManager.registerP2PSetupConnectionProbe(replicator.connectionProbe);
useP2PReplicatorCommands(core, replicator);
useP2PReplicatorUI(core, core, replicator);
useP2PReplicatorUI(core, core, replicator, createInteractiveP2PReplication(replicator));
useRemoteConfiguration(core);
useSetupProtocolFeature(core, setupManager);
@@ -200,11 +201,7 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
createObsidianCompatibilityReviewUi(core.confirm)
);
waitForCompatibilityReview = () => compatibilityReview.openReview();
useReviewHarness(core, this, replicator, compatibilityReview);
// p2pReplicatorResult = useP2PReplicator(core, [
// VIEW_TYPE_P2P,
// (leaf: any) => new P2PReplicatorPaneView(leaf, core, p2pReplicatorResult!),
// ]);
useReviewHarness(core, this, compatibilityReview);
}
);
}
-41
View File
@@ -1,41 +0,0 @@
import { PeriodicProcessor } from "@/common/PeriodicProcessor";
import type { LiveSyncCore } from "@/main";
import { AbstractModule } from "@/modules/AbstractModule";
export class ModulePeriodicProcess extends AbstractModule {
periodicSyncProcessor = new PeriodicProcessor(this.core, async () => await this.services.replication.replicate());
disablePeriodic() {
this.periodicSyncProcessor?.disable();
return Promise.resolve(true);
}
resumePeriodic() {
this.periodicSyncProcessor.enable(
this.settings.periodicReplication ? this.settings.periodicReplicationInterval * 1000 : 0
);
return Promise.resolve(true);
}
private _allOnUnload() {
return this.disablePeriodic();
}
private _everyBeforeRealizeSetting(): Promise<boolean> {
return this.disablePeriodic();
}
private _everyBeforeSuspendProcess(): Promise<boolean> {
return this.disablePeriodic();
}
private _everyAfterResumeProcess(): Promise<boolean> {
return this.resumePeriodic();
}
private _everyAfterRealizeSetting(): Promise<boolean> {
return this.resumePeriodic();
}
override onBindFunction(core: LiveSyncCore, services: typeof core.services): void {
services.appLifecycle.onUnload.addHandler(this._allOnUnload.bind(this));
services.setting.onBeforeRealiseSetting.addHandler(this._everyBeforeRealizeSetting.bind(this));
services.setting.onSettingRealised.addHandler(this._everyAfterRealizeSetting.bind(this));
services.appLifecycle.onSuspending.addHandler(this._everyBeforeSuspendProcess.bind(this));
services.appLifecycle.onResumed.addHandler(this._everyAfterResumeProcess.bind(this));
}
}
-353
View File
@@ -1,353 +0,0 @@
import type PouchDB from "pouchdb-core";
import { fireAndForget } from "octagonal-wheels/promises";
import { AbstractModule } from "@/modules/AbstractModule";
import { Logger, LOG_LEVEL_NOTICE, LOG_LEVEL_INFO } from "octagonal-wheels/common/logger";
import { skipIfDuplicated } from "octagonal-wheels/concurrency/lock";
import { balanceChunkPurgedDBs } from "@vrtmrz/livesync-commonlib/compat/pouchdb/chunks";
import { purgeUnreferencedChunks } from "@vrtmrz/livesync-commonlib/compat/pouchdb/chunks";
import { LiveSyncCouchDBReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import {
type EntryDoc,
type ObsidianLiveSyncSettings,
type RemoteType,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { scheduleTask } from "octagonal-wheels/concurrency/task";
import { EVENT_FILE_SAVED, EVENT_SETTING_SAVED, eventHub } from "@/common/events";
import { $msg } from "@/common/translation";
import type { LiveSyncCore } from "@/main";
import { ReplicateResultProcessor } from "./ReplicateResultProcessor";
import { UnresolvedErrorManager } from "@vrtmrz/livesync-commonlib/compat/services/base/UnresolvedErrorManager";
import { clearHandlers } from "@vrtmrz/livesync-commonlib/compat/replication/SyncParamsHandler";
import type { NecessaryServices } from "@vrtmrz/livesync-commonlib/compat/interfaces/ServiceModule";
import { MARK_LOG_NETWORK_ERROR } from "@vrtmrz/livesync-commonlib/compat/services/lib/logUtils";
import { usesLegacyIndexedDBAdapter } from "@/common/compatibilitySettings.ts";
function isOnlineAndCanReplicate(
errorManager: UnresolvedErrorManager,
host: NecessaryServices<"API", never>,
showMessage: boolean
): Promise<boolean> {
const errorMessage = "Network is offline";
if (!host.services.API.isOnline) {
errorManager.showError(errorMessage, showMessage ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO);
return Promise.resolve(false);
}
errorManager.clearError(errorMessage);
return Promise.resolve(true);
}
async function canReplicateWithPBKDF2(
errorManager: UnresolvedErrorManager,
host: NecessaryServices<"replicator" | "setting", never>,
showMessage: boolean
): Promise<boolean> {
const currentSettings = host.services.setting.currentSettings();
// TODO: check using PBKDF2 salt?
const errorMessage = $msg("Replicator.Message.InitialiseFatalError");
const replicator = host.services.replicator.getActiveReplicator();
if (!replicator) {
errorManager.showError(errorMessage, showMessage ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO);
return false;
}
errorManager.clearError(errorMessage);
// Showing message is false: that because be shown here. (And it is a fatal error, no way to hide it).
// tagged as network error at beginning for error filtering with NetworkWarningStyles
const ensureMessage = `${MARK_LOG_NETWORK_ERROR}Failed to initialise the encryption key, preventing replication.`;
// A remote database rebuild replaces the Security Seed while this process may still hold the previous one.
const ensureResult = await replicator.ensurePBKDF2Salt(currentSettings, showMessage, false);
if (!ensureResult) {
errorManager.showError(ensureMessage, showMessage ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO);
return false;
}
errorManager.clearError(ensureMessage);
return ensureResult; // is true.
}
export class ModuleReplicator extends AbstractModule {
_replicatorType?: RemoteType;
processor: ReplicateResultProcessor = new ReplicateResultProcessor(this);
private _unresolvedErrorManager: UnresolvedErrorManager = new UnresolvedErrorManager(
this.core.services.appLifecycle,
this.core.services.context.events
);
clearErrors() {
this._unresolvedErrorManager.clearErrors();
}
private _normalFileReflectionFilterSignature: string | undefined;
private getNormalFileReflectionFilterSignature(
settings: Pick<
ObsidianLiveSyncSettings,
| "handleFilenameCaseSensitive"
| "ignoreFiles"
| "maxMTimeForReflectEvents"
| "syncIgnoreRegEx"
| "syncInternalFiles"
| "syncMaxSizeInMB"
| "syncOnlyRegEx"
| "useIgnoreFiles"
>
): string {
return JSON.stringify({
handleFilenameCaseSensitive: settings.handleFilenameCaseSensitive ?? false,
ignoreFiles: settings.ignoreFiles ?? "",
maxMTimeForReflectEvents: settings.maxMTimeForReflectEvents ?? 0,
syncIgnoreRegEx: settings.syncIgnoreRegEx ?? "",
syncInternalFiles: settings.syncInternalFiles ?? false,
syncMaxSizeInMB: settings.syncMaxSizeInMB ?? 0,
syncOnlyRegEx: settings.syncOnlyRegEx ?? "",
useIgnoreFiles: settings.useIgnoreFiles ?? false,
});
}
private _everyOnloadAfterLoadSettings(): Promise<boolean> {
this._normalFileReflectionFilterSignature = this.getNormalFileReflectionFilterSignature(this.settings);
eventHub.onEvent(EVENT_FILE_SAVED, () => {
if (this.settings.syncOnSave && !this.core.services.appLifecycle.isSuspended()) {
scheduleTask("perform-replicate-after-save", 250, () => this.services.replication.replicateByEvent());
}
});
eventHub.onEvent(EVENT_SETTING_SAVED, (setting) => {
const previousReflectionFilter = this._normalFileReflectionFilterSignature;
const nextReflectionFilter = this.getNormalFileReflectionFilterSignature(setting);
this._normalFileReflectionFilterSignature = nextReflectionFilter;
if (this.core.settings.suspendParseReplicationResult) {
this.processor.suspend();
} else {
this.processor.resume();
}
if (previousReflectionFilter !== undefined && previousReflectionFilter !== nextReflectionFilter) {
fireAndForget(() => this.processor.reprocessStoredDocuments());
}
});
return Promise.resolve(true);
}
_onReplicatorInitialised(): Promise<boolean> {
// For now, we only need to clear the error related to replicator initialisation, but in the future, if there are more things to do when the replicator is initialised, we can add them here.
clearHandlers();
return Promise.resolve(true);
}
_everyOnDatabaseInitialized(showNotice: boolean): Promise<boolean> {
fireAndForget(() => this.processor.restoreFromSnapshotOnce());
return Promise.resolve(true);
}
async _everyBeforeReplicate(showMessage: boolean): Promise<boolean> {
await this.processor.restoreFromSnapshotOnce();
this.clearErrors();
return true;
}
/**
* Reconciles an IndexedDB-backed local database after replication reports that the remote was cleaned.
*
* The remote milestone remains a supported compatibility signal. The user can either fetch the remote
* database again, or purge unreferenced local chunks before accepting this device again.
*
* @param showMessage Whether to show the recovery choices as user-facing notices.
*/
async cleaned(showMessage: boolean) {
Logger(`The remote database has been cleaned.`, showMessage ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO);
await skipIfDuplicated("cleanup", async () => {
const count = await purgeUnreferencedChunks(this.localDatabase.localDatabase, true);
const message = `The remote database has been cleaned up.
To synchronize, this device must be also cleaned up. ${count} chunk(s) will be erased from this device.
However, If there are many chunks to be deleted, maybe fetching again is faster.
We will lose the history of this device if we fetch the remote database again.
Even if you choose to clean up, you will see this option again if you exit Obsidian and then synchronise again.`;
const CHOICE_FETCH = "Fetch again";
const CHOICE_CLEAN = "Cleanup";
const CHOICE_DISMISS = "Dismiss";
const ret = await this.core.confirm.confirmWithMessage(
"Cleaned",
message,
[CHOICE_FETCH, CHOICE_CLEAN, CHOICE_DISMISS],
CHOICE_DISMISS,
30
);
if (ret == CHOICE_FETCH) {
await this.core.rebuilder.$performRebuildDB("localOnly");
}
if (ret == CHOICE_CLEAN) {
await this.services.replicator.runBoundedRemoteActivity(
async () => {
const replicator = this.services.replicator.getActiveReplicator();
if (!(replicator instanceof LiveSyncCouchDBReplicator)) return;
const remoteDB = await replicator.connectRemoteCouchDBWithSetting(
this.settings,
this.services.API.isMobile(),
true
);
if (typeof remoteDB == "string") {
Logger(remoteDB, LOG_LEVEL_NOTICE);
return false;
}
try {
await purgeUnreferencedChunks(this.localDatabase.localDatabase, false);
this.localDatabase.clearCaches();
// Perform the synchronisation once.
const replicated = await this.services.replicator.runFiniteReplicationActivity(
() => this.core.replicator.openReplication(this.settings, false, showMessage, true),
{ label: "replication" }
);
if (replicated) {
await balanceChunkPurgedDBs(this.localDatabase.localDatabase, remoteDB.db);
await purgeUnreferencedChunks(this.localDatabase.localDatabase, false);
this.localDatabase.clearCaches();
await this.services.replicator.getActiveReplicator()?.markRemoteResolved(this.settings);
Logger(
"The local database has been cleaned up.",
showMessage ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO
);
} else {
Logger(
"Replication has been cancelled. Please try it again.",
showMessage ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO
);
}
} finally {
await remoteDB.db.close();
}
},
{ label: "database-cleanup" }
);
}
});
}
private async onReplicationFailed(showMessage: boolean = false): Promise<boolean> {
const activeReplicator = this.services.replicator.getActiveReplicator();
if (!activeReplicator) {
Logger(`No active replicator found`, LOG_LEVEL_INFO);
return false;
}
if (activeReplicator.tweakSettingsMismatched && activeReplicator.preferredTweakValue) {
await this.services.tweakValue.askResolvingMismatched(activeReplicator.preferredTweakValue);
} else {
if (activeReplicator.remoteLockedAndDeviceNotAccepted) {
if (activeReplicator.remoteCleaned && usesLegacyIndexedDBAdapter(this.settings)) {
await this.cleaned(showMessage);
} else {
const message = $msg("Replicator.Dialogue.Locked.Message");
const CHOICE_FETCH = $msg("Replicator.Dialogue.Locked.Action.Fetch");
const CHOICE_DISMISS = $msg("Replicator.Dialogue.Locked.Action.Dismiss");
const CHOICE_UNLOCK = $msg("Replicator.Dialogue.Locked.Action.Unlock");
const ret = await this.core.confirm.askSelectStringDialogue(
message,
[CHOICE_FETCH, CHOICE_UNLOCK, CHOICE_DISMISS],
{
title: $msg("Replicator.Dialogue.Locked.Title"),
defaultAction: CHOICE_DISMISS,
timeout: 60,
}
);
if (ret == CHOICE_FETCH) {
this._log($msg("Replicator.Dialogue.Locked.Message.Fetch"), LOG_LEVEL_NOTICE);
await this.core.rebuilder.scheduleFetch();
this.services.appLifecycle.scheduleRestart();
return false;
} else if (ret == CHOICE_UNLOCK) {
await activeReplicator.markRemoteResolved(this.settings);
this._log($msg("Replicator.Dialogue.Locked.Message.Unlocked"), LOG_LEVEL_NOTICE);
return false;
}
}
}
}
// TODO: Check again and true/false return. This will be the result for performReplication.
return false;
}
// private async _replicateByEvent(): Promise<boolean | void> {
// const least = this.settings.syncMinimumInterval;
// if (least > 0) {
// return rateLimitedSharedExecution(KEY_REPLICATION_ON_EVENT, least, async () => {
// return await this.services.replication.replicate();
// });
// }
// return await shareRunningResult(`replication`, () => this.services.replication.replicate());
// }
_parseReplicationResult(docs: Array<PouchDB.Core.ExistingDocument<EntryDoc>>): Promise<boolean> {
this.processor.enqueueAll(docs);
return Promise.resolve(true);
}
// _everyBeforeSuspendProcess(): Promise<boolean> {
// this.core.replicator?.closeReplication();
// return Promise.resolve(true);
// }
// private async _replicateAllToServer(
// showingNotice: boolean = false,
// sendChunksInBulkDisabled: boolean = false
// ): Promise<boolean> {
// if (!this.services.appLifecycle.isReady()) return false;
// if (!(await this.services.replication.onBeforeReplicate(showingNotice))) {
// Logger($msg("Replicator.Message.SomeModuleFailed"), LOG_LEVEL_NOTICE);
// return false;
// }
// if (!sendChunksInBulkDisabled) {
// if (this.core.replicator instanceof LiveSyncCouchDBReplicator) {
// if (
// (await this.core.confirm.askYesNoDialog("Do you want to send all chunks before replication?", {
// defaultOption: "No",
// timeout: 20,
// })) == "yes"
// ) {
// await this.core.replicator.sendChunks(this.core.settings, undefined, true, 0);
// }
// }
// }
// const ret = await this.core.replicator.replicateAllToServer(this.settings, showingNotice);
// if (ret) return true;
// const checkResult = await this.services.replication.checkConnectionFailure();
// if (checkResult == "CHECKAGAIN") return await this.services.remote.replicateAllToRemote(showingNotice);
// return !checkResult;
// }
// async _replicateAllFromServer(showingNotice: boolean = false): Promise<boolean> {
// if (!this.services.appLifecycle.isReady()) return false;
// const ret = await this.core.replicator.replicateAllFromServer(this.settings, showingNotice);
// if (ret) return true;
// const checkResult = await this.services.replication.checkConnectionFailure();
// if (checkResult == "CHECKAGAIN") return await this.services.remote.replicateAllFromRemote(showingNotice);
// return !checkResult;
// }
override onBindFunction(core: LiveSyncCore, services: typeof core.services): void {
services.replicator.onReplicatorInitialised.addHandler(this._onReplicatorInitialised.bind(this));
services.databaseEvents.onDatabaseInitialised.addHandler(this._everyOnDatabaseInitialized.bind(this));
services.appLifecycle.onSettingLoaded.addHandler(this._everyOnloadAfterLoadSettings.bind(this));
services.replication.parseSynchroniseResult.addHandler(this._parseReplicationResult.bind(this));
// --> These handlers can be separated.
const isOnlineAndCanReplicateWithHost = isOnlineAndCanReplicate.bind(null, this._unresolvedErrorManager, {
services: {
context: services.context,
API: services.API,
},
serviceModules: {},
});
const canReplicateWithPBKDF2WithHost = canReplicateWithPBKDF2.bind(null, this._unresolvedErrorManager, {
services: {
context: services.context,
replicator: services.replicator,
setting: services.setting,
},
serviceModules: {},
});
services.replication.onBeforeReplicate.addHandler(isOnlineAndCanReplicateWithHost, 10);
services.replication.onBeforeReplicate.addHandler(canReplicateWithPBKDF2WithHost, 20);
// <-- End of handlers that can be separated.
services.replication.onBeforeReplicate.addHandler(this._everyBeforeReplicate.bind(this), 100);
services.replication.onReplicationFailed.addHandler(this.onReplicationFailed.bind(this));
}
}
@@ -1,188 +0,0 @@
import { describe, expect, it, vi } from "vitest";
import { createServiceContext } from "@vrtmrz/livesync-commonlib/context";
import { EVENT_SETTING_SAVED, eventHub } from "@/common/events";
import type { ObsidianLiveSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
const chunkMocks = vi.hoisted(() => ({
purgeUnreferencedChunks: vi.fn(async (_db: unknown, countOnly: boolean) => (countOnly ? 2 : 0)),
balanceChunkPurgedDBs: vi.fn(async () => undefined),
}));
vi.mock("@vrtmrz/livesync-commonlib/compat/pouchdb/chunks", () => chunkMocks);
vi.mock("@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator", () => ({
LiveSyncCouchDBReplicator: class {},
}));
import { LiveSyncCouchDBReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import { ModuleReplicator } from "./ModuleReplicator";
describe("ModuleReplicator", () => {
it("refreshes the remote Security Seed before replication", async () => {
const ensurePBKDF2Salt = vi.fn(async () => true);
let beforeReplicate: ((showMessage: boolean) => Promise<boolean>) | undefined;
const addHandler = vi.fn((handler: (showMessage: boolean) => Promise<boolean>, priority?: number) => {
if (priority === 20) {
beforeReplicate = handler;
}
});
const services = {
API: { isOnline: true },
replicator: {
onReplicatorInitialised: { addHandler: vi.fn() },
getActiveReplicator: () => ({ ensurePBKDF2Salt }),
},
setting: { currentSettings: () => ({}) },
databaseEvents: { onDatabaseInitialised: { addHandler: vi.fn() } },
appLifecycle: { onSettingLoaded: { addHandler: vi.fn() } },
replication: {
parseSynchroniseResult: { addHandler: vi.fn() },
onBeforeReplicate: { addHandler },
onReplicationFailed: { addHandler: vi.fn() },
},
};
const module = {
_unresolvedErrorManager: {
showError: vi.fn(),
clearError: vi.fn(),
},
_onReplicatorInitialised: vi.fn(),
_everyOnDatabaseInitialized: vi.fn(),
_everyOnloadAfterLoadSettings: vi.fn(),
_parseReplicationResult: vi.fn(),
_everyBeforeReplicate: vi.fn(),
onReplicationFailed: vi.fn(),
};
ModuleReplicator.prototype.onBindFunction.call(module, {} as never, services as never);
expect(beforeReplicate).toBeDefined();
await beforeReplicate!(false);
expect(ensurePBKDF2Salt).toHaveBeenCalledWith({}, false, false);
});
it("reprocesses stored documents when the normal-file target filters change", async () => {
eventHub.offAll();
const settings = {
handleFilenameCaseSensitive: false,
ignoreFiles: ".gitignore",
maxMTimeForReflectEvents: 0,
syncOnlyRegEx: "^E2E/allowed/.*",
syncIgnoreRegEx: "",
syncInternalFiles: false,
syncMaxSizeInMB: 0,
suspendParseReplicationResult: false,
useIgnoreFiles: false,
} as ObsidianLiveSyncSettings;
const services = {
context: createServiceContext(),
API: {
addLog: vi.fn(),
addCommand: vi.fn(),
registerWindow: vi.fn(),
addRibbonIcon: vi.fn(),
registerProtocolHandler: vi.fn(),
},
appLifecycle: {
getUnresolvedMessages: { addHandler: vi.fn() },
isSuspended: vi.fn(() => false),
},
};
const core = {
_services: services,
services,
settings,
} as any;
const module = new ModuleReplicator(core);
const reprocessStoredDocuments = vi.fn(async () => 1);
Object.assign(module.processor, { reprocessStoredDocuments });
try {
await (module as any)._everyOnloadAfterLoadSettings();
eventHub.emitEvent(EVENT_SETTING_SAVED, { ...settings });
await Promise.resolve();
expect(reprocessStoredDocuments).not.toHaveBeenCalled();
Object.assign(settings, { syncOnlyRegEx: "" });
eventHub.emitEvent(EVENT_SETTING_SAVED, { ...settings });
await vi.waitFor(() => expect(reprocessStoredDocuments).toHaveBeenCalledOnce());
settings.syncMaxSizeInMB = 10;
eventHub.emitEvent(EVENT_SETTING_SAVED, { ...settings });
await vi.waitFor(() => expect(reprocessStoredDocuments).toHaveBeenCalledTimes(2));
} finally {
eventHub.offAll();
}
});
});
describe("compatibility: cleaned-remote reconciliation for IndexedDB clients", () => {
it("keeps its finite replication and balancing work inside the shared activity boundary", async () => {
const activityFinished = vi.fn();
const runBoundedRemoteActivity = vi.fn(async (task: () => unknown) => {
try {
return await task();
} finally {
activityFinished();
}
});
const runFiniteReplicationActivity = vi.fn(async (task: () => unknown) => await task());
const openReplication = vi.fn(async () => true);
const remoteDatabase = {
close: vi.fn(async () => undefined),
};
const activeReplicator = Object.assign(new LiveSyncCouchDBReplicator({} as any), {
connectRemoteCouchDBWithSetting: vi.fn(async () => ({ db: remoteDatabase })),
markRemoteResolved: vi.fn(async () => undefined),
});
const services = {
context: createServiceContext(),
API: {
addLog: vi.fn(),
addCommand: vi.fn(),
registerWindow: vi.fn(),
addRibbonIcon: vi.fn(),
registerProtocolHandler: vi.fn(),
isMobile: vi.fn(() => false),
},
setting: { saveSettingData: vi.fn(async () => undefined) },
appLifecycle: {
getUnresolvedMessages: { addHandler: vi.fn() },
},
replicator: {
getActiveReplicator: vi.fn(() => activeReplicator),
runBoundedRemoteActivity,
runFiniteReplicationActivity,
},
};
const localDatabase = {
localDatabase: {},
clearCaches: vi.fn(),
};
const core = {
_services: services,
services,
settings: {},
localDatabase,
confirm: { confirmWithMessage: vi.fn(async () => "Cleanup") },
replicator: { openReplication },
} as any;
const module = new ModuleReplicator(core);
await module.cleaned(true);
expect(runBoundedRemoteActivity).toHaveBeenCalledWith(expect.any(Function), {
label: "database-cleanup",
});
expect(runFiniteReplicationActivity).toHaveBeenCalledWith(expect.any(Function), {
label: "replication",
});
expect(openReplication).toHaveBeenCalledOnce();
expect(openReplication.mock.invocationCallOrder[0]).toBeLessThan(activityFinished.mock.invocationCallOrder[0]);
expect(chunkMocks.balanceChunkPurgedDBs).toHaveBeenCalledOnce();
expect(remoteDatabase.close).toHaveBeenCalledOnce();
expect(remoteDatabase.close.mock.invocationCallOrder[0]).toBeLessThan(
activityFinished.mock.invocationCallOrder[0]
);
});
});
@@ -1,50 +0,0 @@
import { fireAndForget } from "octagonal-wheels/promises";
import { REMOTE_MINIO, REMOTE_P2P, type RemoteDBSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { LiveSyncCouchDBReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import type { LiveSyncAbstractReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/LiveSyncAbstractReplicator";
import { AbstractModule } from "@/modules/AbstractModule";
import type { LiveSyncCore } from "@/main";
export class ModuleReplicatorCouchDB extends AbstractModule {
_anyNewReplicator(settingOverride: Partial<RemoteDBSettings> = {}): Promise<LiveSyncAbstractReplicator | false> {
const settings = { ...this.settings, ...settingOverride };
// If new remote types were added, add them here. Do not use `REMOTE_COUCHDB` directly for the safety valve.
if (settings.remoteType == REMOTE_MINIO || settings.remoteType == REMOTE_P2P) {
return Promise.resolve(false);
}
return Promise.resolve(new LiveSyncCouchDBReplicator(this.core));
}
_everyAfterResumeProcess(): Promise<boolean> {
if (this.services.appLifecycle.isSuspended()) return Promise.resolve(true);
if (!this.services.appLifecycle.isReady()) return Promise.resolve(true);
if (this.settings.remoteType != REMOTE_MINIO && this.settings.remoteType != REMOTE_P2P) {
const LiveSyncEnabled = this.settings.liveSync;
const continuous = LiveSyncEnabled;
const eventualOnStart = !LiveSyncEnabled && this.settings.syncOnStart;
// If enabled LiveSync or on start, open replication
if (LiveSyncEnabled || eventualOnStart) {
// And note that we do not open the conflict detection dialogue directly during this process.
// This should be raised explicitly if needed.
fireAndForget(async () => {
const canReplicate = await this.services.replication.isReplicationReady(false);
if (!canReplicate) return;
const openReplication = () =>
this.core.replicator.openReplication(this.settings, continuous, false, false);
if (continuous) {
void openReplication();
} else {
await this.services.replicator.runFiniteReplicationActivity(openReplication, {
label: "replication",
});
}
});
}
}
return Promise.resolve(true);
}
override onBindFunction(core: LiveSyncCore, services: typeof core.services): void {
services.replicator.getNewReplicator.addHandler(this._anyNewReplicator.bind(this));
services.appLifecycle.onResumed.addHandler(this._everyAfterResumeProcess.bind(this));
}
}
@@ -1,89 +0,0 @@
import { describe, expect, it, vi } from "vitest";
import { ModuleReplicatorCouchDB } from "./ModuleReplicatorCouchDB.ts";
function createModule(settings: { liveSync: boolean; syncOnStart: boolean }, isReplicationReady = true) {
const openReplication = vi.fn(async () => true);
const runFiniteReplicationActivity = vi.fn(async (task: () => unknown) => await task());
const services = {
API: {
addLog: vi.fn(),
addCommand: vi.fn(),
registerWindow: vi.fn(),
addRibbonIcon: vi.fn(),
registerProtocolHandler: vi.fn(),
},
appLifecycle: {
isSuspended: vi.fn(() => false),
isReady: vi.fn(() => true),
},
replication: {
isReplicationReady: vi.fn(async () => isReplicationReady),
},
replicator: {
runFiniteReplicationActivity,
},
setting: {
saveSettingData: vi.fn(async () => undefined),
},
};
const core = {
_services: services,
services,
settings: {
remoteType: "",
...settings,
},
replicator: { openReplication },
} as any;
return {
module: new ModuleReplicatorCouchDB(core),
openReplication,
runFiniteReplicationActivity,
};
}
describe("ModuleReplicatorCouchDB resume replication activity", () => {
it("exposes start-up one-shot replication as finite replication activity", async () => {
const { module, openReplication, runFiniteReplicationActivity } = createModule({
liveSync: false,
syncOnStart: true,
});
await module._everyAfterResumeProcess();
await vi.waitFor(() => expect(openReplication).toHaveBeenCalledOnce());
expect(runFiniteReplicationActivity).toHaveBeenCalledWith(expect.any(Function), {
label: "replication",
});
expect(openReplication).toHaveBeenCalledWith(expect.any(Object), false, false, false);
});
it("does not wrap the unbounded continuous channel in another finite activity", async () => {
const { module, openReplication, runFiniteReplicationActivity } = createModule({
liveSync: true,
syncOnStart: false,
});
await module._everyAfterResumeProcess();
await vi.waitFor(() => expect(openReplication).toHaveBeenCalledOnce());
expect(runFiniteReplicationActivity).not.toHaveBeenCalled();
expect(openReplication).toHaveBeenCalledWith(expect.any(Object), true, false, false);
});
it("does not start a one-shot activity when start-up readiness fails", async () => {
const { module, openReplication, runFiniteReplicationActivity } = createModule(
{
liveSync: false,
syncOnStart: true,
},
false
);
await module._everyAfterResumeProcess();
await new Promise((resolve) => setTimeout(resolve, 0));
expect(runFiniteReplicationActivity).not.toHaveBeenCalled();
expect(openReplication).not.toHaveBeenCalled();
});
});
-18
View File
@@ -1,18 +0,0 @@
import { REMOTE_MINIO, type RemoteDBSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { LiveSyncJournalReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/journal/LiveSyncJournalReplicator";
import type { LiveSyncAbstractReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/LiveSyncAbstractReplicator";
import type { LiveSyncCore } from "@/main";
import { AbstractModule } from "@/modules/AbstractModule";
export class ModuleReplicatorMinIO extends AbstractModule {
_anyNewReplicator(settingOverride: Partial<RemoteDBSettings> = {}): Promise<LiveSyncAbstractReplicator | false> {
const settings = { ...this.settings, ...settingOverride };
if (settings.remoteType == REMOTE_MINIO) {
return Promise.resolve(new LiveSyncJournalReplicator(this.core));
}
return Promise.resolve(false);
}
override onBindFunction(core: LiveSyncCore, services: typeof core.services): void {
services.replicator.getNewReplicator.addHandler(this._anyNewReplicator.bind(this));
}
}
@@ -19,6 +19,7 @@ import { stripAllPrefixes, isPlainText } from "@vrtmrz/livesync-commonlib/compat
import { EVENT_CONFLICT_CANCELLED, eventHub } from "@/common/events.ts";
import type { InjectableServiceHub } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableServiceHub";
import type { LiveSyncCore } from "@/main.ts";
import { NO_INTERACTION } from "@vrtmrz/livesync-commonlib/replication";
export class ModuleConflictResolver extends AbstractModule {
private async _resolveConflictByDeletingRev(
@@ -142,7 +143,10 @@ export class ModuleConflictResolver extends AbstractModule {
//auto resolved, but need check again;
if (this.settings.syncAfterMerge && !this.services.appLifecycle.isSuspended()) {
//Wait for the running replication, if not running replication, run it once.
await this.services.replication.replicateByEvent();
await this.services.replication.replicateUnattendedByEvent({
trigger: "merge",
interaction: NO_INTERACTION,
});
}
this._log("[conflict] Automatically merged, but we have to check it again");
await this.services.conflict.queueCheckFor(filename);
@@ -37,7 +37,7 @@ function createModule(files: FilePathWithPrefix[] = []) {
isSuspended: vi.fn(() => false),
},
replication: {
replicateByEvent: vi.fn(async () => true),
replicateUnattendedByEvent: vi.fn(async () => ({ status: "completed" as const })),
},
vault: {
getActiveFilePath: vi.fn(() => undefined),
@@ -20,6 +20,24 @@ import { $msg, translateIfAvailable } from "@/common/translation";
import type { InjectableServiceHub } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableServiceHub";
import type { LiveSyncCore } from "@/main.ts";
import { REMOTE_P2P } from "@vrtmrz/livesync-commonlib/compat/common/models/setting.const";
import { withOwnedRemoteResource } from "@/common/ownedRemoteResource";
import {
CENTRAL_COMPATIBILITY_REJECTION_REASONS,
REMOTE_RESOURCE_KINDS,
type ReplicationAttemptFailure,
type ReplicatorInstance,
} from "@vrtmrz/livesync-commonlib/replication";
interface PreferredRemoteTweakWriter extends ReplicatorInstance {
setPreferredRemoteTweakSettings(setting: ObsidianLiveSyncSettings): Promise<void>;
}
function canSetPreferredRemoteTweakSettings(replicator: ReplicatorInstance): replicator is PreferredRemoteTweakWriter {
return (
"setPreferredRemoteTweakSettings" in replicator &&
typeof replicator.setPreferredRemoteTweakSettings === "function"
);
}
/**
* Localised counterpart of Commonlib's `confName()`, which takes no translator.
@@ -112,11 +130,27 @@ export class ModuleResolvingMismatchedTweaks extends AbstractModule {
});
}
async _anyAfterConnectCheckFailed(): Promise<boolean | "CHECKAGAIN" | undefined> {
if (!this.core.replicator.tweakSettingsMismatched && !this.core.replicator.preferredTweakValue) return false;
const preferred = this.core.replicator.preferredTweakValue;
if (!preferred) return false;
const ret = await this.services.tweakValue.askResolvingMismatched(preferred);
async _anyAfterConnectCheckFailed(failure: ReplicationAttemptFailure): Promise<boolean | "CHECKAGAIN" | undefined> {
const recovery = failure.outcome.recoveryHint;
if (
recovery?.reason !== CENTRAL_COMPATIBILITY_REJECTION_REASONS.TWEAK_MISMATCH ||
!recovery.preferredTweakValue
) {
return false;
}
const ret = await this.services.tweakValue.askResolvingMismatched(
{ ...recovery.preferredTweakValue },
async (setting) => {
let updated = false;
await this.services.replicator.runWithActiveReplicatorContext(async (activeContext) => {
if (activeContext !== failure.context) return;
if (!canSetPreferredRemoteTweakSettings(activeContext.replicator)) return;
await activeContext.replicator.setPreferredRemoteTweakSettings({ ...setting });
updated = true;
});
return updated;
}
);
if (ret == "OK") return false;
if (ret == "CHECKAGAIN") return "CHECKAGAIN";
if (ret == "IGNORE") return true;
@@ -230,19 +264,23 @@ export class ModuleResolvingMismatchedTweaks extends AbstractModule {
return CHOICES[retKey];
}
async _askResolvingMismatchedTweaks(): Promise<"OK" | "CHECKAGAIN" | "IGNORE"> {
if (!this.core.replicator.tweakSettingsMismatched) {
return "OK";
}
const tweaks = this.core.replicator.preferredTweakValue;
if (!tweaks) {
return "IGNORE";
}
const [conf, rebuildRequired] = await this.services.tweakValue.checkAndAskResolvingMismatched(tweaks);
async _askResolvingMismatchedTweaks(
preferredSource: TweakValues,
updatePreferredRemote?: (setting: ObsidianLiveSyncSettings) => Promise<boolean>
): Promise<"OK" | "CHECKAGAIN" | "IGNORE"> {
const [conf, rebuildRequired] = await this.services.tweakValue.checkAndAskResolvingMismatched(preferredSource);
if (!conf) return "IGNORE";
const updateRemote = async () => {
if (updatePreferredRemote) return await updatePreferredRemote(this.settings);
const candidate = this.core.replicator;
if (typeof candidate.setPreferredRemoteTweakSettings !== "function") return false;
await candidate.setPreferredRemoteTweakSettings(this.settings);
return true;
};
if (conf === true) {
await this.core.replicator.setPreferredRemoteTweakSettings(this.settings);
if (!(await updateRemote())) return "IGNORE";
if (rebuildRequired) {
await this.core.rebuilder.$rebuildRemote();
}
@@ -259,7 +297,7 @@ export class ModuleResolvingMismatchedTweaks extends AbstractModule {
// chunk-generation managers now so hash and splitter changes take effect before retrying.
await this.localDatabase.managers.reinitialise();
}
await this.core.replicator.setPreferredRemoteTweakSettings(this.settings);
if (!(await updateRemote())) return "IGNORE";
if (rebuildRequired) {
await this.core.rebuilder.$fetchLocal();
}
@@ -271,12 +309,15 @@ export class ModuleResolvingMismatchedTweaks extends AbstractModule {
async _fetchRemotePreferredTweakValues(trialSetting: RemoteDBSettings): Promise<RemotePreferredTweakResult> {
try {
const replicator = await this.services.replicator.getNewReplicator(trialSetting);
if (!replicator) {
const probe = await this.services.replicator.createRemoteResource(
REMOTE_RESOURCE_KINDS.PREFERRED_TWEAK,
trialSetting
);
if (!probe) {
this._log("The remote type does not support preferred tweak values.", LOG_LEVEL_NOTICE);
return { status: RemotePreferredTweakStatuses.UNSUPPORTED };
}
return await replicator.getRemotePreferredTweakValues(trialSetting);
return await withOwnedRemoteResource(probe, (ownedProbe) => ownedProbe.read());
} catch (ex) {
this._log("Failed to get the preferred tweak values from the remote.", LOG_LEVEL_NOTICE);
return {
@@ -7,6 +7,12 @@ import {
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { ModuleResolvingMismatchedTweaks } from "./ModuleResolveMismatchedTweaks";
import { setLang } from "@/common/translation";
import {
CENTRAL_COMPATIBILITY_REJECTION_REASONS,
REMOTE_RESOURCE_KINDS,
USER_INITIATED_REPLICATION_AUTHORITY,
type ReplicationAttemptFailure,
} from "@vrtmrz/livesync-commonlib/replication";
function createModule(settingsOverride: Partial<typeof DEFAULT_SETTINGS> = {}) {
const askSelectStringDialogue = vi.fn(async (..._args: unknown[]): Promise<string | undefined> => undefined);
@@ -55,29 +61,102 @@ function createModule(settingsOverride: Partial<typeof DEFAULT_SETTINGS> = {}) {
}
describe("ModuleResolvingMismatchedTweaks", () => {
it("uses the failed attempt hint and writes only through that exact active publication", async () => {
const { module, core } = createModule();
const attemptPreferred = {
...(DEFAULT_SETTINGS as unknown as TweakValues),
customChunkSize: 60,
};
const replacementPreferred = {
...(DEFAULT_SETTINGS as unknown as TweakValues),
customChunkSize: 99,
};
let updatePreferredRemote: ((setting: typeof core.settings) => Promise<boolean>) | undefined;
const askResolvingMismatched = vi.fn(
async (_preferred: unknown, update: (setting: typeof core.settings) => Promise<boolean>) => {
updatePreferredRemote = update;
return "IGNORE" as const;
}
);
core._services.tweakValue = { askResolvingMismatched };
core.replicator = {
tweakSettingsMismatched: true,
preferredTweakValue: replacementPreferred,
};
const failedSetPreferred = vi.fn(async (_setting: typeof core.settings) => undefined);
const replacementSetPreferred = vi.fn(async (_setting: typeof core.settings) => undefined);
const failedContext = {
provider: {},
replicator: { setPreferredRemoteTweakSettings: failedSetPreferred },
configurationIdentity: "profile-a",
};
const replacementContext = {
provider: {},
replicator: { setPreferredRemoteTweakSettings: replacementSetPreferred },
configurationIdentity: "profile-b",
};
let activeContext = failedContext;
core._services.replicator = {
runWithActiveReplicatorContext: vi.fn(async (task: (context: typeof failedContext) => unknown) =>
task(activeContext)
),
};
const request = {
context: failedContext,
setting: core.settings,
outcome: {
status: "failed" as const,
error: new Error("directional replication failed"),
recoveryHint: {
reason: CENTRAL_COMPATIBILITY_REJECTION_REASONS.TWEAK_MISMATCH,
preferredTweakValue: attemptPreferred,
},
},
showMessage: true,
interaction: USER_INITIATED_REPLICATION_AUTHORITY,
} as unknown as ReplicationAttemptFailure;
await expect(module._anyAfterConnectCheckFailed(request)).resolves.toBe(true);
expect(askResolvingMismatched).toHaveBeenCalledWith(attemptPreferred, expect.any(Function));
const effectiveSetting = { ...core.settings, customChunkSize: 64 };
await expect(updatePreferredRemote?.(effectiveSetting)).resolves.toBe(true);
expect(failedSetPreferred).toHaveBeenCalledWith(effectiveSetting);
expect(failedSetPreferred.mock.calls[0][0]).not.toBe(effectiveSetting);
activeContext = replacementContext;
await expect(updatePreferredRemote?.({ ...effectiveSetting, customChunkSize: 72 })).resolves.toBe(false);
expect(failedSetPreferred).toHaveBeenCalledOnce();
expect(replacementSetPreferred).not.toHaveBeenCalled();
});
it("returns an unconfigured remote result without a separate connection preflight", async () => {
const { module, core } = createModule();
const tryConnectRemote = vi.fn(async () => true);
const getRemotePreferredTweakValues = vi.fn(async () => ({
const read = vi.fn(async () => ({
status: "not-configured" as const,
reason: "milestone-missing" as const,
}));
const dispose = vi.fn(async () => undefined);
const createRemoteResource = vi.fn(async () => ({ read, dispose }));
core._services.replicator = {
getNewReplicator: vi.fn(async () => ({ tryConnectRemote, getRemotePreferredTweakValues })),
createRemoteResource,
getNewReplicator: vi.fn(() => Promise.reject(new Error("must not borrow a Replicator"))),
};
await expect(module._fetchRemotePreferredTweakValues(core.settings)).resolves.toEqual({
status: "not-configured",
reason: "milestone-missing",
});
expect(getRemotePreferredTweakValues).toHaveBeenCalledOnce();
expect(tryConnectRemote).not.toHaveBeenCalled();
expect(createRemoteResource).toHaveBeenCalledWith(REMOTE_RESOURCE_KINDS.PREFERRED_TWEAK, core.settings);
expect(read).toHaveBeenCalledOnce();
expect(dispose).toHaveBeenCalledOnce();
expect(core._services.replicator.getNewReplicator).not.toHaveBeenCalled();
});
it("returns unsupported when no replicator implements the remote type", async () => {
const { module, core } = createModule();
core._services.replicator = {
getNewReplicator: vi.fn(async () => undefined),
createRemoteResource: vi.fn(async () => undefined),
};
await expect(module._fetchRemotePreferredTweakValues(core.settings)).resolves.toEqual({
@@ -85,6 +164,26 @@ describe("ModuleResolvingMismatchedTweaks", () => {
});
});
it("disposes the preferred-tweak probe when reading fails", async () => {
const { module, core } = createModule();
const error = new Error("remote unavailable");
const dispose = vi.fn(async () => undefined);
core._services.replicator = {
createRemoteResource: vi.fn(async () => ({
read: vi.fn(async () => {
throw error;
}),
dispose,
})),
};
await expect(module._fetchRemotePreferredTweakValues(core.settings)).resolves.toEqual({
status: "unavailable",
error,
});
expect(dispose).toHaveBeenCalledOnce();
});
it("should enable and auto-accept compatible mismatches when the preference is undefined", async () => {
const { module, core, askSelectStringDialogue, applyPartial } = createModule({
autoAcceptCompatibleTweak: undefined,
@@ -247,13 +346,18 @@ describe("ModuleResolvingMismatchedTweaks", () => {
reinitialise.mockImplementation(async () => {
calls.push("reinitialise");
});
const updatePreferredRemote = vi.fn(async () => {
calls.push("set-preferred");
return true;
});
const result = await module._askResolvingMismatchedTweaks();
const result = await module._askResolvingMismatchedTweaks(preferred, updatePreferredRemote);
expect(result).toBe("CHECKAGAIN");
expect(core.settings).toBe(initialSettings);
expect(core.settings.hashAlg).toBe("xxhash32");
expect(calls).toEqual(["save", "reinitialise", "set-preferred"]);
expect(core.replicator.setPreferredRemoteTweakSettings).not.toHaveBeenCalled();
});
});
+10 -2
View File
@@ -4,6 +4,10 @@ import { fireAndForget } from "octagonal-wheels/promises";
import { AbstractModule } from "@/modules/AbstractModule";
import { $msg } from "@/common/translation";
import { copyFileDatabaseInfo } from "@/serviceFeatures/fileDatabaseInfo";
import {
REPLICATION_PROGRESS_PRESENTATIONS,
USER_INITIATED_REPLICATION_AUTHORITY,
} from "@vrtmrz/livesync-commonlib/replication";
// Separated Module for basic menu commands, which are not related to obsidian specific features. It is expected to be used in other platforms with minimal changes.
// However, it is odd that it has here at all; it really ought to be in each respective feature. It will likely be moved eventually. Until now, addCommand pointed to Obsidian's version.
export class ModuleBasicMenu extends AbstractModule {
@@ -12,7 +16,11 @@ export class ModuleBasicMenu extends AbstractModule {
id: "livesync-replicate",
name: $msg("Sync now"),
callback: async () => {
await this.services.replication.replicate();
await this.services.replication.replicateUserInitiated({
trigger: "manual",
progressPresentation: REPLICATION_PROGRESS_PRESENTATIONS.QUIET,
interaction: USER_INITIATED_REPLICATION_AUTHORITY,
});
},
});
this.addCommand({
@@ -85,7 +93,7 @@ export class ModuleBasicMenu extends AbstractModule {
checkCallback: (checking) => {
if (!this.settings.useAdvancedMode) return false;
if (!checking) {
this.core.replicator.terminateSync();
fireAndForget(() => this.services.replication.stopActiveTransfer());
}
return true;
},
@@ -1,5 +1,9 @@
import { describe, expect, it, vi } from "vitest";
import type { Command } from "@/deps";
import {
REPLICATION_PROGRESS_PRESENTATIONS,
USER_INITIATED_REPLICATION_AUTHORITY,
} from "@vrtmrz/livesync-commonlib/replication";
import { ModuleBasicMenu } from "./ModuleBasicMenu";
type RegisteredCommand = Command & {
@@ -25,7 +29,8 @@ function createFixture() {
registerProtocolHandler: vi.fn(),
},
replication: {
replicate: vi.fn(async () => undefined),
replicateUserInitiated: vi.fn(async () => ({ status: "completed" as const })),
stopActiveTransfer: vi.fn(async () => ({ status: "completed" as const })),
},
vault: {
getActiveFilePath: vi.fn((): string | null => "note.md"),
@@ -123,6 +128,19 @@ describe("ModuleBasicMenu command palette", () => {
expect(fixture.getCommand("livesync-runbatch").name).toBe("Apply pending changes now");
});
it("keeps Sync now progress quiet while retaining failure-recovery authority", async () => {
const fixture = createFixture();
await fixture.module._everyOnloadStart();
await fixture.getCommand("livesync-replicate").callback?.();
expect(fixture.services.replication.replicateUserInitiated).toHaveBeenCalledWith({
trigger: "manual",
progressPresentation: REPLICATION_PROGRESS_PRESENTATIONS.QUIET,
interaction: USER_INITIATED_REPLICATION_AUTHORITY,
});
});
it("keeps maintenance commands out of the normal palette", async () => {
const fixture = createFixture();
@@ -136,6 +154,19 @@ describe("ModuleBasicMenu command palette", () => {
expect(fixture.getCommand("livesync-abortsync").checkCallback?.(true)).toBe(true);
});
it("routes an explicit stop through the active provider capability", async () => {
const fixture = createFixture();
fixture.settings.useAdvancedMode = true;
await fixture.module._everyOnloadStart();
expect(fixture.getCommand("livesync-abortsync").checkCallback?.(false)).toBe(true);
await vi.waitFor(() => {
expect(fixture.services.replication.stopActiveTransfer).toHaveBeenCalledOnce();
});
expect(fixture.core.replicator.terminateSync).not.toHaveBeenCalled();
});
it("keeps active-file database information available and opens it in a copy dialogue", async () => {
const fixture = createFixture();
+14 -1
View File
@@ -37,6 +37,16 @@ type ErrorInfo = {
const INCOMPLETE_DOCUMENT_NOTICE_GROUP = "startup-integrity-check";
interface CompromisedChunkCounter {
countCompromisedChunks(): Promise<number | boolean>;
}
function hasCompromisedChunkCounter(value: object | undefined): value is CompromisedChunkCounter {
return (
value !== undefined && "countCompromisedChunks" in value && typeof value.countCompromisedChunks === "function"
);
}
export class ModuleMigration extends AbstractModule<LiveSyncCore> {
constructor(
core: LiveSyncCore,
@@ -253,7 +263,10 @@ export class ModuleMigration extends AbstractModule<LiveSyncCore> {
// Check local database for compromised chunks
const localCompromised = await countCompromisedChunks(this.localDatabase.localDatabase);
const remote = this.services.replicator.getActiveReplicator();
const remoteCompromised = this.services.API.isOnline ? await remote?.countCompromisedChunks() : 0;
const remoteCompromised =
this.services.API.isOnline && hasCompromisedChunkCounter(remote)
? await remote.countCompromisedChunks()
: 0;
if (localCompromised === false) {
Logger(`Failed to count compromised chunks in local database`, LOG_LEVEL_NOTICE);
return false;
@@ -14,6 +14,7 @@ import {
} from "@vrtmrz/livesync-commonlib/compat/mock_and_interop/stores";
import type { LiveSyncCore } from "@/main.ts";
import { compatGlobal } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions";
import { NO_INTERACTION } from "@vrtmrz/livesync-commonlib/replication";
type MutableCommandDefinition = {
callback?: () => void;
@@ -71,7 +72,12 @@ export class ModuleObsidianEvents extends AbstractObsidianModule {
} else {
if (this.settings.syncOnEditorSave) {
this._log("Sync on Editor Save.", LOG_LEVEL_VERBOSE);
fireAndForget(() => this.services.replication.replicateByEvent());
fireAndForget(() =>
this.services.replication.replicateUnattendedByEvent({
trigger: "editor-save",
interaction: NO_INTERACTION,
})
);
}
}
});
@@ -195,11 +201,7 @@ export class ModuleObsidianEvents extends AbstractObsidianModule {
async watchWindowVisibilityAsync() {
if (this.settings.suspendFileWatching) {
if (
this.settings.isConfigured &&
this.services.appLifecycle.isReady() &&
this.hasBoundedActivity()
) {
if (this.settings.isConfigured && this.services.appLifecycle.isReady() && this.hasBoundedActivity()) {
const isHidden = activeWindow.document.hidden;
this.isLastHidden = isHidden;
this.deferredBoundedLifecycle = isHidden ? "suspend-if-hidden" : undefined;
@@ -290,7 +292,10 @@ export class ModuleObsidianEvents extends AbstractObsidianModule {
return;
}
if (this.settings.syncOnFileOpen && !this.services.appLifecycle.isSuspended()) {
await this.services.replication.replicateByEvent();
await this.services.replication.replicateUnattendedByEvent({
trigger: "file-open",
interaction: NO_INTERACTION,
});
}
await this.services.conflict.queueCheckForIfOpen(file.path as FilePathWithPrefix);
}
@@ -2,6 +2,10 @@ import { addIcon } from "@/deps.ts";
import { $msg } from "@/common/translation";
import type { LiveSyncCore } from "@/main.ts";
import { AbstractModule } from "@/modules/AbstractModule.ts";
import {
REPLICATION_PROGRESS_PRESENTATIONS,
USER_INITIATED_REPLICATION_AUTHORITY,
} from "@vrtmrz/livesync-commonlib/replication";
// Obsidian specific menu commands.
export class ModuleObsidianMenu extends AbstractModule {
_everyOnloadStart(): Promise<boolean> {
@@ -17,7 +21,11 @@ export class ModuleObsidianMenu extends AbstractModule {
);
this.addRibbonIcon("replicate", $msg("moduleObsidianMenu.replicate"), async () => {
await this.services.replication.replicate(true);
await this.services.replication.replicateUserInitiated({
trigger: "manual",
progressPresentation: REPLICATION_PROGRESS_PRESENTATIONS.NOTICE,
interaction: USER_INITIATED_REPLICATION_AUTHORITY,
});
}).addClass("livesync-ribbon-replicate");
return Promise.resolve(true);
@@ -0,0 +1,41 @@
import { describe, expect, it, vi } from "vitest";
vi.mock("@/deps.ts", () => ({ addIcon: vi.fn() }));
import {
REPLICATION_PROGRESS_PRESENTATIONS,
USER_INITIATED_REPLICATION_AUTHORITY,
} from "@vrtmrz/livesync-commonlib/replication";
import { ModuleObsidianMenu } from "./ModuleObsidianMenu";
describe("ModuleObsidianMenu ribbon", () => {
it("retains visible progress and full interaction authority", async () => {
let runRibbonAction: (() => Promise<void>) | undefined;
const addClass = vi.fn();
const replicateUserInitiated = vi.fn(async () => ({ status: "completed" as const }));
const services = {
API: {
addLog: vi.fn(),
addCommand: vi.fn(),
registerWindow: vi.fn(),
registerProtocolHandler: vi.fn(),
addRibbonIcon: vi.fn((_icon: string, _title: string, callback: () => Promise<void>) => {
runRibbonAction = callback;
return { addClass };
}),
},
replication: { replicateUserInitiated },
};
const module = new ModuleObsidianMenu({ _services: services, services } as never);
await module._everyOnloadStart();
await runRibbonAction?.();
expect(replicateUserInitiated).toHaveBeenCalledWith({
trigger: "manual",
progressPresentation: REPLICATION_PROGRESS_PRESENTATIONS.NOTICE,
interaction: USER_INITIATED_REPLICATION_AUTHORITY,
});
expect(addClass).toHaveBeenCalledWith("livesync-ribbon-replicate");
});
});
@@ -18,6 +18,7 @@ import type { LiveSyncCore } from "@/main.ts";
import { EVENT_CONFLICT_CANCELLED, EVENT_ON_UNRESOLVED_ERROR, eventHub } from "@/common/events.ts";
import { $msg } from "@/common/translation.ts";
import type { Editor, MarkdownFileInfo, MarkdownView } from "@/deps.ts";
import { NO_INTERACTION } from "@vrtmrz/livesync-commonlib/replication";
export class ModuleInteractiveConflictResolver extends AbstractObsidianModule {
private postponedConflictEpisodes = new Set<FilePathWithPrefix>();
@@ -182,7 +183,10 @@ export class ModuleInteractiveConflictResolver extends AbstractObsidianModule {
// So we have to run replication if configured.
// TODO: Make this is as a event request
if (this.settings.syncAfterMerge && !this.services.appLifecycle.isSuspended()) {
await this.services.replication.replicateByEvent();
await this.services.replication.replicateUnattendedByEvent({
trigger: "merge",
interaction: NO_INTERACTION,
});
}
// And, check it again.
await this.services.conflict.queueCheckFor(filename);
@@ -77,7 +77,9 @@ function createModule(conflictedRevisions: string[] = ["2-right"]) {
queueCheckFor: vi.fn(async () => undefined),
ensureAllProcessed: vi.fn(async () => true),
},
replication: { replicateByEvent: vi.fn(async () => true) },
replication: {
replicateUnattendedByEvent: vi.fn(async () => ({ status: "completed" as const })),
},
vault: { getActiveFilePath: vi.fn(() => path) },
path: { getPath: vi.fn((entry: { path: FilePathWithPrefix }) => entry.path) },
};
@@ -13,11 +13,10 @@ import {
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { delay, isObjectDifferent, sizeToHumanReadable } from "@vrtmrz/livesync-commonlib/compat/common/utils";
import { Logger } from "@vrtmrz/livesync-commonlib/compat/common/logger";
import { checkSyncInfo } from "@vrtmrz/livesync-commonlib/compat/pouchdb/negotiation";
import { testCrypt } from "octagonal-wheels/encryption/encryption";
import ObsidianLiveSyncPlugin from "@/main.ts";
import { scheduleTask } from "@/common/utils.ts";
import { LiveSyncCouchDBReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import { REMOTE_RESOURCE_KINDS } from "@vrtmrz/livesync-commonlib/replication";
import {
type AllSettingItemKey,
type AllStringItemKey,
@@ -78,6 +77,7 @@ import type {
import { createExtraMenuSettingSpecGroup, createGeneralSettingSpecGroups } from "./GeneralSettingSpecs.ts";
import { SetupManager } from "@/modules/features/SetupManager.ts";
import { isP2PMainRemote } from "@/common/remoteConfiguration.ts";
import { withOwnedRemoteResource } from "@/common/ownedRemoteResource.ts";
// For creating a document
// const toc = new Set<string>();
@@ -340,15 +340,18 @@ export class ObsidianLiveSyncSettingTab extends PluginSettingTab {
async testConnection(settingOverride: Partial<ObsidianLiveSyncSettings> = {}): Promise<void> {
const trialSetting = { ...this.editingSettings, ...settingOverride };
const replicator = await this.services.replicator.getNewReplicator(trialSetting);
if (!replicator) {
Logger("No replicator available for the current settings.", LOG_LEVEL_NOTICE);
const probe = await this.services.replicator.createRemoteResource(
REMOTE_RESOURCE_KINDS.CONNECTION,
trialSetting
);
if (!probe) {
Logger("Connection testing is unavailable for the current settings.", LOG_LEVEL_NOTICE);
return;
}
await replicator.tryConnectRemote(trialSetting);
const status = await replicator.getRemoteStatus(trialSetting);
if (status) {
if (status.estimatedSize) {
await withOwnedRemoteResource(probe, async (ownedProbe) => {
await ownedProbe.check({ createIfMissing: true, showResult: true });
const status = await ownedProbe.getStatus();
if (status && status.estimatedSize) {
Logger(
$msg("obsidianLiveSyncSettingTab.logEstimatedSize", {
size: sizeToHumanReadable(status.estimatedSize),
@@ -356,7 +359,7 @@ export class ObsidianLiveSyncSettingTab extends PluginSettingTab {
LOG_LEVEL_NOTICE
);
}
}
});
}
closeSetting() {
@@ -954,35 +957,42 @@ export class ObsidianLiveSyncSettingTab extends PluginSettingTab {
visibility:
this.isConfiguredAs("remoteType", REMOTE_COUCHDB) || this.isConfiguredAs("remoteType", REMOTE_MINIO),
}) as OnUpdateResult;
// E2EE Function
/**
* Checks the edited CouchDB passphrase through an owned synchronisation-
* information resource. A missing document may be created by the check.
* Incompatibility and operational failure retain their distinct existing
* result messages.
*/
checkWorkingPassphrase = async (): Promise<boolean> => {
if (this.editingSettings.remoteType == REMOTE_MINIO) return true;
const settingForCheck: RemoteDBSettings = {
...this.editingSettings,
};
const replicator = this.services.replicator.getNewReplicator(settingForCheck);
if (!(replicator instanceof LiveSyncCouchDBReplicator)) return true;
const db = await replicator.connectRemoteCouchDBWithSetting(
settingForCheck,
this.services.API.isMobile(),
true
const resource = await this.services.replicator.createRemoteResource(
REMOTE_RESOURCE_KINDS.SYNCHRONISATION_INFORMATION,
settingForCheck
);
if (typeof db === "string") {
Logger($msg("obsidianLiveSyncSettingTab.logCheckPassphraseFailed", { db }), LOG_LEVEL_NOTICE);
return false;
}
if (!resource) return true;
try {
if (await checkSyncInfo(db.db)) {
if (await resource.check()) {
// Logger($msg("obsidianLiveSyncSettingTab.logDatabaseConnected"), LOG_LEVEL_NOTICE);
return true;
} else {
Logger($msg("obsidianLiveSyncSettingTab.logPassphraseNotCompatible"), LOG_LEVEL_NOTICE);
return false;
}
} catch (error) {
const reason = error instanceof Error ? error.message : String(error);
Logger(
$msg("obsidianLiveSyncSettingTab.logCheckPassphraseFailed", {
db: reason,
}),
LOG_LEVEL_NOTICE
);
return false;
} finally {
await db.db.close();
await resource.dispose();
}
};
isPassphraseValid = async () => {
@@ -1199,8 +1209,17 @@ export class ObsidianLiveSyncSettingTab extends PluginSettingTab {
new MinioStorageAdapter(this.core.settings, this.core)
);
}
async resetRemoteBucket() {
/**
* Wipe the remote bucket through a short-lived Journal client.
* Journal wipes are batched and non-transactional, so a false result may
* leave a partial wipe which can be retried after all devices are stopped.
*/
async resetRemoteBucket(): Promise<boolean> {
const minioJournal = this.getMinioJournalSyncClient();
await minioJournal.resetBucket();
try {
return await minioJournal.resetBucket();
} finally {
minioJournal.dispose();
}
}
}
@@ -1,12 +1,13 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { DEFAULT_SETTINGS, REMOTE_COUCHDB } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { DEFAULT_SETTINGS, LOG_LEVEL_NOTICE, REMOTE_COUCHDB } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { REMOTE_RESOURCE_KINDS } from "@vrtmrz/livesync-commonlib/replication";
const negotiationMocks = vi.hoisted(() => ({
checkSyncInfo: vi.fn(async () => true),
}));
const settingsInitialisationMocks = vi.hoisted(() => ({
applySettingsWithInitialisationChoice: vi.fn(),
}));
const loggerMocks = vi.hoisted(() => ({
Logger: vi.fn(),
}));
vi.mock("@/deps.ts", () => ({
App: class {},
@@ -20,6 +21,14 @@ vi.mock("@/deps.ts", () => ({
requireApiVersion: vi.fn(() => false),
}));
vi.mock("@/main.ts", () => ({ default: class {} }));
vi.mock("@vrtmrz/livesync-commonlib/compat/common/logger", async (importOriginal) => {
const actual = await importOriginal<typeof import("@vrtmrz/livesync-commonlib/compat/common/logger")>();
return { ...actual, Logger: loggerMocks.Logger };
});
vi.mock("@/common/translation", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/common/translation")>();
return { ...actual, $msg: vi.fn(actual.$msg) };
});
vi.mock("@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions", () => ({
getLanguage: vi.fn(() => "en"),
compatGlobal: {
@@ -38,10 +47,6 @@ vi.mock("@/common/events.ts", () => ({
eventHub: { emitEvent: vi.fn(), onEvent: vi.fn() },
}));
vi.mock("@/modules/features/SetupManager.ts", () => ({ SetupManager: class {} }));
vi.mock("@vrtmrz/livesync-commonlib/compat/pouchdb/negotiation", () => negotiationMocks);
vi.mock("@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator", () => ({
LiveSyncCouchDBReplicator: class {},
}));
vi.mock("./LiveSyncSetting.ts", () => ({ LiveSyncSetting: class {} }));
vi.mock("./SettingPane.ts", () => ({
enableOnly: vi.fn(() => vi.fn()),
@@ -63,27 +68,25 @@ vi.mock("./PanePowerUsers.ts", () => ({ panePowerUsers: vi.fn() }));
vi.mock("./PanePatches.ts", () => ({ panePatches: vi.fn() }));
vi.mock("./PaneMaintenance.ts", () => ({ paneMaintenance: vi.fn() }));
import { LiveSyncCouchDBReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import { ObsidianLiveSyncSettingTab } from "./ObsidianLiveSyncSettingTab";
import { $msg } from "@/common/translation";
beforeEach(() => {
settingsInitialisationMocks.applySettingsWithInitialisationChoice.mockReset();
loggerMocks.Logger.mockClear();
vi.mocked($msg).mockClear();
});
describe("ObsidianLiveSyncSettingTab passphrase verification", () => {
it("closes the finite remote connection after checking synchronisation information", async () => {
const remoteDatabase = {
close: vi.fn(async () => undefined),
};
const replicator = Object.assign(new LiveSyncCouchDBReplicator({} as never), {
connectRemoteCouchDBWithSetting: vi.fn(async () => ({ db: remoteDatabase })),
});
it("awaits and disposes the owned synchronisation-information resource", async () => {
const check = vi.fn(async () => true);
const dispose = vi.fn(async () => undefined);
const createRemoteResource = vi.fn(async () => ({ check, dispose }));
const plugin = {
app: {},
core: {
services: {
API: { isMobile: vi.fn(() => false) },
replicator: { getNewReplicator: vi.fn(() => replicator) },
replicator: { createRemoteResource },
},
},
};
@@ -97,8 +100,160 @@ describe("ObsidianLiveSyncSettingTab passphrase verification", () => {
await expect(tab.checkWorkingPassphrase()).resolves.toBe(true);
expect(negotiationMocks.checkSyncInfo).toHaveBeenCalledWith(remoteDatabase);
expect(remoteDatabase.close).toHaveBeenCalledOnce();
expect(createRemoteResource).toHaveBeenCalledWith(
REMOTE_RESOURCE_KINDS.SYNCHRONISATION_INFORMATION,
expect.objectContaining({ remoteType: REMOTE_COUCHDB })
);
expect(check).toHaveBeenCalledOnce();
expect(dispose).toHaveBeenCalledOnce();
});
it("does not use the general Replicator factory solely to verify synchronisation information", async () => {
const getNewReplicator = vi.fn(() => Promise.reject(new Error("must not construct a Replicator")));
const createRemoteResource = vi.fn(async () => ({
check: vi.fn(async () => true),
dispose: vi.fn(async () => undefined),
}));
const plugin = {
app: {},
core: {
services: {
replicator: { createRemoteResource, getNewReplicator },
},
},
};
const tab = new ObsidianLiveSyncSettingTab({} as never, plugin as never);
Object.assign(tab, {
_editingSettings: {
...DEFAULT_SETTINGS,
remoteType: REMOTE_COUCHDB,
},
});
await expect(tab.checkWorkingPassphrase()).resolves.toBe(true);
expect(getNewReplicator).not.toHaveBeenCalled();
});
it("reports a CouchDB connection or setup failure with the connection-failure message", async () => {
const failure = new Error("remote unavailable");
const check = vi.fn(async () => {
throw failure;
});
const dispose = vi.fn(async () => undefined);
const createRemoteResource = vi.fn(async () => ({ check, dispose }));
const plugin = {
app: {},
core: {
services: {
replicator: { createRemoteResource },
},
},
};
const tab = new ObsidianLiveSyncSettingTab({} as never, plugin as never);
Object.assign(tab, {
_editingSettings: {
...DEFAULT_SETTINGS,
remoteType: REMOTE_COUCHDB,
},
});
await expect(tab.checkWorkingPassphrase()).resolves.toBe(false);
expect(vi.mocked($msg)).toHaveBeenCalledWith("obsidianLiveSyncSettingTab.logCheckPassphraseFailed", {
db: failure.message,
});
expect(vi.mocked($msg)).not.toHaveBeenCalledWith("obsidianLiveSyncSettingTab.logPassphraseNotCompatible");
expect(loggerMocks.Logger).toHaveBeenCalledWith(expect.any(String), LOG_LEVEL_NOTICE);
expect(dispose).toHaveBeenCalledOnce();
});
it("reports an actual synchronisation-information mismatch with the incompatibility message", async () => {
const check = vi.fn(async () => false);
const dispose = vi.fn(async () => undefined);
const createRemoteResource = vi.fn(async () => ({ check, dispose }));
const plugin = {
app: {},
core: {
services: {
replicator: { createRemoteResource },
},
},
};
const tab = new ObsidianLiveSyncSettingTab({} as never, plugin as never);
Object.assign(tab, {
_editingSettings: {
...DEFAULT_SETTINGS,
remoteType: REMOTE_COUCHDB,
},
});
await expect(tab.checkWorkingPassphrase()).resolves.toBe(false);
expect(vi.mocked($msg)).toHaveBeenCalledWith("obsidianLiveSyncSettingTab.logPassphraseNotCompatible");
expect(vi.mocked($msg)).not.toHaveBeenCalledWith(
"obsidianLiveSyncSettingTab.logCheckPassphraseFailed",
expect.anything()
);
expect(loggerMocks.Logger).toHaveBeenCalledWith(expect.any(String), LOG_LEVEL_NOTICE);
expect(dispose).toHaveBeenCalledOnce();
});
});
describe("ObsidianLiveSyncSettingTab connection testing", () => {
it("uses and disposes the flow-specific connection probe without borrowing a Replicator", async () => {
const check = vi.fn(async () => ({ ok: true as const }));
const getStatus = vi.fn(async () => ({ estimatedSize: 1024 }));
const dispose = vi.fn(async () => undefined);
const createRemoteResource = vi.fn(async () => ({ check, getStatus, dispose }));
const getNewReplicator = vi.fn(() => Promise.reject(new Error("must not borrow a Replicator")));
const plugin = {
app: {},
core: {
services: {
replicator: { createRemoteResource, getNewReplicator },
},
},
};
const tab = new ObsidianLiveSyncSettingTab({} as never, plugin as never);
Object.assign(tab, {
_editingSettings: {
...DEFAULT_SETTINGS,
remoteType: REMOTE_COUCHDB,
couchDB_DBNAME: "saved",
},
});
await expect(tab.testConnection({ couchDB_DBNAME: "trial" })).resolves.toBeUndefined();
expect(createRemoteResource).toHaveBeenCalledWith(
REMOTE_RESOURCE_KINDS.CONNECTION,
expect.objectContaining({ remoteType: REMOTE_COUCHDB, couchDB_DBNAME: "trial" })
);
expect(check).toHaveBeenCalledWith({ createIfMissing: true, showResult: true });
expect(getStatus).toHaveBeenCalledOnce();
expect(dispose).toHaveBeenCalledOnce();
expect(getNewReplicator).not.toHaveBeenCalled();
});
});
describe("ObsidianLiveSyncSettingTab Fresh Start Wipe", () => {
it("returns the remote wipe result and disposes its temporary Journal client", async () => {
const resetBucket = vi.fn(async () => false);
const dispose = vi.fn();
const tab = new ObsidianLiveSyncSettingTab(
{} as never,
{
app: {},
core: {},
} as never
);
vi.spyOn(tab, "getMinioJournalSyncClient").mockReturnValue({ resetBucket, dispose } as never);
await expect(tab.resetRemoteBucket()).resolves.toBe(false);
expect(resetBucket).toHaveBeenCalledOnce();
expect(dispose).toHaveBeenCalledOnce();
});
});
@@ -17,8 +17,8 @@ export function paneMaintenance(
paneEl: HTMLElement,
{ addPanel }: PageFunctions
): void {
const isRemoteLockedAndDeviceNotAccepted = () => this.core?.replicator?.remoteLockedAndDeviceNotAccepted;
const isRemoteLocked = () => this.core?.replicator?.remoteLocked;
const isRemoteLockedAndDeviceNotAccepted = () => !!this.core?.replicator?.remoteLockedAndDeviceNotAccepted;
const isRemoteLocked = () => !!this.core?.replicator?.remoteLocked;
// if (this.plugin?.replicator?.remoteLockedAndDeviceNotAccepted) {
this.createEl(
paneEl,
@@ -367,8 +367,13 @@ export function paneMaintenance(
sentIDs: new Set(),
sentFiles: new Set(),
}));
await this.resetRemoteBucket();
Logger(`Deleted all data on remote server`, LOG_LEVEL_NOTICE);
const reset = await this.resetRemoteBucket();
Logger(
reset
? `Deleted all data on remote server`
: `Fresh Start Wipe did not complete. Keep all synchronising devices stopped and run it again.`,
LOG_LEVEL_NOTICE
);
})
)
.addOnUpdate(this.onlyOnMinIO);
@@ -0,0 +1,143 @@
import { afterEach, describe, expect, it, vi } from "vitest";
const maintenanceHarness = vi.hoisted(() => ({
createdSettings: [] as Array<{ name: string; click?: () => Promise<void> }>,
logger: vi.fn(),
}));
vi.mock("@/common/events.ts", () => ({
EVENT_REQUEST_PERFORM_GC_V3: "request-gc-v3",
eventHub: { emitEvent: vi.fn() },
}));
vi.mock("@/common/translation", () => ({
$msg: (message: string) => message,
}));
vi.mock("@/serviceFeatures/setupObsidian/settingsReset.ts", () => ({
createCoreSettingsAfterFullReset: vi.fn(),
createEditingSettingsAfterFullReset: vi.fn(),
}));
vi.mock("@vrtmrz/livesync-commonlib/compat/common/logger", () => ({
LOG_LEVEL_NOTICE: "notice",
Logger: maintenanceHarness.logger,
}));
vi.mock("@vrtmrz/livesync-commonlib/compat/common/types", () => ({
FlagFilesHumanReadable: {
FETCH_ALL: "fetch-all",
REBUILD_ALL: "rebuild-all",
},
FlagFilesOriginal: { SUSPEND_ALL: "suspend-all" },
}));
vi.mock("@vrtmrz/livesync-commonlib/compat/common/utils", () => ({
fireAndForget: (operation: Promise<unknown>) => operation,
}));
vi.mock("@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator", () => ({
LiveSyncCouchDBReplicator: class {},
}));
vi.mock("./LiveSyncSetting.ts", () => ({
LiveSyncSetting: class {
name = "";
click?: () => Promise<void>;
constructor() {
maintenanceHarness.createdSettings.push(this);
}
setName(name: string) {
this.name = name;
return this;
}
setDesc() {
return this;
}
addButton(callback: (button: this) => void) {
callback(this);
return this;
}
setButtonText() {
return this;
}
setDisabled() {
return this;
}
setCta() {
return this;
}
onClick(callback: () => Promise<void>) {
this.click = callback;
return this;
}
addOnUpdate() {
return this;
}
},
}));
vi.mock("./SettingPane", () => ({
visibleOnly: vi.fn(() => vi.fn()),
}));
vi.mock("./settingComponentStyles.ts", () => ({
setButtonDestructiveState: <T>(button: T) => button,
}));
import { paneMaintenance } from "./PaneMaintenance.ts";
afterEach(() => {
maintenanceHarness.createdSettings.length = 0;
maintenanceHarness.logger.mockClear();
vi.clearAllMocks();
});
describe("paneMaintenance Fresh Start Wipe", () => {
it("does not announce success when the remote wipe reports failure", async () => {
const updateCheckPointInfo = vi.fn(async () => undefined);
const resetRemoteBucket = vi.fn(async () => false);
const addPanel = vi.fn((_parent: HTMLElement, heading: string) => ({
then(callback: (paneEl: HTMLElement) => void) {
if (heading === "Rebuilding Operations (Remote Only)") {
callback({} as HTMLElement);
}
return Promise.resolve();
},
}));
const host = {
core: {
replicator: {},
storageAccess: {},
},
createEl: vi.fn(),
getMinioJournalSyncClient: vi.fn(() => ({ updateCheckPointInfo })),
onlyOnCouchDB: vi.fn(),
onlyOnCouchDBOrMinIO: vi.fn(),
onlyOnMinIO: vi.fn(),
resetRemoteBucket,
services: {
appLifecycle: { performRestart: vi.fn() },
database: { resetDatabase: vi.fn() },
databaseEvents: { initialiseDatabase: vi.fn() },
replication: { markLocked: vi.fn(), markUnlocked: vi.fn() },
setting: { saveSettingData: vi.fn() },
},
};
paneMaintenance.call(host as never, {} as HTMLElement, { addPanel } as never);
const freshStartWipe = maintenanceHarness.createdSettings.find(({ name }) => name === "Fresh Start Wipe");
if (!freshStartWipe?.click) {
throw new Error("Fresh Start Wipe action was not registered");
}
await freshStartWipe.click();
expect(resetRemoteBucket).toHaveBeenCalledOnce();
expect(maintenanceHarness.logger).toHaveBeenCalledWith(
"Fresh Start Wipe did not complete. Keep all synchronising devices stopped and run it again.",
"notice"
);
expect(maintenanceHarness.logger).not.toHaveBeenCalledWith("Deleted all data on remote server", "notice");
});
});
@@ -33,7 +33,6 @@ import type { RemoteConfigurationResult } from "@vrtmrz/livesync-commonlib/compa
import SetupRemote from "@/modules/features/SetupWizard/dialogs/SetupRemote.svelte";
import SetupRemoteCouchDB from "@/modules/features/SetupWizard/dialogs/SetupRemoteCouchDB.svelte";
import SetupRemoteBucket from "@/modules/features/SetupWizard/dialogs/SetupRemoteBucket.svelte";
import SetupRemoteP2P from "@/modules/features/SetupWizard/dialogs/SetupRemoteP2P.svelte";
import type {
SetupRemoteCouchDBInitialData,
SetupRemoteCouchDBResultType,
@@ -217,7 +216,7 @@ export function paneRemoteConfig(
}
if (targetRemoteType === REMOTE_P2P) {
const p2pConf = await dialogManager.openWithExplicitCancel(SetupRemoteP2P, baseSettings);
const p2pConf = await setupManager.openP2PSetup(baseSettings);
if (p2pConf === "cancelled" || typeof p2pConf !== "object") {
return false;
}
+25 -4
View File
@@ -36,6 +36,7 @@ import type {
SetupRemoteCouchDBResultType,
SetupRemoteCouchDBInitialData,
SetupRemoteE2EEResultType,
SetupRemoteP2PInitialData,
SetupRemoteP2PResultType,
SetupRemoteResultType,
UseSetupURIResultType,
@@ -48,6 +49,7 @@ import {
type SetupInitialisationMode,
} from "@/serviceFeatures/setupObsidian/setupActivationLifecycle.ts";
import { isP2PMainRemote } from "@/common/remoteConfiguration.ts";
import type { P2PConnectionProbeAdmission } from "@vrtmrz/livesync-commonlib/p2p";
function copySettingsForRemoteProfileUpdate(settings: ObsidianLiveSyncSettings): ObsidianLiveSyncSettings {
return {
@@ -94,6 +96,8 @@ export type ApplySettingsWithInitialisationChoiceOptions = {
* Setup Manager to handle onboarding and configuration setup
*/
export class SetupManager extends AbstractModule {
private p2pSetupConnectionProbe?: P2PConnectionProbeAdmission;
// /**
// * Dialog manager for handling Svelte dialogs
// */
@@ -102,6 +106,26 @@ export class SetupManager extends AbstractModule {
return this.services.UI.dialogManager;
}
/** Bind the stable P2P owner's probe view to host-owned Setup dialogues. */
registerP2PSetupConnectionProbe(connectionProbe: P2PConnectionProbeAdmission): void {
if (this.p2pSetupConnectionProbe && this.p2pSetupConnectionProbe !== connectionProbe) {
throw new Error("The P2P Setup connection probe has already been registered.");
}
this.p2pSetupConnectionProbe = connectionProbe;
}
/** Open P2P Setup with the owner-arbitrated connection-probe boundary. */
openP2PSetup(settings: P2PSyncSetting): Promise<SetupRemoteP2PResultType> {
const connectionProbe = this.p2pSetupConnectionProbe;
if (!connectionProbe) {
throw new Error("The P2P Setup connection probe is not available.");
}
return this.dialogManager.openWithExplicitCancel<SetupRemoteP2PResultType, SetupRemoteP2PInitialData>(
SetupRemoteP2P,
{ settings, connectionProbe }
);
}
/**
* Ask which existing data should be authoritative for pending setting changes,
* then reserve the matching next-start operation before applying them.
@@ -280,10 +304,7 @@ export class SetupManager extends AbstractModule {
currentSetting: ObsidianLiveSyncSettings,
activate = true
): Promise<boolean> {
const p2pConf = await this.dialogManager.openWithExplicitCancel<SetupRemoteP2PResultType, P2PSyncSetting>(
SetupRemoteP2P,
currentSetting
);
const p2pConf = await this.openP2PSetup(currentSetting);
if (p2pConf === "cancelled") {
this._log("Manual configuration cancelled.", LOG_LEVEL_NOTICE);
return await this.onOnboard(userMode);
+31 -1
View File
@@ -8,6 +8,11 @@ import {
import { SettingService } from "@vrtmrz/livesync-commonlib/compat/services/base/SettingService";
import { ServiceContext } from "@vrtmrz/livesync-commonlib/context";
import { createNewVaultSettings } from "@vrtmrz/livesync-commonlib/settings";
import type {
P2PConnectionProbeAdmission,
P2PConnectionProbeAdmissionResult,
P2PConnectionProbeSettings,
} from "@vrtmrz/livesync-commonlib/p2p";
vi.mock("./SetupWizard/dialogs/Intro.svelte", () => ({ default: {} }));
vi.mock("./SetupWizard/dialogs/SelectMethodNewUser.svelte", () => ({ default: {} }));
@@ -124,11 +129,23 @@ function createSetupManager() {
},
});
const p2pSetupConnectionProbe: P2PConnectionProbeAdmission = {
async run<T>(
_settings: P2PConnectionProbeSettings,
runOwnedTrial: () => Promise<T>
): Promise<P2PConnectionProbeAdmissionResult<T>> {
return { status: "trial", result: await runOwnedTrial() };
},
};
const manager = new SetupManager(core);
manager.registerP2PSetupConnectionProbe(p2pSetupConnectionProbe);
return {
manager: new SetupManager(core),
manager,
setting,
dialogManager,
core,
p2pSetupConnectionProbe,
};
}
@@ -138,6 +155,19 @@ describe("SetupManager", () => {
vi.restoreAllMocks();
});
it("opens P2P Setup with the registered owner admission", async () => {
const { manager, setting, dialogManager, p2pSetupConnectionProbe } = createSetupManager();
const settings = setting.currentSettings();
dialogManager.openWithExplicitCancel.mockResolvedValueOnce("cancelled");
await expect(manager.openP2PSetup(settings)).resolves.toBe("cancelled");
expect(dialogManager.openWithExplicitCancel).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ settings, connectionProbe: p2pSetupConnectionProbe })
);
});
it("starts manual new-user setup from the recommended new-Vault settings", async () => {
const { manager, dialogManager } = createSetupManager();
dialogManager.openWithExplicitCancel.mockResolvedValueOnce("configure-manually");
@@ -20,6 +20,8 @@
import { copyTo, pickBucketSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/utils";
import { TYPE_CANCELLED, type SetupRemoteBucketResultType } from "./setupDialogTypes";
import { $msg as translateMessage } from "@/common/translation";
import { withOwnedRemoteResource } from "@/common/ownedRemoteResource";
import { REMOTE_RESOURCE_KINDS } from "@vrtmrz/livesync-commonlib/replication";
const default_setting = pickBucketSyncSettings(DEFAULT_SETTINGS);
@@ -81,13 +83,18 @@
try {
processing = true;
const trialRemoteSetting = generateSetting();
const replicator = await context.services.replicator.getNewReplicator(trialRemoteSetting);
if (!replicator) {
return translateMessage("Failed to create replicator instance.");
const probe = await context.services.replicator.createRemoteResource(
REMOTE_RESOURCE_KINDS.CONNECTION,
trialRemoteSetting
);
if (!probe) {
return translateMessage("Failed to connect to the server. Please check your settings.");
}
try {
const result = await replicator.tryConnectRemote(trialRemoteSetting, false);
if (result) {
const result = await withOwnedRemoteResource(probe, (ownedProbe) =>
ownedProbe.check({ createIfMissing: true, showResult: false })
);
if (result.ok) {
return "";
} else {
return translateMessage("Failed to connect to the server. Please check your settings.");
@@ -29,6 +29,7 @@
} from "./setupDialogTypes";
import { isValidCouchDBServerURL, probeCouchDBConnection } from "./couchDBConnectionProbe";
import { $msg as translateMessage } from "@/common/translation";
import { REMOTE_RESOURCE_KINDS } from "@vrtmrz/livesync-commonlib/replication";
const default_setting = pickCouchDBSyncSettings(DEFAULT_SETTINGS);
@@ -73,16 +74,15 @@
try {
processing = true;
const trialRemoteSetting = generateSetting();
const replicator = await context.services.replicator.getNewReplicator(trialRemoteSetting);
if (!replicator) {
return translateMessage("Failed to create replicator instance.");
const probe = await context.services.replicator.createRemoteResource(
REMOTE_RESOURCE_KINDS.CONNECTION,
trialRemoteSetting
);
if (!probe) {
return translateMessage("Failed to connect to the server. Please check your settings.");
}
try {
const result = await probeCouchDBConnection(
replicator,
trialRemoteSetting,
setupMode === "create-or-connect"
);
const result = await probeCouchDBConnection(probe, setupMode === "create-or-connect");
if (result.ok) {
return "";
} else {
@@ -36,10 +36,14 @@
import { getDialogContext, type GuestDialogProps } from "@/modules/services/LiveSyncUI/svelteDialog";
import { SETTING_KEY_P2P_DEVICE_NAME } from "@vrtmrz/livesync-commonlib/compat/common/types";
import ExtraItems from "@/modules/services/LiveSyncUI/components/ExtraItems.svelte";
import { TYPE_CANCELLED, type SetupRemoteP2PResultType } from "./setupDialogTypes";
import {
TYPE_CANCELLED,
type SetupRemoteP2PInitialData,
type SetupRemoteP2PResultType,
} from "./setupDialogTypes";
import { LOG_LEVEL_VERBOSE, Logger } from "octagonal-wheels/common/logger";
import { $msg as translateMessage } from "@/common/translation";
import { probeP2PSetupConnection } from "./p2pSetupConnectionProbe";
import { coordinateP2PSetupConnectionProbe, probeP2PSetupConnection } from "./p2pSetupConnectionProbe";
const default_setting = pickP2PSyncSettings(DEFAULT_SETTINGS);
let syncSetting = $state<P2PConnectionInfo>({ ...default_setting });
@@ -48,18 +52,18 @@
let error = $state("");
let connectionPathResetNotice = $state(false);
const hasValidTurnServer = $derived(hasValidP2PTurnServerUrl(syncSetting.P2P_turnServers ?? ""));
type Props = GuestDialogProps<SetupRemoteP2PResultType, P2PSyncSetting>;
type Props = GuestDialogProps<SetupRemoteP2PResultType, SetupRemoteP2PInitialData>;
const { setResult, getInitialData }: Props = $props();
let connectionProbe: SetupRemoteP2PInitialData["connectionProbe"] | undefined;
onMount(() => {
let initialData: P2PSyncSetting | undefined = undefined;
if (getInitialData) {
initialData = getInitialData();
if (initialData) {
copyTo(initialData, syncSetting);
}
const initialData = getInitialData?.();
connectionProbe = initialData?.connectionProbe;
const initialSettings = initialData?.settings;
if (initialSettings) {
copyTo(initialSettings, syncSetting);
}
const initialPeerName = (initialData?.P2P_DevicePeerName ?? "").trim();
const initialPeerName = (initialSettings?.P2P_DevicePeerName ?? "").trim();
if (initialPeerName !== "") {
return;
}
@@ -97,58 +101,74 @@
try {
processing = true;
const trialRemoteSetting = generateSetting();
const map = new Map<string, string>();
const store = {
get: (key: string) => {
return Promise.resolve(map.get(key) || null);
},
set: (key: string, value: any) => {
map.set(key, value);
return Promise.resolve();
},
delete: (key: string) => {
map.delete(key);
return Promise.resolve();
},
keys: () => {
return Promise.resolve(Array.from(map.keys()));
},
get db() {
return Promise.resolve(this);
},
} as SimpleStore<any>;
const dummyPouch = new PouchDB<EntryDoc>("dummy");
const env: ReplicatorHostEnv = {
events: context.context.events,
translate: context.context.translate,
settings: trialRemoteSetting,
processReplicatedDocs: async (_docs: any[]) => {
return;
},
confirm: context.services.confirm,
db: dummyPouch,
simpleStore: store,
deviceName: syncSetting.P2P_DevicePeerName || "unnamed-device",
platform: "setup-wizard",
};
const replicator = new TrysteroReplicator(env);
try {
const result = await probeP2PSetupConnection(replicator);
if (!result.ok) {
return translateMessage("Failed to connect to the signalling relay: ${reason}", {
reason: `${result.reason}`,
});
}
return "";
} finally {
try {
await replicator.close();
await dummyPouch.destroy();
} catch (e) {
Logger(e, LOG_LEVEL_VERBOSE, "setup-p2p-cleanup");
}
const admission = connectionProbe;
if (!admission) {
throw new Error("The P2P Setup connection probe is not available.");
}
const result = await coordinateP2PSetupConnectionProbe(admission, trialRemoteSetting, async () => {
const map = new Map<string, unknown>();
const store = {
get: (key: string) => {
return Promise.resolve(map.get(key) || null);
},
set: (key: string, value: unknown) => {
map.set(key, value);
return Promise.resolve();
},
delete: (key: string) => {
map.delete(key);
return Promise.resolve();
},
keys: () => {
return Promise.resolve(Array.from(map.keys()));
},
get db() {
return Promise.resolve(this);
},
} as SimpleStore<unknown>;
const dummyPouch = new PouchDB<EntryDoc>("dummy");
let replicator: TrysteroReplicator | undefined;
try {
const env: ReplicatorHostEnv = {
events: context.context.events,
translate: context.context.translate,
settings: trialRemoteSetting,
processReplicatedDocs: async (_docs: PouchDB.Core.ExistingDocument<EntryDoc>[]) => {
return;
},
confirm: context.services.confirm,
db: dummyPouch,
simpleStore: store,
deviceName: syncSetting.P2P_DevicePeerName || "unnamed-device",
platform: "setup-wizard",
};
replicator = new TrysteroReplicator(env);
return await probeP2PSetupConnection(replicator);
} finally {
try {
await replicator?.dispose();
} catch (e) {
Logger(e, LOG_LEVEL_VERBOSE, "setup-p2p-replicator-cleanup");
}
try {
await dummyPouch.destroy();
} catch (e) {
Logger(e, LOG_LEVEL_VERBOSE, "setup-p2p-database-cleanup");
}
}
});
if (!result.ok) {
if ("kind" in result && result.kind === "blocked") {
return translateMessage(
"The connection test cannot add a signalling relay while P2P is active. Use the active relay settings, or disconnect P2P before testing."
);
}
return translateMessage("Failed to connect to the signalling relay: ${reason}", {
reason: `${result.reason}`,
});
}
return "";
} finally {
processing = false;
}
@@ -1,60 +1,14 @@
import type {
ObsidianLiveSyncSettings,
RemoteDBSettings,
} from "@vrtmrz/livesync-commonlib/compat/common/models/setting.type";
export type CouchDBConnectionProbeResult = { ok: true } | { ok: false; reason: string };
type CouchDBConnectionResult =
| string
| {
db: { close(): Promise<void> };
info: unknown;
};
export interface CouchDBConnectionProbe {
isMobile(): boolean;
connectRemoteCouchDBWithSetting(
settings: RemoteDBSettings,
isMobile: boolean,
performSetup: boolean,
skipInfo: boolean
): CouchDBConnectionResult | Promise<CouchDBConnectionResult>;
}
export function isCouchDBConnectionProbe(value: unknown): value is CouchDBConnectionProbe {
return (
typeof value === "object" &&
value !== null &&
"isMobile" in value &&
typeof value.isMobile === "function" &&
"connectRemoteCouchDBWithSetting" in value &&
typeof value.connectRemoteCouchDBWithSetting === "function"
);
}
import type { RemoteConnectionProbe, RemoteConnectionProbeResult } from "@vrtmrz/livesync-commonlib/replication";
import { withOwnedRemoteResource } from "@/common/ownedRemoteResource";
/** Run the selected CouchDB setup mode within one owned probe lifetime. */
export async function probeCouchDBConnection(
replicator: unknown,
settings: ObsidianLiveSyncSettings,
probe: RemoteConnectionProbe,
createIfMissing: boolean
): Promise<CouchDBConnectionProbeResult> {
if (!isCouchDBConnectionProbe(replicator)) {
return { ok: false, reason: "The CouchDB connection probe is unavailable." };
}
const result = await replicator.connectRemoteCouchDBWithSetting(
settings,
replicator.isMobile(),
createIfMissing,
false
): Promise<RemoteConnectionProbeResult> {
return await withOwnedRemoteResource(probe, (ownedProbe) =>
ownedProbe.check({ createIfMissing, showResult: false })
);
if (typeof result === "string") {
return { ok: false, reason: result };
}
try {
return { ok: true };
} finally {
await result.db.close();
}
}
export function isValidCouchDBServerURL(value: string): boolean {
@@ -1,47 +1,34 @@
import { describe, expect, it, vi } from "vitest";
import type { ObsidianLiveSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/models/setting.type";
import { isValidCouchDBServerURL, probeCouchDBConnection } from "./couchDBConnectionProbe";
const settings = {
couchDB_URI: "https://couch.example",
couchDB_DBNAME: "notes",
} as ObsidianLiveSyncSettings;
describe("CouchDB setup connection policy", () => {
it.each([
[false, "connect to an existing database"],
[true, "create or connect to a database"],
] as const)(
"%s can %s without changing the Commonlib connection contract",
async (createIfMissing, _description) => {
const close = vi.fn(async () => undefined);
const connectRemoteCouchDBWithSetting = vi.fn(async () => ({
db: { close },
info: { db_name: "notes" },
}));
const replicator = {
isMobile: vi.fn(() => false),
connectRemoteCouchDBWithSetting,
tryConnectRemote: vi.fn(),
};
] as const)("%s can %s through an owned connection probe", async (createIfMissing, _description) => {
const check = vi.fn(async () => ({ ok: true as const }));
const dispose = vi.fn(async () => undefined);
const probe = { check, getStatus: vi.fn(), dispose };
await expect(probeCouchDBConnection(replicator, settings, createIfMissing)).resolves.toEqual({ ok: true });
expect(connectRemoteCouchDBWithSetting).toHaveBeenCalledWith(settings, false, createIfMissing, false);
expect(replicator.tryConnectRemote).not.toHaveBeenCalled();
expect(close).toHaveBeenCalledOnce();
}
);
await expect(probeCouchDBConnection(probe, createIfMissing)).resolves.toEqual({ ok: true });
it("returns the connection error without saving or creating through another path", async () => {
const replicator = {
isMobile: vi.fn(() => true),
connectRemoteCouchDBWithSetting: vi.fn(() => "database does not exist"),
expect(check).toHaveBeenCalledWith({ createIfMissing, showResult: false });
expect(dispose).toHaveBeenCalledOnce();
});
it("returns a connection error and still disposes the probe", async () => {
const dispose = vi.fn(async () => undefined);
const probe = {
check: vi.fn(async () => ({ ok: false as const, reason: "database does not exist" })),
getStatus: vi.fn(),
dispose,
};
await expect(probeCouchDBConnection(replicator, settings, false)).resolves.toEqual({
await expect(probeCouchDBConnection(probe, false)).resolves.toEqual({
ok: false,
reason: "database does not exist",
});
expect(dispose).toHaveBeenCalledOnce();
});
it.each([
@@ -1,4 +1,17 @@
export type P2PSetupConnectionProbeResult = { ok: true } | { ok: false; reason: string };
import {
ACTIVE_P2P_RELAY_BINDING_CONFLICT,
type P2PConnectionProbeAdmission,
type P2PConnectionProbeSettings,
} from "@vrtmrz/livesync-commonlib/p2p";
export type P2PSetupConnectionProbeResult =
| { readonly ok: true }
| { readonly ok: false; readonly reason: string }
| {
readonly ok: false;
readonly kind: "blocked";
readonly reason: typeof ACTIVE_P2P_RELAY_BINDING_CONFLICT;
};
export interface P2PSetupConnectionProbe {
setOnSetup(): void | Promise<void>;
@@ -6,6 +19,25 @@ export interface P2PSetupConnectionProbe {
open(): Promise<void>;
}
/** Interpret the stable P2P owner's admission without constructing transport eagerly. */
export async function coordinateP2PSetupConnectionProbe(
admission: P2PConnectionProbeAdmission,
trialSettings: P2PConnectionProbeSettings,
runOwnedTrial: () => Promise<P2PSetupConnectionProbeResult>
): Promise<P2PSetupConnectionProbeResult> {
const settlement = await admission.run(trialSettings, runOwnedTrial);
if (settlement.status === "observed-active") return { ok: true };
if (settlement.status === "blocked") {
return {
ok: false,
kind: "blocked",
reason: settlement.reason,
};
}
return settlement.result;
}
/** Open one separately owned signalling connection and report its outcome. */
export async function probeP2PSetupConnection(
replicator: P2PSetupConnectionProbe
): Promise<P2PSetupConnectionProbeResult> {
@@ -1,7 +1,69 @@
import { describe, expect, it, vi } from "vitest";
import { probeP2PSetupConnection } from "./p2pSetupConnectionProbe";
import { ACTIVE_P2P_RELAY_BINDING_CONFLICT, type P2PConnectionProbeAdmission } from "@vrtmrz/livesync-commonlib/p2p";
import {
coordinateP2PSetupConnectionProbe,
probeP2PSetupConnection,
type P2PSetupConnectionProbeResult,
} from "./p2pSetupConnectionProbe";
describe("P2P setup connection probe", () => {
it("uses a compatible active signalling connection without constructing a trial", async () => {
const runOwnedTrial = vi.fn(async (): Promise<P2PSetupConnectionProbeResult> => ({ ok: true }));
const admission: P2PConnectionProbeAdmission = {
run: vi.fn(async () => ({ status: "observed-active" }) as const),
};
await expect(
coordinateP2PSetupConnectionProbe(admission, { P2P_relays: "wss://relay.example.com" }, runOwnedTrial)
).resolves.toEqual({ ok: true });
expect(admission.run).toHaveBeenCalledOnce();
expect(runOwnedTrial).not.toHaveBeenCalled();
});
it("preserves the typed blocked reason without opening an incompatible trial", async () => {
const runOwnedTrial = vi.fn(async (): Promise<P2PSetupConnectionProbeResult> => ({ ok: true }));
const admission: P2PConnectionProbeAdmission = {
run: vi.fn(
async () =>
({
status: "blocked",
reason: ACTIVE_P2P_RELAY_BINDING_CONFLICT,
}) as const
),
};
await expect(
coordinateP2PSetupConnectionProbe(
admission,
{ P2P_relays: "wss://another-relay.example.com" },
runOwnedTrial
)
).resolves.toEqual({
ok: false,
kind: "blocked",
reason: ACTIVE_P2P_RELAY_BINDING_CONFLICT,
});
expect(admission.run).toHaveBeenCalledOnce();
expect(runOwnedTrial).not.toHaveBeenCalled();
});
it("runs and returns the complete owned trial continuation when no room is active", async () => {
const trialResult = { ok: false, reason: "relay unavailable" } as const;
const runOwnedTrial = vi.fn(async (): Promise<P2PSetupConnectionProbeResult> => trialResult);
const admission: P2PConnectionProbeAdmission = {
run: vi.fn(async (_settings, trial) => ({ status: "trial", result: await trial() }) as const),
};
await expect(
coordinateP2PSetupConnectionProbe(admission, { P2P_relays: "wss://relay.example.com" }, runOwnedTrial)
).resolves.toEqual(trialResult);
expect(admission.run).toHaveBeenCalledOnce();
expect(runOwnedTrial).toHaveBeenCalledOnce();
});
it("accepts an empty room after the signalling connection opens", async () => {
const replicator = {
knownAdvertisements: [],
@@ -4,7 +4,9 @@ import type {
EncryptionSettings,
ObsidianLiveSyncSettings,
P2PConnectionInfo,
P2PSyncSetting,
} from "@vrtmrz/livesync-commonlib/compat/common/models/setting.type";
import type { P2PConnectionProbeAdmission } from "@vrtmrz/livesync-commonlib/p2p";
export const TYPE_IDENTICAL = "identical";
export const TYPE_INDEPENDENT = "independent";
@@ -119,5 +121,9 @@ export type SetupRemoteCouchDBInitialData = {
};
export type SetupRemoteP2PResultType = typeof TYPE_CANCELLED | P2PConnectionInfo;
export type SetupRemoteP2PInitialData = {
settings: P2PSyncSetting;
connectionProbe: P2PConnectionProbeAdmission;
};
export type ScanQRCodeResultType = typeof TYPE_CLOSE;
@@ -37,7 +37,11 @@ describe("ObsidianReplicatorService", () => {
allowSleepDuringSynchronisationOnDesktop: false,
}),
},
appLifecycleService: { onSuspending: handler(), getUnresolvedMessages: handler() },
appLifecycleService: {
onSuspending: handler(),
onUnload: handler(),
getUnresolvedMessages: handler(),
},
databaseEventService: {
onResetDatabase: handler(),
onDatabaseInitialisation: handler(),
@@ -73,7 +77,11 @@ describe("ObsidianReplicatorService", () => {
allowSleepDuringSynchronisationOnDesktop: true,
}),
},
appLifecycleService: { onSuspending: handler(), getUnresolvedMessages: handler() },
appLifecycleService: {
onSuspending: handler(),
onUnload: handler(),
getUnresolvedMessages: handler(),
},
databaseEventService: {
onResetDatabase: handler(),
onDatabaseInitialisation: handler(),
@@ -6,8 +6,8 @@ import {
type EntryLeaf,
type LoadedEntry,
type MetaEntry,
type ObsidianLiveSyncSettings,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { ModuleReplicator } from "./ModuleReplicator";
import { isChunk } from "@vrtmrz/livesync-commonlib/compat/common/typeUtils";
import {
LOG_LEVEL_DEBUG,
@@ -28,12 +28,39 @@ import { promiseWithResolvers, type PromiseWithResolvers } from "octagonal-wheel
const KV_KEY_REPLICATION_RESULT_PROCESSOR_SNAPSHOT = "replicationResultProcessorSnapshot";
const REPROCESS_BATCH_SIZE = 100;
type LocalApplicationActivityOwner = {
runBoundedLocalApplicationActivity<T>(
type ReplicateResultProcessorSettings = Pick<
ObsidianLiveSyncSettings,
"maxMTimeForReflectEvents" | "suspendParseReplicationResult"
>;
type ReplicateResultProcessorServices = Pick<
LiveSyncBaseCore["services"],
"appLifecycle" | "path" | "replication" | "vault"
>;
/**
* Narrow collaborators for applying replicated documents.
*
* `requestActiveReplicatorRetirement` starts the owner transition without
* awaiting it. Result application can still be running inside work admitted by
* that owner, so awaiting retirement here could make each side wait for the
* other to finish.
*
* Runtime databases are deliberately obtained through operation-time
* accessors. Feature composition precedes their initialisation, and database
* reset may replace their backing instances, so retaining an earlier concrete
* database would be invalid.
*/
interface ReplicateResultProcessorContext {
readonly currentSettings: () => ReplicateResultProcessorSettings;
readonly getKeyValueDB: () => LiveSyncBaseCore["kvDB"];
readonly getLocalDatabase: () => LiveSyncBaseCore["localDatabase"];
readonly requestActiveReplicatorRetirement: () => void;
readonly runLocalApplicationActivity: <T>(
task: () => T | PromiseLike<T>,
options?: { label?: string }
): Promise<T>;
};
) => Promise<T>;
readonly services: ReplicateResultProcessorServices;
}
type ReplicateResultProcessorState = {
queued: PouchDB.Core.ExistingDocument<EntryDoc>[];
processing: PouchDB.Core.ExistingDocument<EntryDoc>[];
@@ -52,20 +79,13 @@ export class ReplicateResultProcessor {
private logError(e: unknown) {
Logger(e, LOG_LEVEL_VERBOSE);
}
private replicator: ModuleReplicator;
constructor(private readonly context: ReplicateResultProcessorContext) {}
constructor(replicator: ModuleReplicator) {
this.replicator = replicator;
private get localDatabase() {
return this.context.getLocalDatabase();
}
get localDatabase() {
return this.replicator.core.localDatabase;
}
get services() {
return this.replicator.core.services;
}
get core(): LiveSyncBaseCore {
return this.replicator.core;
private get services() {
return this.context.services;
}
getPath(entry: AnyEntry): string {
@@ -89,9 +109,9 @@ export class ReplicateResultProcessor {
public get isSuspended() {
return (
this._suspended ||
!this.core.services.appLifecycle.isReady ||
this.replicator.settings.suspendParseReplicationResult ||
this.core.services.appLifecycle.isSuspended()
!this.services.appLifecycle.isReady() ||
this.context.currentSettings().suspendParseReplicationResult ||
this.services.appLifecycle.isSuspended()
);
}
@@ -104,7 +124,7 @@ export class ReplicateResultProcessor {
queued: this._queuedChanges.slice(),
processing: this._processingChanges.slice(),
} satisfies ReplicateResultProcessorState;
await this.core.kvDB.set(KV_KEY_REPLICATION_RESULT_PROCESSOR_SNAPSHOT, snapshot);
await this.context.getKeyValueDB().set(KV_KEY_REPLICATION_RESULT_PROCESSOR_SNAPSHOT, snapshot);
this.log(
`Snapshot taken. Queued: ${snapshot.queued.length}, Processing: ${snapshot.processing.length}`,
LOG_LEVEL_DEBUG
@@ -126,9 +146,9 @@ export class ReplicateResultProcessor {
* Restore from snapshot.
*/
public async restoreFromSnapshot() {
const snapshot = await this.core.kvDB.get<ReplicateResultProcessorState>(
KV_KEY_REPLICATION_RESULT_PROCESSOR_SNAPSHOT
);
const snapshot = await this.context
.getKeyValueDB()
.get<ReplicateResultProcessorState>(KV_KEY_REPLICATION_RESULT_PROCESSOR_SNAPSHOT);
if (snapshot) {
// Restoring the snapshot re-runs processing for both queued and processing items.
const newQueue = [...snapshot.processing, ...snapshot.queued, ...this._queuedChanges];
@@ -231,8 +251,8 @@ export class ReplicateResultProcessor {
if (change.type == "versioninfo") {
this.log(`Version info document received: ${change._id}`, LOG_LEVEL_VERBOSE);
if (change.version > VER) {
// Incompatible version, stop replication.
this.core.replicator.closeReplication();
// Fence and retire the active publication through its owner.
this.context.requestActiveReplicatorRetirement();
this.log(
`Remote database updated to incompatible version. update your Self-hosted LiveSync plugin.`,
LOG_LEVEL_NOTICE
@@ -277,15 +297,10 @@ export class ReplicateResultProcessor {
const activityDone = promiseWithResolvers<void>();
this._processingActivityDone = activityDone;
const activityOwner = this.services.replicator as typeof this.services.replicator &
Partial<LocalApplicationActivityOwner>;
this._processingActivity = (
activityOwner.runBoundedLocalApplicationActivity
? activityOwner.runBoundedLocalApplicationActivity(() => activityDone.promise, {
label: "replicated-document-application",
})
: activityDone.promise
)
this._processingActivity = this.context
.runLocalApplicationActivity(() => activityDone.promise, {
label: "replicated-document-application",
})
.catch((error) => this.logError(error))
.finally(() => {
if (this._processingActivityDone === activityDone) this._processingActivityDone = undefined;
@@ -392,7 +407,7 @@ export class ReplicateResultProcessor {
try {
if (isAnyNote(change)) {
const docMtime = change.mtime ?? 0;
const maxMTime = this.replicator.settings.maxMTimeForReflectEvents;
const maxMTime = this.context.currentSettings().maxMTimeForReflectEvents;
if (maxMTime > 0 && docMtime > maxMTime) {
const docPath = this.getPath(change);
this.log(
@@ -1,7 +1,7 @@
import { promiseWithResolvers } from "octagonal-wheels/promises";
import { reactiveSource } from "octagonal-wheels/dataobject/reactive";
import { describe, expect, it, vi } from "vitest";
import type { EntryDoc } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { VER, type EntryDoc } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { ReplicateResultProcessor } from "./ReplicateResultProcessor";
function note(id: string): PouchDB.Core.ExistingDocument<EntryDoc> {
@@ -20,6 +20,7 @@ function note(id: string): PouchDB.Core.ExistingDocument<EntryDoc> {
}
type SetupOptions = {
applicationReady?: boolean;
processSynchroniseResult?: (entry: unknown) => Promise<void>;
setSnapshot?: (key: string, value: unknown) => Promise<unknown>;
};
@@ -28,9 +29,11 @@ function setup(options: SetupOptions = {}) {
const processSynchroniseResult = vi.fn(options.processSynchroniseResult ?? (async () => undefined));
const setSnapshot = vi.fn(options.setSnapshot ?? (async () => undefined));
const runBoundedLocalApplicationActivity = vi.fn(async (task: () => Promise<void>) => await task());
const onCloseActiveReplication = vi.fn(async () => true);
const isReady = vi.fn(() => options.applicationReady ?? true);
const core = {
services: {
appLifecycle: { isReady: true, isSuspended: () => false },
appLifecycle: { isReady, isSuspended: () => false },
path: { getPath: (entry: { path: string }) => entry.path },
replication: {
databaseQueueCount: reactiveSource(0),
@@ -40,7 +43,7 @@ function setup(options: SetupOptions = {}) {
processOptionalSynchroniseResult: vi.fn(async () => false),
processSynchroniseResult,
},
replicator: { runBoundedLocalApplicationActivity },
replicator: { onCloseActiveReplication, runBoundedLocalApplicationActivity },
vault: {
isTargetFile: vi.fn(async () => true),
isFileSizeTooLarge: vi.fn(() => false),
@@ -52,16 +55,48 @@ function setup(options: SetupOptions = {}) {
getRaw: vi.fn(async (id: string) => ({ _id: id, _rev: "1-test" })),
getDBEntryFromMeta: vi.fn(async (entry: object) => ({ ...entry, data: "x" })),
},
replicator: { closeReplication: vi.fn() },
};
const processor = new ReplicateResultProcessor({
core,
settings: { maxMTimeForReflectEvents: 0, suspendParseReplicationResult: false },
currentSettings: () => ({ maxMTimeForReflectEvents: 0, suspendParseReplicationResult: false }),
getKeyValueDB: () => core.kvDB,
getLocalDatabase: () => core.localDatabase,
requestActiveReplicatorRetirement: () => {
void onCloseActiveReplication();
},
runLocalApplicationActivity: runBoundedLocalApplicationActivity,
services: core.services,
} as never);
return { processor, processSynchroniseResult, runBoundedLocalApplicationActivity };
return {
isReady,
onCloseActiveReplication,
processor,
processSynchroniseResult,
runBoundedLocalApplicationActivity,
};
}
describe("ReplicateResultProcessor", () => {
it("suspends result application while the application is not ready", () => {
const { isReady, processor } = setup({ applicationReady: false });
expect(processor.isSuspended).toBe(true);
expect(isReady).toHaveBeenCalledOnce();
});
it("retires active ownership when a newer remote version is observed", async () => {
const { onCloseActiveReplication, processor } = setup();
const versionInfo = {
_id: "versioninfo",
_rev: "1-test",
type: "versioninfo",
version: VER + 1,
} as unknown as PouchDB.Core.ExistingDocument<EntryDoc>;
processor.enqueueAll([versionInfo]);
await vi.waitFor(() => expect(onCloseActiveReplication).toHaveBeenCalledOnce());
});
it("scans normal-file metadata without loading chunk documents and requeues it", async () => {
const documents = [
{ _id: "first", _rev: "1-a", type: "plain", path: "first.md" },
@@ -70,14 +105,16 @@ describe("ReplicateResultProcessor", () => {
const findAllNormalDocs = vi.fn(async function* () {
yield* documents;
});
const getLocalDatabase = vi.fn(() => ({ findAllNormalDocs }));
const processor = new ReplicateResultProcessor({
core: { localDatabase: { findAllNormalDocs } },
getLocalDatabase,
} as never);
const enqueueAll = vi.spyOn(processor, "enqueueAll").mockImplementation(() => undefined);
await expect(processor.reprocessStoredDocuments()).resolves.toBe(2);
expect(findAllNormalDocs).toHaveBeenCalledOnce();
expect(getLocalDatabase).toHaveBeenCalledOnce();
expect(enqueueAll).toHaveBeenCalledOnce();
expect(enqueueAll).toHaveBeenCalledWith(documents);
});
@@ -0,0 +1,71 @@
import type { ObsidianLiveSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { fireAndForget } from "octagonal-wheels/promises";
import { scheduleTask } from "octagonal-wheels/concurrency/task";
import { EVENT_FILE_SAVED, EVENT_SETTING_SAVED, eventHub } from "@/common/events";
type ReflectionFilterSettings = Pick<
ObsidianLiveSyncSettings,
| "handleFilenameCaseSensitive"
| "ignoreFiles"
| "maxMTimeForReflectEvents"
| "syncIgnoreRegEx"
| "syncInternalFiles"
| "syncMaxSizeInMB"
| "syncOnlyRegEx"
| "useIgnoreFiles"
>;
interface AutomaticReplicationTriggerContext {
readonly currentSettings: () => ObsidianLiveSyncSettings;
readonly isSuspended: () => boolean;
readonly replicateDatabaseEvent: () => Promise<unknown>;
readonly reprocessStoredDocuments: () => Promise<number>;
readonly resumeResultApplication: () => void;
readonly suspendResultApplication: () => void;
}
function normalFileReflectionFilterSignature(settings: ReflectionFilterSettings): string {
return JSON.stringify({
handleFilenameCaseSensitive: settings.handleFilenameCaseSensitive ?? false,
ignoreFiles: settings.ignoreFiles ?? "",
maxMTimeForReflectEvents: settings.maxMTimeForReflectEvents ?? 0,
syncIgnoreRegEx: settings.syncIgnoreRegEx ?? "",
syncInternalFiles: settings.syncInternalFiles ?? false,
syncMaxSizeInMB: settings.syncMaxSizeInMB ?? 0,
syncOnlyRegEx: settings.syncOnlyRegEx ?? "",
useIgnoreFiles: settings.useIgnoreFiles ?? false,
});
}
/**
* Create the settings-loaded handler which installs automatic replication and
* result-application reactions. The returned closure owns the previous filter
* signature; it is private composition state rather than a shared service.
*/
export function createAutomaticReplicationTriggers(context: AutomaticReplicationTriggerContext) {
let reflectionFilterSignature: string | undefined;
return function initialiseAutomaticReplicationTriggers(): Promise<boolean> {
reflectionFilterSignature = normalFileReflectionFilterSignature(context.currentSettings());
eventHub.onEvent(EVENT_FILE_SAVED, () => {
if (context.currentSettings().syncOnSave && !context.isSuspended()) {
scheduleTask("perform-replicate-after-save", 250, () => context.replicateDatabaseEvent());
}
});
eventHub.onEvent(EVENT_SETTING_SAVED, (settings) => {
const previousReflectionFilter = reflectionFilterSignature;
const nextReflectionFilter = normalFileReflectionFilterSignature(settings);
reflectionFilterSignature = nextReflectionFilter;
if (settings.suspendParseReplicationResult) {
context.suspendResultApplication();
} else {
context.resumeResultApplication();
}
if (previousReflectionFilter !== undefined && previousReflectionFilter !== nextReflectionFilter) {
fireAndForget(() => context.reprocessStoredDocuments());
}
});
return Promise.resolve(true);
};
}
@@ -0,0 +1,328 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import {
AUTO_MERGED,
DEFAULT_SETTINGS,
REMOTE_P2P,
type FilePathWithPrefix,
type ObsidianLiveSyncSettings,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { NO_INTERACTION } from "@vrtmrz/livesync-commonlib/replication";
import { EVENT_FILE_SAVED, EVENT_SETTING_SAVED, eventHub } from "@/common/events";
const taskMocks = vi.hoisted(() => ({
scheduleTask: vi.fn((_key: string, _delay: number, task: () => unknown) => task()),
}));
vi.mock("octagonal-wheels/concurrency/task", () => taskMocks);
import { ModuleConflictResolver } from "@/modules/coreFeatures/ModuleConflictResolver";
import { ModuleObsidianEvents } from "@/modules/essentialObsidian/ModuleObsidianEvents";
import {
createReplicationSchedulingContext,
realiseReplicationScheduling,
resumeReplicationScheduling,
runPeriodicReplication,
} from "@/serviceFeatures/replicationScheduling";
import { createAutomaticReplicationTriggers } from "./automaticTriggers";
function createApi() {
return {
addLog: vi.fn(),
addCommand: vi.fn(),
registerWindow: vi.fn(),
addRibbonIcon: vi.fn(),
registerProtocolHandler: vi.fn(),
setInterval: vi.fn(),
clearInterval: vi.fn(),
};
}
function p2pSettings(overrides: Partial<typeof DEFAULT_SETTINGS> = {}) {
return {
...DEFAULT_SETTINGS,
remoteType: REMOTE_P2P,
isConfigured: true,
...overrides,
};
}
function createObsidianEventHarness(settings: Partial<typeof DEFAULT_SETTINGS>) {
const save = vi.fn();
const saveCommand = { callback: save };
const replicateUnattendedByEvent = vi.fn(async () => ({ status: "completed" as const }));
const queueCheckForIfOpen = vi.fn(async () => undefined);
const services = {
API: createApi(),
appLifecycle: {
isReady: vi.fn(() => true),
isSuspended: vi.fn(() => false),
},
conflict: { queueCheckForIfOpen },
control: { hasUnloaded: vi.fn(() => false) },
fileProcessing: { commitPendingFileEvents: vi.fn(async () => true) },
replication: { replicateUnattendedByEvent },
};
const core = {
_services: services,
services,
settings: p2pSettings(settings),
} as any;
const plugin = {
app: {
commands: {
commands: { "editor:save-file": saveCommand },
executeCommandById: vi.fn(),
},
},
} as any;
return {
module: new ModuleObsidianEvents(plugin, core),
queueCheckForIfOpen,
replicateUnattendedByEvent,
save,
saveCommand,
services,
};
}
describe("automatic replication triggers while P2P is active", () => {
afterEach(() => {
eventHub.offAll();
taskMocks.scheduleTask.mockClear();
});
it("keeps periodic synchronisation on the provider-independent replication boundary", async () => {
const replicateUnattended = vi.fn(async () => ({ status: "completed" as const }));
const services = {
API: createApi(),
control: { hasUnloaded: vi.fn(() => false) },
replication: { replicateUnattended },
};
const core = {
_services: services,
services,
settings: p2pSettings({ periodicReplication: true, syncOnStart: false }),
} as any;
const context = createReplicationSchedulingContext({
isReady: vi.fn(() => true),
isSuspended: vi.fn(() => false),
currentSettings: vi.fn(() => core.settings),
replicateUnattended,
startContinuous: vi.fn(async () => ({ status: "completed" as const })),
timer: { enable: vi.fn(), disable: vi.fn() },
log: vi.fn(),
});
resumeReplicationScheduling(context);
await runPeriodicReplication(context);
expect(replicateUnattended).toHaveBeenCalledOnce();
expect(replicateUnattended).toHaveBeenCalledWith({
trigger: "periodic",
interaction: NO_INTERACTION,
});
});
it("keeps database-save synchronisation on the event replication boundary", async () => {
const replicateUnattendedByEvent = vi.fn(async (_request: unknown) => ({ status: "completed" as const }));
const settings = p2pSettings({ syncOnSave: true });
const initialise = createAutomaticReplicationTriggers({
currentSettings: () => settings,
isSuspended: vi.fn(() => false),
replicateDatabaseEvent: () =>
replicateUnattendedByEvent({
trigger: "database-event",
interaction: NO_INTERACTION,
}),
reprocessStoredDocuments: vi.fn(async () => 0),
resumeResultApplication: vi.fn(),
suspendResultApplication: vi.fn(),
});
await initialise();
eventHub.emitEvent(EVENT_FILE_SAVED);
await vi.waitFor(() => expect(replicateUnattendedByEvent).toHaveBeenCalledOnce());
expect(replicateUnattendedByEvent).toHaveBeenCalledWith({
trigger: "database-event",
interaction: NO_INTERACTION,
});
});
it("reprocesses stored documents when normal-file target filters change", async () => {
const settings = {
...DEFAULT_SETTINGS,
ignoreFiles: ".gitignore",
syncOnlyRegEx: "^E2E/allowed/.*",
} as ObsidianLiveSyncSettings;
const reprocessStoredDocuments = vi.fn(async () => 1);
const resumeResultApplication = vi.fn();
const suspendResultApplication = vi.fn();
const initialise = createAutomaticReplicationTriggers({
currentSettings: () => settings,
isSuspended: vi.fn(() => false),
replicateDatabaseEvent: vi.fn(async () => undefined),
reprocessStoredDocuments,
resumeResultApplication,
suspendResultApplication,
});
await initialise();
eventHub.emitEvent(EVENT_SETTING_SAVED, { ...settings });
await Promise.resolve();
expect(reprocessStoredDocuments).not.toHaveBeenCalled();
expect(resumeResultApplication).toHaveBeenCalledOnce();
expect(suspendResultApplication).not.toHaveBeenCalled();
eventHub.emitEvent(EVENT_SETTING_SAVED, { ...settings, suspendParseReplicationResult: true });
expect(suspendResultApplication).toHaveBeenCalledOnce();
Object.assign(settings, { syncOnlyRegEx: "" });
eventHub.emitEvent(EVENT_SETTING_SAVED, { ...settings });
await vi.waitFor(() => expect(reprocessStoredDocuments).toHaveBeenCalledOnce());
settings.syncMaxSizeInMB = 10;
eventHub.emitEvent(EVENT_SETTING_SAVED, { ...settings });
await vi.waitFor(() => expect(reprocessStoredDocuments).toHaveBeenCalledTimes(2));
});
it("keeps editor-save synchronisation on the event replication boundary", async () => {
const { module, replicateUnattendedByEvent, save, saveCommand } = createObsidianEventHarness({
syncOnEditorSave: true,
});
module.swapSaveCommand();
saveCommand.callback();
expect(save).toHaveBeenCalledOnce();
await vi.waitFor(() => expect(replicateUnattendedByEvent).toHaveBeenCalledOnce());
expect(replicateUnattendedByEvent).toHaveBeenCalledWith({
trigger: "editor-save",
interaction: NO_INTERACTION,
});
});
it("keeps file-open synchronisation on the event replication boundary", async () => {
const { module, queueCheckForIfOpen, replicateUnattendedByEvent, services } = createObsidianEventHarness({
syncOnFileOpen: true,
});
const file = { path: "opened.md" } as never;
await module.watchWorkspaceOpenAsync(file);
expect(services.fileProcessing.commitPendingFileEvents).toHaveBeenCalledOnce();
expect(replicateUnattendedByEvent).toHaveBeenCalledOnce();
expect(replicateUnattendedByEvent).toHaveBeenCalledWith({
trigger: "file-open",
interaction: NO_INTERACTION,
});
expect(queueCheckForIfOpen).toHaveBeenCalledWith("opened.md");
});
it("keeps post-merge synchronisation on the event replication boundary", async () => {
const replicateUnattendedByEvent = vi.fn(async () => ({ status: "completed" as const }));
const queueCheckFor = vi.fn(async () => undefined);
const path = "merged.md" as FilePathWithPrefix;
const module = {
settings: p2pSettings({ syncAfterMerge: true }),
services: {
appLifecycle: { isSuspended: vi.fn(() => false) },
conflict: { queueCheckFor },
replication: { replicateUnattendedByEvent },
},
checkConflictAndPerformAutoMerge: vi.fn(async () => AUTO_MERGED),
_log: vi.fn(),
};
await (ModuleConflictResolver.prototype as any)._resolveConflict.call(module, path);
expect(replicateUnattendedByEvent).toHaveBeenCalledOnce();
expect(replicateUnattendedByEvent).toHaveBeenCalledWith({
trigger: "merge",
interaction: NO_INTERACTION,
});
expect(queueCheckFor).toHaveBeenCalledWith(path);
});
});
describe("recurring replication scheduling precedence", () => {
afterEach(() => {
eventHub.offAll();
});
function createRecurringSchedulingHarness() {
let resolveContinuous!: (
outcome: { status: "completed" } | { status: "blocked"; reason: "capability-not-applicable" }
) => void;
const startContinuous = vi.fn(
() =>
new Promise<{ status: "completed" } | { status: "blocked"; reason: "capability-not-applicable" }>(
(resolve) => {
resolveContinuous = resolve;
}
)
);
const API = createApi();
const settings = {
...DEFAULT_SETTINGS,
isConfigured: true,
liveSync: true,
syncOnStart: true,
periodicReplication: true,
periodicReplicationInterval: 60,
};
const context = createReplicationSchedulingContext({
isReady: vi.fn(() => true),
isSuspended: vi.fn(() => false),
currentSettings: vi.fn(() => settings),
startContinuous,
replicateUnattended: vi.fn(async () => ({ status: "completed" as const })),
timer: {
enable: (interval) => {
API.setInterval(vi.fn(), interval);
},
disable: () => {
API.clearInterval(0);
},
},
log: vi.fn(),
});
return {
API,
resolveContinuous: (
outcome: { status: "completed" } | { status: "blocked"; reason: "capability-not-applicable" }
) => resolveContinuous(outcome),
resume: async () => {
resumeReplicationScheduling(context);
await Promise.resolve();
},
realiseSettings: async () => {
realiseReplicationScheduling(context);
await Promise.resolve();
},
};
}
it("does not enable the generic periodic timer while Continuous owns recurring synchronisation", async () => {
const harness = createRecurringSchedulingHarness();
await harness.resume();
await harness.realiseSettings();
expect(harness.API.setInterval).not.toHaveBeenCalled();
harness.resolveContinuous({ status: "completed" });
await vi.waitFor(() => expect(harness.API.setInterval).not.toHaveBeenCalled());
});
it("restores the generic periodic timer when Continuous is not applicable", async () => {
const harness = createRecurringSchedulingHarness();
await harness.resume();
await harness.realiseSettings();
harness.resolveContinuous({ status: "blocked", reason: "capability-not-applicable" });
await vi.waitFor(() => expect(harness.API.setInterval).toHaveBeenCalledOnce());
});
});

Some files were not shown because too many files have changed in this diff Show More