hermes-agent/web/src/lib/chat-activation.test.ts
xxxigm 3bcd0c1b00
fix(dashboard): only open the chat PTY once the chat tab is active (#59551)
* fix(dashboard): only open the chat PTY once the chat tab is active

The dashboard mounts ChatPage persistently (hidden with CSS) on every route
so the embedded chat PTY survives tab switches. But the PTY-connect effect
never checked whether the chat tab was active, so it opened `/api/pty` on
mount for ANY dashboard page. On a source/RPi install that spawns the whole
TUI + agent bootstrap (`Installing TUI dependencies…` → `npm install`) merely
by loading /sessions, /system, etc. — work the user never asked for, and the
trigger behind "dashboard loses custom themes on /chat load".

Gate the connect effect on a sticky activation latch: the PTY is not spawned
until the chat tab has been active at least once, and stays connected across
later tab switches so the persistence UX is preserved.

* test(dashboard): cover chat PTY activation latch

Asserts the invariant behind the fix: activation is sticky. It stays false
while the chat tab has never been active (so the persistently-mounted,
hidden ChatPage never opens /api/pty), flips true when the tab activates,
and stays true after the user navigates away (PTY persistence).
2026-07-18 00:50:13 -04:00

26 lines
1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { latchChatActivation } from "./chat-activation";
describe("latchChatActivation", () => {
it("stays inactive while the chat tab has never been active", () => {
// A dashboard sitting on /sessions, /system, … must not flip the latch,
// so the persistently-mounted ChatPage never opens /api/pty (which would
// trigger the TUI/agent bootstrap on every page).
expect(latchChatActivation(false, false)).toBe(false);
});
it("activates when the chat tab becomes active", () => {
expect(latchChatActivation(false, true)).toBe(true);
});
it("stays activated after the chat tab is left (sticky / persistence)", () => {
// Once the user has opened /chat, the PTY must survive navigating away so
// a running agent turn is not torn down on every tab switch.
expect(latchChatActivation(true, false)).toBe(true);
});
it("stays activated while the chat tab remains active", () => {
expect(latchChatActivation(true, true)).toBe(true);
});
});