Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
// @vitest-environment jsdom
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import type { DaemonSessionSummary } from '@qwen-code/sdk/daemon';

const COLLAPSED_SESSION_SECTIONS_STORAGE_KEY =
'qwen-code-web-shell-collapsed-session-groups';

const { connection, workspace, workspaceActions, active, pinned, archived } =
vi.hoisted(() => {
const makeSessions = () => ({
sessions: [] as DaemonSessionSummary[],
loading: false,
error: null,
reload: vi.fn().mockResolvedValue(undefined),
deleteSession: vi.fn().mockResolvedValue(true),
archiveSession: vi.fn().mockResolvedValue(true),
unarchiveSession: vi.fn().mockResolvedValue(true),
exportSession: vi.fn(),
});
return {
connection: {
status: 'connected',
sessionId: null as string | null,
workspaceCwd: '/tmp/project',
capabilities: undefined as
| {
qwenCodeVersion: string;
features: string[];
}
| undefined,
},
workspace: {
capabilities: undefined as
| {
qwenCodeVersion: string;
features: string[];
}
| undefined,
client: {
workspaceByCwd: vi.fn(() => ({
listWorkspaceSessions: vi.fn().mockResolvedValue([]),
listSessionGroups: vi.fn().mockResolvedValue({
groups: [],
colorOptions: [
'red',
'orange',
'yellow',
'green',
'blue',
'purple',
],
}),
})),
},
refreshCapabilities: vi.fn(),
},
workspaceActions: {
addWorkspace: vi.fn(),
removeWorkspace: vi.fn(),
listSessionGroups: vi.fn().mockResolvedValue({
groups: [],
colorOptions: ['red', 'orange', 'yellow', 'green', 'blue', 'purple'],
}),
createSessionGroup: vi.fn(),
updateSessionGroup: vi.fn(),
deleteSessionGroup: vi.fn(),
updateSessionOrganization: vi.fn(),
},
active: makeSessions(),
pinned: makeSessions(),
archived: makeSessions(),
};
});

vi.mock('@qwen-code/webui/daemon-react-sdk', () => ({
useConnection: () => connection,
useActions: () => ({ renameSession: vi.fn() }),
useWorkspace: () => workspace,
useWorkspaceActions: () => workspaceActions,
useSessions: (options?: { archiveState?: string; group?: string }) => {
if (options?.archiveState === 'archived') return archived;
if (options?.group === 'pinned') return pinned;
return active;
},
}));

const { I18nProvider } = await import('../../i18n');
const { WebShellSidebar } = await import('./WebShellSidebar');

globalThis.IS_REACT_ACT_ENVIRONMENT = true;
if (!globalThis.PointerEvent) {
globalThis.PointerEvent = MouseEvent as typeof PointerEvent;
}
if (!Element.prototype.hasPointerCapture) {
Element.prototype.hasPointerCapture = () => false;
}
if (!Element.prototype.setPointerCapture) {
Element.prototype.setPointerCapture = () => {};
}
if (!Element.prototype.releasePointerCapture) {
Element.prototype.releasePointerCapture = () => {};
}
if (!Element.prototype.scrollIntoView) {
Element.prototype.scrollIntoView = () => {};
}

function makeSession(
sessionId: string,
over: Partial<DaemonSessionSummary> = {},
): DaemonSessionSummary {
return {
sessionId,
workspaceCwd: '/tmp/project',
displayName: `Session ${sessionId}`,
createdAt: '2026-01-01T00:00:00.000Z',
updatedAt: '2026-01-01T00:00:00.000Z',
clientCount: 0,
hasActivePrompt: false,
isArchived: false,
isPinned: false,
groupId: null,
color: null,
...over,
} as DaemonSessionSummary;
}

const organizationCapabilities = {
qwenCodeVersion: '1.2.3',
features: ['session_organization'],
};

const namedGroup = {
id: 'group-1',
name: 'Backend',
color: 'green' as const,
order: 0,
createdAt: '2026-07-04T00:00:00.000Z',
updatedAt: '2026-07-04T00:00:00.000Z',
};

let root: Root;
let container: HTMLDivElement;

function renderSidebar() {
act(() => {
root.render(
<I18nProvider language="en">
<WebShellSidebar
collapsed={false}
onCollapsedChange={() => {}}
onOpenSettings={() => {}}
onOpenDaemonStatus={() => {}}
onOpenScheduledTasks={() => {}}
onOpenSessions={() => {}}
onOpenSplitView={() => {}}
onNewSession={() => false}
onLoadSession={() => {}}
onError={() => {}}
/>
</I18nProvider>,
);
});
}

async function flushSidebar() {
await act(async () => {
await Promise.resolve();
await Promise.resolve();
});
}

function groupHeader(label: string): HTMLButtonElement {
const section = container.querySelector<HTMLElement>(
`section[aria-label="${label}"]`,
);
expect(section).not.toBeNull();
const header = section!.querySelector<HTMLButtonElement>(
'button[aria-expanded]',
);
expect(header).not.toBeNull();
return header!;
}

function click(element: HTMLElement): void {
element.dispatchEvent(new MouseEvent('click', { bubbles: true }));
}

beforeEach(() => {
window.localStorage.clear();
container = document.createElement('div');
document.body.appendChild(container);
root = createRoot(container);
connection.sessionId = null;
connection.workspaceCwd = '/tmp/project';
connection.capabilities = organizationCapabilities;
workspace.capabilities = organizationCapabilities;
workspaceActions.listSessionGroups.mockReset();
workspaceActions.listSessionGroups.mockResolvedValue({
groups: [namedGroup],
colorOptions: ['red', 'orange', 'yellow', 'green', 'blue', 'purple'],
});
active.sessions = [
makeSession('session-a', {
displayName: 'API review',
groupId: 'group-1',
}),
makeSession('session-b', {
displayName: 'Release notes',
groupId: null,
}),
];
pinned.sessions = [];
archived.sessions = [];
});

afterEach(() => {
act(() => root.unmount());
container.remove();
window.localStorage.clear();
vi.restoreAllMocks();
});

describe('WebShellSidebar collapsed session group persistence', () => {
it('writes collapsed section ids with the qwen-code-web-shell-* key', async () => {
renderSidebar();
await flushSidebar();

const backend = container.querySelector<HTMLElement>(
'section[aria-label="Backend"]',
);
expect(backend?.textContent).toContain('API review');
act(() => click(groupHeader('Backend')));
await flushSidebar();

expect(groupHeader('Backend').getAttribute('aria-expanded')).toBe('false');
expect(backend?.textContent).not.toContain('API review');
expect(
window.localStorage.getItem(COLLAPSED_SESSION_SECTIONS_STORAGE_KEY),
).toBe(JSON.stringify(['group:group-1']));
Comment on lines +249 to +251

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] Every persistence assertion covers only one named group:* ID. Concrete failure scenario: a refactor that retains only the last toggled ID would keep all current unit and browser tests green, yet collapsing two groups would make the first reopen after reload; recent and color:* restoration are likewise untested. Add a remount case that preserves multiple IDs and at least one non-group section kind, including removal of one ID without losing the other.

— Codex $qreview via Qwen Code /review

});

it('keeps a collapsed named group collapsed across remount', async () => {
window.localStorage.setItem(
COLLAPSED_SESSION_SECTIONS_STORAGE_KEY,
JSON.stringify(['group:group-1']),
);

renderSidebar();
await flushSidebar();

expect(groupHeader('Backend').getAttribute('aria-expanded')).toBe('false');
const backend = container.querySelector<HTMLElement>(
'section[aria-label="Backend"]',
);
expect(backend?.textContent).not.toContain('API review');
expect(container.textContent).toContain('Release notes');
});

it('keeps an expanded group expanded across remount after clearing collapse', async () => {
window.localStorage.setItem(
COLLAPSED_SESSION_SECTIONS_STORAGE_KEY,
JSON.stringify(['group:group-1']),
);

renderSidebar();
await flushSidebar();
act(() => click(groupHeader('Backend')));
await flushSidebar();
expect(container.textContent).toContain('API review');
expect(
window.localStorage.getItem(COLLAPSED_SESSION_SECTIONS_STORAGE_KEY),
).toBe(JSON.stringify([]));

act(() => root.unmount());
container.remove();
container = document.createElement('div');
document.body.appendChild(container);
root = createRoot(container);
renderSidebar();
await flushSidebar();

expect(groupHeader('Backend').getAttribute('aria-expanded')).toBe('true');
expect(container.textContent).toContain('API review');
});
});
47 changes: 46 additions & 1 deletion packages/web-shell/client/components/sidebar/WebShellSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ import {
import styles from './WebShellSidebar.module.css';

const SIDEBAR_WIDTH_STORAGE_KEY = 'qwen-code-web-shell-sidebar-width';
// Same `qwen-code-web-shell-*` prefix as sidebar width / chat width / theme.
// App-wide JSON array of collapsed section ids (`group:<id>`, `recent`, `color:<name>`).
const COLLAPSED_SESSION_SECTIONS_STORAGE_KEY =
'qwen-code-web-shell-collapsed-session-groups';
const SIDEBAR_DEFAULT_WIDTH = 260;
const SIDEBAR_MIN_WIDTH = 220;
const SIDEBAR_MAX_WIDTH = 420;
Expand Down Expand Up @@ -351,6 +355,37 @@ function writeSidebarWidth(width: number): void {
}
}

function readCollapsedSessionSectionIds(): Set<string> {
if (typeof window === 'undefined') return new Set();
try {
const raw = window.localStorage.getItem(
COLLAPSED_SESSION_SECTIONS_STORAGE_KEY,
);
if (!raw) return new Set();
const parsed: unknown = JSON.parse(raw);
if (!Array.isArray(parsed)) return new Set();
return new Set(
parsed.filter(
(item): item is string =>
typeof item === 'string' && item.trim().length > 0,
),
);
} catch {
return new Set();
}
}

function writeCollapsedSessionSectionIds(ids: ReadonlySet<string>): void {
try {
window.localStorage.setItem(
COLLAPSED_SESSION_SECTIONS_STORAGE_KEY,
JSON.stringify(Array.from(ids).sort()),
);
} catch {
// localStorage can be unavailable in private or embedded contexts.
}
}

function IconNewChat() {
return (
<svg viewBox="0 0 24 24" aria-hidden="true">
Expand Down Expand Up @@ -524,7 +559,7 @@ export function WebShellSidebar({
} | null>(null);
const [collapsedSessionSectionIds, setCollapsedSessionSectionIds] = useState<
Set<string>
>(() => new Set());
>(() => readCollapsedSessionSectionIds());
const knownSessionSectionIdsRef = useRef<Set<string>>(new Set());
const [sidebarWidth, setSidebarWidth] = useState(readSidebarWidth);
const [projectExpanded, setProjectExpanded] = useState(false);
Expand Down Expand Up @@ -1937,14 +1972,24 @@ export function WebShellSidebar({
.map((section) => section.id)
.filter((id) => !knownSessionSectionIdsRef.current.has(id));
if (unseenIds.length === 0) return;
// First catalog sync: register ids only. Restored localStorage (or the
// empty default) owns expand/collapse; auto-collapse would otherwise wipe
// expanded sections on every remount because the known set resets.
const isInitialCatalog = knownSessionSectionIdsRef.current.size === 0;
for (const id of unseenIds) knownSessionSectionIdsRef.current.add(id);
if (isInitialCatalog) return;
// Brand-new sections that appear mid-session still start collapsed.
setCollapsedSessionSectionIds((current) => {
const next = new Set(current);
for (const id of unseenIds) next.add(id);
return next;
});
}, [sessionSections]);

useEffect(() => {
writeCollapsedSessionSectionIds(collapsedSessionSectionIds);
}, [collapsedSessionSectionIds]);

const toggleSessionSection = useCallback((sectionId: string) => {
setCollapsedSessionSectionIds((current) => {
const next = new Set(current);
Expand Down
Loading
Loading