M1.15: one modal at a time, restore dialog follows outside restores

- Modal rule (ui/src/commands/modals.ts): palette, settings dialog and the
  close question are exclusive; opening one closes the other. Running any
  command but palette.open (keyboard, palette, uiCommand) closes the
  palette. The restore dialog is queued: hidden while another modal is
  open, back with its list intact afterwards.
- recovery.restore / recovery.discard emit recoveryResolved {paths}; the
  restore dialog drops the settled paths and closes when none are left.
  Rust was needed: discard gave the UI no signal, and a restored entry
  stays in the journal as the buffer's own entry.
- Dimmed restored text (C2 screenshot 09) was the dialog overlay, not an
  editor style; no editor change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-16 21:49:38 +02:00
parent d12a517009
commit 1a38e68480
13 changed files with 442 additions and 12 deletions

View file

@ -0,0 +1,186 @@
/**
* 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, 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;
}
beforeEach(() => {
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);
});
});
afterEach(() => {
useCommandsStore(pinia).stop();
for (const wrapper of mounted) {
wrapper.unmount();
}
});
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]"));
});
});