mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-20 23:20:46 +00:00
Fixes #916. When the user clicks "Restart" after a session disconnects, the renderer reuses the same sessionId and the bridges call startStream again to open a fresh log file for the new connection. The previous connection's close handlers (e.g. SSH conn.once('close'), stream.on('close'), serial 'close', telnet 'close', mosh PTY exit) all still fire asynchronously and call stopStream(sessionId) unconditionally. If they land after the new stream is already active, they silently destroy it and subsequent terminal output for the reconnected session is dropped, matching the bug report where the first connection's IO is saved but the reconnect's is not. Make startStream return a unique token and require stopStream callers to pass it. A stale stop call carrying the previous incarnation's token is now a no-op, so a late close handler from the previous connection cannot kill the freshly-started stream. Each reconnect therefore produces its own timestamped log file, which mirrors the existing auto-save-on-close semantics and is the simpler of the two options the issue offered. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
168 lines
6.5 KiB
JavaScript
168 lines
6.5 KiB
JavaScript
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const {
|
|
appendData,
|
|
hasStream,
|
|
startStream,
|
|
stopStream,
|
|
} = require("./sessionLogStreamManager.cjs");
|
|
|
|
const TEMP_ROOT = path.join(__dirname, ".tmp-session-log-stream-tests");
|
|
|
|
test("txt stream live snapshots include pending ED2 cleared screens", async () => {
|
|
const directory = path.join(TEMP_ROOT, `stream-${Date.now()}-${Math.random().toString(16).slice(2)}`);
|
|
const sessionId = `session-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
|
|
try {
|
|
startStream(sessionId, {
|
|
hostLabel: "host",
|
|
hostname: "host.example",
|
|
directory,
|
|
format: "txt",
|
|
startTime: Date.UTC(2026, 0, 2, 3, 4, 5),
|
|
});
|
|
appendData(sessionId, "before tui\n\x1b[H\x1b[2Jframe one\n\x1b[H\x1b[2Jframe two\n");
|
|
|
|
const filePath = await waitForFileContent(directory, "before tui\n\nframe one\n\nframe two");
|
|
assert.equal(fs.readFileSync(filePath, "utf8"), "before tui\n\nframe one\n\nframe two");
|
|
} finally {
|
|
await stopStream(sessionId);
|
|
fs.rmSync(directory, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("txt stream finalization commits pending ED2 cleared screens", async () => {
|
|
const directory = path.join(TEMP_ROOT, `stream-${Date.now()}-${Math.random().toString(16).slice(2)}`);
|
|
const sessionId = `session-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
|
|
try {
|
|
startStream(sessionId, {
|
|
hostLabel: "host",
|
|
hostname: "host.example",
|
|
directory,
|
|
format: "txt",
|
|
startTime: Date.UTC(2026, 0, 2, 3, 4, 5),
|
|
});
|
|
appendData(sessionId, "before tui\n\x1b[H\x1b[2Jframe one\n\x1b[H\x1b[2Jframe two\n");
|
|
|
|
const filePath = await stopStream(sessionId);
|
|
|
|
assert.equal(fs.readFileSync(filePath, "utf8"), "before tui\n\nframe one\n\nframe two");
|
|
} finally {
|
|
await stopStream(sessionId);
|
|
fs.rmSync(directory, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("startStream returns a token; stopStream with stale token leaves the active stream alone (issue #916)", async () => {
|
|
// Reproduces the bug shape where the user clicks "Restart" after a
|
|
// session disconnect. The renderer reuses the same sessionId, so the
|
|
// bridges call startStream(sessionId, ...) again. If a stale close
|
|
// handler from the previous incarnation later fires
|
|
// stopStream(sessionId), it must NOT clobber the freshly-started stream.
|
|
const directory = path.join(TEMP_ROOT, `stream-${Date.now()}-${Math.random().toString(16).slice(2)}`);
|
|
const sessionId = `session-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
|
|
try {
|
|
// First connection.
|
|
const firstToken = startStream(sessionId, {
|
|
hostLabel: "host",
|
|
hostname: "host.example",
|
|
directory,
|
|
format: "raw",
|
|
startTime: Date.UTC(2026, 0, 2, 3, 4, 5),
|
|
});
|
|
assert.ok(firstToken, "first startStream should return a token");
|
|
appendData(sessionId, "first-session\n");
|
|
const firstPath = await stopStream(sessionId, firstToken);
|
|
assert.ok(firstPath, "first stopStream should return its file path");
|
|
|
|
// User clicks "Restart" - same sessionId, fresh stream.
|
|
const secondToken = startStream(sessionId, {
|
|
hostLabel: "host",
|
|
hostname: "host.example",
|
|
directory,
|
|
format: "raw",
|
|
startTime: Date.UTC(2026, 0, 2, 3, 4, 10),
|
|
});
|
|
assert.ok(secondToken, "second startStream should return a token");
|
|
assert.notEqual(firstToken, secondToken, "tokens must be unique across starts");
|
|
appendData(sessionId, "second-session-before-stale\n");
|
|
|
|
// SIMULATE THE BUG: a stale close handler from the previous SSH
|
|
// connection finally fires and calls stopStream with the OLD token.
|
|
// The current implementation must ignore it.
|
|
const staleResult = await stopStream(sessionId, firstToken);
|
|
assert.equal(staleResult, null, "stale stopStream must be a no-op");
|
|
assert.equal(hasStream(sessionId), true, "second stream must still be active after stale stop");
|
|
|
|
// More output for the new session — must reach the file.
|
|
appendData(sessionId, "second-session-after-stale\n");
|
|
const secondPath = await stopStream(sessionId, secondToken);
|
|
assert.ok(secondPath, "second stopStream should return its file path");
|
|
assert.notEqual(firstPath, secondPath, "second connection should write to a new file");
|
|
|
|
// Both files exist and contain the expected output.
|
|
assert.equal(fs.readFileSync(firstPath, "utf8"), "first-session\n");
|
|
assert.equal(
|
|
fs.readFileSync(secondPath, "utf8"),
|
|
"second-session-before-stale\nsecond-session-after-stale\n",
|
|
);
|
|
} finally {
|
|
// Belt-and-suspenders cleanup; both streams are normally already stopped.
|
|
await stopStream(sessionId);
|
|
fs.rmSync(directory, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("stopStream without a token still tears down the current stream (back-compat)", async () => {
|
|
const directory = path.join(TEMP_ROOT, `stream-${Date.now()}-${Math.random().toString(16).slice(2)}`);
|
|
const sessionId = `session-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
|
|
try {
|
|
startStream(sessionId, {
|
|
hostLabel: "host",
|
|
hostname: "host.example",
|
|
directory,
|
|
format: "raw",
|
|
startTime: Date.UTC(2026, 0, 2, 3, 4, 5),
|
|
});
|
|
appendData(sessionId, "data\n");
|
|
const finalPath = await stopStream(sessionId);
|
|
assert.ok(finalPath);
|
|
assert.equal(fs.readFileSync(finalPath, "utf8"), "data\n");
|
|
assert.equal(hasStream(sessionId), false);
|
|
} finally {
|
|
fs.rmSync(directory, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
async function waitForFileContent(directory, expectedContent) {
|
|
const deadline = Date.now() + 3000;
|
|
let lastContent = "";
|
|
|
|
while (Date.now() < deadline) {
|
|
const filePath = findFirstTxtFile(directory);
|
|
if (filePath && fs.existsSync(filePath)) {
|
|
lastContent = fs.readFileSync(filePath, "utf8");
|
|
if (lastContent === expectedContent) return filePath;
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
}
|
|
|
|
assert.fail(`Timed out waiting for live snapshot content. Last content: ${JSON.stringify(lastContent)}`);
|
|
}
|
|
|
|
function findFirstTxtFile(directory) {
|
|
if (!fs.existsSync(directory)) return null;
|
|
for (const hostDirName of fs.readdirSync(directory)) {
|
|
const hostDir = path.join(directory, hostDirName);
|
|
if (!fs.statSync(hostDir).isDirectory()) continue;
|
|
const fileName = fs.readdirSync(hostDir).find((name) => name.endsWith(".txt"));
|
|
if (fileName) return path.join(hostDir, fileName);
|
|
}
|
|
return null;
|
|
}
|