mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
feat(frontend): add scroll-to-bottom button, improve scroll disengagement, and tall content detection
This commit is contained in:
1 parent
c3802aa021
commit
f492faa84b
10 files changed
+512
-29
No files matched your search
@@ -92,7 +92,7 @@ export function FloatingTTSButton({ messageId, content }: FloatingTTSButtonProps
|
||||
onPointerDown={handlePointerDown}
|
||||
onPointerUp={handlePointerUp}
|
||||
onPointerLeave={handlePointerLeave}
|
||||
className={`absolute -top-5 right-0 md:right-4 z-50 flex items-center transition-all duration-200 shadow-md backdrop-blur-md ring-1 ${buttonToneClasses} ${isLongPressVisual ? 'scale-95 opacity-80' : 'active:scale-95 hover:scale-105'} ${!hasContent && !autoPlay ? 'opacity-50 cursor-not-allowed' : ''}`}
|
||||
className={`flex items-center transition-all duration-200 shadow-md backdrop-blur-md ring-1 ${buttonToneClasses} ${isLongPressVisual ? 'scale-95 opacity-80' : 'active:scale-95 hover:scale-105'} ${!hasContent && !autoPlay ? 'opacity-50 cursor-not-allowed' : ''}`}
|
||||
title={pillTitle}
|
||||
aria-label={pillAriaLabel}
|
||||
disabled={!hasContent && !autoPlay}
|
||||
|
||||
@@ -380,5 +380,35 @@ describe('PromptInput STT Gesture Tests', () => {
|
||||
expect(mockStartRecording).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
|
||||
it('renders mobile mic even when showScrollButton is true and no voice feedback is active', async () => {
|
||||
mocks.useMobile.mockReturnValue(true)
|
||||
|
||||
render(
|
||||
<QueryClientProvider client={createTestQueryClient()}>
|
||||
<PromptInput {...defaultProps} showScrollButton={true} />
|
||||
</QueryClientProvider>
|
||||
)
|
||||
|
||||
const allButtons = screen.getAllByRole('button')
|
||||
const voiceButtons = allButtons.filter((btn) => {
|
||||
const title = (btn.getAttribute('title') || '').toLowerCase()
|
||||
return title.includes('tap to speak') || title.includes('tap to transcribe') || title.includes('hold to speak')
|
||||
})
|
||||
|
||||
expect(voiceButtons.length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it('does NOT render mobile in-row ArrowDown button', async () => {
|
||||
mocks.useMobile.mockReturnValue(true)
|
||||
|
||||
render(
|
||||
<QueryClientProvider client={createTestQueryClient()}>
|
||||
<PromptInput {...defaultProps} showScrollButton={true} />
|
||||
</QueryClientProvider>
|
||||
)
|
||||
|
||||
expect(screen.queryByTitle('Scroll to bottom')).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1244,23 +1244,15 @@ return (
|
||||
|
||||
</div>
|
||||
<div className="flex items-center gap-1.5 md:gap-2 flex-shrink-0">
|
||||
{isMobile && showScrollButton && !showVoiceFeedback ? (
|
||||
{!isMobile && (
|
||||
<button
|
||||
onClick={onScrollToBottom}
|
||||
className="px-4 py-2 rounded-lg bg-black hover:bg-zinc-900 text-white transition-all duration-200 active:scale-95 shadow-md flex items-center justify-center min-w-[52px] border border-zinc-700"
|
||||
title="Scroll to bottom"
|
||||
>
|
||||
<ArrowDown className="w-5 h-5" />
|
||||
</button>
|
||||
) : !isMobile ? (
|
||||
<button
|
||||
onClick={onScrollToBottom}
|
||||
className={`p-2 rounded-lg bg-black hover:bg-zinc-900 text-white transition-all duration-200 active:scale-95 hover:scale-105 shadow-md border border-zinc-700 ${showScrollButton ? 'visible' : 'invisible'}`}
|
||||
className={`p-2 rounded-lg bg-zinc-950/80 hover:bg-zinc-900/90 text-blue-300 hover:text-blue-200 transition-all duration-200 active:scale-95 hover:scale-105 shadow-md border border-blue-400/20 backdrop-blur-md ring-1 ring-blue-400/15 ${showScrollButton ? 'visible' : 'invisible'}`}
|
||||
title="Scroll to bottom"
|
||||
>
|
||||
<ArrowDown className="w-6 h-6" />
|
||||
</button>
|
||||
) : null}
|
||||
)}
|
||||
{showStopButton && (
|
||||
<button
|
||||
onClick={handleStop}
|
||||
@@ -1289,7 +1281,7 @@ return (
|
||||
{sttEnabled && sttSupported && (
|
||||
renderVoiceButton('desktop')
|
||||
)}
|
||||
{isMobile && sttEnabled && sttSupported && !hasPendingPermissionForSession && (!showScrollButton || showVoiceFeedback) && (
|
||||
{isMobile && sttEnabled && sttSupported && !hasPendingPermissionForSession && (
|
||||
renderVoiceButton('mobile')
|
||||
)}
|
||||
<button
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { renderHook, act } from '@testing-library/react'
|
||||
import { useTallScrollContent } from '../useTallScrollContent'
|
||||
|
||||
let mockObserve: ReturnType<typeof vi.fn>
|
||||
let mockDisconnect: ReturnType<typeof vi.fn>
|
||||
let resizeCallback: ((entries: ResizeObserverEntry[]) => void) | null = null
|
||||
|
||||
vi.stubGlobal('ResizeObserver', vi.fn((cb: (entries: ResizeObserverEntry[]) => void) => {
|
||||
resizeCallback = cb
|
||||
return {
|
||||
observe: mockObserve,
|
||||
disconnect: mockDisconnect,
|
||||
unobserve: vi.fn(),
|
||||
}
|
||||
}))
|
||||
|
||||
describe('useTallScrollContent', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockObserve = vi.fn()
|
||||
mockDisconnect = vi.fn()
|
||||
resizeCallback = null
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
function createContainerRef(scrollHeight: number, clientHeight: number) {
|
||||
const el = document.createElement('div')
|
||||
Object.defineProperty(el, 'scrollHeight', { value: scrollHeight, configurable: true })
|
||||
Object.defineProperty(el, 'clientHeight', { value: clientHeight, configurable: true })
|
||||
return { current: el }
|
||||
}
|
||||
|
||||
it('returns false when scrollHeight <= clientHeight * ratio', () => {
|
||||
const ref = createContainerRef(500, 400)
|
||||
|
||||
const { result } = renderHook(() => useTallScrollContent(ref as React.RefObject<HTMLElement>, 1.5))
|
||||
expect(result.current).toBe(false)
|
||||
})
|
||||
|
||||
it('returns true when scrollHeight > clientHeight * ratio', () => {
|
||||
const ref = createContainerRef(2000, 800)
|
||||
|
||||
const { result } = renderHook(() => useTallScrollContent(ref as React.RefObject<HTMLElement>, 1.5))
|
||||
expect(result.current).toBe(true)
|
||||
})
|
||||
|
||||
it('updates from false to true when ResizeObserver fires with taller content', () => {
|
||||
const ref = createContainerRef(500, 400)
|
||||
|
||||
const { result } = renderHook(() => useTallScrollContent(ref as React.RefObject<HTMLElement>, 1.5))
|
||||
expect(result.current).toBe(false)
|
||||
|
||||
Object.defineProperty(ref.current, 'scrollHeight', { value: 2000 })
|
||||
act(() => {
|
||||
resizeCallback?.([] as ResizeObserverEntry[])
|
||||
})
|
||||
|
||||
expect(result.current).toBe(true)
|
||||
})
|
||||
|
||||
it('updates from true to false when ResizeObserver fires with smaller container', () => {
|
||||
const ref = createContainerRef(2000, 800)
|
||||
|
||||
const { result } = renderHook(() => useTallScrollContent(ref as React.RefObject<HTMLElement>, 1.5))
|
||||
expect(result.current).toBe(true)
|
||||
|
||||
Object.defineProperty(ref.current, 'clientHeight', { value: 2000 })
|
||||
act(() => {
|
||||
resizeCallback?.([] as ResizeObserverEntry[])
|
||||
})
|
||||
|
||||
expect(result.current).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -28,6 +28,7 @@ export function useAutoScroll({
|
||||
const userScrolledAtRef = useRef(0)
|
||||
const userDisengagedRef = useRef(false)
|
||||
const pointerStartYRef = useRef<number | null>(null)
|
||||
const pointerActiveRef = useRef(false)
|
||||
const onScrollStateChangeRef = useRef(onScrollStateChange)
|
||||
|
||||
onScrollStateChangeRef.current = onScrollStateChange
|
||||
@@ -61,17 +62,19 @@ export function useAutoScroll({
|
||||
|
||||
const handlePointerDown = (e: PointerEvent) => {
|
||||
pointerStartYRef.current = e.clientY
|
||||
pointerActiveRef.current = true
|
||||
}
|
||||
|
||||
const handlePointerMove = (e: PointerEvent) => {
|
||||
if (pointerStartYRef.current === null) return
|
||||
if (e.clientY > pointerStartYRef.current) {
|
||||
if (e.clientY > pointerStartYRef.current + 4) {
|
||||
markDisengaged()
|
||||
}
|
||||
}
|
||||
|
||||
const handlePointerUp = () => {
|
||||
pointerStartYRef.current = null
|
||||
pointerActiveRef.current = false
|
||||
}
|
||||
|
||||
const handleWheel = (e: WheelEvent) => {
|
||||
@@ -87,6 +90,7 @@ export function useAutoScroll({
|
||||
}
|
||||
|
||||
const handleScroll = () => {
|
||||
if (!hasInitialScrolledRef.current) return
|
||||
const { scrollTop, scrollHeight, clientHeight } = container
|
||||
const isAtBottom = scrollHeight - scrollTop - clientHeight < BOTTOM_THRESHOLD_PX
|
||||
|
||||
@@ -96,7 +100,7 @@ export function useAutoScroll({
|
||||
userDisengagedRef.current = false
|
||||
onScrollStateChangeRef.current?.(false)
|
||||
}
|
||||
} else if (!userDisengagedRef.current) {
|
||||
} else if (pointerActiveRef.current && !userDisengagedRef.current) {
|
||||
userScrolledAtRef.current = Date.now()
|
||||
userDisengagedRef.current = true
|
||||
onScrollStateChangeRef.current?.(true)
|
||||
@@ -146,7 +150,7 @@ export function useAutoScroll({
|
||||
const timeSinceUserScroll = Date.now() - userScrolledAtRef.current
|
||||
const recentlyScrolled = timeSinceUserScroll < SCROLL_LOCK_MS
|
||||
|
||||
if (recentlyScrolled || userDisengagedRef.current) {
|
||||
if (recentlyScrolled || userDisengagedRef.current || pointerActiveRef.current) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { useState, useEffect, useRef } from 'react'
|
||||
|
||||
export function useTallScrollContent(
|
||||
containerRef: React.RefObject<HTMLElement | null>,
|
||||
ratio = 1.5
|
||||
): boolean {
|
||||
const [isTall, setIsTall] = useState(false)
|
||||
const observingRef = useRef<HTMLElement | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
const recompute = () => {
|
||||
const el = containerRef.current
|
||||
if (!el) return
|
||||
setIsTall(el.scrollHeight > el.clientHeight * ratio)
|
||||
}
|
||||
|
||||
const el = containerRef.current
|
||||
|
||||
if (!el || el === observingRef.current) return
|
||||
|
||||
observingRef.current = el
|
||||
recompute()
|
||||
|
||||
const observer = new ResizeObserver(recompute)
|
||||
observer.observe(el)
|
||||
el.addEventListener('scroll', recompute)
|
||||
|
||||
return () => {
|
||||
observer.disconnect()
|
||||
el.removeEventListener('scroll', recompute)
|
||||
observingRef.current = null
|
||||
}
|
||||
})
|
||||
|
||||
return isTall
|
||||
}
|
||||
@@ -11,7 +11,7 @@
|
||||
--color-primary-foreground: #fafafa;
|
||||
--color-muted-foreground: #d4d4d8;
|
||||
--color-accent: #27272a;
|
||||
--color-input: #27272a;
|
||||
--color-input: #323232;
|
||||
--color-muted: #18181b;
|
||||
--color-popover: #18181b;
|
||||
--color-destructive: #dc2626;
|
||||
|
||||
@@ -5,7 +5,7 @@ import { getRepo, initializeAssistantMode } from "@/api/repos";
|
||||
import { MessageThread } from "@/components/message/MessageThread";
|
||||
import { PromptInput, type PromptInputHandle } from "@/components/message/PromptInput";
|
||||
import { FloatingTTSButton } from '@/components/message/FloatingTTSButton'
|
||||
import { X, CornerUpLeft } from "lucide-react";
|
||||
import { X, CornerUpLeft, ArrowDown } from "lucide-react";
|
||||
import { ModelSelectDialog } from "@/components/model/ModelSelectDialog";
|
||||
import { Header } from "@/components/ui/header";
|
||||
import { SessionList } from "@/components/session/SessionList";
|
||||
@@ -27,6 +27,7 @@ import { useSettingsDialog } from "@/hooks/useSettingsDialog";
|
||||
import { useAutoScroll } from "@/hooks/useAutoScroll";
|
||||
import { useMobile } from "@/hooks/useMobile";
|
||||
import { useVisualViewport } from "@/hooks/useVisualViewport";
|
||||
import { useTallScrollContent } from "@/hooks/useTallScrollContent";
|
||||
import { useTTS } from "@/hooks/useTTS";
|
||||
import { getAssistantText, getLatestPlayableAssistantMessage, useAutoPlayLastResponse } from "@/hooks/useAutoPlayLastResponse";
|
||||
import { useEffect, useRef, useCallback, useMemo } from "react";
|
||||
@@ -86,12 +87,14 @@ export function SessionDetail() {
|
||||
const [minimizedQuestion, setMinimizedQuestion] = useState<QuestionRequest | null>(null);
|
||||
const [isHeaderVisible, setIsHeaderVisible] = useState(true);
|
||||
const lastHeaderScrollTopRef = useRef(0);
|
||||
const lastHeaderScrollHeightRef = useRef(0);
|
||||
|
||||
const isMobile = useMobile();
|
||||
const { keyboardHeight } = useVisualViewport();
|
||||
const inputBottomOffset = isMobile ? keyboardHeight : 0;
|
||||
const promptOverlayRef = useRef<HTMLDivElement>(null);
|
||||
const [promptOverlayHeight, setPromptOverlayHeight] = useState(112);
|
||||
const isContentTall = useTallScrollContent(messageContainerRef, 1.5);
|
||||
|
||||
useEffect(() => {
|
||||
const el = promptOverlayRef.current;
|
||||
@@ -216,8 +219,11 @@ export function SessionDetail() {
|
||||
if (!container) return
|
||||
|
||||
lastHeaderScrollTopRef.current = container.scrollTop
|
||||
lastHeaderScrollHeightRef.current = container.scrollHeight
|
||||
setIsHeaderVisible(true)
|
||||
|
||||
const SCROLL_DELTA_THRESHOLD = 8
|
||||
|
||||
const handleScroll = () => {
|
||||
if (!isMobile) {
|
||||
setIsHeaderVisible(true)
|
||||
@@ -225,18 +231,29 @@ export function SessionDetail() {
|
||||
}
|
||||
const currentScrollTop = container.scrollTop
|
||||
const previousScrollTop = lastHeaderScrollTopRef.current
|
||||
const maxScrollTop = container.scrollHeight - container.clientHeight
|
||||
const isAtBottom = maxScrollTop - currentScrollTop < 24
|
||||
const isScrollingDown = currentScrollTop > previousScrollTop + 4
|
||||
const isScrollingUp = currentScrollTop < previousScrollTop - 50
|
||||
|
||||
if (isAtBottom || isScrollingDown) {
|
||||
setIsHeaderVisible(true)
|
||||
} else if (isScrollingUp) {
|
||||
setIsHeaderVisible(false)
|
||||
}
|
||||
const currentScrollHeight = container.scrollHeight
|
||||
const previousScrollHeight = lastHeaderScrollHeightRef.current
|
||||
|
||||
lastHeaderScrollTopRef.current = currentScrollTop
|
||||
lastHeaderScrollHeightRef.current = currentScrollHeight
|
||||
|
||||
if (currentScrollHeight !== previousScrollHeight) return
|
||||
|
||||
const delta = currentScrollTop - previousScrollTop
|
||||
if (Math.abs(delta) < SCROLL_DELTA_THRESHOLD) return
|
||||
|
||||
const isAtTop = currentScrollTop < 24
|
||||
|
||||
if (isAtTop) {
|
||||
setIsHeaderVisible(true)
|
||||
return
|
||||
}
|
||||
|
||||
if (delta > 0) {
|
||||
setIsHeaderVisible(true)
|
||||
} else {
|
||||
setIsHeaderVisible(false)
|
||||
}
|
||||
}
|
||||
|
||||
container.addEventListener('scroll', handleScroll, { passive: true })
|
||||
@@ -538,7 +555,7 @@ export function SessionDetail() {
|
||||
style={{ bottom: inputBottomOffset }}
|
||||
>
|
||||
<div className="relative w-[94%] md:max-w-4xl">
|
||||
<div className="absolute -top-5 right-0 md:right-4 z-50 flex flex-col items-end gap-2">
|
||||
<div className="absolute -top-9 right-0 z-50 flex flex-col items-end gap-2">
|
||||
{ttsEnabled && !hasPromptContent && !isSessionActive && latestPlayableAssistant && (
|
||||
<FloatingTTSButton
|
||||
messageId={latestPlayableAssistant.message.info.id}
|
||||
@@ -561,6 +578,18 @@ export function SessionDetail() {
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{isMobile && showScrollButton && isContentTall && (
|
||||
<div className="absolute -top-9 left-0 z-50 flex flex-col items-start gap-2">
|
||||
<button
|
||||
onClick={scrollToBottom}
|
||||
className="flex items-center justify-center px-3 py-1.5 rounded-lg bg-zinc-950/80 hover:bg-zinc-900/90 text-blue-300 hover:text-blue-200 border border-blue-400/20 shadow-md backdrop-blur-md transition-all duration-200 active:scale-95 hover:scale-105 ring-1 ring-blue-400/15"
|
||||
aria-label="Scroll to bottom"
|
||||
title="Scroll to bottom"
|
||||
>
|
||||
<ArrowDown className="w-5 h-5" />
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
{leaderActive && (
|
||||
<div className="absolute -top-12 left-1/2 -translate-x-1/2 z-50 px-4 py-2 rounded-xl bg-primary/90 text-primary-foreground border border-primary shadow-lg backdrop-blur-md animate-pulse">
|
||||
<span className="text-sm font-medium">Waiting for shortcut key...</span>
|
||||
|
||||
@@ -0,0 +1,308 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { MemoryRouter, Route, Routes } from 'react-router-dom'
|
||||
import { SessionDetail } from '../SessionDetail'
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
useSession: vi.fn(),
|
||||
useMessages: vi.fn(),
|
||||
useSSE: vi.fn(),
|
||||
useRepoActivity: vi.fn(),
|
||||
usePermissions: vi.fn(),
|
||||
useQuestions: vi.fn(),
|
||||
useSSEHealth: vi.fn(),
|
||||
useConfig: vi.fn(),
|
||||
useOpenCodeClient: vi.fn(),
|
||||
useSettings: vi.fn(),
|
||||
useSettingsDialog: vi.fn(),
|
||||
useMobile: vi.fn(),
|
||||
useVisualViewport: vi.fn(),
|
||||
useKeyboardShortcuts: vi.fn(),
|
||||
useAutoScroll: vi.fn(),
|
||||
useDialogParam: vi.fn(),
|
||||
useSidebarAction: vi.fn(),
|
||||
useTallScrollContent: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/config', () => ({
|
||||
OPENCODE_API_ENDPOINT: 'http://localhost:5551/api/opencode',
|
||||
API_BASE_URL: 'http://localhost:5551',
|
||||
SERVER_PORT: 5003,
|
||||
OPENCODE_PORT: 5551,
|
||||
FILE_LIMITS: {},
|
||||
DEFAULTS: {},
|
||||
ALLOWED_MIME_TYPES: [],
|
||||
GIT_PROVIDERS: [],
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useOpenCode', () => ({
|
||||
useSession: mocks.useSession,
|
||||
useAbortSession: vi.fn(() => ({ mutate: vi.fn() })),
|
||||
useUpdateSession: vi.fn(() => ({ mutate: vi.fn() })),
|
||||
useCreateSession: vi.fn(() => ({ mutateAsync: vi.fn() })),
|
||||
useMessages: mocks.useMessages,
|
||||
useConfig: mocks.useConfig,
|
||||
useSendPrompt: vi.fn(() => ({ mutate: vi.fn() })),
|
||||
useSendShell: vi.fn(() => ({ mutate: vi.fn() })),
|
||||
useAgents: vi.fn(() => ({ data: [] })),
|
||||
useOpenCodeClient: mocks.useOpenCodeClient,
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useModelSelection', () => ({
|
||||
useModelSelection: vi.fn(() => ({ model: null, modelString: null })),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useOpenCodeClient', () => ({
|
||||
useOpenCodeClient: mocks.useOpenCodeClient,
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useTTS', () => ({
|
||||
useTTS: vi.fn(() => ({ isEnabled: false })),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useSettings', () => ({
|
||||
useSettings: vi.fn(() => ({
|
||||
preferences: { expandToolCalls: false },
|
||||
updateSettings: vi.fn(),
|
||||
})),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useSettingsDialog', () => ({
|
||||
useSettingsDialog: vi.fn(() => ({ open: vi.fn() })),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useMobile', () => ({
|
||||
useMobile: mocks.useMobile,
|
||||
useSwipeBack: vi.fn(() => ({ ref: vi.fn() })),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useVisualViewport', () => ({
|
||||
useVisualViewport: vi.fn(() => ({ keyboardHeight: 0 })),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useKeyboardShortcuts', () => ({
|
||||
useKeyboardShortcuts: vi.fn(() => ({ leaderActive: false })),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useAutoScroll', () => ({
|
||||
useAutoScroll: mocks.useAutoScroll,
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useDialogParam', () => ({
|
||||
useDialogParam: vi.fn(() => [false, vi.fn()]),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useSidebarAction', () => ({
|
||||
useSidebarAction: vi.fn(() => {}),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useTallScrollContent', () => ({
|
||||
useTallScrollContent: mocks.useTallScrollContent,
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useAutoPlayLastResponse', () => ({
|
||||
getAssistantText: vi.fn(() => ''),
|
||||
getLatestPlayableAssistantMessage: vi.fn(() => null),
|
||||
useAutoPlayLastResponse: vi.fn(() => {}),
|
||||
}))
|
||||
|
||||
vi.mock('@/stores/uiStateStore', () => ({
|
||||
useUIState: vi.fn((selector?: (state: Record<string, unknown>) => unknown) =>
|
||||
typeof selector === 'function'
|
||||
? selector({ isEditingMessage: false, setActivePromptFileBasePath: vi.fn() })
|
||||
: false
|
||||
),
|
||||
}))
|
||||
|
||||
vi.mock('@/stores/sessionStatusStore', () => ({
|
||||
useSessionStatus: vi.fn(() => ({ setStatus: vi.fn() })),
|
||||
useSessionStatusForSession: vi.fn(() => ({ type: 'idle' })),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useSSE', () => ({
|
||||
useSSE: mocks.useSSE,
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useRepoActivity', () => ({
|
||||
useRepoActivity: mocks.useRepoActivity,
|
||||
}))
|
||||
|
||||
vi.mock('@/contexts/EventContext', async (importOriginal) => {
|
||||
const actual = await importOriginal()
|
||||
return {
|
||||
...(actual as object),
|
||||
usePermissions: mocks.usePermissions,
|
||||
useQuestions: mocks.useQuestions,
|
||||
useSSEHealth: mocks.useSSEHealth,
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('@/api/repos', () => ({
|
||||
getRepo: vi.fn(() => Promise.resolve({
|
||||
id: 1,
|
||||
repoUrl: 'https://github.com/test/repo',
|
||||
localPath: '/test/repo',
|
||||
sourcePath: null,
|
||||
fullPath: '/test/repo',
|
||||
branch: 'main',
|
||||
currentBranch: 'main',
|
||||
fullSlug: 'test/repo',
|
||||
repoType: 'github' as const,
|
||||
})),
|
||||
initializeAssistantMode: vi.fn(() => Promise.resolve({ directory: '/test/repo' })),
|
||||
}))
|
||||
|
||||
vi.mock('@/components/model/ModelSelectDialog', () => ({
|
||||
ModelSelectDialog: vi.fn(() => null),
|
||||
}))
|
||||
|
||||
vi.mock('@/components/session/SessionList', () => ({
|
||||
SessionList: vi.fn(() => null),
|
||||
}))
|
||||
|
||||
vi.mock('@/components/file-browser/FileBrowserSheet', () => ({
|
||||
FileBrowserSheet: vi.fn(() => null),
|
||||
}))
|
||||
|
||||
vi.mock('@/components/repo/RepoMcpDialog', () => ({
|
||||
RepoMcpDialog: vi.fn(() => null),
|
||||
}))
|
||||
|
||||
vi.mock('@/components/repo/ResetPermissionsDialog', () => ({
|
||||
ResetPermissionsDialog: vi.fn(() => null),
|
||||
}))
|
||||
|
||||
vi.mock('@/components/repo/RepoLspDialog', () => ({
|
||||
RepoLspDialog: vi.fn(() => null),
|
||||
}))
|
||||
|
||||
vi.mock('@/components/repo/RepoSkillsDialog', () => ({
|
||||
RepoSkillsDialog: vi.fn(() => null),
|
||||
}))
|
||||
|
||||
vi.mock('@/components/source-control', () => ({
|
||||
SourceControlPanel: vi.fn(() => null),
|
||||
}))
|
||||
|
||||
vi.mock('@/components/session/QuestionPrompt', () => ({
|
||||
QuestionPrompt: vi.fn(() => null),
|
||||
}))
|
||||
|
||||
vi.mock('@/components/session/MinimizedQuestionIndicator', () => ({
|
||||
MinimizedQuestionIndicator: vi.fn(() => null),
|
||||
}))
|
||||
|
||||
vi.mock('@/components/notifications/PendingActionsGroup', () => ({
|
||||
PendingActionsGroup: vi.fn(() => null),
|
||||
}))
|
||||
|
||||
vi.mock('@/components/message/PromptInput', () => ({
|
||||
PromptInput: vi.fn(() => <div>MockedPromptInput</div>),
|
||||
}))
|
||||
|
||||
describe('SessionDetail scroll floating button', () => {
|
||||
let mockScrollToBottom: ReturnType<typeof vi.fn>
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
|
||||
mockScrollToBottom = vi.fn()
|
||||
|
||||
mocks.useSession.mockReturnValue({ data: undefined, isLoading: false })
|
||||
mocks.useMessages.mockReturnValue({ data: [], isLoading: false })
|
||||
mocks.useSSE.mockReturnValue({ isConnected: true, isReconnecting: false })
|
||||
mocks.useRepoActivity.mockReturnValue(undefined)
|
||||
mocks.usePermissions.mockReturnValue({
|
||||
pendingCount: 0,
|
||||
hasPermissionsForSession: vi.fn(() => false),
|
||||
syncForSession: vi.fn(),
|
||||
})
|
||||
mocks.useQuestions.mockReturnValue({
|
||||
current: null,
|
||||
pendingCount: 0,
|
||||
hasQuestionsForSession: vi.fn(() => false),
|
||||
reply: vi.fn(),
|
||||
reject: vi.fn(),
|
||||
syncForSession: vi.fn(),
|
||||
})
|
||||
mocks.useSSEHealth.mockReturnValue({ isHealthy: true })
|
||||
mocks.useConfig.mockReturnValue({ data: undefined, isLoading: false })
|
||||
mocks.useOpenCodeClient.mockReturnValue({})
|
||||
mocks.useVisualViewport.mockReturnValue({ keyboardHeight: 0 })
|
||||
mocks.useKeyboardShortcuts.mockReturnValue({ leaderActive: false })
|
||||
mocks.useDialogParam.mockReturnValue([false, vi.fn()])
|
||||
mocks.useSidebarAction.mockReturnValue(undefined)
|
||||
})
|
||||
|
||||
const createQueryClient = () =>
|
||||
new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
|
||||
const renderWith = (opts: {
|
||||
mobile: boolean
|
||||
tallContent: boolean
|
||||
showScrollButton: boolean
|
||||
}) => {
|
||||
mocks.useMobile.mockReturnValue(opts.mobile)
|
||||
mocks.useTallScrollContent.mockReturnValue(opts.tallContent)
|
||||
mocks.useAutoScroll.mockImplementation(
|
||||
({ onScrollStateChange }: { onScrollStateChange?: (v: boolean) => void }) => {
|
||||
if (opts.showScrollButton) {
|
||||
Promise.resolve().then(() => onScrollStateChange?.(true))
|
||||
}
|
||||
return { scrollToBottom: mockScrollToBottom }
|
||||
}
|
||||
)
|
||||
|
||||
return render(
|
||||
<MemoryRouter initialEntries={['/repos/1/sessions/test-session']}>
|
||||
<QueryClientProvider client={createQueryClient()}>
|
||||
<Routes>
|
||||
<Route path="/repos/:id/sessions/:sessionId" element={<SessionDetail />} />
|
||||
</Routes>
|
||||
</QueryClientProvider>
|
||||
</MemoryRouter>
|
||||
)
|
||||
}
|
||||
|
||||
it('renders floating ArrowDown when mobile + showScrollButton + isContentTall', async () => {
|
||||
renderWith({ mobile: true, tallContent: true, showScrollButton: true })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTitle('Scroll to bottom')).toBeInTheDocument()
|
||||
})
|
||||
expect(screen.getByLabelText('Scroll to bottom')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('does NOT render floating ArrowDown when mobile + showScrollButton but not tall content', async () => {
|
||||
renderWith({ mobile: true, tallContent: false, showScrollButton: true })
|
||||
|
||||
await waitFor(() => expect(screen.getByText('MockedPromptInput')).toBeInTheDocument())
|
||||
expect(screen.queryByLabelText('Scroll to bottom')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('does NOT render floating ArrowDown when mobile + tall content but no showScrollButton', async () => {
|
||||
renderWith({ mobile: true, tallContent: true, showScrollButton: false })
|
||||
|
||||
await waitFor(() => expect(screen.getByText('MockedPromptInput')).toBeInTheDocument())
|
||||
expect(screen.queryByLabelText('Scroll to bottom')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('does NOT render floating ArrowDown on desktop even with showScrollButton and tall content', async () => {
|
||||
renderWith({ mobile: false, tallContent: true, showScrollButton: true })
|
||||
|
||||
await waitFor(() => expect(screen.getByText('MockedPromptInput')).toBeInTheDocument())
|
||||
expect(screen.queryByLabelText('Scroll to bottom')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('clicking floating ArrowDown calls scrollToBottom from useAutoScroll', async () => {
|
||||
renderWith({ mobile: true, tallContent: true, showScrollButton: true })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByLabelText('Scroll to bottom')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
fireEvent.click(screen.getByLabelText('Scroll to bottom'))
|
||||
expect(mockScrollToBottom).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
@@ -1 +1,7 @@
|
||||
import '@testing-library/jest-dom/vitest'
|
||||
|
||||
global.ResizeObserver = class ResizeObserver {
|
||||
observe() {}
|
||||
unobserve() {}
|
||||
disconnect() {}
|
||||
}
|
||||
Reference in new issue
Block a user