import { describe, expect, it, vi, afterEach } from "vitest"; import { registerSetupProtocolHandler, useSetupProtocolFeature } from "./setupProtocol"; import { encodeTimeBoundSetupURI } from "@vrtmrz/livesync-commonlib/setup-uri"; import { DEFAULT_SETTINGS } from "@vrtmrz/livesync-commonlib/compat/common/types"; 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) => Promise) | undefined; const host = { services: { API: { registerProtocolHandler: vi.fn( (_action: string, handler: (params: Record) => Promise) => { protocolHandler = handler; } ), }, }, } as any; const log = vi.fn(); const setupManager = { onUseSetupURI: vi.fn(async () => await Promise.resolve(true)), decodeQR: vi.fn(async () => await Promise.resolve(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.each(["ephemeral", "persistent"] as const)( "reconstructs the exact encrypted %s URI after protocol query decoding", async (mode) => { let protocolHandler: ((params: Record) => Promise) | undefined; const host = { services: { API: { registerProtocolHandler: vi.fn( (_action: string, handler: (params: Record) => Promise) => { protocolHandler = handler; } ), }, }, } as any; const setupManager = { onUseSetupURI: vi.fn(async () => true), decodeQR: vi.fn() } as any; registerSetupProtocolHandler(host, vi.fn(), setupManager); const generated = await encodeTimeBoundSetupURI(DEFAULT_SETTINGS, "protocol-passphrase", { mode }); const decodedQuery = new URL(generated.uri.trim()).searchParams.get("settings"); expect(decodedQuery?.startsWith("%$")).toBe(true); await protocolHandler!({ settings: decodedQuery! }); const reconstructed = setupManager.onUseSetupURI.mock.calls[0][1] as string; expect(reconstructed.replace("mock-config://", "obsidian://setuplivesync?settings=")).toBe( generated.uri.trim() ); } ); it("registerSetupProtocolHandler should route settingsQR payload to decodeQR", async () => { let protocolHandler: ((params: Record) => Promise) | undefined; const host = { services: { API: { registerProtocolHandler: vi.fn( (_action: string, handler: (params: Record) => Promise) => { protocolHandler = handler; } ), }, }, } as any; const log = vi.fn(); const setupManager = { onUseSetupURI: vi.fn(async () => await Promise.resolve(true)), decodeQR: vi.fn(async () => await Promise.resolve(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; await loadedHandler(); expect(registerProtocolHandler).toHaveBeenCalledWith("setuplivesync", expect.any(Function)); }); });