Protect bounded remote activity across app lifecycle

This commit is contained in:
vorotamoroz
2026-07-15 20:53:27 +09:00
parent 4cbccf85b4
commit 31e9186930
373 changed files with 1275 additions and 466 deletions
@@ -1,4 +1,16 @@
import { describe, expect, it, vi } from "vitest";
const chunkMocks = vi.hoisted(() => ({
purgeUnreferencedChunks: vi.fn(async (_db: unknown, countOnly: boolean) => (countOnly ? 2 : 0)),
balanceChunkPurgedDBs: vi.fn(async () => undefined),
}));
vi.mock("@lib/pouchdb/chunks", () => chunkMocks);
vi.mock("@lib/replication/couchdb/LiveSyncReplicator", () => ({
LiveSyncCouchDBReplicator: class {},
}));
import { LiveSyncCouchDBReplicator } from "@lib/replication/couchdb/LiveSyncReplicator";
import { ModuleReplicator } from "./ModuleReplicator";
describe("ModuleReplicator", () => {
@@ -46,3 +58,61 @@ describe("ModuleReplicator", () => {
expect(ensurePBKDF2Salt).toHaveBeenCalledWith({}, false, false);
});
});
describe("ModuleReplicator legacy cleanup", () => {
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 openReplication = vi.fn(async () => true);
const activeReplicator = Object.assign(new LiveSyncCouchDBReplicator({} as any), {
connectRemoteCouchDBWithSetting: vi.fn(async () => ({ db: {} })),
markRemoteResolved: vi.fn(async () => undefined),
});
const services = {
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,
},
};
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(openReplication).toHaveBeenCalledOnce();
expect(openReplication.mock.invocationCallOrder[0]).toBeLessThan(activityFinished.mock.invocationCallOrder[0]);
expect(chunkMocks.balanceChunkPurgedDBs).toHaveBeenCalledOnce();
});
});