mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-11-07 00:38:19 +00:00
Add Time-bound and Compatible generation to the Obsidian dialogue, browser peer check, and setup tools, with boundary and compatibility coverage. Show the fixed window end before sharing a Time-bound URI. This review branch depends on unpublished @vrtmrz/livesync-commonlib 0.1.32-next.0. The root npm pin and Deno lockfile remain unchanged until that candidate is available; a fresh install of this commit is not yet expected to build.
163 lines
6.0 KiB
TypeScript
163 lines
6.0 KiB
TypeScript
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<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 () => 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<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 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<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 () => 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<boolean>;
|
|
await loadedHandler();
|
|
|
|
expect(registerProtocolHandler).toHaveBeenCalledWith("setuplivesync", expect.any(Function));
|
|
});
|
|
});
|