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
123 lines
3.6 KiB
JavaScript
123 lines
3.6 KiB
JavaScript
"use strict";
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const { EventEmitter } = require("node:events");
|
|
const { createExecOnSessionApi } = require("./execOnSession.cjs");
|
|
|
|
test("execOnSession closes ssh exec stdin after writing provided input", async () => {
|
|
const writes = [];
|
|
let ended = false;
|
|
const stream = new EventEmitter();
|
|
stream.stderr = new EventEmitter();
|
|
stream.write = (data) => {
|
|
writes.push(data);
|
|
return true;
|
|
};
|
|
stream.end = () => {
|
|
ended = true;
|
|
};
|
|
|
|
const conn = {
|
|
exec(_command, callback) {
|
|
callback(null, stream);
|
|
process.nextTick(() => stream.emit("close", 0));
|
|
},
|
|
};
|
|
const execApi = createExecOnSessionApi({
|
|
sessions: { get: () => ({ conn, type: "ssh" }) },
|
|
});
|
|
|
|
const result = await execApi.execOnSession(null, "s1", "sudo -S -p '' docker ps", 1000, {
|
|
stdin: "secret\n",
|
|
});
|
|
|
|
assert.equal(result.success, true);
|
|
assert.deepEqual(writes, ["secret\n"]);
|
|
assert.equal(ended, true);
|
|
});
|
|
|
|
test("execOnSession reports local maxBuffer errors instead of returning truncated stdout", async () => {
|
|
const execApi = createExecOnSessionApi({
|
|
sessions: { get: () => ({ type: "local", protocol: "local" }) },
|
|
process,
|
|
});
|
|
|
|
const result = await execApi.execOnSession(null, "local", "yes x | head -c 2048", 1000, {
|
|
maxBuffer: 128,
|
|
});
|
|
|
|
assert.equal(result.success, false);
|
|
assert.match(result.error, /maxBuffer|stdout maxBuffer/i);
|
|
});
|
|
|
|
test("execOnSession enforces maxBuffer for SSH streamed stdout", async () => {
|
|
let closed = false;
|
|
const stream = new EventEmitter();
|
|
stream.stderr = new EventEmitter();
|
|
stream.close = () => {
|
|
closed = true;
|
|
};
|
|
|
|
const conn = {
|
|
exec(_command, callback) {
|
|
callback(null, stream);
|
|
process.nextTick(() => {
|
|
stream.emit("data", Buffer.from("x".repeat(256)));
|
|
stream.emit("close", 0);
|
|
});
|
|
},
|
|
};
|
|
const execApi = createExecOnSessionApi({
|
|
sessions: { get: () => ({ conn, type: "ssh" }) },
|
|
});
|
|
|
|
const result = await execApi.execOnSession(null, "s1", "ps", 1000, {
|
|
maxBuffer: 128,
|
|
});
|
|
|
|
assert.equal(result.success, false);
|
|
assert.match(result.error, /maxBuffer/i);
|
|
assert.equal(result.stdout, "");
|
|
assert.equal(closed, true);
|
|
});
|
|
|
|
test("execOnSession closes an SSH stream that arrives after the open timeout", async () => {
|
|
let callback;
|
|
const conn = { exec(_command, next) { callback = next; } };
|
|
const execApi = createExecOnSessionApi({
|
|
sessions: { get: () => ({ conn, type: "ssh" }) },
|
|
});
|
|
|
|
const result = await execApi.execOnSession(null, "s1", "pending", 5);
|
|
assert.equal(result.success, false);
|
|
assert.match(result.error, /open timed out/);
|
|
|
|
const stream = new EventEmitter();
|
|
stream.stderr = new EventEmitter();
|
|
let closed = 0;
|
|
stream.close = () => { closed += 1; };
|
|
callback(null, stream);
|
|
assert.ok(closed > 0);
|
|
});
|
|
|
|
test("execOnSession settles and releases listeners on SSH stream errors", async () => {
|
|
const stream = new EventEmitter();
|
|
stream.stderr = new EventEmitter();
|
|
stream.close = () => {};
|
|
stream.destroy = () => {};
|
|
const conn = { exec(_command, callback) { callback(null, stream); } };
|
|
const execApi = createExecOnSessionApi({
|
|
sessions: { get: () => ({ conn, type: "ssh" }) },
|
|
});
|
|
|
|
const pending = execApi.execOnSession(null, "s1", "fail", 1000);
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
stream.stderr.emit("error", new Error("remote stderr failed"));
|
|
const result = await pending;
|
|
assert.equal(result.success, false);
|
|
assert.match(result.error, /stderr failed/);
|
|
assert.equal(stream.listenerCount("data"), 0);
|
|
assert.equal(stream.stderr.listenerCount("data"), 0);
|
|
});
|