[Init] Initial commit - NetMesh terminal manager
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
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
This commit is contained in:
227
electron/bridges/mcpServerBridge/sessionIdleManager.test.cjs
Normal file
227
electron/bridges/mcpServerBridge/sessionIdleManager.test.cjs
Normal file
@@ -0,0 +1,227 @@
|
||||
"use strict";
|
||||
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
const { createSessionIdleManager } = require("./sessionIdleManager.cjs");
|
||||
|
||||
function createClock() {
|
||||
let now = 0;
|
||||
let nextId = 1;
|
||||
const timers = new Map();
|
||||
|
||||
function runDueTimers() {
|
||||
while (true) {
|
||||
const due = Array.from(timers.entries())
|
||||
.filter(([, timer]) => timer.at <= now)
|
||||
.sort((a, b) => a[1].at - b[1].at)[0];
|
||||
if (!due) break;
|
||||
timers.delete(due[0]);
|
||||
due[1].callback();
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
Date: { now: () => now },
|
||||
setTimeout(callback, delay) {
|
||||
const id = nextId++;
|
||||
timers.set(id, { callback, at: now + delay });
|
||||
return id;
|
||||
},
|
||||
clearTimeout(id) {
|
||||
timers.delete(id);
|
||||
},
|
||||
advance(ms) {
|
||||
now += ms;
|
||||
runDueTimers();
|
||||
},
|
||||
timerCount() {
|
||||
return timers.size;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test("idle sessions close independently and activity renews only the matching session", async () => {
|
||||
const clock = createClock();
|
||||
const closed = [];
|
||||
const manager = createSessionIdleManager({
|
||||
...clock,
|
||||
timeoutMinutes: 1,
|
||||
onIdle: async (entry) => closed.push(entry),
|
||||
});
|
||||
|
||||
manager.track("chat-a", "session-1");
|
||||
manager.track("chat-a", "session-2");
|
||||
|
||||
clock.advance(30_000);
|
||||
await Promise.resolve();
|
||||
assert.deepEqual(closed, []);
|
||||
manager.touch("chat-a", "session-1");
|
||||
|
||||
clock.advance(30_000);
|
||||
await Promise.resolve();
|
||||
assert.deepEqual(closed, [{ chatSessionId: "chat-a", sessionId: "session-2" }]);
|
||||
|
||||
clock.advance(30_000);
|
||||
await Promise.resolve();
|
||||
assert.deepEqual(closed, [
|
||||
{ chatSessionId: "chat-a", sessionId: "session-2" },
|
||||
{ chatSessionId: "chat-a", sessionId: "session-1" },
|
||||
]);
|
||||
});
|
||||
|
||||
test("an in-flight operation cannot time out and gets a fresh idle window when it ends", async () => {
|
||||
const clock = createClock();
|
||||
const closed = [];
|
||||
const manager = createSessionIdleManager({
|
||||
...clock,
|
||||
timeoutMinutes: 1,
|
||||
onIdle: async (entry) => closed.push(entry),
|
||||
});
|
||||
|
||||
manager.track("chat-a", "session-1");
|
||||
assert.equal(manager.beginActivity("chat-a", "session-1"), true);
|
||||
clock.advance(120_000);
|
||||
await Promise.resolve();
|
||||
assert.deepEqual(closed, []);
|
||||
assert.equal(clock.timerCount(), 0);
|
||||
|
||||
manager.endActivity("chat-a", "session-1");
|
||||
clock.advance(59_999);
|
||||
await Promise.resolve();
|
||||
assert.deepEqual(closed, []);
|
||||
clock.advance(1);
|
||||
await Promise.resolve();
|
||||
assert.deepEqual(closed, [{ chatSessionId: "chat-a", sessionId: "session-1" }]);
|
||||
});
|
||||
|
||||
test("activity from another authorized scope renews the same terminal session", async () => {
|
||||
const clock = createClock();
|
||||
const closed = [];
|
||||
const manager = createSessionIdleManager({
|
||||
...clock,
|
||||
timeoutMinutes: 1,
|
||||
onIdle: async (entry) => closed.push(entry),
|
||||
});
|
||||
|
||||
manager.track("chat-owner", "session-1");
|
||||
clock.advance(30_000);
|
||||
assert.equal(manager.beginActivity("chat-other", "session-1"), true);
|
||||
manager.endActivity("chat-other", "session-1");
|
||||
|
||||
clock.advance(59_999);
|
||||
await Promise.resolve();
|
||||
assert.deepEqual(closed, []);
|
||||
clock.advance(1);
|
||||
await Promise.resolve();
|
||||
assert.deepEqual(closed, [{ chatSessionId: "chat-owner", sessionId: "session-1" }]);
|
||||
});
|
||||
|
||||
test("activity starting while an idle check is pending prevents the close", async () => {
|
||||
const clock = createClock();
|
||||
const closed = [];
|
||||
let releaseIdleCheck;
|
||||
const idleCheck = new Promise((resolve) => {
|
||||
releaseIdleCheck = resolve;
|
||||
});
|
||||
let manager;
|
||||
manager = createSessionIdleManager({
|
||||
...clock,
|
||||
timeoutMinutes: 1,
|
||||
onIdle: async (entry) => {
|
||||
await idleCheck;
|
||||
if (!manager.hasActivity(entry.sessionId)) closed.push(entry);
|
||||
},
|
||||
});
|
||||
|
||||
manager.track("chat-owner", "session-1");
|
||||
clock.advance(60_000);
|
||||
assert.equal(manager.beginActivity("chat-other", "session-1"), true);
|
||||
releaseIdleCheck();
|
||||
await idleCheck;
|
||||
await Promise.resolve();
|
||||
assert.deepEqual(closed, []);
|
||||
|
||||
manager.endActivity("chat-other", "session-1");
|
||||
clock.advance(60_000);
|
||||
await Promise.resolve();
|
||||
assert.deepEqual(closed, [{ chatSessionId: "chat-owner", sessionId: "session-1" }]);
|
||||
});
|
||||
|
||||
test("activity that starts and ends during an idle check invalidates the stale check", async () => {
|
||||
const clock = createClock();
|
||||
const closed = [];
|
||||
let releaseIdleCheck;
|
||||
const idleCheck = new Promise((resolve) => {
|
||||
releaseIdleCheck = resolve;
|
||||
});
|
||||
let manager;
|
||||
manager = createSessionIdleManager({
|
||||
...clock,
|
||||
timeoutMinutes: 1,
|
||||
onIdle: async (entry, activityVersion) => {
|
||||
await idleCheck;
|
||||
if (manager.beginIdleClose(entry.sessionId, activityVersion)) {
|
||||
closed.push(entry);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
manager.track("chat-owner", "session-1");
|
||||
clock.advance(60_000);
|
||||
assert.equal(manager.beginActivity("chat-other", "session-1"), true);
|
||||
assert.equal(manager.endActivity("chat-other", "session-1"), true);
|
||||
releaseIdleCheck();
|
||||
await idleCheck;
|
||||
await Promise.resolve();
|
||||
assert.deepEqual(closed, []);
|
||||
|
||||
clock.advance(59_999);
|
||||
await Promise.resolve();
|
||||
assert.deepEqual(closed, []);
|
||||
clock.advance(1);
|
||||
await Promise.resolve();
|
||||
assert.deepEqual(closed, [{ chatSessionId: "chat-owner", sessionId: "session-1" }]);
|
||||
});
|
||||
|
||||
test("a failed close resumes tracking while a successful close can be forgotten", async () => {
|
||||
const clock = createClock();
|
||||
let attempts = 0;
|
||||
const manager = createSessionIdleManager({
|
||||
...clock,
|
||||
timeoutMinutes: 1,
|
||||
onIdle: async ({ sessionId }) => {
|
||||
attempts += 1;
|
||||
if (attempts === 1) manager.resume(sessionId);
|
||||
else manager.forgetSession(sessionId);
|
||||
},
|
||||
});
|
||||
|
||||
manager.track("chat-a", "session-1");
|
||||
clock.advance(60_000);
|
||||
await Promise.resolve();
|
||||
assert.equal(attempts, 1);
|
||||
assert.equal(manager.isTracked("session-1"), true);
|
||||
|
||||
clock.advance(60_000);
|
||||
await Promise.resolve();
|
||||
assert.equal(attempts, 2);
|
||||
assert.equal(manager.isTracked("session-1"), false);
|
||||
assert.equal(clock.timerCount(), 0);
|
||||
});
|
||||
|
||||
test("clearing an AI scope does not discard its idle cleanup fallback", async () => {
|
||||
const clock = createClock();
|
||||
const closed = [];
|
||||
const manager = createSessionIdleManager({
|
||||
...clock,
|
||||
timeoutMinutes: 1,
|
||||
onIdle: async (entry) => closed.push(entry),
|
||||
});
|
||||
|
||||
manager.track("chat-a", "session-1");
|
||||
manager.scopeCleared("chat-a");
|
||||
clock.advance(60_000);
|
||||
await Promise.resolve();
|
||||
|
||||
assert.deepEqual(closed, [{ chatSessionId: "chat-a", sessionId: "session-1" }]);
|
||||
});
|
||||
Reference in New Issue
Block a user