/** * Tests of the stash feature (M4.9): reading an untrusted `git.stashList`, * the dialog's form (message, untracked flag, `flushDirty` first, a failed * save, git's refusal kept in the dialog), the list (entries, keyboard, * apply, pop with a conflict, drop after the question, Escape), the UI * commands `vcs.stash` and `vcs.showStashes` and the Changes window's * buttons. 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"; 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 { UI_COMMANDS } from "../../src/commands/uiCommands"; import ChangesToolWindow from "../../src/components/vcs/changes/ChangesToolWindow.vue"; import { useChangesStore } from "../../src/components/vcs/changes/changes"; import StashDialog from "../../src/components/vcs/stash/StashDialog.vue"; import { MAX_STASHES, toStash, toStashes, useStashStore } from "../../src/components/vcs/stash/stash"; import { useNotificationsStore } from "../../src/stores/notifications"; /** A recorded engine call. */ interface Call { /** Command id. */ command: string; /** Its params. */ params: Record; } /** Every engine call, in order. */ let calls: Call[]; /** What `git.stashList` answers. */ let stashes: unknown[]; /** Errors to throw, by command id. */ let failures: Record; /** What `file.saveAll` answers. */ let saveAll: unknown; /** Pinia of the current test. */ let pinia: Pinia; /** Mounted components, unmounted after each test. */ let mounted: VueWrapper[]; /** Two entries as the engine sends them. */ const ENTRIES = [ { index: 0, hash: "aaaa1111", branch: "main", message: "41aa1cf init", wip: true, date: "2026-10-28T18:08:36+01:00" }, { index: 1, hash: "bbbb2222", branch: "feature/x", message: "half done", wip: false, date: "2026-10-27T10:00:00Z" }, ]; /** * The calls of a command. * * @param command - the command id. */ function callsOf(command: string): Call[] { return calls.filter((c) => c.command === command); } /** Mounts the dialog with the command store started. */ async function mountDialog(): Promise { const wrapper = mount(StashDialog, { attachTo: document.body, global: { plugins: [pinia] } }); mounted.push(wrapper); await useCommandsStore(pinia).start(); await flushPromises(); return wrapper; } /** * The texts of the list's rows. * * @param wrapper - the dialog. */ function rows(wrapper: VueWrapper): string[] { return wrapper.findAll("[data-test=stash-row]").map((row) => row.text()); } beforeEach(() => { pinia = createPinia(); setActivePinia(pinia); calls = []; mounted = []; failures = {}; stashes = ENTRIES.map((e) => ({ ...e })); saveAll = { saved: [], failed: [], skipped: [] }; Element.prototype.scrollIntoView = () => undefined; invoke.mockReset(); listen.mockReset(); listen.mockImplementation(() => Promise.resolve(() => undefined)); invoke.mockImplementation((cmd: string, args?: { command: string; params: Record }) => { if (cmd === "engine_list_commands") { return Promise.resolve([ { id: "git.status", label: "Git Status", enabled: true }, { id: "git.stash", label: "Git Stash", enabled: true }, ]); } if ((cmd !== "engine_execute" && cmd !== "engine_execute_waiting") || !args) { return Promise.resolve(null); } calls.push({ command: args.command, params: args.params }); const failure = failures[args.command]; if (failure) { return Promise.reject(Object.assign(new Error(failure.message), failure)); } switch (args.command) { case "git.status": return Promise.resolve({ available: true, repo: { root: "C:/p", branch: "main", detached: false }, files: [] }); case "git.stashList": return Promise.resolve({ stashes }); case "git.stash": return Promise.resolve({ entry: { ...ENTRIES[1], index: 0, message: typeof args.params.message === "string" ? args.params.message : "" } }); case "file.saveAll": return Promise.resolve(saveAll); default: return Promise.resolve({}); } }); }); afterEach(() => { for (const wrapper of mounted) { wrapper.unmount(); } useChangesStore(pinia).stop(); document.body.innerHTML = ""; }); describe("reading git.stashList", () => { it("drops malformed entries, sorts by index and caps the count", () => { expect( toStashes({ stashes: [ { index: 1, hash: "b", branch: "", message: 5, wip: "yes" }, { index: 0, hash: "a", branch: "main", message: "m", wip: true, date: "d" }, { index: -1, hash: "x" }, { index: 2.5, hash: "y" }, { index: 3, hash: "" }, null, "z", ], }), ).toEqual([ { index: 0, hash: "a", branch: "main", message: "m", wip: true, date: "d" }, { index: 1, hash: "b", branch: null, message: "", wip: false, date: "" }, ]); expect(toStashes(null)).toBeNull(); expect(toStashes({ stashes: "no" })).toBeNull(); expect(toStash([1])).toBeNull(); const many = Array.from({ length: MAX_STASHES + 5 }, (_, i) => ({ index: i, hash: `h${String(i)}` })); expect(toStashes({ stashes: many })).toHaveLength(MAX_STASHES); }); }); describe("the stash form", () => { it("saves first, then stashes with the message and the untracked flag", async () => { const wrapper = await mountDialog(); await useCommandsStore(pinia).execute("vcs.stash"); await flushPromises(); expect(useModalsStore(pinia).current).toBe("stash"); expect(document.activeElement).toBe(wrapper.get("[data-test=stash-message]").element); await wrapper.get("[data-test=stash-message]").setValue("half done"); await wrapper.get("[data-test=stash-untracked]").setValue(true); await wrapper.get("[data-test=stash-form]").trigger("submit"); await flushPromises(); const order = calls.map((c) => c.command).filter((c) => c === "file.saveAll" || c === "git.stash"); expect(order).toEqual(["file.saveAll", "git.stash"]); expect(callsOf("git.stash")[0]?.params).toEqual({ message: "half done", includeUntracked: true }); expect(wrapper.find("[data-test=stash]").exists()).toBe(false); const toast = useNotificationsStore(pinia).toasts.at(-1); expect(toast?.text).toContain("half done"); expect(useModalsStore(pinia).current).toBeNull(); }); it("leaves out a blank message", async () => { const wrapper = await mountDialog(); await useStashStore(pinia).show("stash"); await flushPromises(); await wrapper.get("[data-test=stash-message]").setValue(" "); await wrapper.get("[data-test=stash-submit]").trigger("click"); await flushPromises(); expect(callsOf("git.stash")[0]?.params).toEqual({ includeUntracked: false }); }); it("keeps git's refusal in the dialog", async () => { failures["git.stash"] = { code: "failed", message: "There are no local changes to stash." }; const wrapper = await mountDialog(); await useStashStore(pinia).show("stash"); await flushPromises(); await wrapper.get("[data-test=stash-form]").trigger("submit"); await flushPromises(); expect(wrapper.get("[data-test=stash-error]").text()).toBe("There are no local changes to stash."); expect(wrapper.find("[data-test=stash]").exists()).toBe(true); }); it("cancels when a save fails", async () => { saveAll = { saved: [], failed: [{ id: 1, path: "a.php", message: "denied" }], skipped: [] }; const wrapper = await mountDialog(); await useStashStore(pinia).show("stash"); await flushPromises(); await wrapper.get("[data-test=stash-form]").trigger("submit"); await flushPromises(); expect(callsOf("git.stash")).toHaveLength(0); expect(wrapper.get("[data-test=stash-error]").text()).toContain("could be saved"); }); it("closes on Escape and gives the focus back", async () => { const before = document.createElement("button"); document.body.append(before); before.focus(); const wrapper = await mountDialog(); await useStashStore(pinia).show("stash"); await flushPromises(); await wrapper.get("[data-test=stash-message]").trigger("keydown", { key: "Escape" }); await flushPromises(); expect(wrapper.find("[data-test=stash]").exists()).toBe(false); expect(document.activeElement).toBe(before); }); }); describe("the list", () => { it("shows the entries and applies with Enter after saving", async () => { const wrapper = await mountDialog(); await useCommandsStore(pinia).execute("vcs.showStashes"); await flushPromises(); const texts = rows(wrapper); expect(texts).toHaveLength(2); expect(texts[0]).toContain("stash@{0}"); expect(texts[0]).toContain("WIP: 41aa1cf init"); expect(texts[1]).toContain("half done"); expect(texts[1]).toContain("feature/x"); const list = wrapper.get("[data-test=stash-list]"); expect(document.activeElement).toBe(list.element); await list.trigger("keydown", { key: "ArrowDown" }); await list.trigger("keydown", { key: "Enter" }); await flushPromises(); expect(callsOf("file.saveAll")).toHaveLength(1); expect(callsOf("git.stashApply")[0]?.params).toEqual({ index: 1, pop: false, hash: "bbbb2222" }); expect(callsOf("git.stashList")).toHaveLength(2); expect(useNotificationsStore(pinia).toasts.at(-1)?.text).toBe("Applied stash@{1}"); }); it("shows a conflicted pop's message and reads the list again", async () => { failures["git.stashApply"] = { code: "failed", message: "The stash was applied with conflicts in 1 file. Resolve them in the Changes window.", }; const wrapper = await mountDialog(); await useStashStore(pinia).show("list"); await flushPromises(); await wrapper.get("[data-test=stash-pop]").trigger("click"); await flushPromises(); expect(callsOf("git.stashApply")[0]?.params).toEqual({ index: 0, pop: true, hash: "aaaa1111" }); expect(wrapper.get("[data-test=stash-error]").text()).toContain("conflicts in 1 file"); expect(callsOf("git.stashList")).toHaveLength(2); expect(rows(wrapper)).toHaveLength(2); }); it("drops only after the question, and Escape cancels the question first", async () => { const wrapper = await mountDialog(); await useStashStore(pinia).show("list"); await flushPromises(); const list = wrapper.get("[data-test=stash-list]"); await list.trigger("keydown", { key: "Delete" }); expect(wrapper.get("[data-test=stash-confirm]").text()).toContain("Drop stash@{0}?"); expect(callsOf("git.stashDrop")).toHaveLength(0); await list.trigger("keydown", { key: "Escape" }); expect(wrapper.find("[data-test=stash-confirm]").exists()).toBe(false); expect(wrapper.find("[data-test=stash]").exists()).toBe(true); await wrapper.get("[data-test=stash-drop]").trigger("click"); stashes = [ENTRIES[1]].map((e) => ({ ...e, index: 0 })); await wrapper.get("[data-test=stash-drop-confirm]").trigger("click"); await flushPromises(); expect(callsOf("git.stashDrop")[0]?.params).toEqual({ index: 0, hash: "aaaa1111" }); expect(rows(wrapper)).toHaveLength(1); expect(callsOf("file.saveAll")).toHaveLength(0); }); it("says when the stash is empty and switches to the form", async () => { stashes = []; const wrapper = await mountDialog(); await useStashStore(pinia).show("list"); await flushPromises(); expect(wrapper.get("[data-test=stash-hint]").text()).toBe("The stash is empty."); expect(wrapper.get("[data-test=stash-apply]").attributes("disabled")).toBeDefined(); await wrapper.get("[data-test=stash-switch]").trigger("click"); await flushPromises(); expect(wrapper.find("[data-test=stash-form]").exists()).toBe(true); }); it("shows a failed read", async () => { failures["git.stashList"] = { code: "failed", message: "fatal: bad object" }; const wrapper = await mountDialog(); await useStashStore(pinia).show("list"); await flushPromises(); expect(wrapper.get("[data-test=stash-error]").text()).toBe("fatal: bad object"); }); }); describe("commands and entry points", () => { it("registers vcs.stash and vcs.showStashes in the Git menu without shortcuts", () => { for (const id of ["vcs.stash", "vcs.showStashes"]) { const spec = UI_COMMANDS.find((c) => c.id === id); expect(spec?.menuPath?.startsWith("Git/")).toBe(true); expect(spec?.defaultShortcut).toBeUndefined(); } }); it("opens the dialog from the Changes window's buttons", async () => { useChangesStore(pinia).start(); const dialog = await mountDialog(); const changes = mount(ChangesToolWindow, { attachTo: document.body, global: { plugins: [pinia] } }); mounted.push(changes); await flushPromises(); await changes.get("[data-test=changes-stash]").trigger("click"); await flushPromises(); expect(dialog.find("[data-test=stash-form]").exists()).toBe(true); useStashStore(pinia).hide(); await flushPromises(); await changes.get("[data-test=changes-stashes]").trigger("click"); await flushPromises(); expect(rows(dialog)).toHaveLength(2); }); });