Files
Netcatty/electron/bridges/rendererSessionLifetime.test.cjs
T
KenX049andbincxz bcce829294 feat(tabs): move a tab to its own window without reconnecting (#4202)
* feat(tabs): move a tab to its own window without reconnecting (#4201)

Adds "Move Tab to New Window" to the tab context menu. Instead of opening
a new connection like "Copy Tab to New Window", the tab is hidden from the
tab bar and an attach popup takes over the same live PTY, so bastion
sessions are not re-authenticated and no extra seat is used. Closing the
popup returns the tab (with its screen state) to the window it came from.

- popup lifecycle notifies the home window via terminal-popup:attach-closed
- new runtime poppedOut flag; popped-out tabs still persist for restore and
  are not labelled as AI silent sessions in the tray

* fix(tabs): keep moved tabs alive when their source window closes

- A moved-tab popup registers as the lifetime heir of its session. If the
  source window is destroyed, ownership passes to the popup instead of the
  session being closed (worker and in-process paths). AI observe popups are
  unchanged.
- If the source window is gone when the popup closes, skip the hand-back so
  the popup's ownership closes the session.
- Only offer/allow Move Tab to New Window for connected sessions; attach
  mode cannot connect or reconnect a backend.

* fix(tabs): expose Move Tab to New Window through TerminalHost

The handler was registered but not passed into the terminal domain bag,
so AppView received undefined and the menu item never rendered.

* fix(tabs): address move-to-window focus, crash ownership, live count, history, and AI context review findings

* fix(tabs): activate moved-tab lifetime heir only after output rebind succeeds

* fix(tabs): wait for popup attachment before hiding tab and exclude plugin sessions

* fix(tabs): confirm renderer reattach before resolving move and await output restore before unhiding tab

* fix(tabs): preserve detached output across popup crashes and avoid focusing main window from tray

* fix(tabs): deliver attach snapshots and output restore directly to peer session windows

* fix(tabs): schedule automatic output restore retries when popup recovery fails

* fix(tabs): handle reopen replay preservation, stale restore cancellation, startup workspace move, and shutdown log capture

* fix(tabs): reassert popped-out state after tray reopen

* test(terminal): provide log finalizer stubs in popup snapshot harness

---------

Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
2026-11-24 10:50:36 +08:00

130 lines
4.9 KiB
JavaScript

const assert = require("node:assert/strict");
const EventEmitter = require("node:events");
const test = require("node:test");
const { trackRendererSessionLifetime: track } = require("./rendererSessionLifetime.cjs");
function renderer(id) {
const contents = new EventEmitter();
contents.id = id;
contents.destroyed = false;
contents.isDestroyed = () => contents.destroyed;
contents.destroy = () => { contents.destroyed = true; contents.emit("destroyed"); };
return contents;
}
function fixture() {
const sessions = new Map();
const closed = [];
const close = (_event, payload) => { closed.push(payload); sessions.delete(payload.sessionId); };
const own = (id, contents, bootEpoch = 1) => {
sessions.set(id, { bootEpoch });
track(sessions, id, contents, close);
};
return { sessions, closed, close, own };
}
test("fallback closes only session objects owned by the destroyed renderer", () => {
const f = fixture();
const first = renderer(7), second = renderer(8);
f.own("a", first);
f.own("b", first);
f.own("c", second);
assert.equal(first.listenerCount("destroyed"), 1);
first.destroy();
assert.deepEqual(f.closed, [{ sessionId: "a", bootEpoch: 1 }, { sessionId: "b", bootEpoch: 1 }]);
assert.equal(f.sessions.has("c"), true);
});
test("fallback display rebind is not an ownership handoff; explicit home restore is", () => {
const f = fixture();
const home = renderer(7), popup = renderer(9), nextHome = renderer(8);
f.own("a", home);
track(f.sessions, "a", popup, f.close);
popup.destroy();
assert.equal(f.closed.length, 0);
track(f.sessions, "a", nextHome, f.close, true);
home.destroy();
assert.equal(f.closed.length, 0);
nextHome.destroy();
assert.equal(f.closed.length, 1);
});
test("fallback old owner cannot close a same-ID replacement or synthetic agent session", () => {
const f = fixture();
const home = renderer(7), nextHome = renderer(8);
f.own("a", home);
f.own("a", nextHome, 2);
f.own("agent", { id: 7 });
home.destroy();
assert.equal(f.closed.length, 0);
nextHome.destroy();
assert.deepEqual(f.closed, [{ sessionId: "a", bootEpoch: 2 }]);
assert.equal(f.sessions.has("agent"), true);
});
test("fallback late start into a destroyed renderer closes after setup, not a replacement", async () => {
const f = fixture();
const home = renderer(7), nextHome = renderer(8);
home.destroy();
f.own("late", home);
assert.equal(f.closed.length, 0);
await Promise.resolve();
assert.deepEqual(f.closed, [{ sessionId: "late", bootEpoch: 1 }]);
f.own("replacement", home);
f.own("replacement", nextHome, 2);
await Promise.resolve();
assert.equal(f.closed.length, 1);
assert.equal(f.sessions.has("replacement"), true);
});
test("a moved-tab popup inherits the session only after attaching", () => {
const electronPath = require.resolve("electron");
const previous = require.cache[electronPath];
const popup = renderer(9);
require.cache[electronPath] = {
id: electronPath, filename: electronPath, loaded: true,
exports: { webContents: { fromId: (id) => (id === popup.id ? popup : null) } },
};
const attach = require("./terminalAttachRestore.cjs");
try {
// Before output is rebound, destroying home closes the session instead of stranding it.
const unattached = fixture();
const earlyHome = renderer(6);
unattached.own("early", earlyHome);
attach.registerAttachPopupAuthorization("grant-early", "early", popup.id, { ownsLifetime: true });
assert.equal(attach.getLifetimeHeirWebContentsId("early"), null);
earlyHome.destroy();
assert.deepEqual(unattached.closed, [{ sessionId: "early", bootEpoch: 1 }]);
attach.releaseAttachPopupAuthorization("grant-early");
// Once output is rebound (attached), destroying home transfers lifetime to the popup.
const f = fixture();
const home = renderer(7);
f.own("a", home);
attach.registerAttachPopupAuthorization("grant", "a", popup.id, { ownsLifetime: true });
assert.equal(attach.markAttachPopupAttached("grant", "a", popup.id), true);
assert.equal(attach.getLifetimeHeirWebContentsId("a"), popup.id);
home.destroy();
assert.equal(f.closed.length, 0);
attach.releaseAttachPopupAuthorization("grant");
popup.destroy();
assert.deepEqual(f.closed, [{ sessionId: "a", bootEpoch: 1 }]);
} finally {
attach.releaseAttachPopupAuthorization("grant-early");
attach.releaseAttachPopupAuthorization("grant");
if (previous) require.cache[electronPath] = previous;
else delete require.cache[electronPath];
}
});
test("an AI observe popup does not inherit the session", () => {
const attach = require("./terminalAttachRestore.cjs");
attach.registerAttachPopupAuthorization("observe", "b", 9);
attach.markAttachPopupAttached("observe", "b", 9);
try {
assert.equal(attach.getLifetimeHeirWebContentsId("b"), null);
} finally {
attach.releaseAttachPopupAuthorization("observe");
}
});