/** * Tests of the remote browser, compare and sync (C9 D2): reading untrusted * listings and comparisons, names; the Remote Host tool window (root, * expanding a folder, a file read-only then edited and uploaded, rename, * new folder, and delete only after its question); the sync preview * (uploads preselected, deletions never, Sync blocked until the deletion * is confirmed, `confirmDelete` sent only then); and the server diff tab. * 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 RemoteBrowserToolWindow from "../../src/components/deploy/RemoteBrowserToolWindow.vue"; import SyncDialog from "../../src/components/deploy/SyncDialog.vue"; import { childPath, parentPath, toEntries, toSyncItems, useRemoteBrowserStore, validName, } from "../../src/components/deploy/remoteBrowser"; import { useDiffStore } from "../../src/components/vcs/diff/diff"; interface Call { command: string; params: Record; } let calls: Call[]; let pinia: Pinia; let mounted: VueWrapper[]; function callsOf(command: string): Call[] { return calls.filter((c) => c.command === command); } beforeEach(() => { pinia = createPinia(); setActivePinia(pinia); calls = []; mounted = []; invoke.mockReset(); listen.mockReset(); listen.mockResolvedValue(() => undefined); invoke.mockImplementation((cmd: string, args?: { command: string; params: Record }) => { if ((cmd !== "engine_execute" && cmd !== "engine_execute_waiting") || !args) { return Promise.resolve(null); } calls.push({ command: args.command, params: args.params }); switch (args.command) { case "deploy.servers": return Promise.resolve({ servers: [{ id: "s1", name: "Staging", kind: "sftp" }], defaultServer: "s1" }); case "deploy.browse": return Promise.resolve( args.params.path === "" ? { entries: [{ name: "public", isDir: true }, { name: "index.php", isDir: false, size: 5 }, { name: "../x", isDir: false }] } : { entries: [{ name: "app.php", isDir: false, size: 3 }] }, ); case "deploy.readRemote": return Promise.resolve({ text: " { for (const wrapper of mounted) { wrapper.unmount(); } document.body.innerHTML = ""; }); async function mountBrowser(): Promise { const wrapper = mount(RemoteBrowserToolWindow, { attachTo: document.body, global: { plugins: [pinia] } }); mounted.push(wrapper); await useRemoteBrowserStore(pinia).show(); await flushPromises(); return wrapper; } describe("reading the engine's answers", () => { it("drops unsafe names and malformed differences", () => { expect(toEntries({ entries: [{ name: "a", isDir: true }, { name: ".." }, { name: "x/y" }, null] })).toEqual([ { name: "a", isDir: true, size: null }, ]); const items = toSyncItems({ differences: [{ path: "a", status: "remoteOnly" }, { path: "b", status: "localOnly" }, { path: "c", status: "gone" }], }); expect(items.map((i) => [i.path, i.selected])).toEqual([ ["a", false], ["b", true], ]); expect(validName("ok.php")).toBe(true); expect(validName("a\nb")).toBe(false); expect(validName("..")).toBe(false); expect(childPath("", "a")).toBe("a"); expect(parentPath("a/b/c")).toBe("a/b"); }); }); describe("Remote Host tool window", () => { it("lists the root, expands a folder and edits a file", async () => { const wrapper = await mountBrowser(); expect(wrapper.findAll("[data-test='remote-row']").map((r) => r.attributes("data-path"))).toEqual(["public", "index.php"]); await wrapper.find("[data-path='public'] .remote__name").trigger("click"); await flushPromises(); expect(callsOf("deploy.browse").at(-1)?.params).toEqual({ path: "public" }); expect(wrapper.find("[data-path='public/app.php']").exists()).toBe(true); await wrapper.find("[data-path='index.php'] .remote__name").trigger("click"); await flushPromises(); expect(callsOf("deploy.readRemote")[0]?.params).toEqual({ remotePath: "index.php" }); const text = wrapper.find("[data-test='remote-text']"); expect(text.element.readOnly).toBe(true); await wrapper.find("[data-test='remote-edit']").trigger("click"); await text.setValue(" { const wrapper = await mountBrowser(); await wrapper.find("[data-path='index.php'] [data-test='remote-rename']").trigger("click"); await wrapper.find("[data-test='remote-form-name']").setValue("home.php"); await wrapper.find("[data-test='remote-form']").trigger("submit"); await flushPromises(); expect(callsOf("deploy.renameRemote")[0]?.params).toEqual({ from: "index.php", to: "home.php" }); await wrapper.find("[data-test='remote-new-folder']").trigger("click"); await wrapper.find("[data-test='remote-form-name']").setValue("cache"); await wrapper.find("[data-test='remote-form']").trigger("submit"); await flushPromises(); expect(callsOf("deploy.mkdirRemote")[0]?.params).toEqual({ path: "cache" }); await wrapper.find("[data-path='public'] [data-test='remote-delete']").trigger("click"); await flushPromises(); expect(callsOf("deploy.deleteRemote")).toHaveLength(0); expect(wrapper.find("[data-test='remote-form']").text()).toContain("public"); await wrapper.find("[data-test='remote-delete-confirm']").trigger("click"); await flushPromises(); expect(callsOf("deploy.deleteRemote")[0]?.params).toEqual({ path: "public", confirm: true }); }); }); describe("sync preview", () => { it("needs the extra confirmation before deleting on the server", async () => { const wrapper = mount(SyncDialog, { attachTo: document.body, global: { plugins: [pinia] } }); mounted.push(wrapper); const browser = useRemoteBrowserStore(pinia); await browser.compareFolders([""]); await flushPromises(); expect(callsOf("deploy.compare")[0]?.params).toEqual({ paths: [""] }); expect(wrapper.findAll("[data-test='sync-item']")).toHaveLength(3); expect(wrapper.find("[data-test='sync-confirm']").exists()).toBe(false); await wrapper.find("input[data-path='old.php']").setValue(true); expect(wrapper.find("[data-test='sync-confirm']").exists()).toBe(true); expect(wrapper.find("[data-test='sync-run']").attributes("disabled")).toBeDefined(); await wrapper.find("[data-test='sync-confirm'] input").setValue(true); await wrapper.find("[data-test='sync-run']").trigger("click"); await flushPromises(); expect(callsOf("deploy.sync")[0]?.params).toEqual({ upload: ["a.php", "new.php"], delete: ["old.php"], confirmDelete: true, }); }); it("syncs uploads without confirmDelete when nothing is deleted", async () => { const wrapper = mount(SyncDialog, { attachTo: document.body, global: { plugins: [pinia] } }); mounted.push(wrapper); await useRemoteBrowserStore(pinia).compareFolders(["src"]); await flushPromises(); await wrapper.find("[data-test='sync-run']").trigger("click"); await flushPromises(); expect(callsOf("deploy.sync")[0]?.params).toEqual({ upload: ["a.php", "new.php"], delete: [] }); }); }); describe("server diff tab", () => { it("reads the server's side through deploy.readRemote", async () => { const diff = useDiffStore(pinia); void diff.openRemote("a.php", { serverId: "s1", server: "Staging" }); await flushPromises(); expect(callsOf("deploy.readRemote")[0]?.params).toEqual({ path: "a.php", id: "s1" }); expect(diff.activeTab?.remote?.server).toBe("Staging"); }); });