import { afterEach, expect, it, vi } from "vitest"; import type { App as ObsidianApp, PluginManifest } from "obsidian"; import BasaltPlugin from "./main.ts"; import { App, notices, resetStub } from "./stub.ts"; import { TestServer } from "../core/test-server.ts"; import { nextTurn, within } from "../core/test-async.ts"; let server: TestServer | undefined; let plugin: BasaltPlugin | undefined; afterEach(async () => { plugin?.onunload(); await plugin?.closing; await server?.cleanup(); vi.restoreAllMocks(); }); async function stateIs(kind: string) { if (plugin!.currentState.kind === kind) return; await within( new Promise((resolve) => { const stop = plugin!.watchState((state) => { if (state.kind === kind) { stop(); resolve(); } }); }), kind, 15000, ); } it("a retired reconnect loop must not erase the replacement loop's wake handle", async () => { resetStub(); vi.spyOn(console, "info").mockImplementation(() => undefined); server = new TestServer(); await server.start(); const app = new App(); plugin = new BasaltPlugin( app as unknown as ObsidianApp, { id: "basalt-review", dir: ".obsidian/plugins/basalt-review", } as PluginManifest, ); await plugin.onload(); await plugin.pairFirst(server.setup, "review-device"); await stateIs("synced"); const state = plugin as unknown as { wakeLoop?: () => void }; const oldWake = state.wakeLoop!; await server.stop(); await stateIs("offline"); await server.start(); await plugin.changeServerAddress(server.wsUrl); await stateIs("synced"); const newWake = state.wakeLoop!; expect(typeof newWake).toBe("function"); expect(newWake).not.toBe(oldWake); // Expire only the retired run's real backoff without waiting for its timer. oldWake(); await nextTurn(); try { const port = server.port; await server.stop(); await stateIs("offline"); await server.start(port); notices.length = 0; await plugin.syncNow(); expect(notices.some(({ message }) => message === "Basalt: reconnecting…")).toBe(true); expect(state.wakeLoop).toBe(newWake); await stateIs("synced"); } finally { newWake(); } });