mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-26 19:07:17 +00:00
* 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>
351 lines
11 KiB
TypeScript
351 lines
11 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import type { TerminalSession } from "../domain/models";
|
|
import { copySessionToNewWindowWithCurrentShellImpl, moveSessionToNewWindowImpl } from "./app/AppHandlers";
|
|
|
|
const sourceSession = (overrides: Partial<TerminalSession> = {}): TerminalSession => ({
|
|
id: "session-1",
|
|
hostId: "host-1",
|
|
hostLabel: "Prod SSH",
|
|
hostname: "prod.example.com",
|
|
username: "deploy",
|
|
status: "connected",
|
|
protocol: "ssh",
|
|
port: 22,
|
|
...overrides,
|
|
});
|
|
|
|
test("copySessionToNewWindowWithCurrentShellImpl asks Electron to open a peer window for the selected session", async () => {
|
|
const openedPayloads: unknown[] = [];
|
|
|
|
await copySessionToNewWindowWithCurrentShellImpl(
|
|
() => ({
|
|
classifyLocalShellType: () => "zsh",
|
|
discoveredShells: [],
|
|
netcattyBridge: {
|
|
get: () => ({
|
|
openSessionInNewWindow: async (payload: unknown) => {
|
|
openedPayloads.push(payload);
|
|
return { success: true };
|
|
},
|
|
}),
|
|
},
|
|
resolveShellSetting: () => ({ command: "/bin/zsh" }),
|
|
sessions: [sourceSession()],
|
|
terminalSettings: { localShell: "system-default" },
|
|
}),
|
|
"session-1",
|
|
);
|
|
|
|
assert.equal(openedPayloads.length, 1);
|
|
assert.deepEqual(openedPayloads[0], {
|
|
title: "Prod SSH",
|
|
sourceSession: sourceSession(),
|
|
localShellType: "zsh",
|
|
});
|
|
});
|
|
|
|
test("copySessionToNewWindowWithCurrentShellImpl preserves local start directory in the source session", async () => {
|
|
const openedPayloads: unknown[] = [];
|
|
const localSession = sourceSession({
|
|
hostLabel: "Local Terminal",
|
|
hostname: "localhost",
|
|
protocol: "local",
|
|
localStartDir: "/Users/alice/project with spaces ",
|
|
});
|
|
|
|
await copySessionToNewWindowWithCurrentShellImpl(
|
|
() => ({
|
|
classifyLocalShellType: () => "zsh",
|
|
discoveredShells: [],
|
|
netcattyBridge: {
|
|
get: () => ({
|
|
openSessionInNewWindow: async (payload: unknown) => {
|
|
openedPayloads.push(payload);
|
|
return { success: true };
|
|
},
|
|
}),
|
|
},
|
|
resolveShellSetting: () => ({ command: "/bin/zsh" }),
|
|
sessions: [localSession],
|
|
terminalSettings: { localShell: "system-default" },
|
|
}),
|
|
"session-1",
|
|
);
|
|
|
|
assert.equal(openedPayloads.length, 1);
|
|
assert.deepEqual(openedPayloads[0], {
|
|
title: "Local Terminal",
|
|
sourceSession: localSession,
|
|
localShellType: "zsh",
|
|
});
|
|
});
|
|
|
|
test("copySessionToNewWindowWithCurrentShellImpl does nothing when the source session is gone", async () => {
|
|
let called = false;
|
|
|
|
await copySessionToNewWindowWithCurrentShellImpl(
|
|
() => ({
|
|
classifyLocalShellType: () => "zsh",
|
|
discoveredShells: [],
|
|
netcattyBridge: {
|
|
get: () => ({
|
|
openSessionInNewWindow: async () => {
|
|
called = true;
|
|
return { success: true };
|
|
},
|
|
}),
|
|
},
|
|
resolveShellSetting: () => ({ command: "/bin/zsh" }),
|
|
sessions: [],
|
|
terminalSettings: { localShell: "system-default" },
|
|
}),
|
|
"missing-session",
|
|
);
|
|
|
|
assert.equal(called, false);
|
|
});
|
|
|
|
test("copySessionToNewWindowWithCurrentShellImpl shows an error when Electron cannot open the window", async () => {
|
|
const errors: string[] = [];
|
|
|
|
const result = await copySessionToNewWindowWithCurrentShellImpl(
|
|
() => ({
|
|
classifyLocalShellType: () => "zsh",
|
|
discoveredShells: [],
|
|
netcattyBridge: {
|
|
get: () => ({
|
|
openSessionInNewWindow: async () => ({ success: false }),
|
|
}),
|
|
},
|
|
resolveShellSetting: () => ({ command: "/bin/zsh" }),
|
|
sessions: [sourceSession()],
|
|
terminalSettings: { localShell: "system-default" },
|
|
t: (key: string) => key === "tabs.copyTabToNewWindowFailed" ? "Could not open" : key,
|
|
toast: {
|
|
error: (message: string) => errors.push(message),
|
|
},
|
|
}),
|
|
"session-1",
|
|
);
|
|
|
|
assert.equal(result, false);
|
|
assert.deepEqual(errors, ["Could not open"]);
|
|
});
|
|
|
|
test("copySessionToNewWindowWithCurrentShellImpl shows an error when the bridge is unavailable", async () => {
|
|
const errors: string[] = [];
|
|
|
|
const result = await copySessionToNewWindowWithCurrentShellImpl(
|
|
() => ({
|
|
classifyLocalShellType: () => "zsh",
|
|
discoveredShells: [],
|
|
netcattyBridge: {
|
|
get: () => ({}),
|
|
},
|
|
resolveShellSetting: () => ({ command: "/bin/zsh" }),
|
|
sessions: [sourceSession()],
|
|
terminalSettings: { localShell: "system-default" },
|
|
t: (key: string) => key === "tabs.copyTabToNewWindowFailed" ? "Could not open" : key,
|
|
toast: {
|
|
error: (message: string) => errors.push(message),
|
|
},
|
|
}),
|
|
"session-1",
|
|
);
|
|
|
|
assert.equal(result, false);
|
|
assert.deepEqual(errors, ["Could not open"]);
|
|
});
|
|
|
|
test("copySessionToNewWindowWithCurrentShellImpl shows an error when the bridge throws", async () => {
|
|
const errors: string[] = [];
|
|
|
|
const result = await copySessionToNewWindowWithCurrentShellImpl(
|
|
() => ({
|
|
classifyLocalShellType: () => "zsh",
|
|
discoveredShells: [],
|
|
netcattyBridge: {
|
|
get: () => ({
|
|
openSessionInNewWindow: async () => {
|
|
throw new Error("boom");
|
|
},
|
|
}),
|
|
},
|
|
resolveShellSetting: () => ({ command: "/bin/zsh" }),
|
|
sessions: [sourceSession()],
|
|
terminalSettings: { localShell: "system-default" },
|
|
t: (key: string) => key === "tabs.copyTabToNewWindowFailed" ? "Could not open" : key,
|
|
toast: {
|
|
error: (message: string) => errors.push(message),
|
|
},
|
|
}),
|
|
"session-1",
|
|
);
|
|
|
|
assert.equal(result, false);
|
|
assert.deepEqual(errors, ["Could not open"]);
|
|
});
|
|
|
|
const moveCtx = (openTerminalPopup: ((payload: Record<string, unknown>) => Promise<unknown>) | undefined) => {
|
|
const calls: unknown[][] = [];
|
|
const errors: string[] = [];
|
|
const getCtx = () => ({
|
|
netcattyBridge: { get: () => (openTerminalPopup ? { openTerminalPopup } : {}) },
|
|
sessions: [sourceSession({ id: "other" }), sourceSession()],
|
|
setActiveTabId: (id: string) => calls.push(["active", id]),
|
|
setSessionPoppedOut: (id: string, poppedOut: boolean) => calls.push(["poppedOut", id, poppedOut]),
|
|
t: (key: string) => key,
|
|
toast: { error: (message: string) => errors.push(message) },
|
|
});
|
|
return { calls, errors, getCtx };
|
|
};
|
|
|
|
test("moveSessionToNewWindowImpl hides the tab and attaches a popup to the same live session", async () => {
|
|
const payloads: Record<string, unknown>[] = [];
|
|
const { calls, errors, getCtx } = moveCtx(async (payload) => {
|
|
payloads.push(payload);
|
|
return { success: true };
|
|
});
|
|
|
|
assert.equal(await moveSessionToNewWindowImpl(getCtx, "session-1"), true);
|
|
assert.deepEqual(calls, [["poppedOut", "session-1", true], ["active", "other"]]);
|
|
assert.equal(payloads[0].attachSessionId, "session-1");
|
|
assert.equal(payloads[0].moveSession, true);
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
|
|
test("moveSessionToNewWindowImpl leaves the tab untouched when the popup fails to attach", async () => {
|
|
const { calls, errors, getCtx } = moveCtx(async () => ({ success: false, error: "boom" }));
|
|
|
|
assert.equal(await moveSessionToNewWindowImpl(getCtx, "session-1"), false);
|
|
assert.deepEqual(calls, []);
|
|
assert.deepEqual(errors, ["boom"]);
|
|
});
|
|
|
|
test("moveSessionToNewWindowImpl waits for popup attachment before hiding the tab", async () => {
|
|
let resolveOpen: ((value: unknown) => void) | null = null;
|
|
const { calls, getCtx } = moveCtx(
|
|
() => new Promise((resolve) => { resolveOpen = resolve; }),
|
|
);
|
|
|
|
const pending = moveSessionToNewWindowImpl(getCtx, "session-1");
|
|
await Promise.resolve();
|
|
assert.deepEqual(calls, []);
|
|
|
|
resolveOpen?.({ success: true });
|
|
assert.equal(await pending, true);
|
|
assert.deepEqual(calls, [["poppedOut", "session-1", true], ["active", "other"]]);
|
|
});
|
|
|
|
test("moveSessionToNewWindowImpl ignores tabs without a live backend", async () => {
|
|
const { calls, getCtx } = moveCtx(async () => ({ success: true }));
|
|
const ctx = getCtx();
|
|
const result = await moveSessionToNewWindowImpl(
|
|
() => ({ ...ctx, sessions: [sourceSession({ status: "disconnected" })] }),
|
|
"session-1",
|
|
);
|
|
assert.equal(result, false);
|
|
assert.deepEqual(calls, []);
|
|
});
|
|
|
|
test("moveSessionToNewWindowImpl ignores plugin-backed sessions", async () => {
|
|
const { calls, getCtx } = moveCtx(async () => ({ success: true }));
|
|
const ctx = getCtx();
|
|
const result = await moveSessionToNewWindowImpl(
|
|
() => ({ ...ctx, sessions: [sourceSession({ protocol: "plugin:com.acme.db" })] }),
|
|
"session-1",
|
|
);
|
|
assert.equal(result, false);
|
|
assert.deepEqual(calls, []);
|
|
});
|
|
|
|
test("moveSessionToNewWindowImpl ignores workspace panes", async () => {
|
|
const { calls, getCtx } = moveCtx(async () => ({ success: true }));
|
|
const ctx = getCtx();
|
|
const result = await moveSessionToNewWindowImpl(
|
|
() => ({ ...ctx, sessions: [sourceSession({ workspaceId: "ws-1" })] }),
|
|
"session-1",
|
|
);
|
|
assert.equal(result, false);
|
|
assert.deepEqual(calls, []);
|
|
});
|
|
|
|
test("moveSessionToNewWindowImpl preserves active tab focus when moving an inactive tab", async () => {
|
|
const { calls, errors, getCtx } = moveCtx(async () => ({ success: true }));
|
|
const ctx = getCtx();
|
|
const result = await moveSessionToNewWindowImpl(
|
|
() => ({
|
|
...ctx,
|
|
getActiveTabId: () => "ws-1",
|
|
getOrderedTabs: () => ["ws-1", "session-1"],
|
|
}),
|
|
"session-1",
|
|
);
|
|
assert.equal(result, true);
|
|
assert.deepEqual(calls, [["poppedOut", "session-1", true]]);
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
|
|
test("moveSessionToNewWindowImpl does not steal focus when moving an inactive tab fails", async () => {
|
|
const { calls, errors, getCtx } = moveCtx(async () => ({ success: false, error: "nope" }));
|
|
const ctx = getCtx();
|
|
const result = await moveSessionToNewWindowImpl(
|
|
() => ({
|
|
...ctx,
|
|
getActiveTabId: () => "editor:1",
|
|
getOrderedTabs: () => ["editor:1", "session-1"],
|
|
}),
|
|
"session-1",
|
|
);
|
|
assert.equal(result, false);
|
|
assert.deepEqual(calls, []);
|
|
assert.deepEqual(errors, ["nope"]);
|
|
});
|
|
|
|
test("moveSessionToNewWindowImpl selects the adjacent ordered visible tab when moving the active tab", async () => {
|
|
const { calls, getCtx } = moveCtx(async () => ({ success: true }));
|
|
const ctx = getCtx();
|
|
const result = await moveSessionToNewWindowImpl(
|
|
() => ({
|
|
...ctx,
|
|
sessions: [sourceSession()],
|
|
getActiveTabId: () => "session-1",
|
|
getOrderedTabs: () => ["ws-1", "session-1", "editor:1"],
|
|
}),
|
|
"session-1",
|
|
);
|
|
assert.equal(result, true);
|
|
assert.deepEqual(calls, [["poppedOut", "session-1", true], ["active", "ws-1"]]);
|
|
});
|
|
|
|
test("moveSessionToNewWindowImpl closes the popup if the tab moved into a workspace during startup", async () => {
|
|
const closedPopups: string[] = [];
|
|
let sessions: TerminalSession[] = [sourceSession()];
|
|
const calls: unknown[][] = [];
|
|
const getCtx = () => ({
|
|
netcattyBridge: {
|
|
get: () => ({
|
|
openTerminalPopup: async () => {
|
|
sessions = [sourceSession({ workspaceId: "ws-late" })];
|
|
return { success: true, popupId: "popup-late" };
|
|
},
|
|
closeTerminalPopup: async (popupId: string) => {
|
|
closedPopups.push(popupId);
|
|
return { success: true };
|
|
},
|
|
}),
|
|
},
|
|
sessions,
|
|
setActiveTabId: (id: string) => calls.push(["active", id]),
|
|
setSessionPoppedOut: (id: string, poppedOut: boolean) => calls.push(["poppedOut", id, poppedOut]),
|
|
t: (key: string) => key,
|
|
toast: { error: () => {} },
|
|
});
|
|
|
|
assert.equal(await moveSessionToNewWindowImpl(getCtx, "session-1"), false);
|
|
assert.deepEqual(closedPopups, ["popup-late"]);
|
|
assert.deepEqual(calls, []);
|
|
});
|