mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-04-19 23:48:36 +00:00
port setupManager, setupProtocol to serviceFeature
remove styles on webapp UI, and add stylesheet
This commit is contained in:
34
src/serviceFeatures/setupObsidian/setupManagerHandlers.ts
Normal file
34
src/serviceFeatures/setupObsidian/setupManagerHandlers.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { type SetupManager, UserMode } from "@/modules/features/SetupManager";
|
||||
import type { SetupFeatureHost } from "@lib/serviceFeatures/setupObsidian/types";
|
||||
import { EVENT_REQUEST_OPEN_P2P_SETTINGS, EVENT_REQUEST_OPEN_SETUP_URI } from "@lib/events/coreEvents";
|
||||
import { eventHub } from "@lib/hub/hub";
|
||||
import { fireAndForget } from "@lib/common/utils";
|
||||
import type { NecessaryServices } from "@lib/interfaces/ServiceModule";
|
||||
|
||||
export async function openSetupURI(setupManager: SetupManager) {
|
||||
await setupManager.onUseSetupURI(UserMode.Unknown);
|
||||
}
|
||||
|
||||
export async function openP2PSettings(host: SetupFeatureHost, setupManager: SetupManager) {
|
||||
return await setupManager.onP2PManualSetup(UserMode.Update, host.services.setting.currentSettings(), false);
|
||||
}
|
||||
|
||||
export function useSetupManagerHandlersFeature(
|
||||
host: NecessaryServices<"API" | "UI" | "setting" | "appLifecycle", never>,
|
||||
setupManager: SetupManager
|
||||
) {
|
||||
host.services.appLifecycle.onLoaded.addHandler(() => {
|
||||
host.services.API.addCommand({
|
||||
id: "livesync-opensetupuri",
|
||||
name: "Use the copied setup URI (Formerly Open setup URI)",
|
||||
callback: () => fireAndForget(openSetupURI(setupManager)),
|
||||
});
|
||||
|
||||
eventHub.onEvent(EVENT_REQUEST_OPEN_SETUP_URI, () => fireAndForget(() => openSetupURI(setupManager)));
|
||||
eventHub.onEvent(EVENT_REQUEST_OPEN_P2P_SETTINGS, () =>
|
||||
fireAndForget(() => openP2PSettings(host, setupManager))
|
||||
);
|
||||
|
||||
return Promise.resolve(true);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import { describe, expect, it, vi, afterEach } from "vitest";
|
||||
import { eventHub } from "@lib/hub/hub";
|
||||
import { EVENT_REQUEST_OPEN_P2P_SETTINGS, EVENT_REQUEST_OPEN_SETUP_URI } from "@lib/events/coreEvents";
|
||||
import { openP2PSettings, openSetupURI, useSetupManagerHandlersFeature } from "./setupManagerHandlers";
|
||||
|
||||
vi.mock("@/modules/features/SetupManager", () => {
|
||||
return {
|
||||
UserMode: {
|
||||
Unknown: "unknown",
|
||||
Update: "unknown",
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
describe("setupObsidian/setupManagerHandlers", () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("openSetupURI should delegate to SetupManager.onUseSetupURI", async () => {
|
||||
const setupManager = {
|
||||
onUseSetupURI: vi.fn(async () => true),
|
||||
} as any;
|
||||
|
||||
await openSetupURI(setupManager);
|
||||
expect(setupManager.onUseSetupURI).toHaveBeenCalledWith("unknown");
|
||||
});
|
||||
|
||||
it("openP2PSettings should delegate to SetupManager.onP2PManualSetup", async () => {
|
||||
const settings = { x: 1 };
|
||||
const host = {
|
||||
services: {
|
||||
setting: {
|
||||
currentSettings: vi.fn(() => settings),
|
||||
},
|
||||
},
|
||||
} as any;
|
||||
const setupManager = {
|
||||
onP2PManualSetup: vi.fn(async () => true),
|
||||
} as any;
|
||||
|
||||
await openP2PSettings(host, setupManager);
|
||||
expect(setupManager.onP2PManualSetup).toHaveBeenCalledWith("unknown", settings, false);
|
||||
});
|
||||
|
||||
it("useSetupManagerHandlersFeature should register onLoaded handler that wires command and events", async () => {
|
||||
const addHandler = vi.fn();
|
||||
const addCommand = vi.fn();
|
||||
const onEventSpy = vi.spyOn(eventHub, "onEvent");
|
||||
|
||||
const host = {
|
||||
services: {
|
||||
API: {
|
||||
addCommand,
|
||||
},
|
||||
appLifecycle: {
|
||||
onLoaded: {
|
||||
addHandler,
|
||||
},
|
||||
},
|
||||
setting: {
|
||||
currentSettings: vi.fn(() => ({ x: 1 })),
|
||||
},
|
||||
},
|
||||
} as any;
|
||||
const setupManager = {
|
||||
onUseSetupURI: vi.fn(async () => true),
|
||||
onP2PManualSetup: vi.fn(async () => true),
|
||||
} as any;
|
||||
|
||||
useSetupManagerHandlersFeature(host, setupManager);
|
||||
expect(addHandler).toHaveBeenCalledTimes(1);
|
||||
|
||||
const loadedHandler = addHandler.mock.calls[0][0] as () => Promise<boolean>;
|
||||
await loadedHandler();
|
||||
|
||||
expect(addCommand).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
id: "livesync-opensetupuri",
|
||||
name: "Use the copied setup URI (Formerly Open setup URI)",
|
||||
})
|
||||
);
|
||||
expect(onEventSpy).toHaveBeenCalledWith(EVENT_REQUEST_OPEN_SETUP_URI, expect.any(Function));
|
||||
expect(onEventSpy).toHaveBeenCalledWith(EVENT_REQUEST_OPEN_P2P_SETTINGS, expect.any(Function));
|
||||
});
|
||||
});
|
||||
37
src/serviceFeatures/setupObsidian/setupProtocol.ts
Normal file
37
src/serviceFeatures/setupObsidian/setupProtocol.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "@lib/common/types";
|
||||
import type { LogFunction } from "@lib/services/lib/logUtils";
|
||||
import { createInstanceLogFunction } from "@lib/services/lib/logUtils";
|
||||
import type { SetupFeatureHost } from "@lib/serviceFeatures/setupObsidian/types";
|
||||
import { configURIBase } from "@/common/types";
|
||||
import type { NecessaryServices } from "@lib/interfaces/ServiceModule";
|
||||
import { type SetupManager, UserMode } from "@/modules/features/SetupManager";
|
||||
|
||||
async function handleSetupProtocol(setupManager: SetupManager, conf: Record<string, string>) {
|
||||
if (conf.settings) {
|
||||
await setupManager.onUseSetupURI(UserMode.Unknown, `${configURIBase}${encodeURIComponent(conf.settings)}`);
|
||||
} else if (conf.settingsQR) {
|
||||
await setupManager.decodeQR(conf.settingsQR);
|
||||
}
|
||||
}
|
||||
|
||||
export function registerSetupProtocolHandler(host: SetupFeatureHost, log: LogFunction, setupManager: SetupManager) {
|
||||
try {
|
||||
host.services.API.registerProtocolHandler("setuplivesync", async (conf) => {
|
||||
await handleSetupProtocol(setupManager, conf);
|
||||
});
|
||||
} catch (e) {
|
||||
log("Failed to register protocol handler. This feature may not work in some environments.", LOG_LEVEL_NOTICE);
|
||||
log(e, LOG_LEVEL_VERBOSE);
|
||||
}
|
||||
}
|
||||
|
||||
export function useSetupProtocolFeature(
|
||||
host: NecessaryServices<"API" | "UI" | "setting" | "appLifecycle", never>,
|
||||
setupManager: SetupManager
|
||||
) {
|
||||
const log = createInstanceLogFunction("SF:SetupProtocol", host.services.API);
|
||||
host.services.appLifecycle.onLoaded.addHandler(() => {
|
||||
registerSetupProtocolHandler(host, log, setupManager);
|
||||
return Promise.resolve(true);
|
||||
});
|
||||
}
|
||||
131
src/serviceFeatures/setupObsidian/setupProtocol.unit.spec.ts
Normal file
131
src/serviceFeatures/setupObsidian/setupProtocol.unit.spec.ts
Normal file
@@ -0,0 +1,131 @@
|
||||
import { describe, expect, it, vi, afterEach } from "vitest";
|
||||
import { registerSetupProtocolHandler, useSetupProtocolFeature } from "./setupProtocol";
|
||||
|
||||
vi.mock("@/common/types", () => {
|
||||
return {
|
||||
configURIBase: "mock-config://",
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@/modules/features/SetupManager", () => {
|
||||
return {
|
||||
UserMode: {
|
||||
Unknown: "unknown",
|
||||
Update: "unknown",
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
describe("setupObsidian/setupProtocol", () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("registerSetupProtocolHandler should route settings payload to onUseSetupURI", async () => {
|
||||
let protocolHandler: ((params: Record<string, string>) => Promise<void>) | undefined;
|
||||
const host = {
|
||||
services: {
|
||||
API: {
|
||||
registerProtocolHandler: vi.fn(
|
||||
(_action: string, handler: (params: Record<string, string>) => Promise<void>) => {
|
||||
protocolHandler = handler;
|
||||
}
|
||||
),
|
||||
},
|
||||
},
|
||||
} as any;
|
||||
const log = vi.fn();
|
||||
const setupManager = {
|
||||
onUseSetupURI: vi.fn(async () => true),
|
||||
decodeQR: vi.fn(async () => true),
|
||||
} as any;
|
||||
|
||||
registerSetupProtocolHandler(host, log, setupManager);
|
||||
expect(host.services.API.registerProtocolHandler).toHaveBeenCalledWith("setuplivesync", expect.any(Function));
|
||||
|
||||
await protocolHandler!({ settings: "a b" });
|
||||
expect(setupManager.onUseSetupURI).toHaveBeenCalledWith(
|
||||
"unknown",
|
||||
`mock-config://${encodeURIComponent("a b")}`
|
||||
);
|
||||
expect(setupManager.decodeQR).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("registerSetupProtocolHandler should route settingsQR payload to decodeQR", async () => {
|
||||
let protocolHandler: ((params: Record<string, string>) => Promise<void>) | undefined;
|
||||
const host = {
|
||||
services: {
|
||||
API: {
|
||||
registerProtocolHandler: vi.fn(
|
||||
(_action: string, handler: (params: Record<string, string>) => Promise<void>) => {
|
||||
protocolHandler = handler;
|
||||
}
|
||||
),
|
||||
},
|
||||
},
|
||||
} as any;
|
||||
const log = vi.fn();
|
||||
const setupManager = {
|
||||
onUseSetupURI: vi.fn(async () => true),
|
||||
decodeQR: vi.fn(async () => true),
|
||||
} as any;
|
||||
|
||||
registerSetupProtocolHandler(host, log, setupManager);
|
||||
await protocolHandler!({ settingsQR: "qr-data" });
|
||||
|
||||
expect(setupManager.decodeQR).toHaveBeenCalledWith("qr-data");
|
||||
expect(setupManager.onUseSetupURI).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("registerSetupProtocolHandler should log and continue when registration throws", () => {
|
||||
const host = {
|
||||
services: {
|
||||
API: {
|
||||
registerProtocolHandler: vi.fn(() => {
|
||||
throw new Error("register failed");
|
||||
}),
|
||||
},
|
||||
},
|
||||
} as any;
|
||||
const log = vi.fn();
|
||||
const setupManager = {
|
||||
onUseSetupURI: vi.fn(),
|
||||
decodeQR: vi.fn(),
|
||||
} as any;
|
||||
|
||||
registerSetupProtocolHandler(host, log, setupManager);
|
||||
|
||||
expect(log).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("useSetupProtocolFeature should register onLoaded handler", async () => {
|
||||
const addHandler = vi.fn();
|
||||
const registerProtocolHandler = vi.fn();
|
||||
const host = {
|
||||
services: {
|
||||
API: {
|
||||
addLog: vi.fn(),
|
||||
registerProtocolHandler,
|
||||
},
|
||||
appLifecycle: {
|
||||
onLoaded: {
|
||||
addHandler,
|
||||
},
|
||||
},
|
||||
},
|
||||
} as any;
|
||||
const setupManager = {
|
||||
onUseSetupURI: vi.fn(),
|
||||
decodeQR: vi.fn(),
|
||||
} as any;
|
||||
|
||||
useSetupProtocolFeature(host, setupManager);
|
||||
expect(addHandler).toHaveBeenCalledTimes(1);
|
||||
|
||||
const loadedHandler = addHandler.mock.calls[0][0] as () => Promise<boolean>;
|
||||
await loadedHandler();
|
||||
|
||||
expect(registerProtocolHandler).toHaveBeenCalledWith("setuplivesync", expect.any(Function));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user