Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
159 lines
4.9 KiB
JavaScript
159 lines
4.9 KiB
JavaScript
"use strict";
|
||
|
||
const assert = require("node:assert/strict");
|
||
const { EventEmitter } = require("node:events");
|
||
const test = require("node:test");
|
||
|
||
const { _execRemoteShellCommandForTests: execRemoteShellCommand } = require("./sftpBridge.cjs");
|
||
|
||
function createRemoteStream() {
|
||
const stream = new EventEmitter();
|
||
stream.stderr = new EventEmitter();
|
||
stream.closeCalls = 0;
|
||
stream.destroyCalls = 0;
|
||
stream.close = () => { stream.closeCalls += 1; };
|
||
stream.destroy = () => { stream.destroyCalls += 1; };
|
||
return stream;
|
||
}
|
||
|
||
test("remote delete exec times out while opening and closes a stream that arrives late", async () => {
|
||
let callback;
|
||
let invalidations = 0;
|
||
const sshClient = {
|
||
exec(_command, next) { callback = next; },
|
||
destroy() { invalidations += 1; },
|
||
};
|
||
|
||
await assert.rejects(
|
||
() => execRemoteShellCommand(sshClient, "rm -rf -- /tmp/example", {
|
||
openingTimeoutMs: 10,
|
||
runTimeoutMs: 100,
|
||
maxOutputBytes: 64,
|
||
}),
|
||
/open timed out/i,
|
||
);
|
||
assert.equal(invalidations, 1);
|
||
|
||
const lateStream = createRemoteStream();
|
||
callback(null, lateStream);
|
||
assert.ok(lateStream.closeCalls + lateStream.destroyCalls > 0);
|
||
});
|
||
|
||
test("remote delete exec has a running deadline and destroys the live stream", async () => {
|
||
const stream = createRemoteStream();
|
||
const sshClient = {
|
||
exec(_command, next) { next(null, stream); },
|
||
};
|
||
|
||
await assert.rejects(
|
||
() => execRemoteShellCommand(sshClient, "rm -rf -- /tmp/example", {
|
||
openingTimeoutMs: 100,
|
||
runTimeoutMs: 10,
|
||
maxOutputBytes: 64,
|
||
}),
|
||
/execution timed out/i,
|
||
);
|
||
assert.ok(stream.closeCalls + stream.destroyCalls > 0);
|
||
});
|
||
|
||
test("remote delete exec cancellation closes a stream that arrives after cancellation", async () => {
|
||
let callback;
|
||
let invalidations = 0;
|
||
const controller = new AbortController();
|
||
const sshClient = {
|
||
exec(_command, next) { callback = next; },
|
||
destroy() { invalidations += 1; },
|
||
};
|
||
const pending = execRemoteShellCommand(sshClient, "rm -rf -- /tmp/example", {
|
||
signal: controller.signal,
|
||
openingTimeoutMs: 100,
|
||
runTimeoutMs: 100,
|
||
maxOutputBytes: 64,
|
||
});
|
||
|
||
controller.abort(new Error("cancelled by test"));
|
||
await assert.rejects(() => pending, /cancelled by test/i);
|
||
assert.equal(invalidations, 1);
|
||
|
||
const lateStream = createRemoteStream();
|
||
callback(null, lateStream);
|
||
assert.ok(lateStream.closeCalls + lateStream.destroyCalls > 0);
|
||
});
|
||
|
||
test("remote delete exec rejects and closes the stream when output exceeds its hard limit", async () => {
|
||
const stream = createRemoteStream();
|
||
const sshClient = {
|
||
exec(_command, next) {
|
||
next(null, stream);
|
||
queueMicrotask(() => stream.emit("data", Buffer.alloc(65, "x")));
|
||
},
|
||
};
|
||
|
||
await assert.rejects(
|
||
() => execRemoteShellCommand(sshClient, "rm -rf -- /tmp/example", {
|
||
openingTimeoutMs: 100,
|
||
runTimeoutMs: 100,
|
||
maxOutputBytes: 64,
|
||
}),
|
||
/output exceeded/i,
|
||
);
|
||
assert.ok(stream.closeCalls + stream.destroyCalls > 0);
|
||
});
|
||
|
||
test("remote delete exec preserves split UTF-8 independently on stdout and stderr", async () => {
|
||
const stream = createRemoteStream();
|
||
const sshClient = {
|
||
exec(_command, next) { next(null, stream); },
|
||
};
|
||
const running = execRemoteShellCommand(sshClient, "printf unicode", {
|
||
openingTimeoutMs: 100,
|
||
runTimeoutMs: 100,
|
||
maxOutputBytes: 64,
|
||
});
|
||
const stdout = Buffer.from("中文", "utf8");
|
||
const stderr = Buffer.from("错误", "utf8");
|
||
stream.emit("data", stdout.subarray(0, 2));
|
||
stream.stderr.emit("data", stderr.subarray(0, 1));
|
||
stream.emit("data", stdout.subarray(2));
|
||
stream.stderr.emit("data", stderr.subarray(1));
|
||
stream.emit("close", 0);
|
||
|
||
assert.deepEqual(await running, { stdout: "中文", stderr: "错误", code: 0 });
|
||
});
|
||
|
||
test("remote delete exec rejects before exposing a character cut by its byte limit", async () => {
|
||
const stream = createRemoteStream();
|
||
const sshClient = {
|
||
exec(_command, next) { next(null, stream); },
|
||
};
|
||
const running = execRemoteShellCommand(sshClient, "printf unicode", {
|
||
openingTimeoutMs: 100,
|
||
runTimeoutMs: 100,
|
||
maxOutputBytes: 2,
|
||
});
|
||
stream.emit("data", Buffer.from("中", "utf8"));
|
||
|
||
await assert.rejects(running, (error) => (
|
||
/output exceeded/i.test(error?.message || "") && !/<2F>/u.test(error?.message || "")
|
||
));
|
||
assert.ok(stream.closeCalls + stream.destroyCalls > 0);
|
||
});
|
||
|
||
test("remote extract exec drains stdout without treating progress as a hard limit", async () => {
|
||
const stream = createRemoteStream();
|
||
const sshClient = {
|
||
exec(_command, next) { next(null, stream); },
|
||
};
|
||
const running = execRemoteShellCommand(sshClient, "unzip -qo archive.zip", {
|
||
openingTimeoutMs: 100,
|
||
runTimeoutMs: 100,
|
||
maxOutputBytes: 8,
|
||
discardStdout: true,
|
||
});
|
||
stream.emit("data", Buffer.alloc(64, "x"));
|
||
stream.stderr.emit("data", Buffer.from("ok"));
|
||
stream.emit("close", 0);
|
||
|
||
assert.deepEqual(await running, { stdout: "", stderr: "ok", code: 0 });
|
||
});
|