Files
yaak-mountain-loop/apps/yaak-proxy/lib/windowSync.test.ts
Gregory SchierandClaude Opus 5.5 f7402f5f4f Open Yaak Proxy's Settings in its own window
Settings opens in a window labelled `settings`, created through the shared
window code, as Yaak's does. The page renders the settings alone or the app
by its window label. Opening it again focuses it and moves it to the tab
asked for; Escape closes it, and closing the main window closes it too.
Changes made there reach the main window at once: theme and sizes by an
event carrying the values, and engine settings by an event after each RPC
mutation that makes other windows read the proxy state and settings
queries again. The browser harness, with no second window, keeps the
dialog.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 11:34:34 -07:00

78 lines
2.7 KiB
TypeScript

import { QueryClient } from "@tanstack/react-query";
import { createStore } from "jotai";
import { beforeEach, describe, expect, test, vi } from "vite-plus/test";
const tauri = vi.hoisted(() => ({
handlers: new Map<string, (payload: unknown) => void>(),
sent: [] as { event: string; payload: unknown }[],
label: "main_0",
}));
vi.mock("./tauri", () => ({
currentWindowLabel: () => tauri.label,
broadcast: async (event: string, payload: unknown) => {
tauri.sent.push({ event, payload });
},
subscribe: (event: string, handler: (payload: unknown) => void) => {
tauri.handlers.set(event, handler);
return () => tauri.handlers.delete(event);
},
}));
// The store's module reaches for the window as it loads; reading the state again is all this needs
const refreshProxyState = vi.hoisted(() => vi.fn<() => Promise<void>>());
vi.mock("./store", () => ({ refreshProxyState }));
const { announceEngineChange, isSharedStateQuery, syncFromOtherWindows } =
await import("./windowSync");
describe("windowSync", () => {
beforeEach(() => {
tauri.handlers.clear();
tauri.sent = [];
tauri.label = "main_0";
refreshProxyState.mockReset().mockResolvedValue(undefined);
});
test("a change is announced as coming from this window", () => {
tauri.label = "settings";
announceEngineChange();
expect(tauri.sent).toEqual([{ event: "window_changed_engine", payload: { from: "settings" } }]);
});
test("a change in another window reads the state and the settings' queries again", () => {
const store = createStore();
const queryClient = new QueryClient();
const invalidate = vi.spyOn(queryClient, "invalidateQueries");
syncFromOtherWindows(store, queryClient);
tauri.handlers.get("window_changed_engine")?.({ from: "settings" });
expect(refreshProxyState).toHaveBeenCalledWith(store);
expect(invalidate).toHaveBeenCalledTimes(1);
});
test("a window's own change is not read again, as it has been already", () => {
const queryClient = new QueryClient();
const invalidate = vi.spyOn(queryClient, "invalidateQueries");
syncFromOtherWindows(createStore(), queryClient);
tauri.handlers.get("window_changed_engine")?.({ from: "main_0" });
expect(refreshProxyState).not.toHaveBeenCalled();
expect(invalidate).not.toHaveBeenCalled();
});
test("only what settings and setup show is read again, not flows or their bodies", () => {
for (const cmd of [
"system_proxy_status",
"ca_status",
"get_storage_size",
"get_decrypt_list",
]) {
expect(isSharedStateQuery([cmd, {}])).toBe(true);
}
for (const cmd of ["get_flow_body", "list_flows", "query_schema"]) {
expect(isSharedStateQuery([cmd, {}])).toBe(false);
}
});
});