Lutra/ui/tests/deploy/RemoteBrowser.test.ts
danny 1c6c67194a
Some checks failed
ci / ci (macos-15) (push) Has been cancelled
ci / ci (ubuntu-24.04) (push) Has been cancelled
ci / ci (windows-2025) (push) Has been cancelled
D2: Remote Host browser, compare and sync preview in the UI; docs and G17
Remote Host tool window (server select, tree, read-only viewer with Edit
and Upload, rename, new folder, delete after its question), server tabs
in the diff viewer, sync preview (uploads preselected, deletions never,
extra confirmation before deleting on the server), Compare with Server
in the tree menu and Tools › Deployment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 19:50:14 +01:00

203 lines
8.7 KiB
TypeScript

/**
* 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<string, unknown>;
}
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<string, unknown> }) => {
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: "<?php // server", binary: false, tooLarge: false });
case "deploy.compare":
return Promise.resolve({
differences: [
{ path: "a.php", status: "different", localSize: 1, remoteSize: 2 },
{ path: "new.php", status: "localOnly", localSize: 1 },
{ path: "old.php", status: "remoteOnly", remoteSize: 1 },
],
});
case "deploy.sync":
return Promise.resolve({ files: 2, deleted: 1, failed: 0, skipped: 0, bytes: 2 });
default:
return Promise.resolve({});
}
});
});
afterEach(() => {
for (const wrapper of mounted) {
wrapper.unmount();
}
document.body.innerHTML = "";
});
async function mountBrowser(): Promise<VueWrapper> {
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<HTMLTextAreaElement>("[data-test='remote-text']");
expect(text.element.readOnly).toBe(true);
await wrapper.find("[data-test='remote-edit']").trigger("click");
await text.setValue("<?php // edited");
await wrapper.find("[data-test='remote-upload']").trigger("click");
await flushPromises();
expect(callsOf("deploy.writeRemote")[0]?.params).toEqual({ remotePath: "index.php", text: "<?php // edited" });
});
it("renames, creates a folder and deletes only after the question", async () => {
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");
});
});