mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 16:42:19 +00:00
* feat(sftp): add a one-click Duplicate action for files and folders Duplicate copies the selected entries next to their originals in the same folder, covering files, folders, and multi-selection. It reuses the existing transfer queue and the transfer pipeline's "(copy)" naming helper, so progress, cancel, and error reporting behave like Copy to other pane. The target name is resolved before the transfer starts. Resolving through the batch conflict defaults instead would re-enter processTransfer for a task that is still marked in flight and stall the copy. Adds the action to both the list and tree context menus, plus translations for en, es, ru, zh-CN, and zh-TW. Closes #4132 * fix(sftp): keep Duplicate safe around navigation rows and reserved names Hide and ignore the tree's synthetic ".." row: duplicating it resolved the source to an ancestor directory and could walk the copy into its own output. The transfer hook also drops "." / ".." inputs for duplicate batches, and the same-pane handler filters them defensively. Pick duplicate names around destinations already reserved by a queued or paused transfer. Those rows have no file on disk yet, so stat-only naming returned the same "(copy)" path and the follow-up active-path check turned the second one-click duplicate into a conflict dialog. The naming helper now accepts an occupied-path predicate fed from the active transfer set. * fix(sftp): block stale entry menus on parent navigation --------- Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
110 lines
6.3 KiB
TypeScript
110 lines
6.3 KiB
TypeScript
/** @test issue #4132 — a duplicate must not reuse a destination an earlier copy still holds */
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs/promises";
|
|
import path from "node:path";
|
|
import test from "node:test";
|
|
import React from "react";
|
|
import { act, create, type ReactTestRenderer } from "react-test-renderer";
|
|
import { useSftpTransfers } from "./useSftpTransfers";
|
|
import { createEmptyPane } from "./types";
|
|
import { sftpTransferCenterStore } from "../sftpTransferCenterStore";
|
|
|
|
test("duplicate names around a destination an in-flight copy has not created yet", async () => {
|
|
const dir = await fs.mkdtemp(path.join(process.cwd(), ".duplicate-inflight-"));
|
|
const ownerId = `duplicate-inflight-${crypto.randomUUID()}`;
|
|
const previousWindow = globalThis.window;
|
|
const previousStorage = globalThis.localStorage;
|
|
const globals = globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean };
|
|
const previousAct = globals.IS_REACT_ACT_ENVIRONMENT;
|
|
globals.IS_REACT_ACT_ENVIRONMENT = true;
|
|
let renderer: ReactTestRenderer | undefined;
|
|
let ops!: ReturnType<typeof useSftpTransfers>;
|
|
let releaseFirst: (() => void) | undefined;
|
|
const firstCopyGate = new Promise<void>((resolve) => { releaseFirst = resolve; });
|
|
let streamStarts = 0;
|
|
await fs.writeFile(path.join(dir, "report.txt"), "original data");
|
|
const pane = createEmptyPane("duplicate-inflight-pane");
|
|
pane.connection = { id: ownerId, isLocal: true, currentPath: dir, status: "connected" } as NonNullable<typeof pane.connection>;
|
|
const statPath = async (value: string) => {
|
|
try {
|
|
const stat = await fs.lstat(value);
|
|
return { type: stat.isSymbolicLink() ? "symlink" : stat.isDirectory() ? "directory" : "file", size: stat.size, lastModified: stat.mtimeMs };
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
(globalThis as { window?: unknown }).window = { requestAnimationFrame: (callback: () => void) => setTimeout(callback, 0), cancelAnimationFrame: clearTimeout, netcatty: {
|
|
realpathLocal: fs.realpath, statLocal: statPath, lstatLocal: statPath,
|
|
mkdirLocal: async (value: string) => { await fs.mkdir(value, { recursive: true }); },
|
|
deleteLocalFile: async (value: string) => { await fs.rm(value, { recursive: true, force: true }); },
|
|
renameLocalFile: async (source: string, target: string) => { await fs.rename(source, target); },
|
|
startStreamTransfer: async (options: { sourcePath: string; targetPath: string; transferId: string }) => {
|
|
streamStarts += 1;
|
|
// Hold the first copy before it writes its destination: the second
|
|
// duplicate then sees no "(copy)" on disk even though one is reserved.
|
|
if (streamStarts === 1) await firstCopyGate;
|
|
await fs.copyFile(options.sourcePath, options.targetPath);
|
|
sftpTransferCenterStore.ingestBackgroundEvent({ type: "completed", transferId: options.transferId, transferred: 13, totalBytes: 13, lifecycleEpoch: 0 });
|
|
return {};
|
|
},
|
|
} };
|
|
(globalThis as { localStorage?: unknown }).localStorage = { getItem: () => null, setItem: () => undefined, removeItem: () => undefined };
|
|
function Probe() {
|
|
ops = useSftpTransfers({
|
|
ownerId, getActivePane: () => pane, getPaneByConnectionId: () => pane, getTabByConnectionId: () => ({ side: "left", tabId: pane.id, pane }),
|
|
updateTab: () => undefined, refresh: async () => undefined,
|
|
clearCacheForConnection: () => undefined, handleSessionError: () => undefined,
|
|
sftpSessionsRef: { current: new Map() }, connectionCacheKeyMapRef: { current: new Map() },
|
|
listRemoteFiles: async () => [],
|
|
listLocalFiles: async (value: string) => Promise.all((await fs.readdir(value)).map(async name => {
|
|
const stat = await fs.lstat(path.join(value, name));
|
|
return { name, type: stat.isSymbolicLink() ? "symlink" as const : stat.isDirectory() ? "directory" as const : "file" as const, size: stat.size, sizeFormatted: "", lastModified: stat.mtimeMs, lastModifiedFormatted: "" };
|
|
})),
|
|
});
|
|
return null;
|
|
}
|
|
const sources = [{ name: "report.txt", isDirectory: false }];
|
|
const options = { sourcePath: dir, targetPath: dir, duplicate: true };
|
|
try {
|
|
await act(async () => { renderer = create(React.createElement(Probe)); });
|
|
// First duplicate starts copying and blocks before writing "(copy)".
|
|
let firstRun: ReturnType<typeof ops.startTransfer> | undefined;
|
|
await act(async () => {
|
|
firstRun = ops.startTransfer(sources, "left", "left", options);
|
|
const deadline = Date.now() + 3000;
|
|
while (streamStarts === 0 && Date.now() < deadline) {
|
|
await new Promise(resolve => setTimeout(resolve, 5));
|
|
}
|
|
});
|
|
assert.equal(streamStarts, 1, "the first copy should be in flight");
|
|
|
|
// Second duplicate runs while "(copy)" is still absent from the disk.
|
|
await act(async () => { await ops.startTransfer(sources, "left", "left", options); });
|
|
assert.equal(ops.conflicts.length, 0, "duplicate must not prompt for a reserved name");
|
|
releaseFirst?.();
|
|
await act(async () => { await firstRun; });
|
|
|
|
await act(async () => {
|
|
const deadline = Date.now() + 5000;
|
|
while (Date.now() < deadline) {
|
|
const rows = sftpTransferCenterStore.getOwnerTasks(ownerId).filter(row => !row.parentTaskId);
|
|
if (rows.length === 2 && rows.every(row => ["completed", "failed"].includes(row.status))) return;
|
|
await new Promise(resolve => setTimeout(resolve, 20));
|
|
}
|
|
});
|
|
const roots = sftpTransferCenterStore.getOwnerTasks(ownerId).filter(row => !row.parentTaskId);
|
|
assert.deepEqual(roots.map(row => row.fileName).sort(), ["report (copy 2).txt", "report (copy).txt"]);
|
|
assert.ok(roots.every(row => row.status === "completed"), JSON.stringify(roots.map(row => ({ status: row.status, error: row.error }))));
|
|
assert.equal(await fs.readFile(path.join(dir, "report (copy).txt"), "utf8"), "original data");
|
|
assert.equal(await fs.readFile(path.join(dir, "report (copy 2).txt"), "utf8"), "original data");
|
|
} finally {
|
|
releaseFirst?.();
|
|
await act(async () => { renderer?.unmount(); });
|
|
for (const row of sftpTransferCenterStore.getOwnerTasks(ownerId)) sftpTransferCenterStore.dismiss(row.id);
|
|
(globalThis as { window?: unknown }).window = previousWindow;
|
|
(globalThis as { localStorage?: unknown }).localStorage = previousStorage;
|
|
globals.IS_REACT_ACT_ENVIRONMENT = previousAct;
|
|
await fs.rm(dir, { recursive: true, force: true });
|
|
}
|
|
});
|