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(sidebar): cover active task scrolling through SessionItem

MagicLizi4075bafac2ca
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/SessionCard.visual.test.ts b/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/SessionCard.visual.test.ts
index 2d94a5c52d..8119a025e3 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/SessionCard.visual.test.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/SessionCard.visual.test.ts
@@ -166,10 +166,6 @@ vi.mock('@/lib/sessionAttentionStore', () => ({
useSessionAttentionKind: (sessionId: string) => mocks.attentionKindBySession.get(sessionId),
}));
-vi.mock('../../lib/scrollIntoNearestView', () => ({
- scrollIntoNearestView: vi.fn(),
-}));
-
vi.mock('@/lib/toast', () => ({
toast: {
success: vi.fn(),
@@ -241,7 +237,13 @@ function sessionRowEl(): HTMLElement {
}
describe('SessionCard visual cases', () => {
+ const originalScrollIntoView = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'scrollIntoView');
+
beforeEach(() => {
+ Object.defineProperty(HTMLElement.prototype, 'scrollIntoView', {
+ configurable: true,
+ value: vi.fn(),
+ });
clearRemoteSessionActivity();
mocks.navigate.mockReset();
mocks.dropdownMenuOpen = false;
@@ -257,6 +259,41 @@ describe('SessionCard visual cases', () => {
afterEach(() => {
cleanup();
vi.restoreAllMocks();
+ if (originalScrollIntoView) {
+ Object.defineProperty(HTMLElement.prototype, 'scrollIntoView', originalScrollIntoView);
+ } else {
+ delete (HTMLElement.prototype as Partial<HTMLElement>).scrollIntoView;
+ }
+ });
+
+ it('scrolls a text row into nearest view only when it becomes active', () => {
+ const props = {
+ session: sessionCardVisualCases[0].session,
+ isActive: false,
+ isRunning: false,
+ hasAttentionNotification: false,
+ onClick: vi.fn(),
+ onAction: vi.fn(),
+ onRename: vi.fn(),
+ onTogglePin: vi.fn(),
+ };
+ const view = render(createElement(SessionItem, props));
+ const row = view.container.querySelector(`[data-session-id="${props.session.id}"]`);
+ const scrollIntoView = vi.mocked(HTMLElement.prototype.scrollIntoView);
+ expect(row).toBeTruthy();
+ expect(scrollIntoView).not.toHaveBeenCalled();
+
+ // The old source guard never exercised this transition. Keep the real
+ // SessionItem effect and helper in this unit regression; only stub native DOM.
+ view.rerender(createElement(SessionItem, { ...props, isActive: true }));
+ expect(scrollIntoView).toHaveBeenCalledExactlyOnceWith({ block: 'nearest', behavior: 'smooth' });
+ expect(scrollIntoView.mock.contexts).toEqual([row]);
+
+ const updatedSession = { ...props.session, preview: 'Updated preview' };
+ view.rerender(createElement(SessionItem, { ...props, session: updatedSession, isActive: true }));
+ expect(scrollIntoView).toHaveBeenCalledTimes(1);
+ view.rerender(createElement(SessionItem, { ...props, session: updatedSession, isActive: false }));
+ expect(scrollIntoView).toHaveBeenCalledTimes(1);
});
it.each(['list', 'text'] as const)('reuses the %s presentation for shared navigation without task actions', (variant) => {