mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-08-29 23:07:08 +00:00
Adopt focused P2P service views
This commit is contained in:
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user