mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-26 10:53:56 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd6713cbad | ||
|
|
399f6d6ce5 |
No files matched your search
@@ -7,7 +7,9 @@ import {
|
||||
closeSessionsState,
|
||||
closeSessionWorkspaceLayoutState,
|
||||
detachSessionFromWorkspaceState,
|
||||
isPreferredTabAvailableAfterClose,
|
||||
resolveActiveTabAfterCloseSession,
|
||||
resolveActiveTabAfterCloseWorkspace,
|
||||
} from "./sessionWorkspaceDetach";
|
||||
|
||||
const session = (id: string, workspaceId = "ws-1"): TerminalSession => ({
|
||||
@@ -308,3 +310,188 @@ test("closing a subset of standalone sessions keeps tabs outside the batch", ()
|
||||
assert.deepEqual(result.sessions.map((candidate) => candidate.id), ["s1"]);
|
||||
assert.equal(result.activeTabId, undefined);
|
||||
});
|
||||
|
||||
test("issue #4249 scenario 1: closing terminal tabs right-to-left focuses B -> A -> vault", () => {
|
||||
const standaloneSession = (id: string): TerminalSession => ({
|
||||
...session(id),
|
||||
workspaceId: undefined,
|
||||
});
|
||||
|
||||
const step1 = closeSessionsState({
|
||||
sessions: [standaloneSession("a"), standaloneSession("b"), standaloneSession("c")],
|
||||
workspaces: [],
|
||||
sessionIds: ["c"],
|
||||
currentActiveTabId: "c",
|
||||
preferredTabId: "b",
|
||||
tabOrder: ["a", "b", "c"],
|
||||
});
|
||||
assert.equal(step1.activeTabId, "b");
|
||||
|
||||
const step2 = closeSessionsState({
|
||||
sessions: step1.sessions,
|
||||
workspaces: step1.workspaces,
|
||||
sessionIds: ["b"],
|
||||
currentActiveTabId: step1.activeTabId ?? null,
|
||||
preferredTabId: "b",
|
||||
tabOrder: step1.tabOrder,
|
||||
});
|
||||
assert.equal(step2.activeTabId, "a");
|
||||
|
||||
const step3 = closeSessionsState({
|
||||
sessions: step2.sessions,
|
||||
workspaces: step2.workspaces,
|
||||
sessionIds: ["a"],
|
||||
currentActiveTabId: step2.activeTabId ?? null,
|
||||
preferredTabId: "b",
|
||||
tabOrder: step2.tabOrder,
|
||||
});
|
||||
assert.equal(step3.activeTabId, "vault");
|
||||
});
|
||||
|
||||
test("issue #4249 scenario 2: after visiting vault or sftp, closing C -> B -> A stays on adjacent terminals until the last tab closes", () => {
|
||||
const standaloneSession = (id: string): TerminalSession => ({
|
||||
...session(id),
|
||||
workspaceId: undefined,
|
||||
});
|
||||
|
||||
for (const rootView of ["vault", "sftp"] as const) {
|
||||
const step1 = closeSessionsState({
|
||||
sessions: [standaloneSession("a"), standaloneSession("b"), standaloneSession("c")],
|
||||
workspaces: [],
|
||||
sessionIds: ["c"],
|
||||
currentActiveTabId: "c",
|
||||
preferredTabId: rootView,
|
||||
tabOrder: ["a", "b", "c"],
|
||||
});
|
||||
assert.equal(step1.activeTabId, "b", `closing C after ${rootView} should focus B`);
|
||||
|
||||
const step2 = closeSessionsState({
|
||||
sessions: step1.sessions,
|
||||
workspaces: step1.workspaces,
|
||||
sessionIds: ["b"],
|
||||
currentActiveTabId: step1.activeTabId ?? null,
|
||||
preferredTabId: rootView,
|
||||
tabOrder: step1.tabOrder,
|
||||
});
|
||||
assert.equal(step2.activeTabId, "a", `closing B after ${rootView} should focus A`);
|
||||
|
||||
const step3 = closeSessionsState({
|
||||
sessions: step2.sessions,
|
||||
workspaces: step2.workspaces,
|
||||
sessionIds: ["a"],
|
||||
currentActiveTabId: step2.activeTabId ?? null,
|
||||
preferredTabId: rootView,
|
||||
tabOrder: step2.tabOrder,
|
||||
});
|
||||
assert.equal(step3.activeTabId, rootView, `closing last tab A should fall back to ${rootView}`);
|
||||
}
|
||||
});
|
||||
|
||||
test("closing a terminal tab follows visual tabOrder instead of raw session/workspace arrays", () => {
|
||||
const standaloneSession = (id: string): TerminalSession => ({
|
||||
...session(id),
|
||||
workspaceId: undefined,
|
||||
});
|
||||
|
||||
// Raw arrays have [s1, s2] and [ws-1], while visual tab order is [ws-1, s1, s2].
|
||||
const result = closeSessionsState({
|
||||
sessions: [session("ws-pane", "ws-1"), standaloneSession("s1"), standaloneSession("s2")],
|
||||
workspaces: [workspace(["ws-pane"])],
|
||||
sessionIds: ["s2"],
|
||||
currentActiveTabId: "s2",
|
||||
preferredTabId: "vault",
|
||||
tabOrder: ["ws-1", "s1", "s2"],
|
||||
});
|
||||
|
||||
// Must focus adjacent terminal s1, not jump over s1 to ws-1 or vault.
|
||||
assert.equal(result.activeTabId, "s1");
|
||||
});
|
||||
|
||||
test("closing a middle or leftmost terminal tab picks left neighbor first, then right neighbor", () => {
|
||||
const standaloneSession = (id: string): TerminalSession => ({
|
||||
...session(id),
|
||||
workspaceId: undefined,
|
||||
});
|
||||
|
||||
const closeMiddle = closeSessionsState({
|
||||
sessions: [standaloneSession("a"), standaloneSession("b"), standaloneSession("c")],
|
||||
workspaces: [],
|
||||
sessionIds: ["b"],
|
||||
currentActiveTabId: "b",
|
||||
preferredTabId: "sftp",
|
||||
tabOrder: ["a", "b", "c"],
|
||||
});
|
||||
assert.equal(closeMiddle.activeTabId, "a");
|
||||
|
||||
const closeLeftmost = closeSessionsState({
|
||||
sessions: closeMiddle.sessions,
|
||||
workspaces: [],
|
||||
sessionIds: ["a"],
|
||||
currentActiveTabId: "a",
|
||||
preferredTabId: "sftp",
|
||||
tabOrder: closeMiddle.tabOrder,
|
||||
});
|
||||
assert.equal(closeLeftmost.activeTabId, "c");
|
||||
});
|
||||
|
||||
test("resolveActiveTabAfterCloseWorkspace focuses adjacent terminal/workspace before falling back to root view", () => {
|
||||
const standaloneSession = (id: string): TerminalSession => ({
|
||||
...session(id),
|
||||
workspaceId: undefined,
|
||||
});
|
||||
const ws1 = workspace(["p1"], { id: "ws-1" });
|
||||
|
||||
assert.equal(
|
||||
resolveActiveTabAfterCloseWorkspace({
|
||||
currentActiveTabId: "ws-1",
|
||||
closedWorkspaceId: "ws-1",
|
||||
previousWorkspaces: [ws1],
|
||||
remainingWorkspaces: [],
|
||||
remainingSessions: [standaloneSession("s1"), standaloneSession("s2")],
|
||||
tabOrder: ["s1", "ws-1", "s2"],
|
||||
preferredTabId: "sftp",
|
||||
}),
|
||||
"s1",
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
resolveActiveTabAfterCloseWorkspace({
|
||||
currentActiveTabId: "ws-1",
|
||||
closedWorkspaceId: "ws-1",
|
||||
previousWorkspaces: [ws1],
|
||||
remainingWorkspaces: [],
|
||||
remainingSessions: [],
|
||||
tabOrder: ["ws-1"],
|
||||
preferredTabId: "sftp",
|
||||
}),
|
||||
"sftp",
|
||||
);
|
||||
});
|
||||
|
||||
test("isPreferredTabAvailableAfterClose does not treat vault or sftp as surviving terminal tabs", () => {
|
||||
const standaloneSession = (id: string): TerminalSession => ({
|
||||
...session(id),
|
||||
workspaceId: undefined,
|
||||
});
|
||||
const layoutResult = closeSessionWorkspaceLayoutState([], undefined, "c");
|
||||
const remainingSessions = [standaloneSession("a"), standaloneSession("b")];
|
||||
|
||||
assert.equal(
|
||||
isPreferredTabAvailableAfterClose({
|
||||
preferredTabId: "vault",
|
||||
closedSessionId: "c",
|
||||
layoutResult,
|
||||
remainingSessions,
|
||||
}),
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
isPreferredTabAvailableAfterClose({
|
||||
preferredTabId: "sftp",
|
||||
closedSessionId: "c",
|
||||
layoutResult,
|
||||
remainingSessions,
|
||||
}),
|
||||
false,
|
||||
);
|
||||
});
|
||||
@@ -1,6 +1,10 @@
|
||||
import type { TerminalSession, Workspace } from "../../domain/models";
|
||||
import type { AIChatRecencyEntry } from "../../domain/aiSessionRecency";
|
||||
import { resolveMostRecentlyActiveMemberTerminalId } from "../../domain/aiSessionRecency";
|
||||
import {
|
||||
buildOrderedTerminalTabIds,
|
||||
resolveNextActiveTabAfterTerminalClose,
|
||||
} from "../../domain/tabCloseFocus";
|
||||
import { collectSessionIds, createEmptyWorkspaceRoot, pruneWorkspaceNode } from "../../domain/workspace";
|
||||
|
||||
export type DetachSessionFromWorkspaceStateResult = {
|
||||
@@ -128,7 +132,6 @@ export function isPreferredTabAvailableAfterClose(
|
||||
} {
|
||||
const { preferredTabId, closedSessionId, layoutResult, remainingSessions } = input;
|
||||
if (!preferredTabId || preferredTabId === closedSessionId) return false;
|
||||
if (preferredTabId === "vault" || preferredTabId === "sftp") return true;
|
||||
if (layoutResult.workspaces.some((workspace) => workspace.id === preferredTabId)) return true;
|
||||
return remainingSessions.some((session) => (
|
||||
session.id === preferredTabId
|
||||
@@ -143,6 +146,8 @@ export function resolveActiveTabAfterCloseSession({
|
||||
layoutResult,
|
||||
remainingSessions,
|
||||
preferredTabId,
|
||||
orderedTabIdsBeforeClose,
|
||||
tabOrder,
|
||||
}: {
|
||||
currentActiveTabId: string | null;
|
||||
closedSessionId: string;
|
||||
@@ -150,30 +155,61 @@ export function resolveActiveTabAfterCloseSession({
|
||||
layoutResult: CloseSessionWorkspaceLayoutResult;
|
||||
remainingSessions: readonly TerminalSession[];
|
||||
preferredTabId?: string | null;
|
||||
orderedTabIdsBeforeClose?: readonly string[];
|
||||
tabOrder?: readonly string[];
|
||||
}): string | null {
|
||||
const fallbackWorkspace = layoutResult.workspaces[layoutResult.workspaces.length - 1];
|
||||
const fallbackSolo = remainingSessions.filter((session) => !session.workspaceId && !session.hiddenFromTabs).slice(-1)[0];
|
||||
const preferredInput = {
|
||||
preferredTabId,
|
||||
closedSessionId,
|
||||
layoutResult,
|
||||
remainingSessions,
|
||||
};
|
||||
const preferredFallback = isPreferredTabAvailableAfterClose(preferredInput)
|
||||
? preferredInput.preferredTabId
|
||||
: undefined;
|
||||
const fallback = preferredFallback
|
||||
?? fallbackWorkspace?.id
|
||||
?? fallbackSolo?.id
|
||||
?? "vault";
|
||||
const survivingTabIds = buildOrderedTerminalTabIds({
|
||||
sessions: remainingSessions,
|
||||
workspaces: layoutResult.workspaces,
|
||||
tabOrder,
|
||||
});
|
||||
|
||||
// Workspaces survive member loss (#4098 phase 2), so only a closed session
|
||||
// tab that was active needs a replacement.
|
||||
if (currentActiveTabId === closedSessionId) {
|
||||
return fallback;
|
||||
}
|
||||
return resolveNextActiveTabAfterTerminalClose({
|
||||
currentActiveTabId,
|
||||
closedTabId: closedSessionId,
|
||||
orderedTabIdsBeforeClose,
|
||||
survivingTabIds,
|
||||
preferredTabId,
|
||||
});
|
||||
}
|
||||
|
||||
return null;
|
||||
export function resolveActiveTabAfterCloseWorkspace({
|
||||
currentActiveTabId,
|
||||
closedWorkspaceId,
|
||||
previousWorkspaces,
|
||||
remainingWorkspaces,
|
||||
remainingSessions,
|
||||
tabOrder,
|
||||
preferredTabId,
|
||||
}: {
|
||||
currentActiveTabId: string | null;
|
||||
closedWorkspaceId: string;
|
||||
previousWorkspaces: readonly Workspace[];
|
||||
remainingWorkspaces: readonly Workspace[];
|
||||
remainingSessions: readonly TerminalSession[];
|
||||
tabOrder?: readonly string[];
|
||||
preferredTabId?: string | null;
|
||||
}): string | null {
|
||||
const orderedTabIdsBeforeClose = buildOrderedTerminalTabIds({
|
||||
sessions: remainingSessions,
|
||||
workspaces: previousWorkspaces,
|
||||
tabOrder,
|
||||
});
|
||||
const survivingTabIds = buildOrderedTerminalTabIds({
|
||||
sessions: remainingSessions,
|
||||
workspaces: remainingWorkspaces,
|
||||
tabOrder,
|
||||
});
|
||||
|
||||
return resolveNextActiveTabAfterTerminalClose({
|
||||
currentActiveTabId,
|
||||
closedTabId: closedWorkspaceId,
|
||||
orderedTabIdsBeforeClose,
|
||||
survivingTabIds,
|
||||
preferredTabId,
|
||||
});
|
||||
}
|
||||
|
||||
export function closeSessionsState({
|
||||
@@ -203,6 +239,11 @@ export function closeSessionsState({
|
||||
const targetSession = nextSessions.find((session) => session.id === sessionId);
|
||||
if (!targetSession) continue;
|
||||
|
||||
const orderedTabIdsBeforeClose = buildOrderedTerminalTabIds({
|
||||
sessions: nextSessions,
|
||||
workspaces: nextWorkspaces,
|
||||
tabOrder: nextTabOrder,
|
||||
});
|
||||
const workspaceId = targetSession.workspaceId;
|
||||
const layoutResult = closeSessionWorkspaceLayoutState(
|
||||
nextWorkspaces,
|
||||
@@ -220,7 +261,10 @@ export function closeSessionsState({
|
||||
layoutResult,
|
||||
remainingSessions: nextSessions,
|
||||
preferredTabId,
|
||||
orderedTabIdsBeforeClose,
|
||||
tabOrder: nextTabOrder,
|
||||
});
|
||||
nextTabOrder = nextTabOrder.filter((id) => id !== sessionId);
|
||||
if (activeTabAfterClose) {
|
||||
nextActiveTabId = activeTabAfterClose;
|
||||
resolvedActiveTabId = activeTabAfterClose;
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import React, { act } from 'react';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
import { JSDOM } from 'jsdom';
|
||||
import { createLocalShellHost } from '../../domain/localShellHost';
|
||||
import { activeTabStore } from './activeTabStore';
|
||||
import { useSessionState } from './useSessionState';
|
||||
|
||||
async function mountSessionState(t: test.TestContext) {
|
||||
const dom = new JSDOM('<div id="root"></div>', { url: 'http://localhost' });
|
||||
const globals = {
|
||||
window: dom.window,
|
||||
document: dom.window.document,
|
||||
localStorage: dom.window.localStorage,
|
||||
CustomEvent: dom.window.CustomEvent,
|
||||
Event: dom.window.Event,
|
||||
IS_REACT_ACT_ENVIRONMENT: true,
|
||||
};
|
||||
const previous = new Map<string, PropertyDescriptor | undefined>();
|
||||
for (const [key, value] of Object.entries(globals)) {
|
||||
previous.set(key, Object.getOwnPropertyDescriptor(globalThis, key));
|
||||
Object.defineProperty(globalThis, key, { configurable: true, value });
|
||||
}
|
||||
const root = createRoot(document.getElementById('root')!);
|
||||
let latestState!: ReturnType<typeof useSessionState>;
|
||||
|
||||
function Probe() {
|
||||
latestState = useSessionState({ persistSessionRestore: false });
|
||||
return null;
|
||||
}
|
||||
|
||||
activeTabStore.setActiveTabId('vault', { recordPrevious: false });
|
||||
await act(async () => {
|
||||
root.render(React.createElement(React.StrictMode, null, React.createElement(Probe)));
|
||||
});
|
||||
|
||||
t.after(async () => {
|
||||
await act(async () => root.unmount());
|
||||
// Reset activeTabStore while DOM globals are still live and flush any
|
||||
// queued notification before closing JSDOM so notifyRafId is cleared.
|
||||
activeTabStore.setActiveTabId('vault', { recordPrevious: false });
|
||||
await new Promise<void>((resolve) => {
|
||||
if (typeof requestAnimationFrame === 'function') {
|
||||
requestAnimationFrame(() => resolve());
|
||||
} else {
|
||||
dom.window.setTimeout(resolve, 0);
|
||||
}
|
||||
});
|
||||
dom.window.close();
|
||||
for (const [key, descriptor] of previous) {
|
||||
if (descriptor) Object.defineProperty(globalThis, key, descriptor);
|
||||
else Reflect.deleteProperty(globalThis, key);
|
||||
}
|
||||
});
|
||||
|
||||
return () => latestState;
|
||||
}
|
||||
|
||||
test('useSessionState issue #4249 scenario 1: repeated close focuses C -> B -> A -> vault', async (t) => {
|
||||
const getState = await mountSessionState(t);
|
||||
|
||||
let a = '';
|
||||
let b = '';
|
||||
let c = '';
|
||||
await act(async () => { a = getState().createLocalTerminal(); });
|
||||
await act(async () => { b = getState().createLocalTerminal(); });
|
||||
await act(async () => { c = getState().createLocalTerminal(); });
|
||||
assert.equal(activeTabStore.getActiveTabId(), c);
|
||||
|
||||
await act(async () => { getState().closeSession(c); });
|
||||
assert.equal(activeTabStore.getActiveTabId(), b);
|
||||
|
||||
await act(async () => { getState().closeSession(b); });
|
||||
assert.equal(activeTabStore.getActiveTabId(), a);
|
||||
|
||||
await act(async () => { getState().closeSession(a); });
|
||||
assert.equal(activeTabStore.getActiveTabId(), 'vault');
|
||||
});
|
||||
|
||||
for (const rootView of ['vault', 'sftp'] as const) {
|
||||
test(`useSessionState issue #4249 scenario 2 (${rootView}): clicking ${rootView} then closing C -> B -> A stays on terminals until last tab`, async (t) => {
|
||||
const getState = await mountSessionState(t);
|
||||
|
||||
let a = '';
|
||||
let b = '';
|
||||
let c = '';
|
||||
await act(async () => { a = getState().createLocalTerminal(); });
|
||||
await act(async () => { b = getState().createLocalTerminal(); });
|
||||
await act(async () => { c = getState().createLocalTerminal(); });
|
||||
|
||||
// User clicks Vaults/SFTP once, then clicks back to rightmost terminal C.
|
||||
await act(async () => { getState().setActiveTabId(rootView); });
|
||||
await act(async () => { getState().setActiveTabId(c); });
|
||||
assert.equal(activeTabStore.getActiveTabId(), c);
|
||||
assert.equal(activeTabStore.getPreviousActiveTabId(), rootView);
|
||||
|
||||
// Repeated Cmd/Ctrl+W closes C -> B -> A predictably before falling back.
|
||||
await act(async () => { getState().closeSession(c); });
|
||||
assert.equal(activeTabStore.getActiveTabId(), b);
|
||||
|
||||
await act(async () => { getState().closeSession(b); });
|
||||
assert.equal(activeTabStore.getActiveTabId(), a);
|
||||
|
||||
await act(async () => { getState().closeSession(a); });
|
||||
assert.equal(activeTabStore.getActiveTabId(), rootView);
|
||||
});
|
||||
}
|
||||
|
||||
test('useSessionState closing a workspace tab moves to adjacent terminal tab instead of jumping to vault/sftp', async (t) => {
|
||||
const getState = await mountSessionState(t);
|
||||
|
||||
let a = '';
|
||||
let c = '';
|
||||
await act(async () => { a = getState().createLocalTerminal(); });
|
||||
await act(async () => {
|
||||
getState().createWorkspaceWithHosts('WS', [createLocalShellHost({ id: 'ws-local-1' })]);
|
||||
});
|
||||
const wsId = getState().workspaces[0].id;
|
||||
await act(async () => { c = getState().createLocalTerminal(); });
|
||||
|
||||
// Place workspace visually between A and C: [a, wsId, c].
|
||||
await act(async () => { getState().reorderTabs(wsId, a, 'after'); });
|
||||
assert.deepEqual(getState().orderedTabs, [a, wsId, c]);
|
||||
|
||||
// Visit SFTP, return to C, then close C -> WS -> A.
|
||||
await act(async () => { getState().setActiveTabId('sftp'); });
|
||||
await act(async () => { getState().setActiveTabId(c); });
|
||||
|
||||
await act(async () => { getState().closeSession(c); });
|
||||
assert.equal(activeTabStore.getActiveTabId(), wsId);
|
||||
|
||||
await act(async () => { getState().closeWorkspace(wsId); });
|
||||
assert.equal(activeTabStore.getActiveTabId(), a);
|
||||
|
||||
await act(async () => { getState().closeSession(a); });
|
||||
assert.equal(activeTabStore.getActiveTabId(), 'sftp');
|
||||
});
|
||||
@@ -43,6 +43,7 @@ import {
|
||||
import {
|
||||
closeSessionsState,
|
||||
detachSessionFromWorkspaceState,
|
||||
resolveActiveTabAfterCloseWorkspace,
|
||||
} from './sessionWorkspaceDetach';
|
||||
import { getAISessionsSnapshot } from './aiSessionsStore';
|
||||
import { getPersistedAiSessionsSnapshot } from './aiStateSnapshots';
|
||||
@@ -609,23 +610,33 @@ export const useSessionState = ({
|
||||
for (const sessionId of closedIds) {
|
||||
sessionPresentationStore.clearSession(sessionId);
|
||||
}
|
||||
const preferredTabId = activeTabStore.getPreviousActiveTabId();
|
||||
const tabOrderSnapshot = tabOrderRef.current;
|
||||
const sessionsSnapshot = sessionsRef.current;
|
||||
setTabOrder(prevTabOrder => prevTabOrder.filter(id => id !== workspaceId));
|
||||
setWorkspaces(prevWorkspaces => {
|
||||
const remainingWorkspaces = prevWorkspaces.filter(w => w.id !== workspaceId);
|
||||
const remainingSessions = sessionsSnapshot.filter(s => s.workspaceId !== workspaceId);
|
||||
|
||||
setSessions(prevSessions => prevSessions.filter(s => s.workspaceId !== workspaceId));
|
||||
|
||||
const currentActiveTabId = activeTabStore.getActiveTabId();
|
||||
if (currentActiveTabId === workspaceId) {
|
||||
if (remainingWorkspaces.length > 0) {
|
||||
setActiveTabId(remainingWorkspaces[remainingWorkspaces.length - 1].id);
|
||||
} else {
|
||||
setActiveTabId('vault');
|
||||
}
|
||||
const nextActiveTabId = resolveActiveTabAfterCloseWorkspace({
|
||||
currentActiveTabId: activeTabStore.getActiveTabId(),
|
||||
closedWorkspaceId: workspaceId,
|
||||
previousWorkspaces: prevWorkspaces,
|
||||
remainingWorkspaces,
|
||||
remainingSessions,
|
||||
tabOrder: tabOrderSnapshot,
|
||||
preferredTabId,
|
||||
});
|
||||
if (nextActiveTabId) {
|
||||
// Closing must not rewrite previous-tab history to the closed workspace.
|
||||
activeTabStore.setActiveTabId(nextActiveTabId, { recordPrevious: false });
|
||||
}
|
||||
|
||||
return remainingWorkspaces;
|
||||
});
|
||||
}, [setActiveTabId]);
|
||||
}, []);
|
||||
|
||||
const closeSessions = useCallback((sessionIds: string[]) => {
|
||||
captureClosingTerminalSessions(sessionIds);
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import type { TerminalSession, Workspace } from './models';
|
||||
import {
|
||||
buildOrderedTerminalTabIds,
|
||||
isRootNavigationTabId,
|
||||
resolveAdjacentTabId,
|
||||
resolveNextActiveTabAfterTerminalClose,
|
||||
} from './tabCloseFocus';
|
||||
|
||||
const standaloneSession = (id: string, overrides: Partial<TerminalSession> = {}): TerminalSession => ({
|
||||
id,
|
||||
hostId: id,
|
||||
hostLabel: id,
|
||||
username: 'user',
|
||||
hostname: id,
|
||||
status: 'connected',
|
||||
...overrides,
|
||||
});
|
||||
|
||||
const workspace = (id: string, sessionId = `${id}-pane`): Workspace => ({
|
||||
id,
|
||||
title: id,
|
||||
focusedSessionId: sessionId,
|
||||
root: { id: `${id}-root`, type: 'pane', sessionId },
|
||||
});
|
||||
|
||||
test('isRootNavigationTabId recognizes vault and sftp only', () => {
|
||||
assert.equal(isRootNavigationTabId('vault'), true);
|
||||
assert.equal(isRootNavigationTabId('sftp'), true);
|
||||
assert.equal(isRootNavigationTabId('session-1'), false);
|
||||
assert.equal(isRootNavigationTabId('plugin-view:owner:view'), false);
|
||||
assert.equal(isRootNavigationTabId('editor:file-1'), false);
|
||||
assert.equal(isRootNavigationTabId(null), false);
|
||||
assert.equal(isRootNavigationTabId(undefined), false);
|
||||
});
|
||||
|
||||
test('buildOrderedTerminalTabIds honors visual tabOrder and excludes hidden or workspace-bound sessions', () => {
|
||||
const ordered = buildOrderedTerminalTabIds({
|
||||
sessions: [
|
||||
standaloneSession('s1'),
|
||||
standaloneSession('s2'),
|
||||
standaloneSession('s3'),
|
||||
standaloneSession('hidden', { hiddenFromTabs: true }),
|
||||
standaloneSession('ws-member', { workspaceId: 'ws-1' }),
|
||||
],
|
||||
workspaces: [workspace('ws-1', 'ws-member')],
|
||||
tabOrder: ['s3', 'stale', 'ws-1', 's1'],
|
||||
});
|
||||
|
||||
assert.deepEqual(ordered, ['s3', 'ws-1', 's1', 's2']);
|
||||
});
|
||||
|
||||
test('resolveAdjacentTabId prefers immediate left neighbor, then immediate right neighbor', () => {
|
||||
assert.equal(
|
||||
resolveAdjacentTabId({
|
||||
orderedTabIdsBeforeClose: ['a', 'b', 'c'],
|
||||
survivingTabIds: ['a', 'b'],
|
||||
closedTabId: 'c',
|
||||
}),
|
||||
'b',
|
||||
);
|
||||
assert.equal(
|
||||
resolveAdjacentTabId({
|
||||
orderedTabIdsBeforeClose: ['a', 'b', 'c'],
|
||||
survivingTabIds: ['a', 'c'],
|
||||
closedTabId: 'b',
|
||||
}),
|
||||
'a',
|
||||
);
|
||||
assert.equal(
|
||||
resolveAdjacentTabId({
|
||||
orderedTabIdsBeforeClose: ['a', 'b', 'c'],
|
||||
survivingTabIds: ['b', 'c'],
|
||||
closedTabId: 'a',
|
||||
}),
|
||||
'b',
|
||||
);
|
||||
});
|
||||
|
||||
test('closing a terminal tab never jumps to vault, sftp, or plugin views while other terminal tabs remain (#4249)', () => {
|
||||
for (const nonTerminalView of ['vault', 'sftp', 'plugin-view:demo:main', 'editor:file-1']) {
|
||||
assert.equal(
|
||||
resolveNextActiveTabAfterTerminalClose({
|
||||
currentActiveTabId: 'c',
|
||||
closedTabId: 'c',
|
||||
orderedTabIdsBeforeClose: ['a', 'b', 'c'],
|
||||
survivingTabIds: ['a', 'b'],
|
||||
preferredTabId: nonTerminalView,
|
||||
}),
|
||||
'b',
|
||||
`expected adjacent tab b instead of ${nonTerminalView}`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('closing the last terminal tab falls back to vault or sftp when preferred, otherwise vault', () => {
|
||||
assert.equal(
|
||||
resolveNextActiveTabAfterTerminalClose({
|
||||
currentActiveTabId: 'a',
|
||||
closedTabId: 'a',
|
||||
orderedTabIdsBeforeClose: ['a'],
|
||||
survivingTabIds: [],
|
||||
preferredTabId: 'sftp',
|
||||
}),
|
||||
'sftp',
|
||||
);
|
||||
assert.equal(
|
||||
resolveNextActiveTabAfterTerminalClose({
|
||||
currentActiveTabId: 'a',
|
||||
closedTabId: 'a',
|
||||
orderedTabIdsBeforeClose: ['a'],
|
||||
survivingTabIds: [],
|
||||
preferredTabId: 'vault',
|
||||
}),
|
||||
'vault',
|
||||
);
|
||||
assert.equal(
|
||||
resolveNextActiveTabAfterTerminalClose({
|
||||
currentActiveTabId: 'a',
|
||||
closedTabId: 'a',
|
||||
orderedTabIdsBeforeClose: ['a'],
|
||||
survivingTabIds: [],
|
||||
preferredTabId: 'plugin-view:demo:main',
|
||||
}),
|
||||
'vault',
|
||||
);
|
||||
});
|
||||
|
||||
test('closing a temporary terminal tab still restores a surviving prior terminal tab (#2788)', () => {
|
||||
assert.equal(
|
||||
resolveNextActiveTabAfterTerminalClose({
|
||||
currentActiveTabId: 's7',
|
||||
closedTabId: 's7',
|
||||
orderedTabIdsBeforeClose: ['s1', 's2', 's5', 's6', 's7'],
|
||||
survivingTabIds: ['s1', 's2', 's5', 's6'],
|
||||
preferredTabId: 's5',
|
||||
}),
|
||||
's5',
|
||||
);
|
||||
});
|
||||
|
||||
test('closing an inactive terminal tab returns null', () => {
|
||||
assert.equal(
|
||||
resolveNextActiveTabAfterTerminalClose({
|
||||
currentActiveTabId: 'b',
|
||||
closedTabId: 'c',
|
||||
orderedTabIdsBeforeClose: ['a', 'b', 'c'],
|
||||
survivingTabIds: ['a', 'b'],
|
||||
preferredTabId: 'vault',
|
||||
}),
|
||||
null,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,128 @@
|
||||
import type { TerminalSession, Workspace } from './models';
|
||||
|
||||
export type RootNavigationTabId = 'vault' | 'sftp';
|
||||
|
||||
export function isRootNavigationTabId(
|
||||
tabId: string | null | undefined,
|
||||
): tabId is RootNavigationTabId {
|
||||
return tabId === 'vault' || tabId === 'sftp';
|
||||
}
|
||||
|
||||
function uniqueTabIds(tabIds: readonly string[]): string[] {
|
||||
const seen = new Set<string>();
|
||||
const uniqueIds: string[] = [];
|
||||
for (const tabId of tabIds) {
|
||||
if (!tabId || seen.has(tabId)) continue;
|
||||
seen.add(tabId);
|
||||
uniqueIds.push(tabId);
|
||||
}
|
||||
return uniqueIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the visual left-to-right order of open terminal and workspace tabs
|
||||
* (visible standalone sessions + workspaces), honoring `tabOrder` with any
|
||||
* unordered tabs appended in their natural order.
|
||||
*/
|
||||
export function buildOrderedTerminalTabIds({
|
||||
sessions,
|
||||
workspaces,
|
||||
tabOrder = [],
|
||||
}: {
|
||||
sessions: readonly TerminalSession[];
|
||||
workspaces: readonly Workspace[];
|
||||
tabOrder?: readonly string[];
|
||||
}): string[] {
|
||||
const visibleStandaloneSessionIds = sessions
|
||||
.filter((session) => !session.workspaceId && !session.hiddenFromTabs)
|
||||
.map((session) => session.id);
|
||||
const workspaceIds = workspaces.map((workspace) => workspace.id);
|
||||
const allTabIds = uniqueTabIds([...visibleStandaloneSessionIds, ...workspaceIds]);
|
||||
const allTabIdSet = new Set(allTabIds);
|
||||
const orderedIds = uniqueTabIds(tabOrder.filter((id) => allTabIdSet.has(id)));
|
||||
const orderedIdSet = new Set(orderedIds);
|
||||
const remainingIds = allTabIds.filter((id) => !orderedIdSet.has(id));
|
||||
return [...orderedIds, ...remainingIds];
|
||||
}
|
||||
|
||||
/**
|
||||
* Pick the nearest surviving adjacent tab in visual tab order, preferring the
|
||||
* immediate left neighbor and then the immediate right neighbor (matching
|
||||
* right-to-left close flow and batch/editor tab close behavior).
|
||||
*/
|
||||
export function resolveAdjacentTabId({
|
||||
orderedTabIdsBeforeClose = [],
|
||||
survivingTabIds,
|
||||
closedTabId,
|
||||
}: {
|
||||
orderedTabIdsBeforeClose?: readonly string[];
|
||||
survivingTabIds: readonly string[];
|
||||
closedTabId: string;
|
||||
}): string | undefined {
|
||||
if (survivingTabIds.length === 0) return undefined;
|
||||
|
||||
const closedIndex = orderedTabIdsBeforeClose.indexOf(closedTabId);
|
||||
if (closedIndex !== -1) {
|
||||
const survivingSet = new Set(survivingTabIds);
|
||||
for (let distance = 1; distance < orderedTabIdsBeforeClose.length; distance += 1) {
|
||||
const left = orderedTabIdsBeforeClose[closedIndex - distance];
|
||||
if (left && survivingSet.has(left)) return left;
|
||||
const right = orderedTabIdsBeforeClose[closedIndex + distance];
|
||||
if (right && survivingSet.has(right)) return right;
|
||||
}
|
||||
}
|
||||
|
||||
return survivingTabIds[survivingTabIds.length - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the next active tab after closing a terminal session tab or
|
||||
* workspace tab.
|
||||
*
|
||||
* Rules:
|
||||
* - Closing an inactive tab leaves the active tab unchanged (`null`).
|
||||
* - While other open terminal/workspace tabs remain:
|
||||
* - Restore `preferredTabId` only if it is itself a surviving open
|
||||
* terminal/workspace tab (preserving #2788 temporary-tab return).
|
||||
* - Never jump back to non-terminal views (`vault`, `sftp`, plugins, etc.)
|
||||
* while other terminal/workspace tabs are open (#4249); instead focus the
|
||||
* nearest adjacent surviving terminal/workspace tab (left first, then right).
|
||||
* - Only when the LAST terminal/workspace tab is closed, fall back to
|
||||
* `preferredTabId` if it is a root navigation view (`vault` or `sftp`),
|
||||
* otherwise `vault`.
|
||||
*/
|
||||
export function resolveNextActiveTabAfterTerminalClose({
|
||||
currentActiveTabId,
|
||||
closedTabId,
|
||||
orderedTabIdsBeforeClose,
|
||||
survivingTabIds,
|
||||
preferredTabId,
|
||||
}: {
|
||||
currentActiveTabId: string | null;
|
||||
closedTabId: string;
|
||||
orderedTabIdsBeforeClose?: readonly string[];
|
||||
survivingTabIds: readonly string[];
|
||||
preferredTabId?: string | null;
|
||||
}): string | null {
|
||||
if (currentActiveTabId !== closedTabId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const filteredSurvivingTabIds = survivingTabIds.filter((id) => id !== closedTabId);
|
||||
if (filteredSurvivingTabIds.length > 0) {
|
||||
if (
|
||||
preferredTabId
|
||||
&& preferredTabId !== closedTabId
|
||||
&& filteredSurvivingTabIds.includes(preferredTabId)
|
||||
) {
|
||||
return preferredTabId;
|
||||
}
|
||||
return resolveAdjacentTabId({
|
||||
orderedTabIdsBeforeClose,
|
||||
survivingTabIds: filteredSurvivingTabIds,
|
||||
closedTabId,
|
||||
}) ?? 'vault';
|
||||
}
|
||||
|
||||
return isRootNavigationTabId(preferredTabId) ? preferredTabId : 'vault';
|
||||
}
|
||||
Reference in new issue
Block a user