Files
Netcatty/electron/preload/api.stageUploadFile.test.cjs
T
陈大猫 443202ba4e fix(sftp): instant folder-drop scan UI and drop full-tree pre-scan (#2782)
* fix(sftp): instant folder-drop scan UI and drop full-tree pre-scan

Restore interleaved folder transfers as the only non-compressed path, remove
the folder pre-scan setting, and make local folder drops show a scanning task
with live discovered counts via native listLocalTree (with cancel + progress).

UI shows "done · found" instead of a fake grand-total percentage while
discovery is still growing.

* fix(sftp): make folder scan cancellation bridge-safe

* fix(sftp): preserve path-only terminal drops

* fix(terminal): accept native folder drop paths

* fix(sftp): keep scan task cancellable during setup

* fix(sftp): preserve scan failure state during setup errors

* fix(sftp): keep folder scan task across retries

* fix(sftp): cancel scan task immediately

* fix(sftp): keep cancellation active through session setup

* fix(sftp): settle failed drop upload retry

* fix(sftp): preserve cancellation in final scan cleanup

* fix(sftp): harden folder scan cancellation and conflicts

* fix(sftp): finish Codex follow-ups on folder scan cancel UX

- Abort sibling listLocalTree walks when one multi-root scan fails
- Prefer ERR_*_CANCELLED over matching "cancel" in error text
- Clear scanning phase on settle; only label live rows as Scanning
- Omit directory metadata sizes from drop entries used for totals

* fix(sftp): stream folder discovery into concurrent uploads

Local path-backed folder drops now edge-scan and edge-transfer: listLocalTree
streams entry batches while upload workers consume them, so the first files
start before a 100k-file walk finishes. Cancel aborts the walk, clears the
queue, and stops creating child tasks. Compressed uploads still materialize
fully then pack.

* fix(sftp): harden progressive folder UX, retry, and size-based uploads

Align the SFTP panel with transfer-center progress, keep nested tree batches
until tree-end, honor pause during progressive enqueue, add low-cost retry for
external drag-drop files, and drop whole-file upload digests for industry
size-based resume (download live-log prefix checks unchanged).

* fix(sftp): address multi-agent review on progressive upload and retry

Gate progressive drops to path-backed folders only, prefer pool sessions on
drag-drop retry with in-flight and cancel guards, soft-check upload mtime
without fail-open contentVerifiedSeparately, skip multi-root pause HOL,
and fail closed when the local tree stream misses tree-end.

* fix(sftp): finish multi-root pause HOL and parent rollup guards

Race-wait on all latched parents so resuming a non-head folder can proceed,
and only roll progressive parents to completed after terminal failed status.

* fix(sftp): address Codex progressive conflict and hang findings

Add root-level conflict preflight to progressive folder uploads, wake all
backpressure waiters, preserve cancel listeners across uploadEntriesDirect,
and surface empty-directory mkdir failures.
2026-08-07 02:01:18 +08:00

194 lines
5.8 KiB
JavaScript

"use strict";
const assert = require("node:assert/strict");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const test = require("node:test");
const { createPreloadApi } = require("./api.cjs");
function createFile(name, chunks) {
let index = 0;
return {
name,
stream: () => ({
getReader: () => ({
async read() {
if (index >= chunks.length) return { done: true };
return { done: false, value: Uint8Array.from(chunks[index++]) };
},
releaseLock() {},
}),
}),
};
}
function createBlockingFile(name) {
let markReadStarted;
let finishRead;
const readStarted = new Promise((resolve) => { markReadStarted = resolve; });
return {
file: {
name,
stream: () => ({
getReader: () => ({
read() {
markReadStarted();
return new Promise((resolve) => { finishRead = resolve; });
},
cancel() {
finishRead?.({ done: true });
return Promise.resolve();
},
releaseLock() {},
}),
}),
},
readStarted,
};
}
test("superseding pathless upload staging uses an independent temp file", async (t) => {
const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-preload-stage-race-"));
t.after(() => fs.promises.rm(dir, { recursive: true, force: true }));
const created = [];
const deleted = [];
const ipcRenderer = {
on() {},
removeListener() {},
async invoke(channel, payload) {
if (channel === "netcatty:tempdir:createUploadPath") {
const localPath = path.join(dir, `${payload.transferId}_${payload.fileName}.part`);
created.push({ payload, localPath });
return localPath;
}
if (channel === "netcatty:deleteTempFile") {
deleted.push(payload.filePath);
await fs.promises.unlink(payload.filePath).catch(() => {});
return { success: true };
}
throw new Error(`Unexpected IPC call: ${channel}`);
},
};
const api = createPreloadApi({ ipcRenderer, webUtils: {} });
const firstFile = createBlockingFile("same.bin");
const first = api.stageUploadFile(firstFile.file, "same-transfer");
await firstFile.readStarted;
const second = api.stageUploadFile(createFile("same.bin", [[4, 5, 6]]), "same-transfer");
await assert.rejects(first, /superseded/i);
const secondPath = await second;
assert.equal(created.length, 2);
assert.notEqual(created[0].payload.transferId, created[1].payload.transferId);
assert.notEqual(created[0].localPath, created[1].localPath);
assert.equal(secondPath, created[1].localPath);
assert.deepEqual(await fs.promises.readFile(secondPath), Buffer.from([4, 5, 6]));
await assert.rejects(fs.promises.stat(created[0].localPath), { code: "ENOENT" });
assert.deepEqual(deleted, [created[0].localPath]);
});
test("failed upload path allocation releases the staging controller", async () => {
const api = createPreloadApi({
webUtils: {},
ipcRenderer: {
on() {},
removeListener() {},
async invoke(channel) {
if (channel === "netcatty:tempdir:createUploadPath") throw new Error("path unavailable");
throw new Error(`Unexpected IPC call: ${channel}`);
},
},
});
await assert.rejects(api.stageUploadFile(createFile("file.bin", [[1]]), "failed-path"), /path unavailable/);
assert.deepEqual(await api.cancelStagedUploadFile("failed-path"), { success: false });
});
test("native tree scans use a bridge-safe cancellation id", async () => {
const sent = [];
const invoked = [];
const api = createPreloadApi({
webUtils: {},
ipcRenderer: {
on() {},
removeListener() {},
send(...args) { sent.push(args); },
async invoke(channel, payload) {
invoked.push({ channel, payload });
return [];
},
},
});
await api.listLocalTree("/tmp/project", {
scanId: "scan-123",
onProgress: () => {},
});
await api.cancelLocalTreeScan("scan-123");
assert.deepEqual(invoked, [{
channel: "netcatty:local:tree",
payload: {
path: "/tmp/project",
progressChannel: "netcatty:local:tree-progress:scan-123",
entriesChannel: undefined,
cancelChannel: "netcatty:local:tree-cancel:scan-123",
limits: undefined,
},
}]);
assert.deepEqual(sent, [["netcatty:local:tree-cancel:scan-123"]]);
});
test("listLocalTree keeps the entries listener until the tree-end marker arrives", async () => {
const listeners = new Map();
const batches = [];
let removed = false;
const api = createPreloadApi({
webUtils: {},
ipcRenderer: {
on(channel, handler) {
listeners.set(channel, handler);
},
removeListener(channel) {
if (channel.startsWith("netcatty:local:tree-entries:")) removed = true;
listeners.delete(channel);
},
send() {},
async invoke(channel, payload) {
assert.equal(channel, "netcatty:local:tree");
const entriesChannel = payload.entriesChannel;
const handler = listeners.get(entriesChannel);
assert.equal(typeof handler, "function");
// Simulate the invoke reply racing ahead of a late nested batch.
queueMicrotask(() => {
handler({}, [
{
localPath: "/tmp/project/nested/deep.txt",
relativePath: "project/nested/deep.txt",
type: "file",
size: 1,
lastModified: 1,
},
]);
handler({}, { type: "tree-end" });
});
return [];
},
},
});
await api.listLocalTree("/tmp/project", {
scanId: "scan-nested",
onEntries: (batch) => {
batches.push(batch);
},
});
assert.equal(batches.length, 1);
assert.equal(batches[0][0].relativePath, "project/nested/deep.txt");
assert.equal(removed, true);
});