Lutra/ui/tests/commands/modals.test.ts
danny 9407d5d6c2 H2: warm up first editor and modals outside the test budget
The first CodeMirror view and the first render of the editor area,
the palette and the settings dialog in a jsdom worker cost 0.3-0.6 s
once. The first test of EditorArea, language and modals paid it inside
its own 5 s budget and timed out under load. A beforeAll warm-up now
pays it once; no timeout changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 13:30:57 +01:00

210 lines
7.6 KiB
TypeScript

/**
* Tests of the modal rule (M1.15, `commands/modals.ts`): the palette, the
* settings dialog and the close question never stack, running a command
* closes the palette also when it comes as a `uiCommand`, and the restore
* dialog waits behind the other modals with its list intact. The engine is
* a fake behind the mocked Tauri `invoke` and `listen`.
*/
import { flushPromises, mount, type VueWrapper } from "@vue/test-utils";
import { createPinia, setActivePinia, type Pinia } from "pinia";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { defineComponent, h } from "vue";
const invoke = vi.hoisted(() => vi.fn());
const listen = vi.hoisted(() => vi.fn());
vi.mock("@tauri-apps/api/core", () => ({ invoke }));
vi.mock("@tauri-apps/api/event", () => ({ listen }));
import { useModalsStore } from "../../src/commands/modals";
import { useCommandsStore } from "../../src/commands/store";
import CommandPalette from "../../src/components/palette/CommandPalette.vue";
import RecoveryDialog from "../../src/components/recovery/RecoveryDialog.vue";
import { useRecoveryStore } from "../../src/components/recovery/recovery";
import SettingsDialog from "../../src/components/settings/SettingsDialog.vue";
import { useEditorStore } from "../../src/stores/editor";
/** The engine event callbacks, one per subscription. */
let listeners: ((payload: unknown) => void)[];
/** Pinia of the current test. */
let pinia: Pinia;
/** Mounted components, unmounted after each test. */
let mounted: VueWrapper[];
/**
* Sends an engine event to every subscriber.
*
* @param payload - the event.
*/
async function emit(payload: unknown): Promise<void> {
for (const listener of listeners) {
listener(payload);
}
await flushPromises();
}
/**
* Runs a UI command the way the control channel does.
*
* @param id - the command id.
*/
async function uiCommand(id: string): Promise<void> {
await emit({ kind: "uiCommand", id, params: {} });
}
/** A focusable button standing in for the editor. */
const Host = defineComponent({
setup: () => () => h("button", { id: "host-button" }, "host"),
});
/** Mounts the editor stand-in and the modals the app mounts, and starts the commands. */
async function mountApp(): Promise<VueWrapper> {
const host = mount(Host, { attachTo: document.body });
const recovery = mount(RecoveryDialog, { attachTo: document.body, global: { plugins: [pinia] } });
const palette = mount(CommandPalette, { attachTo: document.body, global: { plugins: [pinia] } });
const settings = mount(SettingsDialog, { attachTo: document.body, global: { plugins: [pinia] } });
mounted.push(host, recovery, palette, settings);
await useCommandsStore(pinia).start();
await flushPromises();
document.getElementById("host-button")?.focus();
return host;
}
/** Whether the palette is shown. */
function paletteShown(): boolean {
return document.querySelector("[data-test=palette]") !== null;
}
/** Whether the settings dialog is shown. */
function settingsShown(): boolean {
return document.querySelector("[data-test=settings]") !== null;
}
/** Whether the restore dialog is shown. */
function recoveryShown(): boolean {
return document.querySelector("[data-test=recovery-dialog]") !== null;
}
/** Fresh Pinia and a fake engine without commands or settings. */
function resetFakeEngine(): void {
pinia = createPinia();
setActivePinia(pinia);
listeners = [];
mounted = [];
listen.mockReset();
listen.mockImplementation((_name: string, callback: (event: { payload: unknown }) => void) => {
listeners.push((payload) => {
callback({ payload });
});
return Promise.resolve(() => undefined);
});
invoke.mockReset();
invoke.mockImplementation((cmd: string, args?: { command: string }) => {
if (cmd === "engine_list_commands") {
return Promise.resolve([]);
}
if (cmd === "engine_execute" && args?.command === "settings.list") {
return Promise.resolve([]);
}
if (cmd === "engine_execute" && args?.command === "settings.get") {
return Promise.resolve({ values: {} });
}
return Promise.resolve(null);
});
}
/** Stops the commands and unmounts every mounted component. */
function unmountAll(): void {
useCommandsStore(pinia).stop();
for (const wrapper of mounted) {
wrapper.unmount();
}
}
/**
* Pays the one-time cost of the first modals outside any test: mounts the
* app's modals, opens the palette and then the settings dialog, and
* unmounts again. The first render of the palette and the settings dialog
* in a jsdom worker runs cold (about 0.3 s on an idle machine, against
* 50-100 ms later). Without this, the first test paid that cost inside its
* own 5 s budget and timed out under load.
*/
async function warmUpModals(): Promise<void> {
resetFakeEngine();
await mountApp();
await uiCommand("palette.open");
await uiCommand("settings.open");
unmountAll();
}
beforeAll(warmUpModals);
beforeEach(resetFakeEngine);
afterEach(unmountAll);
describe("one modal at a time", () => {
it("settings.open from the control channel closes the open palette", async () => {
await mountApp();
await uiCommand("palette.open");
expect(paletteShown()).toBe(true);
await uiCommand("settings.open");
expect(paletteShown()).toBe(false);
expect(settingsShown()).toBe(true);
expect(useModalsStore(pinia).current).toBe("settings");
});
it("any command run while the palette is open closes it first", async () => {
await mountApp();
const toggled = vi.fn();
useCommandsStore(pinia).provide("view.toggleProjectTree", { run: toggled });
await uiCommand("palette.open");
await uiCommand("view.toggleProjectTree");
expect(toggled).toHaveBeenCalledOnce();
expect(paletteShown()).toBe(false);
expect(useModalsStore(pinia).current).toBeNull();
expect(document.activeElement?.id).toBe("host-button");
});
it("opening the palette closes the settings dialog", async () => {
await mountApp();
await uiCommand("settings.open");
await uiCommand("palette.open");
expect(settingsShown()).toBe(false);
expect(paletteShown()).toBe(true);
expect(useModalsStore(pinia).current).toBe("palette");
await uiCommand("palette.open");
expect(paletteShown()).toBe(true);
});
it("the close question closes the palette, and the palette cancels the question", async () => {
await mountApp();
const editor = useEditorStore(pinia);
await uiCommand("palette.open");
editor.closePrompt = { id: 1, name: "a.php" };
await flushPromises();
expect(paletteShown()).toBe(false);
expect(useModalsStore(pinia).current).toBe("closePrompt");
await uiCommand("palette.open");
expect(editor.closePrompt).toBeNull();
expect(paletteShown()).toBe(true);
});
it("the restore dialog waits behind the other modals with its list intact", async () => {
await mountApp();
const entry = { path: "src/a.php", savedAt: 1, diskChanged: false };
await uiCommand("palette.open");
await emit({ kind: "recoveryAvailable", entries: [entry] });
expect(recoveryShown()).toBe(false);
expect(paletteShown()).toBe(true);
await uiCommand("settings.open");
expect(recoveryShown()).toBe(false);
document.querySelector<HTMLElement>("[data-test=settings-dialog]")?.dispatchEvent(
new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true }),
);
await flushPromises();
expect(settingsShown()).toBe(false);
expect(recoveryShown()).toBe(true);
expect(useRecoveryStore(pinia).entries).toEqual([entry]);
expect(document.activeElement).toBe(document.querySelector("[data-test=recovery-restore-all]"));
});
});