Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"searchRegex": "Use Regular Expression",
"searchResultCount": "{{index}} of {{count}}",
"searchWholeWord": "Match Whole Word",
"sessionTakenOver": "Session was opened in another tab. Reconnecting...",
"sessionTakenOver": "This session was opened in another window or tab.",
"tmuxAttached": "Attached clients",
"tmuxAttachedCount_one": "{{count}} attached",
"tmuxAttachedCount_other": "{{count}} attached",
Expand Down
15 changes: 6 additions & 9 deletions src/frontend/terminal/Terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2621,23 +2621,20 @@ const TerminalInner = forwardRef<TerminalHandle, SSHTerminalProps>(
webSocketRef.current.close();
}
} else if (msg.type === "sessionTakenOver") {
// Another window has the session now. Taking it straight back
// makes the two windows take it from each other forever.
sessionIdRef.current = null;

if (terminal) {
terminal.clear();
}
shouldNotReconnectRef.current = true;
setIsConnected(false);
setIsConnecting(true);
setIsConnecting(false);
updateConnectionError(t("terminal.sessionTakenOver"));

addLog({
type: "warning",
stage: "connection",
message: t("terminal.sessionTakenOver"),
});

const cols = terminal?.cols || 80;
const rows = terminal?.rows || 24;
connectToHost(cols, rows);
webSocketRef.current?.close();
} else if (msg.type === "tmux_sessions_available") {
setTmuxSessionPicker({
sessions: msg.sessions,
Expand Down
42 changes: 42 additions & 0 deletions tests/frontend/terminal/session-taken-over.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @vitest-environment node
import fs from "node:fs";
import vm from "node:vm";
import { describe, expect, it, vi } from "vitest";

const source = fs.readFileSync(
new URL("../../../src/frontend/terminal/Terminal.tsx", import.meta.url),
"utf8",
);
const start = source.indexOf('if (msg.type === "sessionTakenOver") {');
const branch = source.slice(
start,
source.indexOf('} else if (msg.type === "tmux_sessions_available")', start),
);

describe("a session another window took over", () => {
it("stays stopped instead of taking the session straight back", () => {
const context = {
msg: { type: "sessionTakenOver" },
t: (key: string) => key,
terminal: { cols: 80, rows: 24, clear: vi.fn() },
sessionIdRef: { current: "s1" },
shouldNotReconnectRef: { current: false },
webSocketRef: { current: { close: vi.fn() } },
setIsConnected: vi.fn(),
setIsConnecting: vi.fn(),
updateConnectionError: vi.fn(),
addLog: vi.fn(),
connectToHost: vi.fn(),
};
vm.runInNewContext(`${branch}}`, context);

expect(context.connectToHost).not.toHaveBeenCalled();
expect(context.shouldNotReconnectRef.current).toBe(true);
expect(context.sessionIdRef.current).toBeNull();
expect(context.setIsConnecting).toHaveBeenCalledWith(false);
expect(context.updateConnectionError).toHaveBeenCalledWith(
"terminal.sessionTakenOver",
);
expect(context.webSocketRef.current.close).toHaveBeenCalledOnce();
});
});