Adopt focused P2P service views

This commit is contained in:
vorotamoroz
2026-08-28 03:04:53 +00:00
parent cc000f2cdf
commit ad83ae858d
20 changed files with 276 additions and 112 deletions
@@ -0,0 +1,57 @@
import type { ObsidianLiveSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { beforeEach, describe, expect, it, vi } from "vitest";
const runtimeMocks = vi.hoisted(() => {
const connect = vi.fn().mockResolvedValue(undefined);
const makeSureOpened = vi.fn().mockResolvedValue(undefined);
const start = vi.fn().mockResolvedValue(undefined);
const shutdown = vi.fn().mockResolvedValue(undefined);
const removeStatusListener = vi.fn();
const onEvent = vi.fn(() => removeStatusListener);
const WebPeerRuntime = vi.fn(function () {
return {
events: { onEvent },
p2p: {
transportLifecycle: { connect },
},
currentReplicator: { makeSureOpened },
start,
shutdown,
};
});
return {
WebPeerRuntime,
connect,
makeSureOpened,
onEvent,
removeStatusListener,
shutdown,
start,
};
});
vi.mock("@/apps/webpeer/src/WebPeerRuntime", () => ({
WebPeerRuntime: runtimeMocks.WebPeerRuntime,
}));
import { P2PCheckSession } from "@/apps/webpeer/src/P2PCheckSession";
describe("P2P connection-check session", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("opens an explicit check through the P2P transport lifecycle", async () => {
const session = new P2PCheckSession();
await session.start({} as ObsidianLiveSyncSettings, "browser-check", vi.fn());
expect(runtimeMocks.start).toHaveBeenCalledOnce();
expect(runtimeMocks.connect).toHaveBeenCalledOnce();
expect(runtimeMocks.makeSureOpened).not.toHaveBeenCalled();
await session.stop();
expect(runtimeMocks.shutdown).toHaveBeenCalledOnce();
});
});
+29 -1
View File
@@ -34,7 +34,7 @@ describe("WebPeer runtime composition", () => {
expect(runtime.context).toBe(context);
expect(runtime.services.context).toBe(context);
expect(runtime.events).toBe(context.events);
expect(runtime.currentReplicator).toBe(runtime.p2p.replicator);
expect(runtime.paneHost.p2p.transportLifecycle).toBe(runtime.p2p.transportLifecycle);
expect(runtime.paneHost.services).toBe(runtime.services);
expect(runtime.paneHost.p2p).toBe(runtime.p2p);
expect(runtime.paneHost.showPeerMenu).toBeTypeOf("function");
@@ -64,6 +64,34 @@ describe("WebPeer runtime composition", () => {
expect(layoutReady).toHaveBeenCalledOnce();
});
it("delegates automatic P2P startup to the resumed lifecycle handler", async () => {
vi.useFakeTimers();
try {
const runtime = new WebPeerRuntime({
store: createMemoryStore(),
});
vi.spyOn(runtime.services.setting, "loadSettings").mockResolvedValue(undefined);
vi.spyOn(runtime.services.setting, "currentSettings").mockReturnValue({
...DEFAULT_SETTINGS,
P2P_AutoStart: true,
P2P_Enabled: true,
});
vi.spyOn(runtime.services.database, "openDatabase").mockResolvedValue(true);
const onResumed = vi
.spyOn(runtime.services.appLifecycle, "onResumed")
.mockResolvedValue(true);
const open = vi.spyOn(runtime.p2p.transportLifecycle, "connect").mockResolvedValue(undefined);
await runtime.start();
expect(onResumed).toHaveBeenCalledOnce();
await vi.advanceTimersByTimeAsync(100);
expect(open).not.toHaveBeenCalled();
} finally {
vi.useRealTimers();
}
});
it("rejects start when the browser-local database cannot be opened", async () => {
const runtime = new WebPeerRuntime({
store: createMemoryStore(),