Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

test: cover useShellSession attach/detach contract; trim unused refitTerminal from public API (#2532)

[._.]/ Adam Eivy94a70ac208c3
diff --git a/client/src/hooks/README.md b/client/src/hooks/README.md
index 8723ea43ce..3dc73a2f83 100644
--- a/client/src/hooks/README.md
+++ b/client/src/hooks/README.md
@@ -69,7 +69,7 @@ grep -i "what you want to do" client/src/hooks/README.md
| Hook | Purpose | Use when |
|---|---|---|
| `useSocket` | Shared socket instance + connection status. | You need to subscribe to a socket event. |
-| `useShellSession` | All Socket.IO `shell:*` session/terminal state + lifecycle for the Shell page: owns the xterm instance, the attach/detach/generation-guard contract (`pendingAttachRef {target,generation}`, `claim`, `userIdleRef`, `mountedRef`), URL sync, and every session action. Returns `{ terminalRef, connected, sessions, activeSessionId, activeSession, interactiveCount, liveRunCount, isLiveRun, emitShellInput, sendCommand, sendCtrlC, sendNavKey, refitTerminal, restartSession, stopSession, startNewSession, switchToSession, killOtherSession }` so the route stays presentational. | The Shell page (`pages/Shell.jsx`) — don't re-roll the single-subscriber PTY attach lifecycle. |
+| `useShellSession` | All Socket.IO `shell:*` session/terminal state + lifecycle for the Shell page: owns the xterm instance, the attach/detach/generation-guard contract (`pendingAttachRef {target,generation}`, `claim`, `userIdleRef`, `mountedRef`), URL sync, and every session action. Returns `{ terminalRef, connected, sessions, activeSessionId, activeSession, interactiveCount, liveRunCount, isLiveRun, emitShellInput, sendCommand, sendCtrlC, sendNavKey, restartSession, stopSession, startNewSession, switchToSession, killOtherSession }` so the route stays presentational. | The Shell page (`pages/Shell.jsx`) — don't re-roll the single-subscriber PTY attach lifecycle. |
| `useUpdateChecker` | Detect stale client bundle; show reload toast. | Wire once at app root. |
| `useMounted` | `mountedRef` whose `.current` is true while mounted. | Async deferred work that must abort on unmount. |
| `usePrevious` | Returns the value from the previous render — snapshot updated in a `useEffect`. | Compare-and-act on prop/state change from inside a `useEffect`. |
diff --git a/client/src/hooks/useShellSession.js b/client/src/hooks/useShellSession.js
index f1f2f17fee..d5d2449833 100644
--- a/client/src/hooks/useShellSession.js
+++ b/client/src/hooks/useShellSession.js
@@ -792,7 +792,6 @@ export function useShellSession({ isFullscreen } = {}) {
sendCommand,
sendCtrlC,
sendNavKey,
- refitTerminal,
restartSession,
stopSession,
startNewSession,
diff --git a/client/src/hooks/useShellSession.test.jsx b/client/src/hooks/useShellSession.test.jsx
new file mode 100644
index 0000000000..5c4229127a
--- /dev/null
+++ b/client/src/hooks/useShellSession.test.jsx
@@ -0,0 +1,96 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+import { renderHook, act, cleanup } from '@testing-library/react';
+import { MemoryRouter } from 'react-router-dom';
+
+// Capture the socket handlers the hook registers so the test can drive the
+// shell:* protocol, and record every emit so we can assert the client half of
+// the attach/detach contract. The terminal-init effect is a no-op here (its
+// `terminalRef` DOM node is never attached in renderHook), so the socket/session
+// logic runs without a real xterm instance.
+// Hoisted so the vi.mock factory (itself hoisted to the top of the file) can
+// reference the same objects the tests drive.
+const { handlers, emitted, socketMock } = vi.hoisted(() => {
+ const handlers = new Map();
+ const emitted = [];
+ const socketMock = {
+ connected: true,
+ on: (event, fn) => { handlers.set(event, fn); },
+ off: (event, fn) => { if (handlers.get(event) === fn) handlers.delete(event); },
+ emit: (event, ...args) => { emitted.push([event, ...args]); },
+ };
+ return { handlers, emitted, socketMock };
+});
+vi.mock('../services/socket', () => ({ default: socketMock, getSocket: () => socketMock }));
+vi.mock('../components/ThemeContext', () => ({
+ useThemeContext: () => ({ themeId: 'test', theme: { mode: 'night' } }),
+}));
+
+import { useShellSession, MAX_SESSIONS } from './useShellSession.js';
+
+const wrapper = ({ children }) => (
+ <MemoryRouter initialEntries={['/shell']}>{children}</MemoryRouter>
+);
+const fire = (event, payload) => act(() => { handlers.get(event)?.(payload); });
+const lastEmit = (event) => [...emitted].reverse().find(([e]) => e === event);
+const session = (id, over = {}) => ({ sessionId: id, attached: false, external: false, createdAt: Date.now(), ...over });
+
+describe('useShellSession', () => {
+ beforeEach(() => { handlers.clear(); emitted.length = 0; socketMock.connected = true; });
+ afterEach(cleanup);
+
+ it('requests the session list on mount when the socket is already connected', () => {
+ renderHook(() => useShellSession({}), { wrapper });
+ expect(emitted).toContainEqual(['shell:list']);
+ });
+
+ it('auto-starts a fresh session when the list is empty and the user is not idle', () => {
+ renderHook(() => useShellSession({}), { wrapper });
+ fire('shell:sessions', []);
+ expect(lastEmit('shell:start')).toBeTruthy();
+ });
+
+ it('activates a started session and marks it connected', () => {
+ const { result } = renderHook(() => useShellSession({}), { wrapper });
+ fire('shell:sessions', []); // → startSession, pending target 'new'
+ fire('shell:started', { sessionId: 'abc' });
+ expect(result.current.activeSessionId).toBe('abc');
+ expect(result.current.connected).toBe(true);
+ });
+
+ it('ignores a shell:attached whose id does not match the pending target (strict-equality guard)', () => {
+ const { result } = renderHook(() => useShellSession({}), { wrapper });
+ // First load with one free survivor → auto-attach to s1 (claim:true), pending target 's1'.
+ fire('shell:sessions', [session('s1')]);
+ expect(lastEmit('shell:attach')).toEqual(['shell:attach', { sessionId: 's1', claim: true }]);
+ // A stale/mismatched attach response must not activate.
+ fire('shell:attached', { sessionId: 'other', bufferedOutput: '' });
+ expect(result.current.activeSessionId).toBeNull();
+ // The matching response activates.
+ fire('shell:attached', { sessionId: 's1', bufferedOutput: '' });
+ expect(result.current.activeSessionId).toBe('s1');
+ });
+
+ it('drops the active view when the server detaches the displayed session', () => {
+ const { result } = renderHook(() => useShellSession({}), { wrapper });
+ fire('shell:sessions', [session('s1')]);
+ fire('shell:attached', { sessionId: 's1', bufferedOutput: '' });
+ expect(result.current.activeSessionId).toBe('s1');
+ fire('shell:detached', { sessionId: 's1', reason: 'attached-elsewhere' });
+ expect(result.current.activeSessionId).toBeNull();
+ expect(result.current.connected).toBe(false);
+ });
+
+ it('surfaces derived counts and the active session', () => {
+ const { result } = renderHook(() => useShellSession({}), { wrapper });
+ fire('shell:sessions', [session('s1'), session('run1', { external: true })]);
+ fire('shell:attached', { sessionId: 's1', bufferedOutput: '' });
+ expect(result.current.interactiveCount).toBe(1);
+ expect(result.current.liveRunCount).toBe(1);
+ expect(result.current.isLiveRun).toBe(false);
+ expect(result.current.activeSession?.sessionId).toBe('s1');
+ });
+
+ it('exports the shell session cap', () => {
+ expect(MAX_SESSIONS).toBe(20);
+ });
+});