Protect bounded remote activity across app lifecycle

This commit is contained in:
vorotamoroz
2026-07-15 07:53:16 +00:00
parent 4cbccf85b4
commit 31e9186930
373 changed files with 1275 additions and 466 deletions
@@ -0,0 +1,89 @@
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 runBoundedRemoteActivity = 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: {
runBoundedRemoteActivity,
},
setting: {
saveSettingData: vi.fn(async () => undefined),
},
};
const core = {
_services: services,
services,
settings: {
remoteType: "",
...settings,
},
replicator: { openReplication },
} as any;
return {
module: new ModuleReplicatorCouchDB(core),
openReplication,
runBoundedRemoteActivity,
};
}
describe("ModuleReplicatorCouchDB resume replication activity", () => {
it("runs start-up one-shot replication through bounded remote activity", async () => {
const { module, openReplication, runBoundedRemoteActivity } = createModule({
liveSync: false,
syncOnStart: true,
});
await module._everyAfterResumeProcess();
await vi.waitFor(() => expect(openReplication).toHaveBeenCalledOnce());
expect(runBoundedRemoteActivity).toHaveBeenCalledWith(expect.any(Function), {
label: "replication",
});
expect(openReplication).toHaveBeenCalledWith(expect.any(Object), false, false, false);
});
it("does not treat continuous replication as bounded activity", async () => {
const { module, openReplication, runBoundedRemoteActivity } = createModule({
liveSync: true,
syncOnStart: false,
});
await module._everyAfterResumeProcess();
await vi.waitFor(() => expect(openReplication).toHaveBeenCalledOnce());
expect(runBoundedRemoteActivity).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, runBoundedRemoteActivity } = createModule(
{
liveSync: false,
syncOnStart: true,
},
false
);
await module._everyAfterResumeProcess();
await new Promise((resolve) => setTimeout(resolve, 0));
expect(runBoundedRemoteActivity).not.toHaveBeenCalled();
expect(openReplication).not.toHaveBeenCalled();
});
});