Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

test(desktop): cover retained right sidebar mount via hook

qsoyqf2b1ff5de738
diff --git a/apps/desktop/src/renderer/__tests__/orcaWorkflowRoute.test.ts b/apps/desktop/src/renderer/__tests__/orcaWorkflowRoute.test.ts
index 815e4f050a..16d1a80e78 100644
--- a/apps/desktop/src/renderer/__tests__/orcaWorkflowRoute.test.ts
+++ b/apps/desktop/src/renderer/__tests__/orcaWorkflowRoute.test.ts
@@ -465,8 +465,6 @@ describe('OrcaWorkflowRoute source invariants', () => {
});
it('waits for detached bootstrap before mounting or writing the embedded right sidebar', () => {
- expect(mainLayoutSource).toContain('const rightSidebarMountable = rsbWindow.loaded && !rsbDetached;');
- expect(mainLayoutSource).toContain('rightSidebarMountable && (rightSidebarAvailable || rightSidebarEverMounted) ? (');
expect(mainLayoutSource).toContain('if (!sessionId || !rsbWindow.loaded || rsbDetached) return;');
expect(mainLayoutSource).toContain("routeSidebarCommand({ type: 'open-terminal', sessionId })");
expect(mainLayoutSource).toContain('const windowState = getRsbWindowUiState();');
diff --git a/apps/desktop/src/renderer/components/layout/MainLayout.tsx b/apps/desktop/src/renderer/components/layout/MainLayout.tsx
index 79b885ef9e..d67f6fcfbf 100644
--- a/apps/desktop/src/renderer/components/layout/MainLayout.tsx
+++ b/apps/desktop/src/renderer/components/layout/MainLayout.tsx
@@ -76,6 +76,7 @@ import { useSidebarResize } from '@/hooks/useSidebarResize';
import { useSidebarCardMode } from '@/hooks/useSidebarCardMode';
import { useSidebarPeek } from '@/hooks/useSidebarPeek';
import { useMacFullscreen } from '@/hooks/useMacFullscreen';
+import { useRetainedRightSidebarMount } from '@/hooks/useRetainedRightSidebarMount';
import { useRightSidebarResize } from '@/hooks/useRightSidebarResize';
import { isSecondaryWindow } from '@/lib/secondaryWindow';
import { useUpdateNotice } from '@/hooks/useUpdateNotice';
@@ -291,15 +292,13 @@ export function MainLayout() {
// 实例)会经 Outlet context 的 setRightSidebarAvailable 置 true,卸载时置 false。
// 据此决定是否渲染右栏面板:离开聊天视图 → 面板卸载,不会卡在展开态且无入口关闭。
const [rightSidebarAvailable, setRightSidebarAvailable] = useState(false);
- // 右栏一旦在场过,经过不声明右栏的路由(如无项目目录的新建页)时仍保持挂载并强制折叠:
- // 卸载 Shell 会把内置浏览器的 webview 宿主移出 DOM,销毁 guest 并丢失页面内存态(#6485)。
- // detached / 窗口状态未就绪时内嵌右栏本就不渲染,标记随之复位。
- const rightSidebarMountable = rsbWindow.loaded && !rsbDetached;
- const [rightSidebarEverMounted, setRightSidebarEverMounted] = useState(false);
- useEffect(() => {
- if (!rightSidebarMountable) setRightSidebarEverMounted(false);
- else if (rightSidebarAvailable) setRightSidebarEverMounted(true);
- }, [rightSidebarAvailable, rightSidebarMountable]);
+ // 经过不声明右栏的路由时保持挂载并强制折叠,避免销毁内置浏览器 guest(#6485)。
+ const { mounted: rightSidebarMounted, forceCollapsed: rightSidebarForceCollapsed } =
+ useRetainedRightSidebarMount({
+ available: rightSidebarAvailable,
+ loaded: rsbWindow.loaded,
+ detached: rsbDetached,
+ });
// 当前 cc-agent session id —— 由 CCAgentSessionView 的路由主实例(ownsRoute=true)经 outlet context
// 推上来,Shell 据此从 module-level store 拉对应桶的 tab 列表持久化数据。null = 不在聊天会话内。
const [rightSidebarSessionId, setRightSidebarSessionId] = useState<string | null>(null);
@@ -1630,12 +1629,12 @@ export function MainLayout() {
// (仅全屏聊天视图在场),此后保持挂载(不可用时强制折叠);detached(在新窗口显示)时内嵌不渲染
// (webview 池 / 终端等资源不能双实例,内容由子窗口 renderer 独占)。
rightTabs:
- rightSidebarMountable && (rightSidebarAvailable || rightSidebarEverMounted) ? (
+ rightSidebarMounted ? (
<RightSidebar
ref={rightSidebarRef}
// 路由不声明右栏时仅派生强制折叠,不写入 isRightSidebarCollapsed,
// 也不落按会话记忆的折叠偏好。
- isCollapsed={isRightSidebarCollapsed || !rightSidebarAvailable}
+ isCollapsed={isRightSidebarCollapsed || rightSidebarForceCollapsed}
// width 只在非 maximize 受控,且只是兜底 —— RSB 优先消费
// 引擎 PaneWidthContext 的实时值;maximize 时 RightSidebar 内部走
// flex-1 自己撑满(2026-07-01 bug 修)。拖宽把手 = 引擎分割线。
diff --git a/apps/desktop/src/renderer/hooks/__tests__/useRetainedRightSidebarMount.test.ts b/apps/desktop/src/renderer/hooks/__tests__/useRetainedRightSidebarMount.test.ts
new file mode 100644
index 0000000000..002dbb4a9e
--- /dev/null
+++ b/apps/desktop/src/renderer/hooks/__tests__/useRetainedRightSidebarMount.test.ts
@@ -0,0 +1,53 @@
+// @vitest-environment jsdom
+import { cleanup, renderHook } from '@testing-library/react';
+import { afterEach, describe, expect, it } from 'vitest';
+
+import {
+ useRetainedRightSidebarMount,
+ type RetainedRightSidebarMountInput,
+} from '../useRetainedRightSidebarMount';
+
+afterEach(cleanup);
+
+const route = { available: true, loaded: true, detached: false };
+const noSidebarRoute = { ...route, available: false };
+
+function mountSequence(initial: RetainedRightSidebarMountInput) {
+ const view = renderHook((props: RetainedRightSidebarMountInput) => useRetainedRightSidebarMount(props), {
+ initialProps: initial,
+ });
+ return {
+ step: (next: RetainedRightSidebarMountInput) => {
+ view.rerender(next);
+ return view.result.current;
+ },
+ current: () => view.result.current,
+ };
+}
+
+// #6485: 任务 → 无项目目录的新建页 → 任务。右栏若在中间卸载,内置浏览器的 webview 宿主
+// 会被移出 DOM,Electron guest 被销毁,页面内输入与滚动位置丢失。
+describe('useRetainedRightSidebarMount', () => {
+ it('keeps the sidebar mounted but force-collapsed on a route that does not declare it', () => {
+ const view = mountSequence(route);
+ expect(view.current()).toEqual({ mounted: true, forceCollapsed: false });
+
+ expect(view.step(noSidebarRoute)).toEqual({ mounted: true, forceCollapsed: true });
+ expect(view.step(route)).toEqual({ mounted: true, forceCollapsed: false });
+ });
+
+ it('does not mount a sidebar that no route has ever declared', () => {
+ const view = mountSequence(noSidebarRoute);
+ expect(view.current()).toEqual({ mounted: false, forceCollapsed: true });
+ });
+
+ it.each([
+ ['the detached window owns the sidebar', { ...noSidebarRoute, detached: true }],
+ ['the window state is not loaded yet', { ...noSidebarRoute, loaded: false }],
+ ])('drops the retained mount when %s, and does not resurrect it afterwards', (_name, blocked) => {
+ const view = mountSequence(route);
+ expect(view.step(blocked).mounted).toBe(false);
+ expect(view.step(noSidebarRoute)).toEqual({ mounted: false, forceCollapsed: true });
+ expect(view.step(route).mounted).toBe(true);
+ });
+});
diff --git a/apps/desktop/src/renderer/hooks/useRetainedRightSidebarMount.ts b/apps/desktop/src/renderer/hooks/useRetainedRightSidebarMount.ts
new file mode 100644
index 0000000000..4319e8b194
--- /dev/null
+++ b/apps/desktop/src/renderer/hooks/useRetainedRightSidebarMount.ts
@@ -0,0 +1,38 @@
+import { useEffect, useState } from 'react';
+
+export interface RetainedRightSidebarMountInput {
+ /** 当前路由是否声明了右栏(全屏聊天视图等)。 */
+ available: boolean;
+ /** 右栏窗口状态(detach 偏好)是否已从 main 同步完成。 */
+ loaded: boolean;
+ /** 右栏是否已在独立窗口中显示(此时内嵌右栏不渲染)。 */
+ detached: boolean;
+}
+
+export interface RetainedRightSidebarMount {
+ /** 是否渲染内嵌右栏。 */
+ mounted: boolean;
+ /** 路由不声明右栏但右栏仍保持挂载时为 true:只派生强制折叠,不写入折叠偏好。 */
+ forceCollapsed: boolean;
+}
+
+/**
+ * 右栏一旦在场过,经过不声明右栏的路由(如无项目目录的新建页)时仍保持挂载并强制折叠:
+ * 卸载 Shell 会把内置浏览器的 webview 宿主移出 DOM,销毁 Electron guest 并丢失页面内存态
+ * (#6485)。detach / 窗口状态未就绪时内嵌右栏本就不渲染,在场标记随之复位,之后重新附着
+ * 不会凭空挂出一个隐藏右栏。
+ */
+export function useRetainedRightSidebarMount({
+ available,
+ loaded,
+ detached,
+}: RetainedRightSidebarMountInput): RetainedRightSidebarMount {
+ const mountable = loaded && !detached;
+ const [everMounted, setEverMounted] = useState(false);
+ useEffect(() => {
+ if (!mountable) setEverMounted(false);
+ else if (available) setEverMounted(true);
+ }, [available, mountable]);
+
+ return { mounted: mountable && (available || everMounted), forceCollapsed: !available };
+}